Permalink
Browse files

Updated sampledb storage and retrieval to push details into a separat…

…e key in leveldb
  • Loading branch information...
1 parent 6e677f4 commit 7e9ce207aa2cd7a0159a85f7fd29414ecdf30eae @DamonOehlman committed Aug 31, 2011
Showing with 61 additions and 29 deletions.
  1. +55 −26 lib/sampledb.js
  2. +6 −3 test/db.js
View
@@ -9,51 +9,70 @@ SampleDB.prototype.close = function() {
this.storage.close();
}; // close
-SampleDB.prototype.getSince = function(time, callback, maxItems) {
+SampleDB.prototype.getSince = function(time, callback, opts) {
+ // initialise options
+ opts = opts || {};
+ opts.maxItems = opts.maxItems || 1000;
+
+ // initialise variables
var error,
targetKey = time.toString(),
iterator = this.storage.newIterator({}),
- itemCount = maxItems || 1000,
+ itemCount = 0,
startTick,
lastTick,
- items = {};
+ lastType,
+ items = {},
+ currentItem;
// seek to the first item after the specified tick count
iterator.seek(new Buffer(targetKey));
// while we have items, and we haven't returned more than max items
- while (iterator.valid() && itemCount--) {
+ while (iterator.valid() && (itemCount < opts.maxItems)) {
var keyParts = (iterator.key() || '').toString().split('_'),
tick = keyParts[0],
type = keyParts[1] || 'misc',
- rawValue = (iterator.value() || '').toString(),
- item;
+ rawValue = (iterator.value() || '').toString();
- if (rawValue) {
- item = JSON.parse(rawValue);
-
- // ensure we have a start value
- startTick = startTick || tick;
+ // if this item is different to the last, then create a new current item
+ if (lastTick !== tick || lastType !== type) {
+ if (currentItem) {
+ // check that we have a types array for the current type
+ if (! items[lastType]) {
+ items[lastType] = [];
+ } // if
- // check that we have a types array for the current type
- if (! items[type]) {
- items[type] = [];
+ // if we have a current item, then add it to the list
+ items[lastType].push(currentItem);
+ itemCount++;
} // if
- // calculate the item tick diff
- item.delta = tick - startTick;
+ // create a new item
+ currentItem = {};
+ } // if
+
+ // if we hae a raw value, and the item is either a data value (key will be tick_type)
+ // or we are collecting detail values as well, then process this item
+ if (rawValue && (keyParts.length < 3 || opts.includeDetail)) {
+ // update the appropriate item data section
+ currentItem[keyParts.length < 3 ? 'data' : 'details'] = JSON.parse(rawValue);
- // add the item to the items array
- items[type].push(item);
+ // ensure we have a start value
+ startTick = startTick || tick;
- // update the last tick
- lastTick = tick;
+ // calculate the item tick diff
+ currentItem.delta = tick - startTick;
} // if
+ // update the last tick
+ lastTick = tick;
+ lastType = type;
+
// get the next value
iterator.next();
} // while
-
+
// trigger the callback
if (callback) {
callback(error, {
@@ -68,11 +87,21 @@ SampleDB.prototype.getSince = function(time, callback, maxItems) {
SampleDB.prototype.save = function(type, data, details) {
var tick = new Date().getTime(),
- entry = JSON.stringify({
- data: data,
- details: details
- }),
entryKey = tick + '_' + type;
- this.storage.put({}, new Buffer(entryKey), new Buffer(entry));
+ // first save the base data
+ this.storage.put(
+ {},
+ new Buffer(entryKey),
+ new Buffer(JSON.stringify(data))
+ );
+
+ // next add the details
+ if (details) {
+ this.storage.put(
+ {},
+ new Buffer(entryKey + '_' + details),
+ new Buffer(JSON.stringify(details))
+ );
+ } // if
};
View
@@ -10,7 +10,10 @@ vows.describe('Collection to DB').addBatch({
callback = this.callback;
setTimeout(function() {
- collectionProcess.db.getSince(startTick, callback);
+ collectionProcess.db.getSince(startTick, callback, {
+ maxItems: 1000,
+ includeDetail: false
+ });
collectionProcess.stop();
}, 2000);
},
@@ -20,15 +23,15 @@ vows.describe('Collection to DB').addBatch({
},
'samples have been collected': function(err, results) {
- assert.ok(results.items.cpu && results.items.cpu.length > 0);
+ assert.ok(results.items.cpu && results.items.cpu.length > 5);
},
'start tick in results is sensible': function(err, results) {
assert.ok(Math.abs(results.start - startTick) < 500);
},
'end tick in results is sensible': function(err, results) {
- assert.ok(Math.abs(results.end - (startTick + 2000)) < 500)
+ assert.ok(Math.abs(results.end - (startTick + 2000)) < 500);
}
}
}).run();

0 comments on commit 7e9ce20

Please sign in to comment.