hamok
Version:
Lightweight Distributed Object Storage on RAFT consensus algorithm
688 lines (687 loc) • 35.4 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StorageCodec = void 0;
const HamokCodec_1 = require("../common/HamokCodec");
const UpdateEntries_1 = require("./messagetypes/UpdateEntries");
const HamokMessage_1 = require("./HamokMessage");
const ClearEntries_1 = require("./messagetypes/ClearEntries");
const GetEntries_1 = require("./messagetypes/GetEntries");
const GetKeys_1 = require("./messagetypes/GetKeys");
const DeleteEntries_1 = require("./messagetypes/DeleteEntries");
const RemoveEntries_1 = require("./messagetypes/RemoveEntries");
const InsertEntries_1 = require("./messagetypes/InsertEntries");
const GetSize_1 = require("./messagetypes/GetSize");
const logger_1 = require("../common/logger");
const StorageAppliedCommit_1 = require("./messagetypes/StorageAppliedCommit");
const StorageHelloNotification_1 = require("./messagetypes/StorageHelloNotification");
const StorageStateNotification_1 = require("./messagetypes/StorageStateNotification");
const logger = (0, logger_1.createLogger)('StorageCodec');
class StorageCodec {
keyCodec;
valueCodec;
static strCodec = (0, HamokCodec_1.createStrToUint8ArrayCodec)();
constructor(keyCodec, valueCodec) {
this.keyCodec = keyCodec;
this.valueCodec = valueCodec;
// empty
}
encode(input, callback) {
let result;
switch (input.constructor) {
case ClearEntries_1.ClearEntriesRequest:
result = this.encodeClearEntriesRequest(input);
break;
case ClearEntries_1.ClearEntriesResponse:
result = this.encodeClearEntriesResponse(input);
break;
case ClearEntries_1.ClearEntriesNotification:
result = this.encodeClearEntriesNotification(input);
break;
case GetEntries_1.GetEntriesRequest:
result = this.encodeGetEntriesRequest(input);
break;
case GetEntries_1.GetEntriesResponse:
result = this.encodeGetEntriesResponse(input);
break;
case GetKeys_1.GetKeysRequest:
result = this.encodeGetKeysRequest(input);
break;
case GetKeys_1.GetKeysResponse:
result = this.encodeGetKeysResponse(input);
break;
case GetSize_1.GetSizeRequest:
result = this.encodeGetSizeRequest(input);
break;
case GetSize_1.GetSizeResponse:
result = this.encodeGetSizeResponse(input);
break;
case DeleteEntries_1.DeleteEntriesRequest:
result = this.encodeDeleteEntriesRequest(input);
break;
case DeleteEntries_1.DeleteEntriesResponse:
result = this.encodeDeleteEntriesResponse(input);
break;
case DeleteEntries_1.DeleteEntriesNotification:
result = this.encodeDeleteEntriesNotification(input);
break;
case RemoveEntries_1.RemoveEntriesRequest:
result = this.encodeRemoveEntriesRequest(input);
break;
case RemoveEntries_1.RemoveEntriesResponse:
result = this.encodeRemoveEntriesResponse(input);
break;
case RemoveEntries_1.RemoveEntriesNotification:
result = this.encodeDeleteEntriesNotification(input);
break;
case RemoveEntries_1.EntriesRemovedNotification:
result = this.encodeEntriesRemovedNotification(input);
break;
case InsertEntries_1.InsertEntriesRequest:
result = this.encodeInsertEntriesRequest(input);
break;
case InsertEntries_1.InsertEntriesResponse:
result = this.encodeInsertEntriesResponse(input);
break;
case InsertEntries_1.InsertEntriesNotification:
result = this.encodeInsertEntriesNotification(input);
break;
case InsertEntries_1.EntriesInsertedNotification:
result = this.encodeEntriesInsertedNotification(input);
break;
case UpdateEntries_1.UpdateEntriesRequest:
result = this.encodeUpdateEntriesRequest(input);
break;
case UpdateEntries_1.UpdateEntriesResponse:
result = this.encodeUpdateEntriesResponse(input);
break;
case UpdateEntries_1.UpdateEntriesNotification:
result = this.encodeUpdateEntriesNotification(input);
break;
case UpdateEntries_1.EntryUpdatedNotification:
result = this.encodeEntryUpdatedNotification(input);
break;
case StorageAppliedCommit_1.StorageAppliedCommitNotification:
result = this.encodeStorageAppliedCommitNotification(input);
break;
case StorageHelloNotification_1.StorageHelloNotification:
result = this.encodeStorageHelloNotification(input);
break;
case StorageStateNotification_1.StorageStateNotification:
result = this.encodeStorageStateNotification(input);
break;
default:
throw new Error(`Cannot encode input${input}`);
}
logger.trace('Encoded message %o', input);
if (callback) {
callback(input.constructor.name, result);
}
return result;
}
decode(message, callback) {
let type;
let result;
switch (message.type) {
case HamokMessage_1.HamokMessage_MessageType.CLEAR_ENTRIES_REQUEST:
type = callback ? 'ClearEntriesRequest' : undefined;
result = this.decodeClearEntriesRequest(message);
break;
case HamokMessage_1.HamokMessage_MessageType.CLEAR_ENTRIES_RESPONSE:
type = callback ? 'ClearEntriesResponse' : undefined;
result = this.decodeClearEntriesResponse(message);
break;
case HamokMessage_1.HamokMessage_MessageType.CLEAR_ENTRIES_NOTIFICATION:
type = callback ? 'ClearEntriesNotification' : undefined;
result = this.decodeClearEntriesNotification(message);
break;
case HamokMessage_1.HamokMessage_MessageType.GET_ENTRIES_REQUEST:
type = callback ? 'GetEntriesRequest' : undefined;
result = this.decodeGetEntriesRequest(message);
break;
case HamokMessage_1.HamokMessage_MessageType.GET_ENTRIES_RESPONSE:
type = callback ? 'GetEntriesResponse' : undefined;
result = this.decodeGetEntriesResponse(message);
break;
case HamokMessage_1.HamokMessage_MessageType.GET_SIZE_REQUEST:
type = callback ? 'GetSizeRequest' : undefined;
result = this.decodeGetSizeRequest(message);
break;
case HamokMessage_1.HamokMessage_MessageType.GET_SIZE_RESPONSE:
type = callback ? 'GetSizeResponse' : undefined;
result = this.decodeGetSizeResponse(message);
break;
case HamokMessage_1.HamokMessage_MessageType.GET_KEYS_REQUEST:
type = callback ? 'GetKeysRequest' : undefined;
result = this.decodeGetKeysRequest(message);
break;
case HamokMessage_1.HamokMessage_MessageType.GET_KEYS_RESPONSE:
type = callback ? 'GetKeysResponse' : undefined;
result = this.decodeGetKeysResponse(message);
break;
case HamokMessage_1.HamokMessage_MessageType.DELETE_ENTRIES_REQUEST:
type = callback ? 'DeleteEntriesRequest' : undefined;
result = this.decodeDeleteEntriesRequest(message);
break;
case HamokMessage_1.HamokMessage_MessageType.DELETE_ENTRIES_RESPONSE:
type = callback ? 'DeleteEntriesResponse' : undefined;
result = this.decodeDeleteEntriesResponse(message);
break;
case HamokMessage_1.HamokMessage_MessageType.DELETE_ENTRIES_NOTIFICATION:
type = callback ? 'DeleteEntriesNotification' : undefined;
result = this.decodeDeleteEntriesNotification(message);
break;
case HamokMessage_1.HamokMessage_MessageType.REMOVE_ENTRIES_REQUEST:
type = callback ? 'RemoveEntriesRequest' : undefined;
result = this.decodeRemoveEntriesRequest(message);
break;
case HamokMessage_1.HamokMessage_MessageType.REMOVE_ENTRIES_RESPONSE:
type = callback ? 'RemoveEntriesResponse' : undefined;
result = this.decodeRemoveEntriesResponse(message);
break;
case HamokMessage_1.HamokMessage_MessageType.REMOVE_ENTRIES_NOTIFICATION:
type = callback ? 'RemoveEntriesNotification' : undefined;
result = this.decodeRemoveEntriesNotification(message);
break;
case HamokMessage_1.HamokMessage_MessageType.ENTRIES_REMOVED_NOTIFICATION:
type = callback ? 'EntriesRemovedNotification' : undefined;
result = this.decodeEntriesRemovedNotification(message);
break;
case HamokMessage_1.HamokMessage_MessageType.INSERT_ENTRIES_REQUEST:
type = callback ? 'InsertEntriesRequest' : undefined;
result = this.decodeInsertEntriesRequest(message);
break;
case HamokMessage_1.HamokMessage_MessageType.INSERT_ENTRIES_RESPONSE:
type = callback ? 'InsertEntriesResponse' : undefined;
result = this.decodeInsertEntriesResponse(message);
break;
case HamokMessage_1.HamokMessage_MessageType.INSERT_ENTRIES_NOTIFICATION:
type = callback ? 'InsertEntriesNotification' : undefined;
result = this.decodeInsertEntriesNotification(message);
break;
case HamokMessage_1.HamokMessage_MessageType.ENTRIES_INSERTED_NOTIFICATION:
type = callback ? 'EntriesInsertedNotification' : undefined;
result = this.decodeEntriesInsertedNotification(message);
break;
case HamokMessage_1.HamokMessage_MessageType.UPDATE_ENTRIES_REQUEST:
type = callback ? 'UpdateEntriesRequest' : undefined;
result = this.decodeUpdateEntriesRequest(message);
break;
case HamokMessage_1.HamokMessage_MessageType.UPDATE_ENTRIES_RESPONSE:
type = callback ? 'UpdateEntriesResponse' : undefined;
result = this.decodeUpdateEntriesResponse(message);
break;
case HamokMessage_1.HamokMessage_MessageType.UPDATE_ENTRIES_NOTIFICATION:
type = callback ? 'UpdateEntriesNotification' : undefined;
result = this.decodeUpdateEntriesNotification(message);
break;
case HamokMessage_1.HamokMessage_MessageType.ENTRY_UPDATED_NOTIFICATION:
type = callback ? 'EntryUpdatedNotification' : undefined;
result = this.decodeEntryUpdatedNotification(message);
break;
case HamokMessage_1.HamokMessage_MessageType.STORAGE_APPLIED_COMMIT_NOTIFICATION:
type = callback ? 'StorageAppliedCommitNotification' : undefined;
result = this.decodeStorageAppliedCommitNotification(message);
break;
case HamokMessage_1.HamokMessage_MessageType.STORAGE_HELLO_NOTIFICATION:
type = callback ? 'StorageHelloNotification' : undefined;
result = this.decodeStorageHelloNotification(message);
break;
case HamokMessage_1.HamokMessage_MessageType.STORAGE_STATE_NOTIFICATION:
type = callback ? 'StorageStateNotification' : undefined;
result = this.decodeStorageStateNotification(message);
break;
}
logger.trace('Decoded message %o', message);
if (!result) {
throw new Error(`Cannot decode message ${message.type}`);
}
if (type && callback) {
callback(type, result);
}
return result;
}
encodeStorageHelloNotification(notification) {
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.STORAGE_HELLO_NOTIFICATION,
sourceId: notification.sourceEndpointId,
});
}
decodeStorageHelloNotification(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.STORAGE_HELLO_NOTIFICATION) {
throw new Error('decodeStorageCreatedNotification(): Message type must be STORAGE_HELLO_NOTIFICATION');
}
return new StorageHelloNotification_1.StorageHelloNotification(message.sourceId);
}
encodeStorageStateNotification(storageState) {
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.STORAGE_STATE_NOTIFICATION,
sourceId: storageState.sourceEndpointId,
snapshot: storageState.serializedStorageSnapshot ? StorageCodec.strCodec.encode(storageState.serializedStorageSnapshot) : undefined,
raftCommitIndex: storageState.commitIndex,
});
}
decodeStorageStateNotification(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.STORAGE_STATE_NOTIFICATION) {
throw new Error('decodeStorageSnapshot(): Message type must be STORAGE_STATE_NOTIFICATION');
}
return new StorageStateNotification_1.StorageStateNotification(message.sourceId, message.raftCommitIndex, message.snapshot ? StorageCodec.strCodec.decode(message.snapshot) : undefined);
}
encodeClearEntriesRequest(request) {
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.CLEAR_ENTRIES_REQUEST,
requestId: request.requestId,
sourceId: request.sourceEndpointId,
});
}
decodeClearEntriesRequest(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.CLEAR_ENTRIES_REQUEST) {
throw new Error('decodeClearEntriesRequest(): Message type must be CLEAR_ENTRIES_REQUEST');
}
return new ClearEntries_1.ClearEntriesRequest(message.requestId, message.sourceId);
}
encodeClearEntriesResponse(response) {
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.CLEAR_ENTRIES_RESPONSE,
requestId: response.requestId,
destinationId: response.destinationEndpointId,
});
}
decodeClearEntriesResponse(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.CLEAR_ENTRIES_RESPONSE) {
throw new Error('decodeClearEntriesResponse(): Message type must be CLEAR_ENTRIES_RESPONSE');
}
return new ClearEntries_1.ClearEntriesResponse(message.requestId, message.destinationId);
}
encodeClearEntriesNotification(notification) {
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.CLEAR_ENTRIES_NOTIFICATION,
sourceId: notification.sourceEndpointId,
destinationId: notification.destinationEndpointId,
});
}
decodeClearEntriesNotification(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.CLEAR_ENTRIES_NOTIFICATION) {
throw new Error('decodeClearEntriesNotification(): Message type must be CLEAR_ENTRIES_NOTIFICATION');
}
return new ClearEntries_1.ClearEntriesNotification(message.sourceId, message.destinationId);
}
encodeGetEntriesRequest(request) {
const keys = this.encodeKeys(request.keys);
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.GET_ENTRIES_REQUEST,
requestId: request.requestId,
keys,
sourceId: request.sourceEndpointId,
});
}
decodeGetEntriesRequest(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.GET_ENTRIES_REQUEST) {
throw new Error('decodeGetEntriesRequest(): Message type must be GET_ENTRIES_REQUEST');
}
const keys = this.decodeKeys(message.keys);
return new GetEntries_1.GetEntriesRequest(keys, message.requestId, message.sourceId);
}
encodeGetEntriesResponse(response) {
const [keys, values] = this.encodeEntries(response.foundEntries);
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.GET_ENTRIES_RESPONSE,
requestId: response.requestId,
keys,
values,
destinationId: response.destinationEndpointId,
});
}
decodeGetEntriesResponse(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.GET_ENTRIES_RESPONSE) {
throw new Error('decodeGetEntriesResponse(): Message type must be GET_ENTRIES_RESPONSE');
}
const foundEntries = this.decodeEntries(message.keys, message.values);
return new GetEntries_1.GetEntriesResponse(message.requestId, foundEntries, message.destinationId);
}
encodeGetSizeRequest(request) {
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.GET_SIZE_REQUEST,
requestId: request.requestId,
sourceId: request.sourceEndpointId
});
}
decodeGetSizeRequest(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.GET_SIZE_REQUEST) {
throw new Error('decodeGetSizeRequest(): Message type must be GET_SIZE_REQUEST');
}
return new GetSize_1.GetSizeRequest(message.requestId, message.sourceId);
}
encodeGetSizeResponse(response) {
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.GET_SIZE_RESPONSE,
requestId: response.requestId,
storageSize: response.size,
destinationId: response.destinationEndpointId,
});
}
decodeGetSizeResponse(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.GET_SIZE_RESPONSE) {
throw new Error('decodeGetSizeResponse(): Message type must be GET_SIZE_RESPONSE');
}
return new GetSize_1.GetSizeResponse(message.requestId, message.storageSize, message.destinationId);
}
encodeGetKeysRequest(request) {
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.GET_KEYS_REQUEST,
requestId: request.requestId,
sourceId: request.sourceEndpointId
});
}
decodeGetKeysRequest(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.GET_KEYS_REQUEST) {
throw new Error('decodeGetKeysRequest(): Message type must be GET_KEYS_REQUEST');
}
return new GetKeys_1.GetKeysRequest(message.requestId, message.sourceId);
}
encodeGetKeysResponse(response) {
const keys = this.encodeKeys(response.keys);
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.GET_KEYS_RESPONSE,
requestId: response.requestId,
destinationId: response.destinationEndpointId,
keys
});
}
decodeGetKeysResponse(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.GET_KEYS_RESPONSE) {
throw new Error('decodeGetKeysResponse(): Message type must be GET_ENTRIES_RESPONSE');
}
const keys = this.decodeKeys(message.keys);
return new GetKeys_1.GetKeysResponse(message.requestId, keys, message.destinationId);
}
encodeDeleteEntriesRequest(request) {
const keys = this.encodeKeys(request.keys);
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.DELETE_ENTRIES_REQUEST,
requestId: request.requestId,
keys,
sourceId: request.sourceEndpointId
});
}
decodeDeleteEntriesRequest(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.DELETE_ENTRIES_REQUEST) {
throw new Error('decodeDeleteEntriesRequest(): Message type must be DELETE_ENTRIES_REQUEST');
}
const keys = this.decodeKeys(message.keys);
return new DeleteEntries_1.DeleteEntriesRequest(message.requestId, keys, message.sourceId);
}
encodeDeleteEntriesResponse(response) {
const keys = this.encodeKeys(response.deletedKeys);
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.DELETE_ENTRIES_RESPONSE,
requestId: response.requestId,
keys,
destinationId: response.destinationEndpointId
});
}
decodeDeleteEntriesResponse(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.DELETE_ENTRIES_RESPONSE) {
throw new Error('decodeDeleteEntriesResponse(): Message type must be DELETE_ENTRIES_RESPONSE');
}
const deletedKeys = this.decodeKeys(message.keys);
return new DeleteEntries_1.DeleteEntriesResponse(message.requestId, deletedKeys, message.destinationId);
}
encodeDeleteEntriesNotification(notification) {
const keys = this.encodeKeys(notification.keys);
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.DELETE_ENTRIES_NOTIFICATION,
keys,
sourceId: notification.sourceEndpointId,
destinationId: notification.destinationEndpointId
});
}
decodeDeleteEntriesNotification(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.DELETE_ENTRIES_NOTIFICATION) {
throw new Error('decodeDeleteNotification(): Message type must be DELETE_ENTRIES_NOTIFICATION');
}
const keys = this.decodeKeys(message.keys);
return new DeleteEntries_1.DeleteEntriesNotification(keys, message.sourceId, message.destinationId);
}
encodeRemoveEntriesRequest(request) {
const keys = this.encodeKeys(request.keys);
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.REMOVE_ENTRIES_REQUEST,
requestId: request.requestId,
keys,
sourceId: request.sourceEndpointId,
prevValue: request.prevValue !== undefined ? this.valueCodec.encode(request.prevValue) : undefined,
});
}
decodeRemoveEntriesRequest(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.REMOVE_ENTRIES_REQUEST) {
throw new Error('decodeRemoveRequest(): Message type must be REMOVE_ENTRIES_REQUEST');
}
const keys = this.decodeKeys(message.keys);
return new RemoveEntries_1.RemoveEntriesRequest(message.requestId, keys, message.prevValue !== undefined ? this.valueCodec.decode(message.prevValue) : undefined, message.sourceId);
}
encodeRemoveEntriesResponse(response) {
const [keys, values] = this.encodeEntries(response.removedEntries);
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.REMOVE_ENTRIES_RESPONSE,
requestId: response.requestId,
keys,
values,
destinationId: response.destinationEndpointId
});
}
decodeRemoveEntriesResponse(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.REMOVE_ENTRIES_RESPONSE) {
throw new Error('decodeRemoveResponse(): Message type must be REMOVE_ENTRIES_RESPONSE');
}
const removedEntries = this.decodeEntries(message.keys, message.values);
return new RemoveEntries_1.RemoveEntriesResponse(message.requestId, removedEntries, message.destinationId);
}
encodeRemoveEntriesNotification(notification) {
const keys = this.encodeKeys(notification.keys);
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.REMOVE_ENTRIES_NOTIFICATION,
keys,
sourceId: notification.sourceEndpointId,
destinationId: notification.destinationEndpointId
});
}
decodeRemoveEntriesNotification(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.REMOVE_ENTRIES_NOTIFICATION) {
throw new Error('decodeRemoveNotification(): Message type must be REMOVE_ENTRIES_NOTIFICATION');
}
const keys = this.decodeKeys(message.keys);
return new RemoveEntries_1.RemoveEntriesNotification(keys, message.sourceId, message.destinationId);
}
encodeEntriesRemovedNotification(notification) {
const [keys, values] = this.encodeEntries(notification.entries);
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.ENTRIES_REMOVED_NOTIFICATION,
keys,
values,
sourceId: notification.sourceEndpointId,
destinationId: notification.destinationEndpointId,
});
}
decodeEntriesRemovedNotification(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.ENTRIES_REMOVED_NOTIFICATION) {
throw new Error('decodeEntriesRemovedNotification(): Message type must be ENTRIES_REMOVED_NOTIFICATION');
}
const entries = this.decodeEntries(message.keys, message.values);
return new RemoveEntries_1.EntriesRemovedNotification(entries, message.sourceId, message.destinationId);
}
encodeInsertEntriesRequest(request) {
const [keys, values] = this.encodeEntries(request.entries);
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.INSERT_ENTRIES_REQUEST,
requestId: request.requestId,
keys,
values,
sourceId: request.sourceEndpointId
});
}
decodeInsertEntriesRequest(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.INSERT_ENTRIES_REQUEST) {
throw new Error('decodeInsertRequest(): Message type must be INSERT_ENTRIES_REQUEST');
}
const entries = this.decodeEntries(message.keys, message.values);
return new InsertEntries_1.InsertEntriesRequest(message.requestId, entries, message.sourceId);
}
encodeInsertEntriesResponse(response) {
const [keys, values] = this.encodeEntries(response.existingEntries);
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.INSERT_ENTRIES_RESPONSE,
requestId: response.requestId,
keys,
values,
destinationId: response.destinationEndpointId
});
}
decodeInsertEntriesResponse(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.INSERT_ENTRIES_RESPONSE) {
throw new Error('decodeInsertResponse(): Message type must be INSERT_ENTRIES_RESPONSE');
}
const entries = this.decodeEntries(message.keys, message.values);
return new InsertEntries_1.InsertEntriesResponse(message.requestId, entries, message.destinationId);
}
encodeInsertEntriesNotification(notification) {
const [keys, values] = this.encodeEntries(notification.entries);
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.INSERT_ENTRIES_NOTIFICATION,
sourceId: notification.sourceEndpointId,
keys,
values,
destinationId: notification.destinationEndpointId
});
}
decodeInsertEntriesNotification(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.INSERT_ENTRIES_NOTIFICATION) {
throw new Error('decodeInsertNotification(): Message type must be INSERT_ENTRIES_NOTIFICATION');
}
const entries = this.decodeEntries(message.keys, message.values);
return new InsertEntries_1.InsertEntriesNotification(entries, message.sourceId, message.destinationId);
}
encodeEntriesInsertedNotification(notification) {
const [keys, values] = this.encodeEntries(notification.entries);
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.ENTRIES_INSERTED_NOTIFICATION,
keys,
values,
sourceId: notification.sourceEndpointId,
destinationId: notification.destinationEndpointId,
});
}
decodeEntriesInsertedNotification(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.ENTRIES_INSERTED_NOTIFICATION) {
throw new Error('decodeEntriesInsertedNotification(): Message type must be ENTRIES_INSERTED_NOTIFICATION');
}
const entries = this.decodeEntries(message.keys, message.values);
return new InsertEntries_1.EntriesInsertedNotification(entries, message.sourceId, message.destinationId);
}
encodeUpdateEntriesRequest(request) {
const [keys, values] = this.encodeEntries(request.entries);
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.UPDATE_ENTRIES_REQUEST,
sourceId: request.sourceEndpointId,
requestId: request.requestId,
keys,
values,
// prevValue: request.prevValue !== undefined ? this.valueCodec.encode(request.prevValue) : undefined,
prevValue: request.prevValue !== undefined ? this.valueCodec.encode(request.prevValue) : undefined,
});
}
decodeUpdateEntriesRequest(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.UPDATE_ENTRIES_REQUEST) {
throw new Error('decodeUpdateEntriesRequest(): Message type must be UPDATE_ENTRIES_REQUEST');
}
const entries = this.decodeEntries(message.keys, message.values);
return new UpdateEntries_1.UpdateEntriesRequest(message.requestId, entries, message.sourceId, message.prevValue !== undefined ? this.valueCodec.decode(message.prevValue) : undefined);
}
encodeUpdateEntriesResponse(response) {
const [keys, values] = this.encodeEntries(response.updatedEntries);
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.UPDATE_ENTRIES_RESPONSE,
requestId: response.requestId,
keys,
values,
destinationId: response.destinationEndpointId,
});
}
decodeUpdateEntriesResponse(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.UPDATE_ENTRIES_RESPONSE) {
throw new Error('decodeUpdateEntriesResponse(): Message type must be UPDATE_ENTRIES_RESPONSE');
}
const updatedEntries = this.decodeEntries(message.keys, message.values);
return new UpdateEntries_1.UpdateEntriesResponse(message.requestId, updatedEntries, message.destinationId);
}
encodeUpdateEntriesNotification(notification) {
const [keys, values] = this.encodeEntries(notification.updatedEntries);
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.UPDATE_ENTRIES_NOTIFICATION,
keys,
values,
sourceId: notification.sourceEndpointId,
destinationId: notification.destinationEndpointId,
});
}
decodeUpdateEntriesNotification(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.UPDATE_ENTRIES_NOTIFICATION) {
throw new Error('decodeUpdateEntriesResponse(): Message type must be UPDATE_ENTRIES_RESPONSE');
}
const updatedEntries = this.decodeEntries(message.keys, message.values);
return new UpdateEntries_1.UpdateEntriesNotification(updatedEntries, message.sourceId, message.destinationId);
}
encodeEntryUpdatedNotification(notification) {
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.ENTRY_UPDATED_NOTIFICATION,
keys: [this.keyCodec.encode(notification.key)],
values: [this.valueCodec.encode(notification.newValue)],
prevValue: this.valueCodec.encode(notification.oldValue),
sourceId: notification.sourceEndpointId,
destinationId: notification.destinationEndpointId,
});
}
decodeEntryUpdatedNotification(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.ENTRY_UPDATED_NOTIFICATION) {
throw new Error('decodeEntriesUpdatedNotification(): Message type must be ENTRY_UPDATED_NOTIFICATION');
}
else if (message.keys.length < 1) {
throw new Error('decodeEntriesUpdatedNotification(): Message must have at least one key');
}
else if (message.values.length < 1) {
throw new Error('decodeEntriesUpdatedNotification(): Message must have at least one value');
}
else if (message.prevValue === undefined) {
throw new Error('decodeEntriesUpdatedNotification(): Message must have a prevValue');
}
const key = this.keyCodec.decode(message.keys[0]);
const newValue = this.valueCodec.decode(message.values[0]);
const oldValue = this.valueCodec.decode(message.prevValue);
return new UpdateEntries_1.EntryUpdatedNotification(key, newValue, oldValue, message.sourceId, message.destinationId);
}
encodeStorageAppliedCommitNotification(notification) {
return new HamokMessage_1.HamokMessage({
type: HamokMessage_1.HamokMessage_MessageType.STORAGE_APPLIED_COMMIT_NOTIFICATION,
raftCommitIndex: notification.appliedCommitIndex,
sourceId: notification.sourceEndpointId,
});
}
decodeStorageAppliedCommitNotification(message) {
if (message.type !== HamokMessage_1.HamokMessage_MessageType.STORAGE_APPLIED_COMMIT_NOTIFICATION) {
throw new Error('decodeStorageAppliedCommitNotification(): Message type must be STORAGE_APPLIED_COMMIT_NOTIFICATION');
}
return new StorageAppliedCommit_1.StorageAppliedCommitNotification(message.raftCommitIndex, message.sourceId);
}
encodeKeys(keys) {
return (0, HamokCodec_1.encodeSet)(keys, this.keyCodec);
}
decodeKeys(keys) {
return (0, HamokCodec_1.decodeSet)(keys, this.keyCodec);
}
encodeEntries(entries) {
return (0, HamokCodec_1.encodeMap)(entries, this.keyCodec, this.valueCodec);
}
decodeEntries(keys, values) {
return (0, HamokCodec_1.decodeMap)(keys, values, this.keyCodec, this.valueCodec);
}
}
exports.StorageCodec = StorageCodec;