Skip to content

Commit fc844eb

Browse files
author
Lars-Magnus Skog
authored
Merge pull request facebook#28 from Level/abstract-leveldown-v4
update abstract-leveldown to v4
2 parents c04895d + 3b9d251 commit fc844eb

20 files changed

+198
-73
lines changed

leveldown.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,21 @@ LevelDOWN.prototype._batch = function (operations, options, callback) {
5353
}
5454

5555

56-
LevelDOWN.prototype._approximateSize = function (start, end, callback) {
56+
LevelDOWN.prototype.approximateSize = function (start, end, callback) {
57+
if (start == null ||
58+
end == null ||
59+
typeof start === 'function' ||
60+
typeof end === 'function') {
61+
throw new Error('approximateSize() requires valid `start`, `end` and `callback` arguments')
62+
}
63+
64+
if (typeof callback !== 'function') {
65+
throw new Error('approximateSize() requires a callback argument')
66+
}
67+
68+
start = this._serializeKey(start)
69+
end = this._serializeKey(end)
70+
5771
this.binding.approximateSize(start, end, callback)
5872
}
5973

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"main": "leveldown.js",
3232
"typings": "leveldown.d.ts",
3333
"dependencies": {
34-
"abstract-leveldown": "~3.0.0",
34+
"abstract-leveldown": "~4.0.0",
3535
"bindings": "~1.3.0",
3636
"fast-future": "~1.0.2",
3737
"nan": "~2.8.0",

test/approximate-size-test.js

+122-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,123 @@
1-
const test = require('tape')
2-
, testCommon = require('abstract-leveldown/testCommon')
3-
, leveldown = require('../')
4-
, abstract = require('abstract-leveldown/abstract/approximate-size-test')
1+
const test = require('tape')
2+
const leveldown = require('..')
3+
const testCommon = require('abstract-leveldown/testCommon')
54

6-
abstract.all(leveldown, test, testCommon)
5+
var db
6+
7+
test('setUp common for approximate size', testCommon.setUp)
8+
9+
test('setUp db', function (t) {
10+
db = leveldown(testCommon.location())
11+
console.log('db', db)
12+
db.open(t.end.bind(t))
13+
})
14+
15+
test('test argument-less approximateSize() throws', function (t) {
16+
t.throws(
17+
db.approximateSize.bind(db)
18+
, { name: 'Error', message: 'approximateSize() requires valid `start`, `end` and `callback` arguments' }
19+
, 'no-arg approximateSize() throws'
20+
)
21+
t.end()
22+
})
23+
24+
test('test callback-less, 1-arg, approximateSize() throws', function (t) {
25+
t.throws(
26+
db.approximateSize.bind(db, 'foo')
27+
, { name: 'Error', message: 'approximateSize() requires valid `start`, `end` and `callback` arguments' }
28+
, 'callback-less, 1-arg approximateSize() throws'
29+
)
30+
t.end()
31+
})
32+
33+
test('test callback-less, 2-arg, approximateSize() throws', function (t) {
34+
t.throws(
35+
db.approximateSize.bind(db, 'foo', 'bar')
36+
, { name: 'Error', message: 'approximateSize() requires a callback argument' }
37+
, 'callback-less, 2-arg approximateSize() throws'
38+
)
39+
t.end()
40+
})
41+
42+
test('test callback-less, 3-arg, approximateSize() throws', function (t) {
43+
t.throws(
44+
db.approximateSize.bind(db, function () {})
45+
, { name: 'Error', message: 'approximateSize() requires valid `start`, `end` and `callback` arguments' }
46+
, 'callback-only approximateSize() throws'
47+
)
48+
t.end()
49+
})
50+
51+
test('test callback-only approximateSize() throws', function (t) {
52+
t.throws(
53+
db.approximateSize.bind(db, function () {})
54+
, { name: 'Error', message: 'approximateSize() requires valid `start`, `end` and `callback` arguments' }
55+
, 'callback-only approximateSize() throws'
56+
)
57+
t.end()
58+
})
59+
60+
test('test 1-arg + callback approximateSize() throws', function (t) {
61+
t.throws(
62+
db.approximateSize.bind(db, 'foo', function () {})
63+
, { name: 'Error', message: 'approximateSize() requires valid `start`, `end` and `callback` arguments' }
64+
, '1-arg + callback approximateSize() throws'
65+
)
66+
t.end()
67+
})
68+
69+
test('test custom _serialize*', function (t) {
70+
t.plan(4)
71+
var db = leveldown(testCommon.location())
72+
db._serializeKey = function (data) { return data }
73+
db.approximateSize = function (start, end, callback) {
74+
t.deepEqual(start, { foo: 'bar' })
75+
t.deepEqual(end, { beep: 'boop' })
76+
process.nextTick(callback)
77+
}
78+
db.open(function () {
79+
db.approximateSize({ foo: 'bar' }, { beep: 'boop' }, function (err) {
80+
t.error(err)
81+
db.close(t.error.bind(t))
82+
})
83+
})
84+
})
85+
86+
test('test approximateSize()', function (t) {
87+
var data = Array.apply(null, Array(10000)).map(function () {
88+
return 'aaaaaaaaaa'
89+
}).join('')
90+
91+
db.batch(Array.apply(null, Array(10)).map(function (x, i) {
92+
return { type: 'put', key: 'foo' + i, value: data }
93+
}), function (err) {
94+
t.error(err)
95+
96+
// cycle open/close to ensure a pack to .sst
97+
98+
db.close(function (err) {
99+
t.error(err)
100+
101+
db.open(function (err) {
102+
t.error(err)
103+
104+
db.approximateSize('!', '~', function (err, size) {
105+
t.error(err)
106+
107+
t.equal(typeof size, 'number')
108+
// account for snappy compression, original would be ~100000
109+
t.ok(size > 40000, 'size reports a reasonable amount (' + size + ')')
110+
111+
db.close(function (err) {
112+
t.error(err)
113+
t.end()
114+
})
115+
})
116+
})
117+
})
118+
})
119+
})
120+
121+
test('tearDown', function (t) {
122+
db.close(testCommon.tearDown.bind(null, t))
123+
})

test/batch-test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const test = require('tape')
2-
, testCommon = require('abstract-leveldown/testCommon')
32
, leveldown = require('../')
43
, abstract = require('abstract-leveldown/abstract/batch-test')
54

6-
abstract.all(leveldown, test, testCommon)
5+
abstract.all(leveldown, test)

test/chained-batch-test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const test = require('tape')
2-
, testCommon = require('abstract-leveldown/testCommon')
32
, leveldown = require('../')
43
, abstract = require('abstract-leveldown/abstract/chained-batch-test')
54

6-
abstract.all(leveldown, test, testCommon)
5+
abstract.all(leveldown, test)

test/cleanup-hanging-iterators-test.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const test = require('tape')
2-
, testCommon = require('abstract-leveldown/testCommon')
32
, leveldown = require('../')
43
, makeTest = require('./make')
54

test/close-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports.tearDown = function () {
1515

1616
module.exports.all = function (leveldown) {
1717
module.exports.setUp()
18-
module.exports.close(leveldown, test, testCommon)
18+
module.exports.close(leveldown, test)
1919
module.exports.tearDown()
2020
}
2121

test/compact-range-test.js

+18-14
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ test('setUp db', function (t) {
1212
})
1313

1414
test('test compactRange() frees disk space after key deletion', function (t) {
15-
var key1 = '000000';
16-
var key2 = '000001';
17-
var val1 = Buffer(64).fill(1);
18-
var val2 = Buffer(64).fill(1);
15+
var key1 = '000000'
16+
var key2 = '000001'
17+
var val1 = Buffer(64).fill(1)
18+
var val2 = Buffer(64).fill(1)
1919
db.put(key1, val1, function() {
2020
db.put(key2, val2, function() {
2121
db.compactRange(key1, key2, function() {
@@ -25,13 +25,17 @@ test('test compactRange() frees disk space after key deletion', function (t) {
2525
db.compactRange(key1, key2, function() {
2626
db.approximateSize('0', 'z', function(err, sizeAfterCompact) {
2727
t.ok(sizeAfterCompact < sizeAfterPuts);
28-
t.end();
29-
});
30-
});
31-
});
32-
});
33-
});
34-
});
35-
});
36-
});
37-
});
28+
t.end()
29+
})
30+
})
31+
})
32+
})
33+
})
34+
})
35+
})
36+
})
37+
})
38+
39+
test('tearDown', function (t) {
40+
db.close(testCommon.tearDown.bind(null, t))
41+
})

test/data/testdata.bin

-4.58 KB
Binary file not shown.

test/del-test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const test = require('tape')
2-
, testCommon = require('abstract-leveldown/testCommon')
32
, leveldown = require('../')
43
, abstract = require('abstract-leveldown/abstract/del-test')
54

6-
abstract.all(leveldown, test, testCommon)
5+
abstract.all(leveldown, test)

test/destroy-test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const test = require('tape')
33
, path = require('path')
44
, mkfiletree = require('mkfiletree')
55
, readfiletree = require('readfiletree')
6-
, testCommon = require('abstract-leveldown/testCommon')
76
, leveldown = require('../')
87
, makeTest = require('./make')
98

@@ -58,7 +57,7 @@ makeTest('test destroy() cleans and removes leveldb-only dir', function (db, t,
5857
db.close(function (err) {
5958
t.notOk(err, 'no error')
6059
leveldown.destroy(location, function () {
61-
t.notOk(fs.existsSync(), 'directory completely removed')
60+
t.notOk(fs.existsSync(location), 'directory completely removed')
6261
done(false)
6362
})
6463
})

test/get-test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const test = require('tape')
2-
, testCommon = require('abstract-leveldown/testCommon')
32
, leveldown = require('../')
43
, abstract = require('abstract-leveldown/abstract/get-test')
54

6-
abstract.all(leveldown, test, testCommon)
5+
abstract.all(leveldown, test)

test/iterator-range-test.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const test = require('tape')
2+
, leveldown = require('..')
3+
, abstract = require('abstract-leveldown/abstract/iterator-range-test')
4+
5+
abstract.all(leveldown, test)

test/iterator-test.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
const test = require('tape')
2-
, testCommon = require('abstract-leveldown/testCommon')
32
, leveldown = require('../')
43
, abstract = require('abstract-leveldown/abstract/iterator-test')
54
, make = require('./make')
65
, iota = require('iota-array')
76
, lexi = require('lexicographic-integer')
87
, util = require('util')
98

10-
abstract.all(leveldown, test, testCommon)
9+
abstract.all(leveldown, test)
1110

1211
make('iterator throws if key is not a string or buffer', function (db, t, done) {
1312
var keys = [null, undefined, 1, true, false]
@@ -288,4 +287,4 @@ function not (n) {
288287

289288
function even (n) {
290289
return n % 2 === 0
291-
}
290+
}

test/make.js

+26-21
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,33 @@ function makeTest (name, testFn) {
88
test(name, function (t) {
99
cleanup(function () {
1010
var loc = location()
11-
, db = leveldown(loc)
12-
, done = function (close) {
13-
if (close === false)
14-
return cleanup(t.end.bind(t))
15-
db.close(function (err) {
16-
t.notOk(err, 'no error from close()')
17-
cleanup(t.end.bind(t))
18-
})
19-
}
11+
var db = leveldown(loc)
12+
var done = function (close) {
13+
if (close === false) {
14+
cleanup(function (err) {
15+
t.error(err, 'no error after cleanup')
16+
t.end()
17+
})
18+
return
19+
}
20+
db.close(function (err) {
21+
t.notOk(err, 'no error from close()')
22+
cleanup(function (err) {
23+
t.error(err, 'no error after cleanup')
24+
t.end()
25+
})
26+
})
27+
}
2028
db.open(function (err) {
21-
t.notOk(err, 'no error from open()')
22-
db.batch(
23-
[
24-
{ type: 'put', key: 'one', value: '1' }
25-
, { type: 'put', key: 'two', value: '2' }
26-
, { type: 'put', key: 'three', value: '3' }
27-
]
28-
, function (err) {
29-
t.notOk(err, 'no error from batch()')
30-
testFn(db, t, done, loc)
31-
}
32-
)
29+
t.notOk(err, 'no error from open()')
30+
db.batch([
31+
{ type: 'put', key: 'one', value: '1' },
32+
{ type: 'put', key: 'two', value: '2' },
33+
{ type: 'put', key: 'three', value: '3' }
34+
], function (err) {
35+
t.notOk(err, 'no error from batch()')
36+
testFn(db, t, done, loc)
37+
})
3338
})
3439
})
3540
})

test/open-test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const test = require('tape')
2-
, testCommon = require('abstract-leveldown/testCommon')
32
, leveldown = require('../')
43
, abstract = require('abstract-leveldown/abstract/open-test')
54

6-
abstract.all(leveldown, test, testCommon)
5+
abstract.all(leveldown, test)

test/put-get-del-test.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
const test = require('tape')
2-
, testCommon = require('abstract-leveldown/testCommon')
32
, leveldown = require('../')
4-
, fs = require('fs')
5-
, path = require('path')
6-
, testBuffer = fs.readFileSync(path.join(__dirname, 'data/testdata.bin'))
73
, abstract = require('abstract-leveldown/abstract/put-get-del-test')
84

9-
abstract.all(leveldown, test, testCommon, testBuffer)
5+
abstract.all(leveldown, test)

test/put-test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const test = require('tape')
2-
, testCommon = require('abstract-leveldown/testCommon')
32
, leveldown = require('../')
43
, abstract = require('abstract-leveldown/abstract/put-test')
54

6-
abstract.all(leveldown, test, testCommon)
5+
abstract.all(leveldown, test)

test/ranges-test.js

-6
This file was deleted.

test/repair-test.js

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const test = require('tape')
33
, path = require('path')
44
, mkfiletree = require('mkfiletree')
55
, readfiletree = require('readfiletree')
6-
, testCommon = require('abstract-leveldown/testCommon')
76
, leveldown = require('../')
87
, makeTest = require('./make')
98

0 commit comments

Comments
 (0)