getMessages working
This commit is contained in:
parent
7273776e55
commit
b825f51ce4
@ -1,5 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
var imports = require('soop').imports();
|
var soop = require('soop');
|
||||||
|
var imports = soop.imports();
|
||||||
var levelup = require('levelup');
|
var levelup = require('levelup');
|
||||||
var config = require('../config/config');
|
var config = require('../config/config');
|
||||||
var Rpc = imports.rpc || require('./Rpc');
|
var Rpc = imports.rpc || require('./Rpc');
|
||||||
@ -21,29 +22,27 @@ var db;
|
|||||||
|
|
||||||
var MessageDb = function(opts) {
|
var MessageDb = function(opts) {
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
this.path = config.leveldb + '/messages' + (opts.name ? ('-' + opts.name) : '')
|
this.path = config.leveldb + '/messages' + (opts.name ? ('-' + opts.name) : '');
|
||||||
if (!db) {
|
this.db = opts.db || db || levelup(this.path, {
|
||||||
db = levelup(this.path, {
|
maxOpenFiles: MAX_OPEN_FILES,
|
||||||
maxOpenFiles: MAX_OPEN_FILES
|
valueEncoding : 'json'
|
||||||
});
|
});
|
||||||
}
|
db = this.db;
|
||||||
this.db = db;
|
|
||||||
this.initEvents();
|
this.initEvents();
|
||||||
};
|
};
|
||||||
util.inherits(MessageDb, EventEmitter);
|
util.inherits(MessageDb, EventEmitter);
|
||||||
|
|
||||||
MessageDb.prototype.initEvents = function() {
|
MessageDb.prototype.initEvents = function() {
|
||||||
this.db.on('put', function(key, value) {
|
this.db.on('put', function(key, value) {
|
||||||
console.log('putting ' + key + '=>' + value);
|
|
||||||
var spl = key.split('-');
|
var spl = key.split('-');
|
||||||
var from = spl[0];
|
var from = spl[1];
|
||||||
var to = spl[1];
|
var to = spl[2];
|
||||||
var ts = spl[2];
|
var ts = spl[3];
|
||||||
var message = value;
|
var message = value;
|
||||||
sockets.broadcastMessage(from, to, ts, message);
|
sockets.broadcastMessage(from, to, ts, message);
|
||||||
});
|
});
|
||||||
this.db.on('ready', function() {
|
this.db.on('ready', function() {
|
||||||
console.log('Database ready!');
|
//console.log('Database ready!');
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -63,14 +62,32 @@ MessageDb.prototype.addMessage = function(m, from, to, cb) {
|
|||||||
this.db.put(key, value, cb);
|
this.db.put(key, value, cb);
|
||||||
};
|
};
|
||||||
|
|
||||||
MessageDb.prototype.getMessages = function(from, to, from_ts, to_ts, cb) {
|
MessageDb.prototype.getMessages = function(from, to, lower_ts, upper_ts, cb) {
|
||||||
// TODO
|
var list = [];
|
||||||
this.db.get(messageKey(from, to), function(err, val) {
|
var opts = {
|
||||||
if (err && err.notFound) return cb();
|
start: messageKey(from, to, upper_ts.getTime()),
|
||||||
if (err) return cb(err);
|
end: messageKey(from, to, lower_ts.getTime()),
|
||||||
|
//limit: limit, TODO
|
||||||
|
reverse: 1,
|
||||||
|
};
|
||||||
|
|
||||||
return cb(null, val);
|
db.createReadStream(opts)
|
||||||
});
|
.on('data', function(data) {
|
||||||
|
var spl = data.key.split('-');
|
||||||
|
var from = spl[1];
|
||||||
|
var to = spl[2];
|
||||||
|
var ts = spl[3];
|
||||||
|
list.push({
|
||||||
|
ts: ts,
|
||||||
|
message: data.value,
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.on('error', function(err) {
|
||||||
|
return cb(err);
|
||||||
|
})
|
||||||
|
.on('end', function() {
|
||||||
|
return cb(null, list.reverse());
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = require('soop')(MessageDb);
|
module.exports = soop(MessageDb);
|
||||||
|
|||||||
13
package.json
13
package.json
@ -71,16 +71,17 @@
|
|||||||
"bufferput": "git://github.com/bitpay/node-bufferput.git"
|
"bufferput": "git://github.com/bitpay/node-bufferput.git"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"chai": "*",
|
||||||
"grunt": "~0.4.2",
|
"grunt": "~0.4.2",
|
||||||
"grunt-cli": "~0.1.11",
|
"grunt-cli": "~0.1.11",
|
||||||
"grunt-env": "~0.4.1",
|
"grunt-concurrent": "~0.4.2",
|
||||||
"grunt-contrib-jshint": "~0.8.0",
|
"grunt-contrib-jshint": "~0.8.0",
|
||||||
"grunt-contrib-watch": "~0.5.3",
|
"grunt-contrib-watch": "~0.5.3",
|
||||||
"grunt-concurrent": "~0.4.2",
|
"grunt-env": "~0.4.1",
|
||||||
"grunt-nodemon": "~0.2.0",
|
"grunt-markdown": "~0.5.0",
|
||||||
"grunt-mocha-test": "~0.8.1",
|
"grunt-mocha-test": "~0.8.1",
|
||||||
"should": "2.1.1",
|
"grunt-nodemon": "~0.2.0",
|
||||||
"chai": "*",
|
"memdown": "^0.10.2",
|
||||||
"grunt-markdown": "~0.5.0"
|
"should": "2.1.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,33 +2,57 @@
|
|||||||
|
|
||||||
var chai = require('chai');
|
var chai = require('chai');
|
||||||
var should = chai.should;
|
var should = chai.should;
|
||||||
|
var expect = chai.expect;
|
||||||
|
|
||||||
var MessageDb = require('../lib/MessageDb');
|
var MessageDb = require('../lib/MessageDb');
|
||||||
var bitcore = require('bitcore');
|
var bitcore = require('bitcore');
|
||||||
var SIN = bitcore.SIN;
|
var SIN = bitcore.SIN;
|
||||||
|
var levelup = require('levelup');
|
||||||
|
var memdown = require('memdown');
|
||||||
|
|
||||||
describe('MessageDb', function() {
|
describe('MessageDb', function() {
|
||||||
|
var opts = {
|
||||||
|
name: 'test-MessageDb',
|
||||||
|
db: levelup({
|
||||||
|
db: memdown,
|
||||||
|
sync: true,
|
||||||
|
valueEncoding : 'json'
|
||||||
|
})
|
||||||
|
};
|
||||||
it('should be able to create instance', function() {
|
it('should be able to create instance', function() {
|
||||||
var mdb = new MessageDb(opts);
|
var mdb = new MessageDb(opts);
|
||||||
|
expect(mdb).to.exist;
|
||||||
});
|
});
|
||||||
it('should be able to close instance', function() {
|
it('should be able to create default instance', function() {
|
||||||
var mdb = new MessageDb(opts);
|
var mdb = MessageDb.default();
|
||||||
mdb.close();
|
expect(mdb).to.exist;
|
||||||
});
|
});
|
||||||
var opts = {
|
|
||||||
name: 'test-MessageDb'
|
|
||||||
};
|
|
||||||
var from = new SIN(new Buffer('dadbad00', 'hex'));
|
var from = new SIN(new Buffer('dadbad00', 'hex'));
|
||||||
var to = new SIN(new Buffer('bacacafe', 'hex'));
|
var to = new SIN(new Buffer('bacacafe', 'hex'));
|
||||||
var message = {
|
var message = {
|
||||||
a: 1,
|
a: 1,
|
||||||
b: 2
|
b: 2
|
||||||
};
|
};
|
||||||
it('should be able to add messages', function(done) {
|
it('should be able to add and read messages', function(done) {
|
||||||
var mdb = new MessageDb(opts);
|
var mdb = new MessageDb(opts);
|
||||||
console.log(to.toString());
|
|
||||||
mdb.addMessage(message, from, to, function(err) {
|
mdb.addMessage(message, from, to, function(err) {
|
||||||
done();
|
expect(err).to.not.exist;
|
||||||
|
var lower_ts = new Date('01/01/2014');
|
||||||
|
var upper_ts = new Date();
|
||||||
|
mdb.getMessages(from, to, lower_ts, upper_ts, function(err, messages) {
|
||||||
|
expect(err).to.not.exist;
|
||||||
|
messages.length.should.equal(1);
|
||||||
|
var m = messages[0].message;
|
||||||
|
m.a.should.equal(1);
|
||||||
|
m.b.should.equal(2);
|
||||||
|
done();
|
||||||
|
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
it('should be able to close instance', function() {
|
||||||
|
var mdb = new MessageDb(opts);
|
||||||
|
mdb.close();
|
||||||
|
expect(mdb).to.exist;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user