Skip to content

Commit 296890c

Browse files
committed
fix: support profiles and shard split thresholds in mfs
Adds IPIP-499 profiles and shard split thresholds to MFS operations
1 parent 2d30414 commit 296890c

File tree

9 files changed

+118
-401
lines changed

9 files changed

+118
-401
lines changed

packages/mfs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"interface-datastore": "^9.0.2",
5454
"ipfs-unixfs": "^12.0.0",
5555
"ipfs-unixfs-exporter": "^15.0.2",
56-
"ipfs-unixfs-importer": "^16.0.1",
56+
"ipfs-unixfs-importer": "^16.1.4",
5757
"it-map": "^3.1.4",
5858
"multiformats": "^13.4.1"
5959
},

packages/mfs/test/rm.spec.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ describe('rm', () => {
105105
content: Uint8Array.from([0, 1, 2, 3, 4])
106106
}], blockstore, {
107107
wrapWithDirectory: true,
108-
shardSplitThresholdBytes
108+
shardSplitThresholdBytes,
109+
shardSplitStrategy: 'links-bytes'
109110
}))
110111

111112
if (importResult == null) {
@@ -122,19 +123,22 @@ describe('rm', () => {
122123

123124
// create the same shard with unixfs command
124125
await fs.writeBytes(Uint8Array.from([0, 1, 2, 3, 4]), `${dirPath}/file-1.txt`, {
125-
shardSplitThresholdBytes
126+
shardSplitThresholdBytes,
127+
shardSplitStrategy: 'links-bytes'
126128
})
127129

128130
await expect(fs.stat(dirPath)).to.eventually.have.nested.property('unixfs.type', 'directory')
129131

130132
await fs.writeBytes(Uint8Array.from([0, 1, 2, 3, 4]), `${dirPath}/file-2.txt`, {
131-
shardSplitThresholdBytes
133+
shardSplitThresholdBytes,
134+
shardSplitStrategy: 'links-bytes'
132135
})
133136

134137
await expect(fs.stat(dirPath)).to.eventually.have.nested.property('unixfs.type', 'hamt-sharded-directory')
135138

136139
await fs.rm(`${dirPath}/file-2.txt`, {
137-
shardSplitThresholdBytes
140+
shardSplitThresholdBytes,
141+
shardSplitStrategy: 'links-bytes'
138142
})
139143

140144
const dirStats = await fs.stat(dirPath)
@@ -151,7 +155,8 @@ describe('rm', () => {
151155
content: Uint8Array.from([0, 1, 2, 3, 4])
152156
}], blockstore, {
153157
wrapWithDirectory: true,
154-
shardSplitThresholdBytes
158+
shardSplitThresholdBytes,
159+
shardSplitStrategy: 'links-bytes'
155160
}))
156161

157162
if (importResult == null) {
@@ -168,19 +173,22 @@ describe('rm', () => {
168173

169174
// create the same shard with unixfs command
170175
await fs.writeBytes(Uint8Array.from([0, 1, 2, 3, 4]), `${dirPath}/file-1.txt`, {
171-
shardSplitThresholdBytes
176+
shardSplitThresholdBytes,
177+
shardSplitStrategy: 'links-bytes'
172178
})
173179

174180
await expect(fs.stat(dirPath)).to.eventually.have.nested.property('unixfs.type', 'hamt-sharded-directory')
175181

176182
await fs.writeBytes(Uint8Array.from([0, 1, 2, 3, 4]), `${dirPath}/file-2.txt`, {
177-
shardSplitThresholdBytes
183+
shardSplitThresholdBytes,
184+
shardSplitStrategy: 'links-bytes'
178185
})
179186

180187
await expect(fs.stat(dirPath)).to.eventually.have.nested.property('unixfs.type', 'hamt-sharded-directory')
181188

182189
await fs.rm(`${dirPath}/file-2.txt`, {
183-
shardSplitThresholdBytes
190+
shardSplitThresholdBytes,
191+
shardSplitStrategy: 'links-bytes'
184192
})
185193

186194
const dirStats = await fs.stat(dirPath)
@@ -204,7 +212,8 @@ describe('rm', () => {
204212

205213
// remove the file that caused the sub-shard to be created and the CID should be the same as the importer
206214
await fs.rm(`${dirPath}/${fileName}`, {
207-
shardSplitThresholdBytes: 1
215+
shardSplitThresholdBytes: 1,
216+
shardSplitStrategy: 'links-bytes'
208217
})
209218

210219
// should still be a shard
@@ -231,7 +240,8 @@ describe('rm', () => {
231240

232241
// remove the file that caused the sub-shard to be created and the CID should be the same as the importer
233242
await fs.rm(`${dirPath}/${fileName}`, {
234-
shardSplitThresholdBytes: 1
243+
shardSplitThresholdBytes: 1,
244+
shardSplitStrategy: 'links-bytes'
235245
})
236246

237247
// should still be a shard

packages/unixfs/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,10 @@
7373
"@libp2p/logger": "^6.0.5",
7474
"@libp2p/utils": "^7.0.5",
7575
"@multiformats/murmur3": "^2.1.8",
76-
"hamt-sharding": "^3.0.6",
7776
"interface-blockstore": "^6.0.1",
7877
"ipfs-unixfs": "^12.0.0",
7978
"ipfs-unixfs-exporter": "^15.0.2",
80-
"ipfs-unixfs-importer": "^16.0.1",
79+
"ipfs-unixfs-importer": "^16.1.4",
8180
"it-all": "^3.0.9",
8281
"it-first": "^3.0.9",
8382
"it-glob": "^3.0.4",

packages/unixfs/src/commands/utils/add-link.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { sha256 } from 'multiformats/hashes/sha2'
77
// @ts-expect-error no types
88
import SparseArray from 'sparse-array'
99
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
10-
import { DEFAULT_SHARD_SPLIT_THRESHOLD_BYTES } from '../../constants.ts'
1110
import { AlreadyExistsError, InvalidParametersError, InvalidPBNodeError } from '../../errors.js'
1211
import { wrapHash } from './consumable-hash.js'
1312
import { hamtBucketBits, hamtHashFn } from './hamt-constants.js'
@@ -22,8 +21,7 @@ import type { Directory } from './cid-to-directory.js'
2221
import type { GetStore, PutStore } from '../../unixfs.js'
2322
import type { PBNode, PBLink } from '@ipld/dag-pb'
2423
import type { AbortOptions } from '@libp2p/interface'
25-
import type { ImportResult } from 'ipfs-unixfs-importer'
26-
import type { Version } from 'multiformats/cid'
24+
import type { ImporterOptions, ImportResult } from 'ipfs-unixfs-importer'
2725

2826
const log = logger('helia:unixfs:components:utils:add-link')
2927

@@ -32,10 +30,8 @@ export interface AddLinkResult {
3230
cid: CID
3331
}
3432

35-
export interface AddLinkOptions extends AbortOptions {
33+
export interface AddLinkOptions extends AbortOptions, Pick<ImporterOptions, 'profile' | 'shardSplitThresholdBytes' | 'shardSplitStrategy' | 'shardFanoutBits' | 'cidVersion'> {
3634
allowOverwriting?: boolean
37-
shardSplitThresholdBytes?: number
38-
cidVersion?: Version
3935
}
4036

4137
export async function addLink (parent: Directory, child: Required<PBLink>, blockstore: GetStore & PutStore, options: AddLinkOptions): Promise<AddLinkResult> {
@@ -55,7 +51,7 @@ export async function addLink (parent: Directory, child: Required<PBLink>, block
5551

5652
const result = await addToDirectory(parent, child, blockstore, options)
5753

58-
if (await isOverShardThreshold(result.node, blockstore, options.shardSplitThresholdBytes ?? DEFAULT_SHARD_SPLIT_THRESHOLD_BYTES, options)) {
54+
if (await isOverShardThreshold(result.node, blockstore, options)) {
5955
log('converting directory to sharded directory')
6056

6157
const converted = await convertToShardedDirectory(result, blockstore)

0 commit comments

Comments
 (0)