Comparing version 0.0.3 to 0.1.0
@@ -1,3 +0,2 @@ | ||
const async = require('async'); | ||
const asyncCache = require('async-cache'); | ||
const LRU = require('lru-cache'); | ||
@@ -12,4 +11,4 @@ const sector = require('./sector'); | ||
let index = 0 ; | ||
for(let i = 2; i < key.byteLength; i++) { | ||
let index = 0; | ||
for (let i = 2; i < key.byteLength; i++) { | ||
index *= 16; | ||
@@ -25,11 +24,9 @@ index += key[i]; | ||
function bromba(path, { vlen, max = Infinity }) { | ||
const cache = asyncCache({ | ||
function bromba(path, { vlen, max = 10 * 1024 * 1024}) { | ||
const cache = new LRU({ | ||
max, | ||
maxAge: Infinity, | ||
stale: true, | ||
length: s => s.size(), | ||
load: loadSectorFromDisk, | ||
fetchMethod: loadSectorFromDisk, | ||
dispose: flushSector | ||
}); | ||
let closing = false; | ||
@@ -40,53 +37,49 @@ function nameToFile(name) { | ||
function loadSectorFromDisk(name, fn) { | ||
let file = nameToFile(name); | ||
let s = sector({ vlen }); | ||
s.readFromFile(file, fn); | ||
function loadSectorFromDisk(name) { | ||
const file = nameToFile(name); | ||
const s = sector({ vlen }); | ||
return s.readFromFile(file); | ||
} | ||
function flushSector(name, s, fn) { | ||
let file = nameToFile(name); | ||
s.writeToFile(file, fn); | ||
async function flushSector(v, name) { | ||
if (closing) { | ||
// if we are closing all sectors are flushed already | ||
return; | ||
} | ||
const file = nameToFile(name); | ||
const s = await v; | ||
return s.writeToFile(file); | ||
} | ||
function putOne(key, value, fn) { | ||
async function putOne(key, value) { | ||
const { name, index } = keyToLocation(key); | ||
cache.get(name, function(err, sector) { | ||
if (!err) { | ||
sector.putValue(index, value); | ||
} | ||
fn(err); | ||
}); | ||
const sector = await cache.fetch(name); | ||
return sector.putValue(index, value); | ||
} | ||
function putMany(keys, values, fn) { | ||
async.eachOf(keys, (k, i, fn) => putOne(k, values[i], fn), fn); | ||
function putMany(keys, values) { | ||
return Promise.all(keys.map((k, i) => putOne(k, values[i]))); | ||
} | ||
function getOne(key, fn) { | ||
async function getOne(key) { | ||
const { name, index } = keyToLocation(key); | ||
cache.get(name, function(err, sector) { | ||
if(err) { return fn(err); } | ||
fn(null, sector.getValue(index)); | ||
}); | ||
const sector = await cache.fetch(name); | ||
return sector.getValue(index); | ||
} | ||
function getMany(keys, fn) { | ||
async.map(keys, getOne, fn); | ||
function getMany(keys) { | ||
return Promise.all(keys.map(getOne)); | ||
} | ||
function open(fn = () => {}) { | ||
// nothing to do on open | ||
fn(); | ||
function open() { | ||
} | ||
function close(fn = () => {}) { | ||
async.each( | ||
cache.keys(), | ||
(name, fn) => flushSector(name, cache.peek(name), fn), | ||
err => { | ||
cache.reset(); | ||
fn(err); | ||
} | ||
); | ||
async function close() { | ||
const flushTasks = Array | ||
.from(cache.entries()) | ||
.map(([k, v]) => flushSector(v, k)); | ||
await Promise.all(flushTasks); | ||
closing = true; | ||
cache.clear(); | ||
closing = false; | ||
} | ||
@@ -93,0 +86,0 @@ |
@@ -1,3 +0,2 @@ | ||
const { waterfall } = require('async'); | ||
const { writeFile, readFile, mkdir } = require('fs'); | ||
const { writeFile, readFile, mkdir } = require('fs').promises; | ||
const { dirname } = require('path'); | ||
@@ -66,42 +65,28 @@ const { compress, uncompress } = require('snappy'); | ||
function readFromFile(file, fn) { | ||
readFile(file, onread); | ||
function onread(err, cbuffer) { | ||
async function readFromFile(file) { | ||
try { | ||
const cbuffer = await readFile(file); | ||
data = await uncompress(cbuffer); | ||
dirty = false; | ||
if (!err) { | ||
return uncompress(cbuffer, ondata); | ||
} | ||
return self; | ||
} catch(err) { | ||
if (err.code === 'ENOENT') { | ||
// it's OK if file does not exist yet | ||
return fn(null, self); | ||
return self; | ||
} | ||
fn(err); | ||
throw err; | ||
} | ||
function ondata(err, buffer) { | ||
if (err) { | ||
return fn(err); | ||
} | ||
dirty = false; | ||
data = buffer; | ||
fn(err, self); | ||
} | ||
} | ||
function writeToFile(file, fn = () => {}) { | ||
async function writeToFile(file) { | ||
if (!dirty) { | ||
return fn(null, self); | ||
return self; | ||
} | ||
dirty = false; | ||
waterfall([ | ||
fn => mkdir(dirname(file), { recursive: true }, fn), | ||
fn => compress(data, fn), | ||
(b, fn) => writeFile(file, b, fn) | ||
], err => { | ||
if (err) { | ||
console.error(err); | ||
} | ||
fn(err); | ||
}); | ||
const [ buffer ] = await Promise.all([ | ||
compress(data), | ||
mkdir(dirname(file), { recursive: true }) | ||
]); | ||
await writeFile(file, buffer); | ||
return self; | ||
} | ||
@@ -108,0 +93,0 @@ |
{ | ||
"name": "bromba", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"description": "Fast wasteful short buffer disk storage.", | ||
@@ -18,10 +18,8 @@ "author": { | ||
"dependencies": { | ||
"async": "~2", | ||
"async-cache": "~1", | ||
"snappy": "^6.2.2" | ||
"lru-cache": "^7.10.1", | ||
"snappy": "~7" | ||
}, | ||
"devDependencies": { | ||
"jshint": "~2", | ||
"tap-dot": "~2", | ||
"tape": "~4", | ||
"tape": "~5", | ||
"tmp": "~0" | ||
@@ -28,0 +26,0 @@ }, |
[![NPM version][npm-image]][npm-url] | ||
[![Build Status][travis-image]][travis-url] | ||
[![Build Status][build-image]][build-url] | ||
[![Dependency Status][deps-image]][deps-url] | ||
[![Dev Dependency Status][deps-dev-image]][deps-dev-url] | ||
@@ -24,16 +23,11 @@ # bromba | ||
// keys and values have to be Arrays of Buffer's | ||
let keys = [2, 3, 4].map(x => Buffer.from([x])); | ||
// keys and values have to be Arrays of Buffer's | ||
db.putMany(keys, values, function(err) { | ||
// for each key in an keys set corresponding value from values | ||
}); | ||
// for each key in an keys set corresponding value from values | ||
await db.putMany(keys, values) | ||
db.getMany(keys, function(err, values) { | ||
if (err) { | ||
// something went wrong | ||
} | ||
// values for each key from keys | ||
}); | ||
// values for each key from keys | ||
const values = await db.getMany(keys); | ||
``` | ||
@@ -45,12 +39,9 @@ | ||
[npm-image]: https://img.shields.io/npm/v/bromba.svg | ||
[npm-image]: https://img.shields.io/npm/v/bromba | ||
[npm-url]: https://npmjs.org/package/bromba | ||
[travis-url]: https://travis-ci.com/pirxpilot/bromba | ||
[travis-image]: https://img.shields.io/travis/com/pirxpilot/bromba.svg | ||
[build-url]: https://github.com/pirxpilot/bromba/actions/workflows/check.yaml | ||
[build-image]: https://img.shields.io/github/workflow/status/pirxpilot/bromba/check | ||
[deps-image]: https://img.shields.io/david/pirxpilot/bromba.svg | ||
[deps-url]: https://david-dm.org/pirxpilot/bromba | ||
[deps-dev-image]: https://img.shields.io/david/dev/pirxpilot/bromba.svg | ||
[deps-dev-url]: https://david-dm.org/pirxpilot/bromba?type=dev | ||
[deps-image]: https://img.shields.io/librariesio/release/npm/bromba | ||
[deps-url]: https://libraries.io/npm/bromba |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2
3
5495
5
159
46
+ Addedlru-cache@^7.10.1
+ Added@napi-rs/snappy-android-arm-eabi@7.2.2(transitive)
+ Added@napi-rs/snappy-android-arm64@7.2.2(transitive)
+ Added@napi-rs/snappy-darwin-arm64@7.2.2(transitive)
+ Added@napi-rs/snappy-darwin-x64@7.2.2(transitive)
+ Added@napi-rs/snappy-freebsd-x64@7.2.2(transitive)
+ Added@napi-rs/snappy-linux-arm-gnueabihf@7.2.2(transitive)
+ Added@napi-rs/snappy-linux-arm64-gnu@7.2.2(transitive)
+ Added@napi-rs/snappy-linux-arm64-musl@7.2.2(transitive)
+ Added@napi-rs/snappy-linux-x64-gnu@7.2.2(transitive)
+ Added@napi-rs/snappy-linux-x64-musl@7.2.2(transitive)
+ Added@napi-rs/snappy-win32-arm64-msvc@7.2.2(transitive)
+ Added@napi-rs/snappy-win32-ia32-msvc@7.2.2(transitive)
+ Added@napi-rs/snappy-win32-x64-msvc@7.2.2(transitive)
+ Addedlru-cache@7.18.3(transitive)
+ Addedsnappy@7.2.2(transitive)
- Removedasync@~2
- Removedasync-cache@~1
- Removedansi-regex@2.1.1(transitive)
- Removedaproba@1.2.0(transitive)
- Removedare-we-there-yet@1.1.7(transitive)
- Removedasync@2.6.4(transitive)
- Removedasync-cache@1.1.0(transitive)
- Removedbindings@1.5.0(transitive)
- Removedbl@1.2.3(transitive)
- Removedbuffer-alloc@1.2.0(transitive)
- Removedbuffer-alloc-unsafe@1.1.0(transitive)
- Removedbuffer-fill@1.0.0(transitive)
- Removedchownr@1.1.4(transitive)
- Removedcode-point-at@1.1.0(transitive)
- Removedconsole-control-strings@1.1.0(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removeddecompress-response@3.3.0(transitive)
- Removeddeep-extend@0.6.0(transitive)
- Removeddelegates@1.0.0(transitive)
- Removeddetect-libc@1.0.3(transitive)
- Removedend-of-stream@1.4.4(transitive)
- Removedexpand-template@2.0.3(transitive)
- Removedfile-uri-to-path@1.0.0(transitive)
- Removedfs-constants@1.0.0(transitive)
- Removedgauge@2.7.4(transitive)
- Removedgithub-from-package@0.0.0(transitive)
- Removedhas-unicode@2.0.1(transitive)
- Removedinherits@2.0.4(transitive)
- Removedini@1.3.8(transitive)
- Removedis-fullwidth-code-point@1.0.0(transitive)
- Removedisarray@1.0.0(transitive)
- Removedlodash@4.17.21(transitive)
- Removedlru-cache@4.1.5(transitive)
- Removedmimic-response@1.0.1(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmkdirp@0.5.6(transitive)
- Removednan@2.22.0(transitive)
- Removednapi-build-utils@1.0.2(transitive)
- Removednode-abi@2.30.1(transitive)
- Removednoop-logger@0.1.1(transitive)
- Removednpmlog@4.1.2(transitive)
- Removednumber-is-nan@1.0.1(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedonce@1.4.0(transitive)
- Removedos-homedir@1.0.2(transitive)
- Removedprebuild-install@5.3.0(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedpseudomap@1.0.2(transitive)
- Removedpump@1.0.32.0.1(transitive)
- Removedrc@1.2.8(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedsemver@5.7.2(transitive)
- Removedset-blocking@2.0.0(transitive)
- Removedsignal-exit@3.0.7(transitive)
- Removedsimple-concat@1.0.1(transitive)
- Removedsimple-get@2.8.2(transitive)
- Removedsnappy@6.3.5(transitive)
- Removedstring-width@1.0.2(transitive)
- Removedstring_decoder@1.1.1(transitive)
- Removedstrip-ansi@3.0.1(transitive)
- Removedstrip-json-comments@2.0.1(transitive)
- Removedtar-fs@1.16.3(transitive)
- Removedtar-stream@1.6.2(transitive)
- Removedto-buffer@1.1.1(transitive)
- Removedtunnel-agent@0.6.0(transitive)
- Removedutil-deprecate@1.0.2(transitive)
- Removedwhich-pm-runs@1.1.0(transitive)
- Removedwide-align@1.1.5(transitive)
- Removedwrappy@1.0.2(transitive)
- Removedxtend@4.0.2(transitive)
- Removedyallist@2.1.2(transitive)
Updatedsnappy@~7