Skip to content

Commit

Permalink
fix: small fixes to nodes utils parsing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tegefaulkes authored and emmacasolin committed Jun 14, 2022
1 parent 891fb8c commit 376292a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 38 deletions.
31 changes: 14 additions & 17 deletions src/nodes/NodeGraph.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DB, DBTransaction, LevelPath } from '@matrixai/db';
import type { DB, DBTransaction, KeyPath, LevelPath } from '@matrixai/db';
import type {
NodeId,
NodeAddress,
Expand Down Expand Up @@ -191,14 +191,14 @@ class NodeGraph {
});
}

for await (const [key, nodeData] of tran.iterator<NodeData>(
for await (const [keyPath, nodeData] of tran.iterator<NodeData>(
{
reverse: order !== 'asc',
valueAsBuffer: false,
},
this.nodeGraphBucketsDbPath,
)) {
const { nodeId } = nodesUtils.parseBucketsDbKey(key as Array<Buffer>);
const { nodeId } = nodesUtils.parseBucketsDbKey(keyPath);
yield [nodeId, nodeData];
}
}
Expand Down Expand Up @@ -273,13 +273,11 @@ class NodeGraph {
const bucketKey = nodesUtils.bucketKey(bucketIndex);
// Remove the oldest entry in the bucket
const oldestNodeIds: Array<NodeId> = [];
for await (const [key] of tran.iterator({ limit }, [
for await (const [keyPath] of tran.iterator({ limit }, [
...this.nodeGraphLastUpdatedDbPath,
bucketKey,
])) {
const { nodeId } = nodesUtils.parseLastUpdatedBucketDbKey(
key as Array<Buffer>,
);
const { nodeId } = nodesUtils.parseLastUpdatedBucketDbKey(keyPath);
oldestNodeIds.push(nodeId);
}
return oldestNodeIds;
Expand Down Expand Up @@ -421,7 +419,7 @@ class NodeGraph {
this.nodeGraphBucketsDbPath,
)) {
const { bucketIndex: bucketIndex_, nodeId } =
nodesUtils.parseBucketsDbKey(key as Array<Buffer>);
nodesUtils.parseBucketsDbKey(key);
if (bucketIndex == null) {
// First entry of the first bucket
bucketIndex = bucketIndex_;
Expand Down Expand Up @@ -467,7 +465,7 @@ class NodeGraph {
this.nodeGraphLastUpdatedDbPath,
)) {
const { bucketIndex: bucketIndex_, nodeId } =
nodesUtils.parseLastUpdatedBucketsDbKey(key as Array<Buffer>);
nodesUtils.parseLastUpdatedBucketsDbKey(key);
bucketsDbIterator.seek([key[0], key[2]]);
// @ts-ignore
// eslint-disable-next-line
Expand Down Expand Up @@ -535,7 +533,7 @@ class NodeGraph {
)) {
// The key is a combined bucket key and node ID
const { bucketIndex: bucketIndexOld, nodeId } =
nodesUtils.parseBucketsDbKey(key as Array<Buffer>);
nodesUtils.parseBucketsDbKey(key);
const nodeIdEncoded = nodesUtils.encodeNodeId(nodeId);
const nodeIdKey = nodesUtils.bucketDbKey(nodeId);
// If the new own node ID is one of the existing node IDs, it is just dropped
Expand All @@ -555,18 +553,17 @@ class NodeGraph {
if (countNew < this.nodeBucketLimit) {
await tran.put([...metaPathNew, 'count'], countNew + 1);
} else {
let oldestIndexKey: Array<Buffer> | undefined = undefined;
let oldestIndexKey: KeyPath | undefined = undefined;
let oldestNodeId: NodeId | undefined = undefined;
for await (const [key] of tran.iterator(
{
limit: 1,
},
indexPathNew,
)) {
oldestIndexKey = key as Array<Buffer>;
({ nodeId: oldestNodeId } = nodesUtils.parseLastUpdatedBucketDbKey(
key as Array<Buffer>,
));
oldestIndexKey = key;
({ nodeId: oldestNodeId } =
nodesUtils.parseLastUpdatedBucketDbKey(key));
}
await tran.del([
...bucketPathNew,
Expand Down Expand Up @@ -730,7 +727,7 @@ class NodeGraph {
},
this.nodeGraphBucketsDbPath,
)) {
const info = nodesUtils.parseBucketsDbKey(key as Array<Buffer>);
const info = nodesUtils.parseBucketsDbKey(key);
nodeIds.push([info.nodeId, nodeData]);
}
}
Expand All @@ -754,7 +751,7 @@ class NodeGraph {
},
this.nodeGraphBucketsDbPath,
)) {
const info = nodesUtils.parseBucketsDbKey(key as Array<Buffer>);
const info = nodesUtils.parseBucketsDbKey(key);
nodeIds.push([info.nodeId, nodeData]);
}
}
Expand Down
41 changes: 20 additions & 21 deletions src/nodes/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type {
NodeId,
NodeIdEncoded,
NodeBucket,
NodeBucketIndex,
NodeId,
NodeIdEncoded,
} from './types';
import type { KeyPath } from '@matrixai/db';
import { IdInternal } from '@matrixai/id';
import lexi from 'lexicographic-integer';
import { utils as dbUtils } from '@matrixai/db';
Expand Down Expand Up @@ -143,18 +144,18 @@ function lastUpdatedKey(lastUpdated: number): Buffer {
* The keys look like `!<lexi<NodeBucketIndex, 'hex')>!<NodeId>`
* It is assumed that the `!` is the sublevel prefix.
*/
function parseBucketsDbKey(keyBufferArray: Array<Buffer>): {
function parseBucketsDbKey(keyPath: KeyPath): {
bucketIndex: NodeBucketIndex;
bucketKey: string;
nodeId: NodeId;
} {
const [bucketKeyBuffer, nodeIdBuffer] = keyBufferArray;
if (bucketKeyBuffer == null || nodeIdBuffer == null) {
const [bucketKeyPath, nodeIdKey] = keyPath;
if (bucketKeyPath == null || nodeIdKey == null) {
throw new TypeError('Buffer is not an NodeGraph buckets key');
}
const bucketKey = bucketKeyBuffer.toString();
const bucketKey = bucketKeyPath.toString();
const bucketIndex = lexi.unpack(bucketKey);
const nodeId = IdInternal.fromBuffer<NodeId>(nodeIdBuffer);
const nodeId = IdInternal.fromBuffer<NodeId>(Buffer.from(nodeIdKey));
return {
bucketIndex,
bucketKey,
Expand All @@ -167,33 +168,31 @@ function parseBucketsDbKey(keyBufferArray: Array<Buffer>): {
* The keys look like `<NodeId>`
*/
function parseBucketDbKey(keyBuffer: Buffer): NodeId {
const nodeId = IdInternal.fromBuffer<NodeId>(keyBuffer);
return nodeId;
return IdInternal.fromBuffer<NodeId>(keyBuffer);
}

/**
* Parse the NodeGraph index sublevel key
* The keys look like `!<lexi<NodeBucketIndex, 'hex')>!<lexi(lastUpdated, 'hex')>-<NodeIdString>`
* It is assumed that the `!` is the sublevel prefix.
*/
function parseLastUpdatedBucketsDbKey(keyBufferArray: Array<Buffer>): {
function parseLastUpdatedBucketsDbKey(keyPath: KeyPath): {
bucketIndex: NodeBucketIndex;
bucketKey: string;
lastUpdated: number;
nodeId: NodeId;
} {
const [bucketKeyBuffer, ...lastUpdatedBufferArray] = keyBufferArray;
if (bucketKeyBuffer == null || lastUpdatedBufferArray == null) {
const [bucketLevel, ...lastUpdatedKeyPath] = keyPath;
if (bucketLevel == null || lastUpdatedKeyPath == null) {
throw new TypeError('Buffer is not an NodeGraph index key');
}
const bucketKey = bucketKeyBuffer.toString();
const bucketKey = bucketLevel.toString();
const bucketIndex = lexi.unpack(bucketKey);
if (bucketIndex == null) {
throw new TypeError('Buffer is not an NodeGraph index key');
}
const { lastUpdated, nodeId } = parseLastUpdatedBucketDbKey(
lastUpdatedBufferArray,
);
const { lastUpdated, nodeId } =
parseLastUpdatedBucketDbKey(lastUpdatedKeyPath);
return {
bucketIndex,
bucketKey,
Expand All @@ -207,19 +206,19 @@ function parseLastUpdatedBucketsDbKey(keyBufferArray: Array<Buffer>): {
* The keys look like `<lexi(lastUpdated, 'hex')>-<NodeIdString>`
* It is assumed that the `!` is the sublevel prefix.
*/
function parseLastUpdatedBucketDbKey(keyBufferArray: Array<Buffer>): {
function parseLastUpdatedBucketDbKey(keyPath: KeyPath): {
lastUpdated: number;
nodeId: NodeId;
} {
const [lastUpdatedBuffer, nodeIdBuffer] = keyBufferArray;
if (lastUpdatedBuffer == null || nodeIdBuffer == null) {
const [lastUpdatedLevel, nodeIdKey] = keyPath;
if (lastUpdatedLevel == null || nodeIdKey == null) {
throw new TypeError('Buffer is not an NodeGraph index bucket key');
}
const lastUpdated = lexi.unpack(lastUpdatedBuffer.toString());
const lastUpdated = lexi.unpack(lastUpdatedLevel.toString());
if (lastUpdated == null) {
throw new TypeError('Buffer is not an NodeGraph index bucket key');
}
const nodeId = IdInternal.fromBuffer<NodeId>(nodeIdBuffer);
const nodeId = IdInternal.fromBuffer<NodeId>(Buffer.from(nodeIdKey));
return {
lastUpdated,
nodeId,
Expand Down

0 comments on commit 376292a

Please sign in to comment.