-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from wongterrencew/improve_performance_with_le…
…vel_db Improve performance with level db
- Loading branch information
Showing
14 changed files
with
771 additions
and
683 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/* @flow */ | ||
module.exports = require('./level-db-wrapper') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* @flow */ | ||
const levelup = require('levelup') | ||
const promisify = require('q-level') | ||
|
||
type ConstructorOptions = { | ||
adapter?: string | ||
} | ||
|
||
type DBConfig = { | ||
valueEncoding: string, | ||
db: Object | ||
} | ||
|
||
class LevelDBWrapper { | ||
location: string; | ||
adapter: ?string; | ||
dbConfig: DBConfig; | ||
db: Object; | ||
|
||
constructor (location: string, options?: ConstructorOptions = {}) { | ||
if (!location) { | ||
throw new Error('Argument `location: string` is required!') | ||
} | ||
|
||
this.location = location | ||
this.adapter = options.adapter | ||
|
||
this.dbConfig = { | ||
valueEncoding: 'json', | ||
db: this.adapter === 'memory' | ||
? require('memdown') | ||
: require('leveldown') | ||
} | ||
|
||
this.db = promisify(levelup(this.location, this.dbConfig)) | ||
} | ||
|
||
async get (key: string): Promise<any> { | ||
return this.db.get(key) | ||
} | ||
|
||
async put (key: string, value: any): Promise<?Error> { | ||
return this.db.put(key, value) | ||
} | ||
|
||
async destroy (): Promise<any> { | ||
if (this.adapter === 'memory') { | ||
return this.dbConfig.db.clearGlobalStore(true) | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
this.dbConfig.db.destroy(this.location, err => { | ||
if (err) reject(err) | ||
resolve() | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
module.exports = LevelDBWrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* @flow */ | ||
declare function describe(name:string, callback:Function):void; | ||
declare function it(name:string, callback:Function):void; | ||
declare function beforeEach(callback:Function):void; | ||
declare function afterEach(callback:Function):void; | ||
|
||
const DB = require('./') | ||
const expect = require('expect') | ||
|
||
describe('level-db-wrapper', function () { | ||
beforeEach(function () { | ||
const location = './mydb' | ||
const options = {adapter: 'memory'} | ||
this.db = new DB(location, options) | ||
}) | ||
|
||
afterEach(function () { | ||
return this.db.destroy() | ||
}) | ||
|
||
it('throws an error when getting no existing thing', async function () { | ||
let error | ||
try { | ||
await this.db.get('does not exist') | ||
} catch (e) { | ||
error = e | ||
} | ||
|
||
expect(error).toBeTruthy() | ||
}) | ||
|
||
it('can put', async function () { | ||
const key = 'key' | ||
const value = 'value' | ||
await this.db.put(key, value) | ||
const got = await this.db.get(key) | ||
|
||
expect(got).toBe(value) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/* @flow */ | ||
module.exports = require('./level-queue') |
Oops, something went wrong.