-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: mutated cached block and encoding length for batched shards
- Loading branch information
Alan Shaw
committed
Jan 25, 2024
1 parent
586fd06
commit 7c327d4
Showing
4 changed files
with
63 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,65 @@ | ||
// eslint-disable-next-line no-unused-vars | ||
import * as Link from 'multiformats/link' | ||
import { tokensToLength } from 'cborg/length' | ||
import { Token, Type } from 'cborg' | ||
import * as API from './api.js' | ||
import { configure } from '../shard.js' | ||
|
||
/** Byte length of a v1, dag-cbor, sha-256 CID */ | ||
const ShardLinkByteLength = 36 | ||
|
||
const CID_TAG = new Token(Type.tag, 42) | ||
|
||
/** | ||
* @param {API.BatcherShardInit} [init] | ||
* @returns {API.BatcherShard} | ||
*/ | ||
export const create = init => ({ | ||
base: init?.base, | ||
prefix: init?.prefix ?? '', | ||
entries: init?.entries ?? [], | ||
entries: [...init?.entries ?? []], | ||
...configure(init) | ||
}) | ||
|
||
/** @param {API.BatcherShard} shard */ | ||
export const encodedLength = (shard) => { | ||
let entriesLength = 0 | ||
for (const entry of shard.entries) { | ||
entriesLength += entryEncodedLength(entry) | ||
} | ||
const tokens = [ | ||
new Token(Type.map, 3), | ||
new Token(Type.string, 'entries'), | ||
new Token(Type.array, shard.entries.length), | ||
new Token(Type.string, 'maxKeyLength'), | ||
new Token(Type.uint, shard.maxKeyLength), | ||
new Token(Type.string, 'maxSize'), | ||
new Token(Type.uint, shard.maxSize) | ||
] | ||
return tokensToLength(tokens) + entriesLength | ||
} | ||
|
||
/** @param {API.BatcherShardEntry} entry */ | ||
const entryEncodedLength = entry => { | ||
const tokens = [ | ||
new Token(Type.array, entry.length), | ||
new Token(Type.string, entry[0]) | ||
] | ||
if (Array.isArray(entry[1])) { | ||
tokens.push(new Token(Type.array, entry[1].length)) | ||
for (const item of entry[1]) { | ||
tokens.push(CID_TAG) | ||
if (Link.isLink(item)) { | ||
tokens.push(new Token(Type.bytes, { length: item.byteLength + 1 })) | ||
} else { | ||
// `item is BatcherShard and does not have a CID yet, however, when it | ||
// does, it will be this long. | ||
tokens.push(new Token(Type.bytes, { length: ShardLinkByteLength + 1 })) | ||
} | ||
} | ||
} else { | ||
tokens.push(CID_TAG) | ||
tokens.push(new Token(Type.bytes, { length: entry[1].byteLength + 1 })) | ||
} | ||
return tokensToLength(tokens) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters