-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsharding.ts
117 lines (96 loc) · 3 KB
/
sharding.ts
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
import path from 'node:path'
import { base32upper } from 'multiformats/bases/base32'
import { CID } from 'multiformats/cid'
import type { MultibaseCodec } from 'multiformats/bases/interface'
export interface ShardingStrategy {
extension: string
encode(cid: CID): { dir: string, file: string }
decode(path: string): CID
}
export interface NextToLastInit {
/**
* The file extension to use. default: '.data'
*/
extension?: string
/**
* How many characters to take from the end of the CID. default: 2
*/
prefixLength?: number
/**
* The multibase codec to use - nb. should be case insensitive.
* default: base32upper
*/
base?: MultibaseCodec<string>
}
/**
* A sharding strategy that takes the last few characters of a multibase encoded
* CID and uses them as the directory to store the block in. This prevents
* storing all blocks in a single directory which would overwhelm most
* filesystems.
*/
export class NextToLast implements ShardingStrategy {
public extension: string
private readonly prefixLength: number
private readonly base: MultibaseCodec<string>
constructor (init: NextToLastInit = {}) {
this.extension = init.extension ?? '.data'
this.prefixLength = init.prefixLength ?? 2
this.base = init.base ?? base32upper
}
encode (cid: CID): { dir: string, file: string } {
const str = this.base.encoder.encode(cid.multihash.bytes)
const prefix = str.substring(str.length - this.prefixLength)
return {
dir: prefix,
file: `${str}${this.extension}`
}
}
decode (str: string): CID {
let fileName = path.basename(str)
if (fileName.endsWith(this.extension)) {
fileName = fileName.substring(0, fileName.length - this.extension.length)
}
return CID.decode(this.base.decoder.decode(fileName))
}
}
export interface FlatDirectoryInit {
/**
* The file extension to use. default: '.data'
*/
extension?: string
/**
* How many characters to take from the end of the CID. default: 2
*/
prefixLength?: number
/**
* The multibase codec to use - nb. should be case insensitive.
* default: base32padupper
*/
base?: MultibaseCodec<string>
}
/**
* A sharding strategy that does not do any sharding and stores all files
* in one directory. Only for testing, do not use in production.
*/
export class FlatDirectory implements ShardingStrategy {
public extension: string
private readonly base: MultibaseCodec<string>
constructor (init: NextToLastInit = {}) {
this.extension = init.extension ?? '.data'
this.base = init.base ?? base32upper
}
encode (cid: CID): { dir: string, file: string } {
const str = this.base.encoder.encode(cid.multihash.bytes)
return {
dir: '',
file: `${str}${this.extension}`
}
}
decode (str: string): CID {
let fileName = path.basename(str)
if (fileName.endsWith(this.extension)) {
fileName = fileName.substring(0, fileName.length - this.extension.length)
}
return CID.decode(this.base.decoder.decode(fileName))
}
}