Skip to content
This repository was archived by the owner on Apr 6, 2020. It is now read-only.

Commit 0f7e549

Browse files
committed
Use get in other DBManager methods
1 parent 8e74126 commit 0f7e549

File tree

1 file changed

+31
-56
lines changed

1 file changed

+31
-56
lines changed

src/dbManager.js

Lines changed: 31 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ module.exports = class DBManager {
1818
constructor (db, common) {
1919
this._db = db
2020
this._common = common
21-
this._opts = {
22-
keyEncoding: 'binary',
23-
valueEncoding: 'binary'
24-
}
2521
this._cache = {
2622
td: new Cache({ max: 1024 }),
2723
header: new Cache({ max: 512 }),
@@ -32,22 +28,38 @@ module.exports = class DBManager {
3228
}
3329

3430
getHeads () {
35-
return this._db.get(headsKey, {
36-
keyEncoding: 'binary',
37-
valueEncoding: 'json'
38-
})
31+
return this.get(headsKey, { valueEncoding: 'json' })
3932
}
4033

4134
getHeadHeader () {
42-
return this._db.get(headHeaderKey, this._opts)
35+
return this.get(headHeaderKey)
4336
}
4437

4538
getHeadBlock () {
46-
return this._db.get(headBlockKey, this._opts)
39+
return this.get(headBlockKey)
4740
}
4841

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)
5163
}
5264

5365
batch (ops) {
@@ -60,74 +72,37 @@ module.exports = class DBManager {
6072
*/
6173
async _hashToNumber (hash) {
6274
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' }))
7276
}
7377

7478
/**
7579
* Performs a block number to block hash lookup
7680
* @method _numberToHash
7781
*/
78-
async _numberToHash (number) {
82+
_numberToHash (number) {
7983
if (number.ltn(0)) {
8084
throw new level.errors.NotFoundError()
8185
}
8286

8387
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' })
9389
}
9490

9591
async _getTd (hash, number) {
9692
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' })
10594
return new BN(rlp.decode(td))
10695
}
10796

10897
async _getBody (hash, number) {
10998
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' }))
119100
}
120101

121102
async _getHeader (hash, number) {
122103
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 })
131106
}
132107

133108
async _getBlock (blockTag) {

0 commit comments

Comments
 (0)