Skip to content
This repository was archived by the owner on Dec 2, 2024. It is now read-only.

Commit 3f4a8fb

Browse files
committed
Standard
1 parent b43e233 commit 3f4a8fb

File tree

10 files changed

+264
-257
lines changed

10 files changed

+264
-257
lines changed

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,27 @@ directory transparently.
3333
This means that if you have 2 programs, 1 that gets:
3434

3535
```js
36-
var level = require('level-party');
37-
var db = level(__dirname + '/data', { valueEncoding: 'json' });
36+
var level = require('level-party')
37+
var db = level(__dirname + '/data', { valueEncoding: 'json' })
3838

3939
setInterval(function () {
40-
db.get('a', function (err, value) {
41-
console.log('a=', value);
42-
});
43-
}, 250);
40+
db.get('a', function (err, value) {
41+
console.log('a=', value)
42+
})
43+
}, 250)
4444
```
4545

4646
And 1 that puts:
4747

4848
```js
49-
var level = require('level-party');
50-
var db = level(__dirname + '/data', { valueEncoding: 'json' });
49+
var level = require('level-party')
50+
var db = level(__dirname + '/data', { valueEncoding: 'json' })
5151

52-
var n = Math.floor(Math.random() * 100000);
52+
var n = Math.floor(Math.random() * 100000)
5353

5454
setInterval(function () {
55-
db.put('a', n + 1);
56-
}, 1000);
55+
db.put('a', n + 1)
56+
}, 1000)
5757
```
5858

5959
and you start them up in any order, everything will just work! No more
@@ -91,7 +91,7 @@ and the leader goes down while you are reading that stream level-party will resu
9191
[**This disables leveldb snapshotting**](https://github.com/level/leveldown#snapshots) so if your app relies on this you should disable this by setting `opts.retry = false`
9292

9393
```js
94-
var db = level('./data', {retry: false}) // will not retry streams / gets / puts if the leader goes down
94+
var db = level('./data', { retry: false }) // will not retry streams / gets / puts if the leader goes down
9595
```
9696

9797
## Windows support

example/bulk-insert.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
var level = require('../');
2-
var db = level(__dirname + '/data', { valueEncoding: 'json' });
1+
var level = require('..')
2+
var path = require('path')
3+
var db = level(path.join(__dirname, 'data'), { valueEncoding: 'json' })
34

45
db.on('leader', function () {
56
console.log('i am the leader now')
@@ -8,7 +9,7 @@ db.on('leader', function () {
89
var tick = 0
910

1011
function loop () {
11-
db.put('hello-' + tick, {hello: 'world-' + tick}, function () {
12+
db.put('hello-' + tick, { hello: 'world-' + tick }, function () {
1213
console.log('inserted %d values', tick)
1314
if (tick === 10000) process.exit()
1415
tick++

example/get.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
var level = require('../');
2-
var db = level(__dirname + '/data', { valueEncoding: 'json' });
1+
var level = require('..')
2+
var path = require('path')
3+
var db = level(path.join(__dirname, 'data'), { valueEncoding: 'json' })
34

45
db.on('leader', function () {
56
console.log('i am the leader now')
67
})
78

89
setInterval(function () {
9-
db.get('a', function (err, value) {
10-
console.log('a=', value);
11-
});
12-
}, 250);
10+
db.get('a', function (err, value) {
11+
if (err) throw err
12+
console.log('a=', value)
13+
})
14+
}, 250)

example/put.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
var level = require('../');
2-
var db = level(__dirname + '/data', { valueEncoding: 'json' });
1+
var level = require('..')
2+
var path = require('path')
3+
var db = level(path.join(__dirname, 'data'), { valueEncoding: 'json' })
34

45
db.on('leader', function () {
56
console.log('i am the leader now')
67
})
78

8-
var n = Math.floor(Math.random() * 100000);
9+
var n = Math.floor(Math.random() * 100000)
910

1011
setInterval(function () {
11-
db.put('a', n++);
12-
}, 1000);
12+
db.put('a', n++, function (err) {
13+
if (err) throw err
14+
})
15+
}, 1000)

example/read-stream.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
var level = require('../');
2-
var db = level(__dirname + '/data', { valueEncoding: 'json' });
1+
var level = require('..')
2+
var path = require('path')
3+
var db = level(path.join(__dirname, 'data'), { valueEncoding: 'json' })
34

45
db.on('leader', function () {
56
console.log('i am the leader now')
67
})
78

89
var rs = db.createReadStream()
9-
var tick = 0
1010

1111
rs.on('end', function () {
1212
console.log('(end)')
@@ -16,4 +16,4 @@ rs.on('end', function () {
1616
setInterval(function () {
1717
var next = rs.read()
1818
if (next) console.log(next)
19-
}, 250);
19+
}, 250)

index.js

Lines changed: 87 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,89 @@
1-
var level = require('level');
2-
var has = require('has');
3-
var pump = require('pump');
4-
var fs = require('fs');
5-
var net = require('net');
6-
var path = require('path');
7-
var multileveldown = require('multileveldown');
1+
'use strict'
2+
3+
var level = require('level')
4+
var has = require('has')
5+
var pump = require('pump')
6+
var fs = require('fs')
7+
var net = require('net')
8+
var path = require('path')
9+
var multileveldown = require('multileveldown')
810

911
module.exports = function (dir, opts) {
10-
if (!opts) opts = {};
11-
if (!has(opts, 'retry')) opts.retry = true;
12-
13-
var sockPath = process.platform === 'win32' ?
14-
'\\\\.\\pipe\\level-party\\' + path.resolve(dir) :
15-
path.join(dir, 'level-party.sock');
16-
17-
var client = multileveldown.client(opts);
18-
19-
client.open(tryConnect);
20-
21-
function tryConnect () {
22-
if (!client.isOpen()) return;
23-
24-
var socket = net.connect(sockPath);
25-
var connected = false;
26-
27-
socket.on('connect', function () {
28-
connected = true;
29-
});
30-
31-
// we pass socket as the ref option so we dont hang the event loop
32-
pump(socket, client.createRpcStream({ref: socket}), socket, function () {
33-
if (!client.isOpen()) return;
34-
35-
var db = level(dir, opts, onopen);
36-
37-
function onopen (err) {
38-
if (err) {
39-
if (connected) return tryConnect();
40-
return setTimeout(tryConnect, 100);
41-
}
42-
43-
fs.unlink(sockPath, function (err) {
44-
if (err && err.code !== 'ENOENT') return db.emit('error', err);
45-
if (!client.isOpen()) return;
46-
47-
var sockets = [];
48-
var server = net.createServer(function (sock) {
49-
if (sock.unref) sock.unref();
50-
sockets.push(sock);
51-
pump(sock, multileveldown.server(db), sock, function () {
52-
sockets.splice(sockets.indexOf(sock), 1);
53-
});
54-
});
55-
56-
client.close = shutdown;
57-
client.emit('leader');
58-
client.forward(db);
59-
60-
server.listen(sockPath, onlistening);
61-
62-
function shutdown (cb) {
63-
sockets.forEach(function (sock) {
64-
sock.destroy();
65-
});
66-
server.close(function () {
67-
db.close(cb);
68-
});
69-
}
70-
71-
function onlistening () {
72-
if (server.unref) server.unref();
73-
if (client.isFlushed()) return;
74-
75-
var sock = net.connect(sockPath);
76-
pump(sock, client.createRpcStream(), sock);
77-
client.once('flush', function () {
78-
sock.destroy();
79-
});
80-
}
81-
});
82-
}
83-
});
84-
};
85-
86-
return client;
87-
};
12+
if (!opts) opts = {}
13+
if (!has(opts, 'retry')) opts.retry = true
14+
15+
var sockPath = process.platform === 'win32'
16+
? '\\\\.\\pipe\\level-party\\' + path.resolve(dir)
17+
: path.join(dir, 'level-party.sock')
18+
19+
var client = multileveldown.client(opts)
20+
21+
client.open(tryConnect)
22+
23+
function tryConnect () {
24+
if (!client.isOpen()) return
25+
26+
var socket = net.connect(sockPath)
27+
var connected = false
28+
29+
socket.on('connect', function () {
30+
connected = true
31+
})
32+
33+
// we pass socket as the ref option so we dont hang the event loop
34+
pump(socket, client.createRpcStream({ ref: socket }), socket, function () {
35+
if (!client.isOpen()) return
36+
37+
var db = level(dir, opts, onopen)
38+
39+
function onopen (err) {
40+
if (err) {
41+
if (connected) return tryConnect()
42+
return setTimeout(tryConnect, 100)
43+
}
44+
45+
fs.unlink(sockPath, function (err) {
46+
if (err && err.code !== 'ENOENT') return db.emit('error', err)
47+
if (!client.isOpen()) return
48+
49+
var sockets = []
50+
var server = net.createServer(function (sock) {
51+
if (sock.unref) sock.unref()
52+
sockets.push(sock)
53+
pump(sock, multileveldown.server(db), sock, function () {
54+
sockets.splice(sockets.indexOf(sock), 1)
55+
})
56+
})
57+
58+
client.close = shutdown
59+
client.emit('leader')
60+
client.forward(db)
61+
62+
server.listen(sockPath, onlistening)
63+
64+
function shutdown (cb) {
65+
sockets.forEach(function (sock) {
66+
sock.destroy()
67+
})
68+
server.close(function () {
69+
db.close(cb)
70+
})
71+
}
72+
73+
function onlistening () {
74+
if (server.unref) server.unref()
75+
if (client.isFlushed()) return
76+
77+
var sock = net.connect(sockPath)
78+
pump(sock, client.createRpcStream(), sock)
79+
client.once('flush', function () {
80+
sock.destroy()
81+
})
82+
}
83+
})
84+
}
85+
})
86+
};
87+
88+
return client
89+
}

test/bytewise.js

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,38 @@
1-
var test = require('tape');
2-
var level = require('../');
3-
var path = require('path');
4-
var bytewise = require('bytewise');
5-
var os = require('os');
6-
var tmpdir = require('osenv').tmpdir();
7-
var datadir = path.join(tmpdir, 'level-party-' + Math.random());
1+
var test = require('tape')
2+
var level = require('..')
3+
var path = require('path')
4+
var bytewise = require('bytewise')
5+
var tmpdir = require('osenv').tmpdir()
6+
var datadir = path.join(tmpdir, 'level-party-' + Math.random())
87

9-
var lopts = { keyEncoding: bytewise, valueEncoding: 'json' };
8+
var lopts = { keyEncoding: bytewise, valueEncoding: 'json' }
109

1110
test('bytewise key encoding', function (t) {
12-
t.plan(7);
13-
var adb = level(datadir, lopts);
14-
var bdb = level(datadir, lopts);
15-
var value = Math.floor(Math.random() * 100000);
16-
17-
adb.put([ 'a' ], value, function (err) {
18-
t.ifError(err);
19-
var times = 0;
20-
21-
bdb.get([ 'a' ], function (err, x) {
22-
t.ifError(err);
23-
t.equal(x, value);
24-
});
25-
26-
adb.createReadStream().on('data', function (row) {
27-
t.deepEqual(row.key, [ 'a' ]);
28-
t.deepEqual(row.value, value);
29-
});
30-
bdb.createReadStream().on('data', function (row) {
31-
t.deepEqual(row.key, [ 'a' ]);
32-
t.deepEqual(row.value, value);
33-
});
34-
});
35-
36-
t.on('end', function () {
37-
adb.close();
38-
bdb.close();
39-
});
40-
});
11+
t.plan(7)
12+
var adb = level(datadir, lopts)
13+
var bdb = level(datadir, lopts)
14+
var value = Math.floor(Math.random() * 100000)
15+
16+
adb.put(['a'], value, function (err) {
17+
t.ifError(err)
18+
19+
bdb.get(['a'], function (err, x) {
20+
t.ifError(err)
21+
t.equal(x, value)
22+
})
23+
24+
adb.createReadStream().on('data', function (row) {
25+
t.deepEqual(row.key, ['a'])
26+
t.deepEqual(row.value, value)
27+
})
28+
bdb.createReadStream().on('data', function (row) {
29+
t.deepEqual(row.key, ['a'])
30+
t.deepEqual(row.value, value)
31+
})
32+
})
33+
34+
t.on('end', function () {
35+
adb.close()
36+
bdb.close()
37+
})
38+
})

0 commit comments

Comments
 (0)