This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathhamt-utils.js
311 lines (261 loc) · 7.85 KB
/
hamt-utils.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import * as dagPB from '@ipld/dag-pb'
import {
Bucket,
createHAMT
} from 'hamt-sharding'
import { DirSharded } from './dir-sharded.js'
import debug from 'debug'
import { UnixFS } from 'ipfs-unixfs'
import last from 'it-last'
import { CID } from 'multiformats/cid'
import {
hamtHashCode,
hamtHashFn,
hamtBucketBits
} from './hamt-constants.js'
const log = debug('ipfs:mfs:core:utils:hamt-utils')
/**
* @typedef {import('multiformats/cid').CIDVersion} CIDVersion
* @typedef {import('ipfs-unixfs').Mtime} Mtime
* @typedef {import('../').MfsContext} MfsContext
* @typedef {import('@ipld/dag-pb').PBNode} PBNode
* @typedef {import('@ipld/dag-pb').PBLink} PBLink
*/
/**
* @param {MfsContext} context
* @param {PBLink[]} links
* @param {Bucket<any>} bucket
* @param {object} options
* @param {PBNode} options.parent
* @param {CIDVersion} options.cidVersion
* @param {boolean} options.flush
* @param {string} options.hashAlg
*/
export const updateHamtDirectory = async (context, links, bucket, options) => {
if (!options.parent.Data) {
throw new Error('Could not update HAMT directory because parent had no data')
}
// update parent with new bit field
const data = Uint8Array.from(bucket._children.bitField().reverse())
const node = UnixFS.unmarshal(options.parent.Data)
const dir = new UnixFS({
type: 'hamt-sharded-directory',
data,
fanout: bucket.tableSize(),
hashType: hamtHashCode,
mode: node.mode,
mtime: node.mtime
})
const hasher = await context.hashers.getHasher(options.hashAlg)
const parent = {
Data: dir.marshal(),
Links: links.sort((a, b) => (a.Name || '').localeCompare(b.Name || ''))
}
const buf = dagPB.encode(parent)
const hash = await hasher.digest(buf)
const cid = CID.create(options.cidVersion, dagPB.code, hash)
if (options.flush) {
await context.repo.blocks.put(cid, buf)
}
return {
node: parent,
cid,
size: links.reduce((sum, link) => sum + (link.Tsize || 0), buf.length)
}
}
/**
* @param {MfsContext} context
* @param {PBLink[]} links
* @param {Bucket<any>} rootBucket
* @param {Bucket<any>} parentBucket
* @param {number} positionAtParent
*/
export const recreateHamtLevel = async (context, links, rootBucket, parentBucket, positionAtParent) => {
// recreate this level of the HAMT
const bucket = new Bucket({
hash: rootBucket._options.hash,
bits: rootBucket._options.bits
}, parentBucket, positionAtParent)
parentBucket._putObjectAt(positionAtParent, bucket)
await addLinksToHamtBucket(context, links, bucket, rootBucket)
return bucket
}
/**
* @param {PBLink[]} links
*/
export const recreateInitialHamtLevel = async (links) => {
const bucket = createHAMT({
hashFn: hamtHashFn,
bits: hamtBucketBits
})
// populate sub bucket but do not recurse as we do not want to pull whole shard in
await Promise.all(
links.map(async link => {
const linkName = (link.Name || '')
if (linkName.length === 2) {
const pos = parseInt(linkName, 16)
const subBucket = new Bucket({
hash: bucket._options.hash,
bits: bucket._options.bits
}, bucket, pos)
bucket._putObjectAt(pos, subBucket)
return Promise.resolve()
}
return bucket.put(linkName.substring(2), {
size: link.Tsize,
cid: link.Hash
})
})
)
return bucket
}
/**
* @param {MfsContext} context
* @param {PBLink[]} links
* @param {Bucket<any>} bucket
* @param {Bucket<any>} rootBucket
*/
export const addLinksToHamtBucket = async (context, links, bucket, rootBucket) => {
await Promise.all(
links.map(async link => {
const linkName = (link.Name || '')
if (linkName.length === 2) {
log('Populating sub bucket', linkName)
const pos = parseInt(linkName, 16)
const block = await context.repo.blocks.get(link.Hash)
const node = dagPB.decode(block)
const subBucket = new Bucket({
hash: rootBucket._options.hash,
bits: rootBucket._options.bits
}, bucket, pos)
bucket._putObjectAt(pos, subBucket)
await addLinksToHamtBucket(context, node.Links, subBucket, rootBucket)
return Promise.resolve()
}
return rootBucket.put(linkName.substring(2), {
size: link.Tsize,
cid: link.Hash
})
})
)
}
/**
* @param {number} position
*/
export const toPrefix = (position) => {
return position
.toString(16)
.toUpperCase()
.padStart(2, '0')
.substring(0, 2)
}
/**
* @param {MfsContext} context
* @param {string} fileName
* @param {PBNode} rootNode
*/
export const generatePath = async (context, fileName, rootNode) => {
// start at the root bucket and descend, loading nodes as we go
const rootBucket = await recreateInitialHamtLevel(rootNode.Links)
const position = await rootBucket._findNewBucketAndPos(fileName)
// the path to the root bucket
/** @type {{ bucket: Bucket<any>, prefix: string, node?: PBNode }[]} */
const path = [{
bucket: position.bucket,
prefix: toPrefix(position.pos)
}]
let currentBucket = position.bucket
while (currentBucket !== rootBucket) {
path.push({
bucket: currentBucket,
prefix: toPrefix(currentBucket._posAtParent)
})
// @ts-ignore - only the root bucket's parent will be undefined
currentBucket = currentBucket._parent
}
path.reverse()
path[0].node = rootNode
// load PbNode for each path segment
for (let i = 0; i < path.length; i++) {
const segment = path[i]
if (!segment.node) {
throw new Error('Could not generate HAMT path')
}
// find prefix in links
const link = segment.node.Links
.filter(link => (link.Name || '').substring(0, 2) === segment.prefix)
.pop()
// entry was not in shard
if (!link) {
// reached bottom of tree, file will be added to the current bucket
log(`Link ${segment.prefix}${fileName} will be added`)
// return path
continue
}
// found entry
if (link.Name === `${segment.prefix}${fileName}`) {
log(`Link ${segment.prefix}${fileName} will be replaced`)
// file already existed, file will be added to the current bucket
// return path
continue
}
// found subshard
log(`Found subshard ${segment.prefix}`)
const block = await context.repo.blocks.get(link.Hash)
const node = dagPB.decode(block)
// subshard hasn't been loaded, descend to the next level of the HAMT
if (!path[i + 1]) {
log(`Loaded new subshard ${segment.prefix}`)
await recreateHamtLevel(context, node.Links, rootBucket, segment.bucket, parseInt(segment.prefix, 16))
const position = await rootBucket._findNewBucketAndPos(fileName)
// i--
path.push({
bucket: position.bucket,
prefix: toPrefix(position.pos),
node: node
})
continue
}
const nextSegment = path[i + 1]
// add intermediate links to bucket
await addLinksToHamtBucket(context, node.Links, nextSegment.bucket, rootBucket)
nextSegment.node = node
}
await rootBucket.put(fileName, true)
path.reverse()
return {
rootBucket,
path
}
}
/**
* @param {MfsContext} context
* @param {{ name: string, size: number, cid: CID }[]} contents
* @param {object} [options]
* @param {Mtime} [options.mtime]
* @param {number} [options.mode]
*/
export const createShard = async (context, contents, options = {}) => {
const shard = new DirSharded({
root: true,
dir: true,
parent: undefined,
parentKey: undefined,
path: '',
dirty: true,
flat: false,
mtime: options.mtime,
mode: options.mode
}, options)
for (let i = 0; i < contents.length; i++) {
await shard._bucket.put(contents[i].name, {
size: contents[i].size,
cid: contents[i].cid
})
}
const res = await last(shard.flush(context.repo.blocks))
if (!res) {
throw new Error('Flushing shard yielded no result')
}
return res
}