1
1
import { LevelUp } from 'levelup'
2
- import { DB , BatchDBOp } from './db'
3
-
4
- export const ENCODING_OPTS = { keyEncoding : 'binary' , valueEncoding : 'binary' }
2
+ import { DB , BatchDBOp , ENCODING_OPTS } from './db'
5
3
6
4
export type Checkpoint = {
7
5
// We cannot use a Buffer => Buffer map directly. If you create two Buffers with the same internal value,
@@ -55,12 +53,12 @@ export class CheckpointDB extends DB {
55
53
if ( value === null ) {
56
54
batchOp . push ( {
57
55
type : 'del' ,
58
- key : Buffer . from ( key , 'hex ' ) ,
56
+ key : Buffer . from ( key , 'binary ' ) ,
59
57
} )
60
58
} else {
61
59
batchOp . push ( {
62
60
type : 'put' ,
63
- key : Buffer . from ( key , 'hex ' ) ,
61
+ key : Buffer . from ( key , 'binary ' ) ,
64
62
value,
65
63
} )
66
64
}
@@ -69,9 +67,7 @@ export class CheckpointDB extends DB {
69
67
} else {
70
68
// dump everything into the current (higher level) cache
71
69
const currentKeyValueMap = this . checkpoints [ this . checkpoints . length - 1 ] . keyValueMap
72
- keyValueMap . forEach ( function ( value , key ) {
73
- currentKeyValueMap . set ( key , value )
74
- } )
70
+ keyValueMap . forEach ( ( value , key ) => currentKeyValueMap . set ( key , value ) )
75
71
}
76
72
}
77
73
@@ -91,7 +87,7 @@ export class CheckpointDB extends DB {
91
87
async get ( key : Buffer ) : Promise < Buffer | null > {
92
88
// Lookup the value in our cache. We return the latest checkpointed value (which should be the value on disk)
93
89
for ( let index = this . checkpoints . length - 1 ; index >= 0 ; index -- ) {
94
- const value = this . checkpoints [ index ] . keyValueMap . get ( key . toString ( 'hex ' ) )
90
+ const value = this . checkpoints [ index ] . keyValueMap . get ( key . toString ( 'binary ' ) )
95
91
if ( value !== undefined ) {
96
92
return value
97
93
}
@@ -101,7 +97,7 @@ export class CheckpointDB extends DB {
101
97
const value = await super . get ( key )
102
98
if ( this . isCheckpoint ) {
103
99
// Since we are a checkpoint, put this value in cache, so future `get` calls will not look the key up again from disk.
104
- this . checkpoints [ this . checkpoints . length - 1 ] . keyValueMap . set ( key . toString ( 'hex ' ) , value )
100
+ this . checkpoints [ this . checkpoints . length - 1 ] . keyValueMap . set ( key . toString ( 'binary ' ) , value )
105
101
}
106
102
107
103
return value
@@ -115,7 +111,7 @@ export class CheckpointDB extends DB {
115
111
async put ( key : Buffer , val : Buffer ) : Promise < void > {
116
112
if ( this . isCheckpoint ) {
117
113
// put value in cache
118
- this . checkpoints [ this . checkpoints . length - 1 ] . keyValueMap . set ( key . toString ( 'hex ' ) , val )
114
+ this . checkpoints [ this . checkpoints . length - 1 ] . keyValueMap . set ( key . toString ( 'binary ' ) , val )
119
115
} else {
120
116
await super . put ( key , val )
121
117
}
@@ -128,7 +124,7 @@ export class CheckpointDB extends DB {
128
124
async del ( key : Buffer ) : Promise < void > {
129
125
if ( this . isCheckpoint ) {
130
126
// delete the value in the current cache
131
- this . checkpoints [ this . checkpoints . length - 1 ] . keyValueMap . set ( key . toString ( 'hex ' ) , null )
127
+ this . checkpoints [ this . checkpoints . length - 1 ] . keyValueMap . set ( key . toString ( 'binary ' ) , null )
132
128
} else {
133
129
// delete the value on disk
134
130
await this . _leveldb . del ( key , ENCODING_OPTS )
0 commit comments