-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add in-memory blockstore implementation (#1)
A simple blockstore that's good for testing.
- Loading branch information
1 parent
67d1667
commit ab37d40
Showing
4 changed files
with
97 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict' | ||
|
||
const errCode = require('err-code') | ||
|
||
/** | ||
* @param {Error} [err] | ||
*/ | ||
function notFoundError (err) { | ||
err = err || new Error('Not Found') | ||
return errCode(err, 'ERR_NOT_FOUND') | ||
} | ||
|
||
module.exports = { | ||
notFoundError | ||
} |
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,78 @@ | ||
'use strict' | ||
|
||
const Adapter = require('./adapter') | ||
const { base32 } = require('multiformats/bases/base32') | ||
const raw = require('multiformats/codecs/raw') | ||
const { CID } = require('multiformats/cid') | ||
const Digest = require('multiformats/hashes/digest') | ||
const Errors = require('./errors') | ||
|
||
/** | ||
* @typedef {import('./types').Pair} Pair | ||
* @typedef {import('./types').Blockstore} Blockstore | ||
* @typedef {import('interface-store').Options} Options | ||
*/ | ||
|
||
/** | ||
* @class MemoryBlockstore | ||
* @implements {Blockstore} | ||
*/ | ||
class MemoryBlockstore extends Adapter { | ||
constructor () { | ||
super() | ||
|
||
/** @type {Record<string, Uint8Array>} */ | ||
this.data = {} | ||
} | ||
|
||
open () { | ||
return Promise.resolve() | ||
} | ||
|
||
close () { | ||
return Promise.resolve() | ||
} | ||
|
||
/** | ||
* @param {CID} key | ||
* @param {Uint8Array} val | ||
*/ | ||
async put (key, val) { // eslint-disable-line require-await | ||
this.data[base32.encode(key.multihash.bytes)] = val | ||
} | ||
|
||
/** | ||
* @param {CID} key | ||
*/ | ||
async get (key) { | ||
const exists = await this.has(key) | ||
if (!exists) throw Errors.notFoundError() | ||
return this.data[base32.encode(key.multihash.bytes)] | ||
} | ||
|
||
/** | ||
* @param {CID} key | ||
*/ | ||
async has (key) { // eslint-disable-line require-await | ||
return this.data[base32.encode(key.multihash.bytes)] !== undefined | ||
} | ||
|
||
/** | ||
* @param {CID} key | ||
*/ | ||
async delete (key) { // eslint-disable-line require-await | ||
delete this.data[base32.encode(key.multihash.bytes)] | ||
} | ||
|
||
async * _all () { | ||
yield * Object.entries(this.data) | ||
.map(([key, value]) => ({ key: CID.createV1(raw.code, Digest.decode(base32.decode(key))), value })) | ||
} | ||
|
||
async * _allKeys () { | ||
yield * Object.entries(this.data) | ||
.map(([key]) => CID.createV1(raw.code, Digest.decode(base32.decode(key)))) | ||
} | ||
} | ||
|
||
module.exports = MemoryBlockstore |