@x5e/gink
Version:
an eventually consistent database
228 lines • 10.7 kB
JavaScript
;
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
function fulfill(value) { resume("next", value); }
function reject(value) { resume("throw", value); }
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Group = void 0;
const Container_1 = require("./Container");
const Bundler_1 = require("./Bundler");
const utils_1 = require("./utils");
const factories_1 = require("./factories");
const builders_1 = require("./builders");
class Group extends Container_1.Container {
constructor(database, address, containerBuilder) {
super(database, address, builders_1.Behavior.GROUP);
if (this.address.timestamp < 0) {
(0, utils_1.ensure)(address.offset === builders_1.Behavior.GROUP);
}
else {
(0, utils_1.ensure)(containerBuilder.getBehavior() === builders_1.Behavior.GROUP);
}
}
/**
* Includes a Muid or Container in the group.
* @param key either a container or a Muid to include
* @param change an optional bundler to put this change into
* @returns a promise that resolves to the Muid for the inclusion
*/
async include(key, change) {
return await this.addEntry(key, Container_1.Container.INCLUSION, change);
}
/**
* Excludes a Muid or Container from the group.
* @param key either a Muid or container to exclude
* @param change an optional bundler to put this in
* @returns a promise that resolves to the Muid for the exclusion
*/
async exclude(key, change) {
return await this.addEntry(key, Container_1.Container.DELETION, change);
}
/**
* This returns the number of inclusions only, NOT exclusions.
* @returns how many containers are included in the group
*/
async size() {
return (await this.includedAsArray()).length;
}
/**
* Whether or not the given key is explicitly included in the group.
* @param key either a Muid or container to check if it is included
* @param asOf optional timestamp to look back to
* @returns a promise that resolves to a boolean stating whether the key is explicitly included
*/
async isIncluded(key, asOf) {
if ("address" in key) {
key = key.address;
}
const entry = await this.database.store.getEntryByKey(this.address, key, asOf);
if (entry && !entry.deletion) {
return true;
}
return false;
}
/**
* Function to iterate over the containers in the group.
* @param asOf optional timestamp to look back to
* @returns an async iterator across all containers in the group
*/
getMembers(asOf) {
const thisGroup = this;
let container;
return (function () {
return __asyncGenerator(this, arguments, function* () {
const entries = yield __await(thisGroup.database.store.getKeyedEntries(thisGroup.address, asOf));
for (const [key, entry] of entries) {
container = yield __await((0, factories_1.interpret)(entry, thisGroup.database));
if ("behavior" in container) {
yield yield __await(container);
}
}
});
})();
}
/**
* Dumps the contents of this group to a javascript array.Only includes explicitly included members.
* useful for debugging and could also be used to export data by walking the tree
* @param asOf effective time to get the dump for: leave undefined to get data as of the present
* @returns an array containing Values (e.g. numbers, strings) and Containers (e.g. other Lists, Boxes, Directories)
*/
async includedAsArray(asOf) {
const thisList = this;
let toArray = [];
let container;
const entries = await thisList.database.store.getKeyedEntries(thisList.address, asOf);
for (const [key, entry] of entries) {
container = await (0, factories_1.interpret)(entry, thisList.database);
if ("behavior" in container) {
toArray.push(container);
}
else {
throw Error("All entries should be containers - something is broken");
}
}
return toArray;
}
/**
*
* @param args Optional arguments, including:
* @argument toTime Optional time to reset to. If absent, the container will be cleared.
* @argument bundlerOrComment Optional bundler or comment to add this change to
* @argument skipProperties If true, do not reset properties of this container. By default,
* all properties associated with this container will be reset to the time specified in toTime.
* @argument recurse NOTE: THIS FLAG IS IGNORED. Recursive reset for Inclusion-based containers
* is not yet implemented, but this arg needs to be accepted for other containers recursively
* resetting this one.
* @argument seen NOTE: THIS FLAG IS IGNORED. Recursive reset for Inclusion-based containers is
* not yet implemented, but this arg needs to be accepted for other containers recursively
* resetting this one.
*/
async reset(args) {
const toTime = args === null || args === void 0 ? void 0 : args.toTime;
const bundlerOrComment = args === null || args === void 0 ? void 0 : args.bundlerOrComment;
const skipProperties = args === null || args === void 0 ? void 0 : args.skipProperties;
let immediate = false;
let bundler;
if (bundlerOrComment instanceof Bundler_1.Bundler) {
bundler = bundlerOrComment;
}
else {
immediate = true;
bundler = new Bundler_1.Bundler(bundlerOrComment);
}
if (!toTime) {
// If no time is specified, we are resetting to epoch, which is just a clear
this.clear(false, bundler);
}
else {
const union = new Set();
const entriesThen = await this.database.store.getKeyedEntries(this.address, toTime);
const entriesNow = await this.database.store.getKeyedEntries(this.address);
for (const [key, entry] of entriesThen) {
const storageKey = entry.storageKey;
union.add(storageKey);
}
for (const [key, entry] of entriesNow) {
const storageKey = entry.storageKey;
union.add(storageKey);
}
for (const key of union) {
const genericKey = (0, utils_1.fromStorageKey)(key);
const thenEntry = await this.database.store.getEntryByKey(this.address, genericKey, toTime);
const nowEntry = await this.database.store.getEntryByKey(this.address, genericKey);
(0, utils_1.ensure)(nowEntry || thenEntry, "both then and now undefined?");
if (!nowEntry) {
// This key was present then, but not now, so we need to add it back
(0, utils_1.ensure)(thenEntry, "missing then entry?");
await this.addEntry(genericKey, thenEntry.value, bundler);
}
else if (!thenEntry) {
// This key is present now, but not then, so we need to delete it
(0, utils_1.ensure)(nowEntry, "missing now entry?");
await this.addEntry(genericKey, Container_1.Container.DELETION, bundler);
}
else if (nowEntry.deletion !== thenEntry.deletion) {
if (nowEntry.deletion) {
// Present then, deleted now. Need to revive.
await this.addEntry(genericKey, Container_1.Container.INCLUSION, bundler);
}
else if (thenEntry.deletion) {
// Present now, deleted then. Need to delete.
await this.addEntry(genericKey, Container_1.Container.DELETION, bundler);
}
}
else {
(0, utils_1.ensure)(nowEntry.deletion === thenEntry.deletion, "last case should be same entry");
}
}
}
if (!skipProperties) {
await this.resetProperties(toTime, bundler);
}
if (immediate) {
await this.database.addBundler(bundler);
}
}
/**
* Generates a JSON representation of the data in the group.
* Mostly intended for demo/debug purposes.
* @param indent true to pretty print (not yet implemented)
* @param asOf optional timestamp to look back to
* @param seen (internal use only! This prevents cycles from breaking things)
* @returns a JSON string
*/
async toJson(indent = false, asOf, seen) {
//TODO(https://github.com/google/gink/issues/62): add indentation
(0, utils_1.ensure)(indent === false, "indent not implemented");
if (seen === undefined)
seen = new Set();
const mySig = (0, utils_1.muidToString)(this.address);
if (seen.has(mySig))
return "null";
seen.add(mySig);
const asArray = await this.includedAsArray(asOf);
let returning = "[";
let first = true;
for (const container of asArray) {
if (first) {
first = false;
}
else {
returning += ",";
}
returning += await (0, factories_1.toJson)(container, indent === false ? false : +indent + 1, asOf, seen);
}
returning += "]";
return returning;
}
}
exports.Group = Group;
//# sourceMappingURL=Group.js.map