-
Notifications
You must be signed in to change notification settings - Fork 0
/
lrudbx.nim
302 lines (267 loc) · 10.4 KB
/
lrudbx.nim
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
import nimdbx
import nimdbx / [CRUD, Cursor, Collatable]
import tables
import macros
import sugar
import hashes
import sets
import frosty/streams
{.experimental: "notnil".}
type
trNode = tuple[prev: int, value: string, next: int]
CollectionNotNil = Collection not nil
CollectionCache = Table[string, CollectionNotNil]
LruDbx* = ref object
db: nimdbx.Database.Database
coll: CollectionCache
const
headKey = int64.low
stubHeadKey = int64.low+1
tailKey = int64.low+2
stubTailKey = int64.low+3
sizeKey = int64.low+4
reservedKeys = [headKey, stubHeadKey, tailKey, stubTailKey, sizeKey].toHashSet
MAX_COLLECTIONS = 30
MAX_DB_SIZE = 4096 * 1024 * 1024
MAX_PAIR_COLLECTION_SIZE = MAX_DB_SIZE.div MAX_COLLECTIONS
proc checkHeadTail(c: Collection not nil) =
c.inTransaction do (ct: CollectionTransaction):
if ct.get(sizeKey).asByteSeq.len == 0:
# head
ct[headKey] = stubHeadKey
ct[stubHeadKey] = freeze((prev: 0, value: "", next: stubTailKey))
# tail
ct[tailKey] = stubTailKey
ct[stubTailKey] = freeze((prev: stubHeadKey, value: "", next: 0))
# size
ct[sizeKey] = (8 * 4).int64
ct.commit()
proc getCollection(k: string, cache: LruDbx): Collection not nil =
try:
return cache.coll[k]
except:
var c: Collection = cache.db.openCollectionOrNil(k, keytype = IntegerKeys)
var cnn: Collection not nil
if c.isnil:
c = cache.db.createCollection(k, keytype = IntegerKeys)
if c.isnil:
raise newException(ValueError, "Cannot create collection")
else:
cnn = c
else:
cnn = c
checkHeadTail(cnn)
cache.coll[k] = cnn
return cnn
proc `$`(cache: LruDbx): string =
cache.db.path & " with " & $len(cache.coll) & " collections."
proc initLruDbx(path: string, colls: seq[string] = @[]): LruDbx =
new(result)
result.db = openDatabase(path, maxFileSize = MAX_DB_SIZE,
maxcollections = MAX_COLLECTIONS)
for c in colls:
discard getCollection(c, result)
macro doTx(coll: string, what: untyped): untyped =
result = quote do:
getCollection(`coll`).inTransaction do (ct {.inject.}: CollectionTransaction):
`what`
ct.commit()
macro doSnap(coll: string, what: untyped): untyped =
result = quote do:
pairCollection(`coll`).inSnapshot do (cs {.inject.}: CollectionSnapshot):
`what`
proc `[]`*(cache: LruDbx, k: string): Collection not nil =
getCollection(k, cache)
proc nodeSize(node: trNode): int {.inline.} =
len(node.value) * sizeof(node.value) + 8 + 8 # prev and next keys are int64
proc adjustSize(ct: CollectionTransaction, thisNodeKey: int64, v: string) =
let
collSize = ct[sizeKey].int64
headNodeKey = ct[headKey].int64
var
newSize = collSize + len(v) * sizeof(v) + 8 + 8
tailNode: trNode
thisNode: trNode
initialTailKey = ct[tailKey].int64
curNodeKey = initialTailKey
tailNodeKey = initialTailKey
# check if key already exists and subtract its size
let thisNodeDataOut = ct[thisNodeKey]
if thisNodeDataOut.asData.len > 0:
thisNode = thaw[trNode](thisNodeDataOut)
newSize -= thisNode.nodeSize
# keep deleting nodes from the tail until the
# new size is within bound
# echo "looping size"
var i = 0
while newSize > MAX_PAIR_COLLECTION_SIZE:
# echo "trying to decode node with key: ", curNodeKey
tailNode = thaw[trNode](ct[curNodeKey])
# echo fmt"success! next node going to be {tailNode.prev}"
tailNodeKey = curNodeKey
ct.del(curNodeKey)
i += 1
newSize -= tailNode.nodeSize
if tailNode.prev == headNodeKey:
# echo "Stopping because we reached head"
break
elif tailNode.prev == thisNodeKey:
# echo "The tail previous node is this node"
curNodeKey = thisNode.prev
else:
# echo "setting current node to previous node"
curNodeKey = tailNode.prev
# echo fmt"removed {i} nodes ", "updating? ", tailNodeKey != initialTailKey, fmt" newsize? {newSize}, {collSize}"
# echo "tailNodekey: ", tailNodeKey, " tailnodePrev: ", tailNode.prev, " initialKey: ", initialTailKey
if tailNodeKey != curNodeKey:
# update the new tail
ct[tailKey] = tailNodeKey
ct[tailNodeKey] = freeze((prev: tailNode.prev,
value: tailNode.value,
next: 0))
# update size
if newSize != collSize:
ct[sizeKey] = newSize
proc `[]=`*(c: Collection not nil, k: Hash, v: string) =
c.inTransaction do (ct: CollectionTransaction):
let
thisKey = k.int64
headNodeKey = ct[headKey].int64
headNode = thaw[trNode](ct[headNodeKey])
adjustSize(ct, thisKey, v)
if thisKey == headNodeKey:
ct[thisKey] = freeze((prev: 0,
value: v,
next: headNode.next))
else:
ct[thisKey] = freeze((prev: 0,
value: v,
next: headNodeKey))
ct[headNodeKey] = freeze((prev: thisKey,
value: headNode.value,
next: headNode.next))
ct[headKey] = thisKey
ct.commit()
template `[]=`*(c: CollectionNotNil, k: string, v: untyped) =
c[hash(k)] = v
proc `[]`*(c: Collection not nil, k: Hash): string =
var o: string
c.inTransaction do (ct: CollectionTransaction):
var thisNode: trNode
let
thisKey = k.int64
headNodeKey = ct[headKey]
thisDataOut = ct[thisKey]
if thisDataOut.asData.len != 0:
thisNode = thaw[trNode](thisDataOut)
if thisKey == headNodekey:
o = thisNode.value
else:
let headNode = thaw[trNode](ct[headNodeKey])
ct[thisKey] = freeze((prev: 0,
value: thisNode.value,
next: headNodeKey))
ct[headNodeKey] = freeze((prev: thisKey,
value: headNode.value,
next: headNode.next))
ct[headKey] = thisKey
o = thisNode.value
ct.commit()
return o
template `[]`*(c: CollectionNotNil, k: string) =
c[hash(k)]
template get*(c, k: string, def) =
c.get(hash(k))
proc get*(c: CollectionNotNil, k: Hash, def: string): string =
result = c[k]
if result == "":
result = def
template del*(c, k: string) =
c.del(hash(k))
proc del*(c: CollectionNotNil, k: Hash) =
c.inTransaction do (ct: CollectionTransaction):
let
thisKey = k.int64
headNodeKey = ct[headKey].asInt64
tailNodeKey = ct[tailKey].asInt64
thisDataOut = ct[thisKey.asdata]
if thisDataOut.asData.len != 0:
let thisNode = thaw[trNode](thisDataOut)
ct.del(thisKey.asdata)
if thisKey == headNodeKey:
let newHeadNodeKey = thisNode.next.int64
let newHeadNode = thaw[trNode](ct[newHeadNodeKey])
ct[newHeadNodeKey] = freeze((prev: nil,
value: newHeadNode.value,
next: newHeadNode.next))
ct[headKey] = newHeadNodeKey
elif thisKey == tailNodeKey:
let
newTailNodeKey = thisNode.prev.asdata
newTailNode = thaw[trNode](ct[newTailNodeKey])
ct[newTailNodeKey] = freeze((prev: newTailNode.prev,
value: newTailNode.value,
next: nil))
ct[tailKey] = newTailNodeKey
else:
let
prevNodeKey = thisNode.prev.asdata
nextNodeKey = thisNode.next.asdata
prevNode = thaw[trNode](ct[prevNodeKey])
nextNode = thaw[trNode](ct[nextNodeKey])
ct[prevNodeKey] = freeze((prev: prevNode.prev,
value: prevNode.value,
next: nextNodeKey))
ct[nextNodeKey] = freeze((prev: prevNodeKey,
value: nextNode.value,
next: nextNode.next))
ct.commit()
template cursIter(c: Collection, what: untyped): untyped =
let cs = c.beginSnapshot
defer: cs.finish()
var curs {.inject.} = makeCursor(cs)
curs.first
# FIXME: the first key is skipped in the iteration...
if curs.toBool():
if not (curs.key.int64 in reservedKeys):
yield what
while curs.next():
if curs.key in reservedKeys:
continue
yield what
iterator keys*(c: Collection not nil): int64 =
cursIter(c, curs.key.int64)
iterator values*(c: Collection not nil): trNode =
cursIter(c, thaw[trNode](curs.value))
iterator items*(c: Collection not nil): (int64, trNode) =
cursIter(c, (curs.key.int64, thaw[trNode](curs.value)))
proc clear*(c: Collection not nil) =
let ks = collect(for k in keys(c): k)
c.inTransaction do (ct: CollectionTransaction):
for k in ks:
ct.del(k.int64)
ct.commit()
proc sizeof*(c: Collection not nil): int =
var size: int
c.inSnapshot do (cs: CollectionSnapshot):
size = cs[sizeKey].int
return size
proc del*(cache: LruDbx, coll: string) =
## NOTE: deleting collections requires DB reopen
cache[coll].inTransaction do (ct: CollectionTransaction):
ct.deleteCollection()
cache.coll.del(coll)
ct.commit()
cache.db.close
let path = cache.db.path
cache.db = openDatabase(path, maxFileSize = MAX_DB_SIZE,
maxcollections = MAX_COLLECTIONS)
for c in cache.coll.keys:
discard getCollection(c, cache)
proc hasKey*(c: CollectionNotNil, k: int64): bool =
var res: bool
c.inSnapshot do (cs: CollectionSnapshot):
res = cs[k].asData.len != 0
res
proc hasKey*(c: CollectionNotNil, k: string): bool =
c.hasKey hash(k).int64