-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.js
223 lines (198 loc) · 5.36 KB
/
plugin.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
const bipf = require('bipf')
const clarify = require('clarify-error')
// @ts-expect-error
const pullLevel = require('pull-level')
const pull = require('pull-stream')
const Plugin = require('ssb-db2/indexes/plugin')
const BIPF_AUTHOR = bipf.allocAndEncode('author')
// See https://github.com/achou11/ssb-storage-used/issues/4 for context
const PREFIX_FACTOR = 1 / Math.log2(1.1)
/**
* @param {string | number} value
* @returns {number}
*/
function toInt(value) {
if (typeof value === 'number') return value
return parseInt(value, 10)
}
// Index of feedId to storage used in bytes
module.exports = class StorageUsed extends Plugin {
/**
*
* @param {*} log
* @param {*} dir
*/
constructor(log, dir) {
super(log, dir, 'storageUsed', 1, undefined, undefined)
/**
* Map of feedId to bytes stored
* @type {Map<string, number>}
*/
this.bytesStored = new Map()
/**
* Map of feed id to the calculated prefix used for level key
* @type {Map<string, string>}
*/
this.feedIdToPrefix = new Map()
}
// See https://github.com/Microsoft/TypeScript/issues/27965 for relevant details
// @ts-expect-error
onLoaded(cb) {
const META = '\x00'
pull(
pullLevel.read(this.level, { gt: META }),
pull.drain(
({ key, value }) => {
const [prefix, author] = this._unpackKey(key)
this.bytesStored.set(author, toInt(value))
this.feedIdToPrefix.set(author, prefix)
},
(err) => {
if (err && err !== true) {
cb(clarify(err, 'StorageUsed.onLoaded() failed'))
} else cb()
}
)
)
}
// See https://github.com/Microsoft/TypeScript/issues/27965 for relevant details
// @ts-expect-error
reset() {
this.bytesStored.clear()
this.feedIdToPrefix.clear()
}
/**
* @param {string} prefix
* @param {string} author
* @returns
*/
_packKey(prefix, author) {
return prefix + author
}
/**
* @param {string} key
* @returns {[string, string]}
*/
_unpackKey(key) {
const prefix = key.slice(0, 2)
const author = key.slice(2)
return [prefix, author]
}
/**
* <PREFIX><AUTHOR>:string => bytes:number
*
* @param {import('./types/helpers').BipfRecord} record
* @param {number} seq
* @param {number} pValue
*/
processRecord(record, seq, pValue) {
const buf = record.value
const pAuthor = bipf.seekKey2(buf, pValue, BIPF_AUTHOR, 0)
const author = bipf.decode(buf, pAuthor)
const size = this._calculateBytesStored(author, buf.length)
const prefix = this._calculatePrefix(size)
const key = this._packKey(prefix, author)
const prevPrefix = this.feedIdToPrefix.get(author)
const prefixHasChanged = prevPrefix !== prefix
if (prevPrefix && prefixHasChanged) {
const prevKey = this._packKey(prevPrefix, author)
this.batch.push({
type: 'del',
key: prevKey,
})
}
this.batch.push({
type: 'put',
key,
value: size,
})
// Update in-memory maps
this.bytesStored.set(author, size)
if (!this.feedIdToPrefix.has(author) || prefixHasChanged) {
this.feedIdToPrefix.set(author, prefix)
}
}
/**
* @param {string} feedId
* @returns {number}
*/
getBytesStored(feedId) {
return this.bytesStored.get(feedId) || 0
}
/**
* @returns {pull.Source<[string, number]>}
*/
stream() {
const self = this
let prefix = 0
/**
* @param {*} errOrEnd
* @param {import('./types/helpers').CB<*>} cb
*/
function chunkedStream(errOrEnd, cb) {
if (errOrEnd) return cb(errOrEnd)
if (prefix >= 100) return cb(true)
const stringPrefix = self._createPrefixString(prefix)
pull(
pullLevel.read(self.level, {
gte: stringPrefix,
lte: stringPrefix + '~',
keys: true,
values: true,
}),
pull.collect((err, chunk) => {
prefix++
cb(
null,
chunk
.map((c) => {
const feedId = self._unpackKey(c.key)[1]
const bytes = toInt(c.value)
return /** @type {const} */ ([feedId, bytes])
})
.sort((a, b) => b[1] - a[1])
)
})
)
}
// @ts-ignore
return pull(
chunkedStream,
pull.filter((chunk) => chunk.length > 0),
pull.map(pull.values),
pull.flatten()
)
}
/**
* @param {string} author
* @param {number} bufferLength
* @returns {number}
*/
_calculateBytesStored(author, bufferLength) {
const newTotal = (this.bytesStored.get(author) || 0) + bufferLength
return newTotal
}
/**
* The prefix is a two digit number represented as a string that represents the following:
* Prefix N means the feed's bytes used is less than 2^(100-N) KB and more than 2^(99-N) KB
*
* @param {number} bufferLength
* @returns {string}
*/
_calculatePrefix(bufferLength) {
// If actual total is less than 1kb, round up to 1kb
const kbTotal = Math.max(bufferLength, 1024) / 1024
const prefix = Math.max(
0,
99 - Math.floor(Math.log2(kbTotal) * PREFIX_FACTOR)
)
return this._createPrefixString(prefix)
}
/**
* @param {number} prefix a one or two digit number
* @returns {string}
*/
_createPrefixString(prefix) {
return prefix < 10 ? `0${prefix}` : `${prefix}`
}
}