You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.8 KiB
JavaScript

"use strict";
const sortedBtree = require("sorted-btree").default;
const { toBigint, toBuffer } = require("../storage-encoder/bigint/buffer");
// TODO: Implement specific safety behaviours:
// - consistent view during range iteration (via shallow clone?)
// - consistent view during transactions (via shallow clone?)
module.exports = function createMemoryBackend(_options) {
let store = new sortedBtree();
return {
getKey: function (key) {
// return value, or throw if non-existent
return store.get(toBigint(key));
},
putKey: function (key, value) {
// TODO: compare-and-set in API?
store.set(toBigint(key), value);
},
getKeyRange: function* (lowestKey, lowestInclusive, highestKey, highestInclusive) {
let $lowestKey = toBigint(lowestKey);
let $highestKey = toBigint(highestKey);
// inclusive on both ends! return iterator/stream of {key,value} pairs
let firstValue = (lowestInclusive)
? store.get($lowestKey)
: undefined; // FIXME: Check .has instead
if (firstValue != null) {
yield { key: lowestKey, value: firstValue };
}
let currentKey = $lowestKey;
while (true) {
let pair = store.nextHigherPair(currentKey);
if (pair != null) {
let [ key, value ] = pair;
if (
(key === $highestKey && !highestInclusive)
|| (key > $highestKey)
) {
break;
} else {
currentKey = key;
yield { key: toBuffer(key), value: value };
}
} else {
// We've run out of items
break;
}
}
},
runInTransaction: function (callback) {
}
};
};
/*
storeRecord(collection, id)
deleteRecord(collection, id)
hasRecord(collection, id)
fetchRecord(collection, id)
fetchRecords(collection, lowestID, highestID)
addToIndex(index, id)
removeFromIndex(index, id)
fetchIndexRecord(index, id)
fetchIndexRecords(index, lowestID, highestID)
*/