Skip to content

Commit

Permalink
refactor: address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw committed Apr 30, 2024
1 parent 1e4b9ad commit a52f48d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 52 deletions.
1 change: 1 addition & 0 deletions packages/upload-api/src/index/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { MultihashDigest, UnknownLink } from 'multiformats'

export type { Result } from '@ucanto/interface'
export type { UnknownFormat } from '@web3-storage/capabilities/types'
export type { MultihashDigest, UnknownLink }

export type ShardDigest = MultihashDigest
export type SliceDigest = MultihashDigest
Expand Down
65 changes: 31 additions & 34 deletions packages/upload-api/src/index/lib/digest-map.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { base58btc } from 'multiformats/bases/base58'

/** @type {WeakMap<import('multiformats').MultihashDigest, string>} */
/** @type {WeakMap<Uint8Array, string>} */
const cache = new WeakMap()

/** @param {import('multiformats').MultihashDigest} digest */
const toBase58String = (digest) => {
let str = cache.get(digest)
let str = cache.get(digest.bytes)
if (!str) {
str = base58btc.encode(digest.bytes)
cache.set(digest, str)
cache.set(digest.bytes, str)
}
return str
}
Expand All @@ -18,24 +18,26 @@ const toBase58String = (digest) => {
* @template Value
* @implements {Map<Key, Value>}
*/
export class DigestMap extends Map {
/** @type {Map<string, Key>} */
#keys
export class DigestMap {
/** @type {Map<string, [Key, Value]>} */
#data

/**
* @param {Array<[Key, Value]>} [entries]
*/
constructor(entries) {
super()
this.#keys = new Map()
this.#data = new Map()
for (const [k, v] of entries ?? []) {
this.set(k, v)
}
}

get [Symbol.toStringTag] () {
return 'DigestMap'
}

clear() {
super.clear()
this.#keys.clear()
this.#data.clear()
}

/**
Expand All @@ -44,52 +46,48 @@ export class DigestMap extends Map {
*/
delete(key) {
const mhstr = toBase58String(key)
super.delete(mhstr)
return this.#keys.delete(mhstr)
return this.#data.delete(mhstr)
}

/**
* @param {(value: Value, key: Key, map: Map<Key, Value>) => void} callbackfn
* @param {any} [thisArg]
*/
forEach(callbackfn, thisArg) {
super.forEach((v, k) => {
const key = this.#keys.get(k)
/* c8 ignore next line */
if (!key) throw new Error('internal inconsistency')
callbackfn.call(thisArg, v, key, this)
})
for (const [k, v] of this.#data.values()) {
callbackfn.call(thisArg, v, k, this)
}
}

/**
* @param {Key} key
* @returns {Value|undefined}
*/
get(key) {
return super.get(toBase58String(key))
const data = this.#data.get(toBase58String(key))
if (data) return data[1]
}

/**
* @param {Key} key
* @returns {boolean}
*/
has(key) {
return super.has(toBase58String(key))
return this.#data.has(toBase58String(key))
}

/**
* @param {Key} key
* @param {Value} value
*/
set(key, value) {
const mhstr = toBase58String(key)
this.#keys.set(mhstr, key)
return super.set(mhstr, value)
this.#data.set(toBase58String(key), [key, value])
return this
}

/** @returns {number} */
get size() {
return super.size
return this.#data.size
}

/** @returns */
Expand All @@ -99,21 +97,20 @@ export class DigestMap extends Map {

/** @returns {IterableIterator<[Key, Value]>} */
*entries() {
for (const [k, v] of super.entries()) {
const key = this.#keys.get(k)
/* c8 ignore next line */
if (!key) throw new Error('internal inconsistency')
yield [key, v]
}
yield * this.#data.values()
}

/** @returns {IterableIterator<Key>} */
keys() {
return this.#keys.values()
*keys() {
for (const [k] of this.#data.values()) {
yield k
}
}

/** @returns {IterableIterator<Value>} */
values() {
return super.values()
*values() {
for (const [,v] of this.#data.values()) {
yield v
}
}
}
37 changes: 19 additions & 18 deletions packages/upload-api/src/index/lib/sharded-dag-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,23 @@ export const extract = async (archive) => {
)
}

const rootBlock = blocks.get(header.roots[0])
if (!rootBlock) {
return error(
new DecodeFailure(
`failed to find root block in archive: ${header.roots[0]}`
)
)
return view({ root: header.roots[0], blocks })
}

/**
* @param {object} source
* @param {API.UnknownLink} source.root
* @param {Map<API.MultihashDigest, Uint8Array>} source.blocks
* @returns {API.Result<API.ShardedDAGIndex, API.DecodeFailure|API.UnknownFormat>}
*/
export const view = ({ root, blocks }) => {
const rootBytes = blocks.get(root.multihash)
if (!rootBytes) {
return error(new DecodeFailure(`missing root block: ${root}`))
}

const [version, dagIndexData] = ShardedDAGIndexSchema.match(
dagCBOR.decode(rootBlock.bytes)
dagCBOR.decode(rootBytes)
)
switch (version) {
case 'index/sharded/dag@0.1':
Expand All @@ -66,18 +72,13 @@ export const extract = async (archive) => {
shards: new DigestMap(),
}
for (const shard of dagIndexData.shards) {
const shardBlock = blocks.get(shard.multihash)
if (!shardBlock)
return error(
new DecodeFailure(
`failed to find shard block in archive: ${base58btc.encode(
shard.multihash.digest
)}`
)
)
const shardBytes = blocks.get(shard.multihash)
if (!shardBytes) {
return error(new DecodeFailure(`missing shard block: ${shard}`))
}

const blobIndexData = BlobIndexSchema.from(
dagCBOR.decode(shardBlock.bytes)
dagCBOR.decode(shardBytes)
)
const blobIndex = new DigestMap()
for (const [digest, offset, length] of blobIndexData[1]) {
Expand Down

0 comments on commit a52f48d

Please sign in to comment.