@@ -18,10 +18,6 @@ module.exports = class DBManager {
18
18
constructor ( db , common ) {
19
19
this . _db = db
20
20
this . _common = common
21
- this . _opts = {
22
- keyEncoding : 'binary' ,
23
- valueEncoding : 'binary'
24
- }
25
21
this . _cache = {
26
22
td : new Cache ( { max : 1024 } ) ,
27
23
header : new Cache ( { max : 512 } ) ,
@@ -32,22 +28,38 @@ module.exports = class DBManager {
32
28
}
33
29
34
30
getHeads ( ) {
35
- return this . _db . get ( headsKey , {
36
- keyEncoding : 'binary' ,
37
- valueEncoding : 'json'
38
- } )
31
+ return this . get ( headsKey , { valueEncoding : 'json' } )
39
32
}
40
33
41
34
getHeadHeader ( ) {
42
- return this . _db . get ( headHeaderKey , this . _opts )
35
+ return this . get ( headHeaderKey )
43
36
}
44
37
45
38
getHeadBlock ( ) {
46
- return this . _db . get ( headBlockKey , this . _opts )
39
+ return this . get ( headBlockKey )
47
40
}
48
41
49
- get ( k ) {
50
- return this . _db . get ( k , this . _opts )
42
+ async get ( key , opts = { } ) {
43
+ const dbOpts = {
44
+ keyEncoding : opts . keyEncoding || 'binary' ,
45
+ valueEncoding : opts . valueEncoding || 'binary'
46
+ }
47
+
48
+ if ( opts . cache ) {
49
+ if ( ! this . _cache [ opts . cache ] ) {
50
+ throw new Error ( `Invalid cache: ${ opts . cache } ` )
51
+ }
52
+
53
+ let value = this . _cache [ opts . cache ] . get ( key )
54
+ if ( ! value ) {
55
+ value = await this . _db . get ( key , dbOpts )
56
+ this . _cache [ opts . cache ] . set ( key , value )
57
+ }
58
+
59
+ return value
60
+ }
61
+
62
+ return this . _db . get ( key , dbOpts )
51
63
}
52
64
53
65
batch ( ops ) {
@@ -60,74 +72,37 @@ module.exports = class DBManager {
60
72
*/
61
73
async _hashToNumber ( hash ) {
62
74
const key = hashToNumberKey ( hash )
63
- let number = this . _cache . hashToNumber . get ( key )
64
- if ( number ) {
65
- return new BN ( number )
66
- }
67
-
68
- number = await this . get ( key )
69
- this . _cache . hashToNumber . set ( key , number )
70
-
71
- return new BN ( number )
75
+ return new BN ( await this . get ( key , { cache : 'hashToNumber' } ) )
72
76
}
73
77
74
78
/**
75
79
* Performs a block number to block hash lookup
76
80
* @method _numberToHash
77
81
*/
78
- async _numberToHash ( number ) {
82
+ _numberToHash ( number ) {
79
83
if ( number . ltn ( 0 ) ) {
80
84
throw new level . errors . NotFoundError ( )
81
85
}
82
86
83
87
const key = numberToHashKey ( number )
84
- let hash = this . _cache . numberToHash . get ( key )
85
- if ( hash ) {
86
- return hash
87
- }
88
-
89
- hash = await this . _db . get ( key )
90
- this . _cache . numberToHash . set ( key , hash )
91
-
92
- return hash
88
+ return this . get ( key , { cache : 'numberToHash' } )
93
89
}
94
90
95
91
async _getTd ( hash , number ) {
96
92
const key = tdKey ( number , hash )
97
- let td = this . _cache . td . get ( key )
98
- if ( td ) {
99
- return new BN ( rlp . decode ( td ) )
100
- }
101
-
102
- td = await this . _db . get ( key )
103
- this . _cache . td . set ( key , td )
104
-
93
+ const td = await this . get ( key , { cache : 'td' } )
105
94
return new BN ( rlp . decode ( td ) )
106
95
}
107
96
108
97
async _getBody ( hash , number ) {
109
98
const key = bodyKey ( number , hash )
110
- let encodedBody = this . _cache . body . get ( key )
111
- if ( encodedBody ) {
112
- return rlp . decode ( encodedBody )
113
- }
114
-
115
- encodedBody = await this . _db . get ( key )
116
- this . _cache . body . set ( key , encodedBody )
117
-
118
- return rlp . decode ( encodedBody )
99
+ return rlp . decode ( await this . get ( key , { cache : 'body' } ) )
119
100
}
120
101
121
102
async _getHeader ( hash , number ) {
122
103
const key = headerKey ( number , hash )
123
- let encodedHeader = this . _cache . header . get ( key )
124
- if ( encodedHeader ) {
125
- return new Block . Header ( rlp . decode ( encodedHeader ) , { common : this . _common } )
126
- }
127
-
128
- encodedHeader = await this . _db . get ( key )
129
- this . _cache . header . set ( key , encodedHeader )
130
- return new Block . Header ( rlp . decode ( encodedHeader ) , { common : this . _common } )
104
+ let encodedHeader = await this . get ( key , { cache : 'header' } )
105
+ return new Block . Header ( rlp . decode ( encodedHeader ) , { common : this . _common } )
131
106
}
132
107
133
108
async _getBlock ( blockTag ) {
0 commit comments