-
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.
- Loading branch information
Alan Shaw
committed
Mar 18, 2024
1 parent
371fef2
commit 373e485
Showing
9 changed files
with
17 additions
and
75 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Empty file.
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,65 +1,13 @@ | ||
// 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 ?? []], | ||
...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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
import { | ||
Batcher, | ||
BatcherShardEntry, | ||
ShardDiff, | ||
ShardBlockView, | ||
BlockFetcher, | ||
ShardLink, | ||
|
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