Skip to content

Commit 6948431

Browse files
committed
cli: refactor --batch from file to accept large inputs
allowing to use it for dump imports Also being more tolerant on the output, to accept the output of a --all dump
1 parent a7ec7c9 commit 6948431

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,38 @@ lev --batch '[
8888
```
8989
or from a file
9090
```sh
91+
# there should be one entry per line
92+
# either as valid JSON
9193
echo '[
9294
{"type":"del","key":"father"},
9395
{"type":"put","key":"name","value":"Yuri Irsenovich Kim"},
9496
{"type":"put","key":"dob","value":"16 February 1941"},
9597
{"type":"put","key":"spouse","value":"Kim Young-sook"},
9698
{"type":"put","key":"occupation","value":"Clown"}
9799
]' > ops.json
100+
# or as newline-delimited JSON
101+
echo '
102+
{"type":"del","key":"father"}
103+
{"type":"put","key":"name","value":"Yuri Irsenovich Kim"}
104+
{"type":"put","key":"dob","value":"16 February 1941"}
105+
{"type":"put","key":"spouse","value":"Kim Young-sook"}
106+
{"type":"put","key":"occupation","value":"Clown"}
107+
' > ops.json
98108
lev --batch ./ops.json
99109
```
100110

111+
If the type is omitted, defaults to `put`, which allows to use the command to do imports/exports, in combination with [`--all`](#--all):
112+
```sh
113+
lev --all > leveldb.export
114+
lev /tmp/my-new-db --batch leveldb.export
115+
```
116+
If it's a large export, you can compress it on the fly
117+
```sh
118+
lev --all | gzip -9 > leveldb.export.gz
119+
gzip -dk leveldb.export.gz
120+
lev /tmp/my-new-db --batch leveldb.export
121+
```
122+
101123
## --keys
102124
List all the keys in the current range. Will tabularize the output by default (see `--line`).
103125
```sh
@@ -117,6 +139,11 @@ Emit as a new-line delimited stream of json.
117139
```sh
118140
lev --all
119141
```
142+
It can be used to create an export of the database, to be imported with [`--batch`](#--batch)
143+
```sh
144+
lev --all > leveldb.export
145+
lev /tmp/my-new-db --batch leveldb.export
146+
```
120147

121148
## --start <key-pattern>
122149
Specify the start of the current range. You can also use `gt` or `gte`.

lib/batch_from_file.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
var fs = require('fs');
2+
var path = require('path');
3+
var split = require('split');
4+
var printAndExit = require('./print_and_exit');
5+
6+
module.exports = function(db, file) {
7+
var batch = [];
8+
var total = 0;
9+
10+
fs.createReadStream(path.resolve(file))
11+
.pipe(split())
12+
.on('data', function(line) {
13+
if (line[0] !== '{') return
14+
line = line.replace(/,$/, '')
15+
16+
//
17+
// add each line to the batch
18+
//
19+
var entry = JSON.parse(line);
20+
entry.type = entry.type || 'put';
21+
batch.push(entry)
22+
23+
//
24+
// run operations by batches of 10000 entries
25+
//
26+
if (batch.length >= 10000) {
27+
var stream = this;
28+
stream.pause();
29+
db.batch(batch, function(err, val) {
30+
if (err) return printAndExit(err, val);
31+
total += batch.length
32+
console.log('ops run:', total)
33+
batch = [];
34+
stream.resume();
35+
})
36+
}
37+
38+
})
39+
.on('close', function() {
40+
if (batch.length > 0) {
41+
db.batch(batch, function(err, val) {
42+
if (err) return printAndExit(err, val);
43+
total += batch.length;
44+
console.log('total ops:', total)
45+
});
46+
}
47+
else {
48+
console.log('total ops:', total)
49+
}
50+
})
51+
}
52+

lib/cli.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ module.exports = function(db, args) {
9494
}
9595
else if (args.batch) {
9696
if (args.batch[0] !== '[') {
97-
args.batch = require('fs').readFileSync(args.batch).toString()
97+
require('./batch_from_file')(db, args.batch)
98+
return
9899
}
99100
var batch = JSON.parse(args.batch);
100101
db.batch(batch, printAndExit);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"minimist": "^1.1.0",
4242
"multilevel": "^7.3.0",
4343
"rc": "^0.5.4",
44+
"split": "^1.0.1",
4445
"tabulate": "^1.0.0",
4546
"xtend": "^4.0.0"
4647
}

0 commit comments

Comments
 (0)