getMessages working
This commit is contained in:
parent
7273776e55
commit
b825f51ce4
@ -1,5 +1,6 @@
|
||||
'use strict';
|
||||
var imports = require('soop').imports();
|
||||
var soop = require('soop');
|
||||
var imports = soop.imports();
|
||||
var levelup = require('levelup');
|
||||
var config = require('../config/config');
|
||||
var Rpc = imports.rpc || require('./Rpc');
|
||||
@ -21,29 +22,27 @@ var db;
|
||||
|
||||
var MessageDb = function(opts) {
|
||||
opts = opts || {};
|
||||
this.path = config.leveldb + '/messages' + (opts.name ? ('-' + opts.name) : '')
|
||||
if (!db) {
|
||||
db = levelup(this.path, {
|
||||
maxOpenFiles: MAX_OPEN_FILES
|
||||
});
|
||||
}
|
||||
this.db = db;
|
||||
this.path = config.leveldb + '/messages' + (opts.name ? ('-' + opts.name) : '');
|
||||
this.db = opts.db || db || levelup(this.path, {
|
||||
maxOpenFiles: MAX_OPEN_FILES,
|
||||
valueEncoding : 'json'
|
||||
});
|
||||
db = this.db;
|
||||
this.initEvents();
|
||||
};
|
||||
util.inherits(MessageDb, EventEmitter);
|
||||
|
||||
MessageDb.prototype.initEvents = function() {
|
||||
this.db.on('put', function(key, value) {
|
||||
console.log('putting ' + key + '=>' + value);
|
||||
var spl = key.split('-');
|
||||
var from = spl[0];
|
||||
var to = spl[1];
|
||||
var ts = spl[2];
|
||||
var from = spl[1];
|
||||
var to = spl[2];
|
||||
var ts = spl[3];
|
||||
var message = value;
|
||||
sockets.broadcastMessage(from, to, ts, message);
|
||||
});
|
||||
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);
|
||||
};
|
||||
|
||||
MessageDb.prototype.getMessages = function(from, to, from_ts, to_ts, cb) {
|
||||
// TODO
|
||||
this.db.get(messageKey(from, to), function(err, val) {
|
||||
if (err && err.notFound) return cb();
|
||||
if (err) return cb(err);
|
||||
MessageDb.prototype.getMessages = function(from, to, lower_ts, upper_ts, cb) {
|
||||
var list = [];
|
||||
var opts = {
|
||||
start: messageKey(from, to, upper_ts.getTime()),
|
||||
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"
|
||||
},
|
||||
"devDependencies": {
|
||||
"chai": "*",
|
||||
"grunt": "~0.4.2",
|
||||
"grunt-cli": "~0.1.11",
|
||||
"grunt-env": "~0.4.1",
|
||||
"grunt-concurrent": "~0.4.2",
|
||||
"grunt-contrib-jshint": "~0.8.0",
|
||||
"grunt-contrib-watch": "~0.5.3",
|
||||
"grunt-concurrent": "~0.4.2",
|
||||
"grunt-nodemon": "~0.2.0",
|
||||
"grunt-env": "~0.4.1",
|
||||
"grunt-markdown": "~0.5.0",
|
||||
"grunt-mocha-test": "~0.8.1",
|
||||
"should": "2.1.1",
|
||||
"chai": "*",
|
||||
"grunt-markdown": "~0.5.0"
|
||||
"grunt-nodemon": "~0.2.0",
|
||||
"memdown": "^0.10.2",
|
||||
"should": "2.1.1"
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,33 +2,57 @@
|
||||
|
||||
var chai = require('chai');
|
||||
var should = chai.should;
|
||||
var expect = chai.expect;
|
||||
|
||||
var MessageDb = require('../lib/MessageDb');
|
||||
var bitcore = require('bitcore');
|
||||
var SIN = bitcore.SIN;
|
||||
var levelup = require('levelup');
|
||||
var memdown = require('memdown');
|
||||
|
||||
describe('MessageDb', function() {
|
||||
var opts = {
|
||||
name: 'test-MessageDb',
|
||||
db: levelup({
|
||||
db: memdown,
|
||||
sync: true,
|
||||
valueEncoding : 'json'
|
||||
})
|
||||
};
|
||||
it('should be able to create instance', function() {
|
||||
var mdb = new MessageDb(opts);
|
||||
expect(mdb).to.exist;
|
||||
});
|
||||
it('should be able to close instance', function() {
|
||||
var mdb = new MessageDb(opts);
|
||||
mdb.close();
|
||||
it('should be able to create default instance', function() {
|
||||
var mdb = MessageDb.default();
|
||||
expect(mdb).to.exist;
|
||||
});
|
||||
var opts = {
|
||||
name: 'test-MessageDb'
|
||||
};
|
||||
var from = new SIN(new Buffer('dadbad00', 'hex'));
|
||||
var to = new SIN(new Buffer('bacacafe', 'hex'));
|
||||
var message = {
|
||||
a: 1,
|
||||
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);
|
||||
console.log(to.toString());
|
||||
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