This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hamt.js
162 lines (137 loc) · 4.04 KB
/
hamt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// This is a modified find-cid-in-shard.js from ipfs-unixfs-exporter
import { Bucket, createHAMT } from 'hamt-sharding'
import { decode } from '@ipld/dag-pb'
import { murmur3128 } from '@multiformats/murmur3'
/**
* @typedef {{ cid: CID, bytes: Uint8Array }} Block
* @typedef {{ get: (key: CID) => Promise<Block|undefined> }} Blockstore
* @typedef {import('multiformats/cid').CID} CID
* @typedef {import('../types').ExporterOptions} ExporterOptions
* @typedef {import('@ipld/dag-pb').PBNode} PBNode
* @typedef {import('@ipld/dag-pb').PBLink} PBLink
*/
// FIXME: this is copy/pasted from ipfs-unixfs-importer/src/options.js
/**
* @param {Uint8Array} buf
*/
const hashFn = async function (buf) {
return (await murmur3128.encode(buf))
// Murmur3 outputs 128 bit but, accidentally, IPFS Go's
// implementation only uses the first 64, so we must do the same
// for parity..
.slice(0, 8)
// Invert buffer because that's how Go impl does it
.reverse()
}
/**
* @param {PBLink[]} links
* @param {Bucket<boolean>} bucket
* @param {Bucket<boolean>} rootBucket
*/
const addLinksToHamtBucket = (links, bucket, rootBucket) => {
return Promise.all(
links.map(link => {
if (link.Name == null) {
// TODO(@rvagg): what do? this is technically possible
throw new Error('Unexpected Link without a Name')
}
if (link.Name.length === 2) {
const pos = parseInt(link.Name, 16)
return bucket._putObjectAt(pos, new Bucket({
hash: rootBucket._options.hash,
bits: rootBucket._options.bits
}, bucket, pos))
}
return rootBucket.put(link.Name.substring(2), true)
})
)
}
/**
* @param {number} position
*/
const toPrefix = (position) => {
return position
.toString(16)
.toUpperCase()
.padStart(2, '0')
.substring(0, 2)
}
/**
* @param {import('hamt-sharding').Bucket.BucketPosition<boolean>} position
*/
const toBucketPath = (position) => {
let bucket = position.bucket
const path = []
while (bucket._parent) {
path.push(bucket)
bucket = bucket._parent
}
path.push(bucket)
return path.reverse()
}
/**
* @typedef {object} ShardTraversalContext
* @property {number} hamtDepth
* @property {Bucket<boolean>} rootBucket
* @property {Bucket<boolean>} lastBucket
*
* @param {PBNode} node
* @param {string} name
* @param {Blockstore} blockstore
* @param {ShardTraversalContext} [context]
* @param {{ signal: AbortSignal }} [options]
* @returns {AsyncIterable<Block>}
*/
export async function * findShardedBlock (node, name, blockstore, context, options) {
if (!context) {
const rootBucket = createHAMT({ hashFn })
context = {
rootBucket,
hamtDepth: 1,
lastBucket: rootBucket
}
}
await addLinksToHamtBucket(node.Links, context.lastBucket, context.rootBucket)
const position = await context.rootBucket._findNewBucketAndPos(name)
let prefix = toPrefix(position.pos)
const bucketPath = toBucketPath(position)
if (bucketPath.length > context.hamtDepth) {
context.lastBucket = bucketPath[context.hamtDepth]
prefix = toPrefix(context.lastBucket._posAtParent)
}
const link = node.Links.find(link => {
if (link.Name == null) {
return false
}
const entryPrefix = link.Name.substring(0, 2)
const entryName = link.Name.substring(2)
if (entryPrefix !== prefix) {
// not the entry or subshard we're looking for
return false
}
if (entryName && entryName !== name) {
// not the entry we're looking for
return false
}
return true
})
if (!link) {
return
}
if (link.Name != null && link.Name.substring(2) === name) {
const block = await blockstore.get(link.Hash, options)
if (!block) {
throw new Error(`missing block: ${link.Hash}`)
}
yield block
return
}
context.hamtDepth++
const block = await blockstore.get(link.Hash, options)
if (!block) {
throw new Error(`missing block: ${link.Hash}`)
}
yield block
node = decode(block.bytes)
yield * findShardedBlock(node, name, blockstore, context, options)
}