Skip to content

Commit

Permalink
fix: tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw committed Mar 18, 2024
1 parent 371fef2 commit 373e485
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 75 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@
"@ipld/car": "^5.2.4",
"@ipld/dag-cbor": "^9.0.6",
"archy": "^1.0.0",
"cborg": "^4.0.7",
"cli-color": "^2.0.3",
"multiformats": "^12.1.3",
"sade": "^1.8.1"
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions src/batch/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ export {

export interface BatcherShard extends ShardConfig {
base?: ShardBlockView
prefix: string
entries: BatcherShardEntry[]
}

export interface BatcherShardInit extends ShardOptions {
base?: ShardBlockView
prefix?: string
entries?: BatcherShardEntry[]
}

Expand Down
25 changes: 13 additions & 12 deletions src/batch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@ class Batcher {
* @param {API.BlockFetcher} init.blocks Block storage.
* @param {API.BatcherShardEntry[]} init.entries The entries in this shard.
* @param {string} init.prefix Key prefix.
* @param {number} init.maxSize
* @param {number} init.maxKeyLength
* @param {number} init.version Shard compatibility version.
* @param {string} init.keyChars Characters allowed in keys, referring to a known character set.
* @param {number} init.maxKeySize Max key size in bytes.
* @param {API.ShardBlockView} init.base Original shard this batcher is based on.
*/
constructor ({ blocks, entries, prefix, maxSize, maxKeyLength, base }) {
constructor ({ blocks, entries, prefix, version, keyChars, maxKeySize, base }) {
this.blocks = blocks
this.prefix = prefix
this.entries = [...entries]
this.base = base
this.maxSize = maxSize
this.maxKeyLength = maxKeyLength
this.version = version
this.keyChars = keyChars
this.maxKeySize = maxKeySize
}

/**
Expand All @@ -46,12 +48,11 @@ class Batcher {
* @param {object} init
* @param {API.BlockFetcher} init.blocks Block storage.
* @param {API.ShardLink} init.link CID of the shard block.
* @param {string} init.prefix
*/
static async create ({ blocks, link, prefix }) {
static async create ({ blocks, link }) {
const shards = new ShardFetcher(blocks)
const base = await shards.get(link)
return new Batcher({ blocks, prefix, base, ...base.value })
return new Batcher({ blocks, base, ...base.value })
}
}

Expand Down Expand Up @@ -169,8 +170,8 @@ export const traverse = async (shards, key, shard) => {
if (key <= k) break
if (key.startsWith(k) && Array.isArray(v)) {
if (Shard.isShardLink(v[0])) {
const blk = await shards.get(v[0], shard.prefix + k)
const batcher = BatcherShard.create({ base: blk, prefix: blk.prefix, ...blk.value })
const blk = await shards.get(v[0])
const batcher = BatcherShard.create({ base: blk, ...blk.value })
shard.entries[i] = [k, v[1] == null ? [batcher] : [batcher, v[1]]]
return traverse(shards, key.slice(k.length), batcher)
}
Expand Down Expand Up @@ -208,7 +209,7 @@ export const commit = async shard => {
}
}

const block = await Shard.encodeBlock(Shard.withEntries(entries, shard), shard.prefix)
const block = await Shard.encodeBlock(Shard.withEntries(entries, shard))
additions.push(block)

if (shard.base && shard.base.cid.toString() === block.cid.toString()) {
Expand All @@ -231,7 +232,7 @@ const asShardEntry = entry => /** @type {API.ShardEntry} */ (entry)
* @param {API.ShardLink} root CID of the root shard block.
* @returns {Promise<API.Batcher>}
*/
export const create = (blocks, root) => Batcher.create({ blocks, link: root, prefix: '' })
export const create = (blocks, root) => Batcher.create({ blocks, link: root })

export class BatchCommittedError extends Error {
/**
Expand Down
Empty file added src/batch/index2.js
Empty file.
52 changes: 0 additions & 52 deletions src/batch/shard.js
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)
}
1 change: 0 additions & 1 deletion src/crdt/batch/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
Batcher,
BatcherShardEntry,
ShardDiff,
ShardBlockView,
BlockFetcher,
ShardLink,
Expand Down
5 changes: 1 addition & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
// eslint-disable-next-line no-unused-vars
import * as API from './api.js'
import { ShardFetcher } from './shard.js'
import { ShardFetcher, isPrintableASCII } from './shard.js'
import * as Shard from './shard.js'

/** @param {string} s */
const isPrintableASCII = s => /^[\x20-\x7E]*$/.test(s)

/**
* Put a value (a CID) for the given key. If the key exists it's value is
* overwritten.
Expand Down
3 changes: 3 additions & 0 deletions src/shard.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,6 @@ export const putEntry = (target, newEntry) => {

return entries
}

/** @param {string} s */
export const isPrintableASCII = s => /^[\x20-\x7E]*$/.test(s)

0 comments on commit 373e485

Please sign in to comment.