Skip to content
This repository
Browse code

collection is all asynchronous, no event emitters and key_map

  • Loading branch information...
commit 30d78f6b9a7f12515963785e9c17a0557a9df98e 1 parent 0ab1dc5
Pedro Teixeira authored
18 LICENSE
... ... @@ -0,0 +1,18 @@
  1 +Copyright 2010 Pedro Teixeira. All rights reserved.
  2 +Permission is hereby granted, free of charge, to any person obtaining a copy
  3 +of this software and associated documentation files (the "Software"), to
  4 +deal in the Software without restriction, including without limitation the
  5 +rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  6 +sell copies of the Software, and to permit persons to whom the Software is
  7 +furnished to do so, subject to the following conditions:
  8 +
  9 +The above copyright notice and this permission notice shall be included in
  10 +all copies or substantial portions of the Software.
  11 +
  12 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14 +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15 +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16 +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  17 +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  18 +IN THE SOFTWARE.
102 benchmarks/benchmark_collection.js
@@ -3,54 +3,68 @@ module.exports.run = function(benchmark, next) {
3 3 var OBJECT_COUNT= 100000;
4 4
5 5 benchmark.start('opening collection');
6   - var collection = require(__dirname + '/../lib/alfred/collection.js').open(__dirname + '/../tmp/collection_bench.alf');
7   - benchmark.end();
8   - var assert = require('assert');
  6 + require(__dirname + '/../lib/alfred/collection.js').open(__dirname + '/../tmp/collection_bench.alf', function(err, collection) {
  7 + if (err) {
  8 + throw err;
  9 + }
  10 +
  11 + benchmark.end();
  12 + var assert = require('assert');
9 13
10   - var createRandomString = function(string_length) {
11   - var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
12   - var randomstring = '';
13   - for (var i=0; i<string_length; i++) {
14   - var rnum = Math.floor(Math.random() * chars.length);
15   - randomstring += chars.substring(rnum,rnum+1);
16   - }
17   - return randomstring;
18   - };
  14 + var createRandomString = function(string_length) {
  15 + var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
  16 + var randomstring = '';
  17 + for (var i=0; i<string_length; i++) {
  18 + var rnum = Math.floor(Math.random() * chars.length);
  19 + randomstring += chars.substring(rnum,rnum+1);
  20 + }
  21 + return randomstring;
  22 + };
19 23
20   - var createRandomObject = function() {
21   - return {
22   - a: createRandomString(10),
23   - b: createRandomString(50),
24   - c: createRandomString(50)
  24 + var createRandomObject = function() {
  25 + return {
  26 + a: createRandomString(10),
  27 + b: createRandomString(50),
  28 + c: createRandomString(50)
  29 + };
25 30 };
26   - };
27 31
28   - benchmark.start('prunning collection');
29   - collection.prune(function() {
30   - benchmark.end();
31   - benchmark.start('populating collection', OBJECT_COUNT);
32   - for (var i = 0; i < OBJECT_COUNT; i ++) {
33   - record = createRandomObject();
34   - collection.write(record);
35   - }
36   - benchmark.end();
37   -
38   - // wait for flush
39   - setTimeout(function() {
40   - var index = 0;
41   - benchmark.start('reading collection', OBJECT_COUNT);
42   - collection.read(function(error, record) {
43   - assert.equal(error, null);
44   - if (record === null) {
45   - benchmark.end();
46   - collection.end();
47   - next();
48   - } else {
49   - index ++;
50   - }
51   - });
52   - }, 2000);
53   -
  32 + benchmark.start('prunning collection');
  33 + collection.clear(function(err) {
  34 + if (err) {
  35 + throw err;
  36 + }
  37 + benchmark.end();
  38 + benchmark.start('populating collection', OBJECT_COUNT);
  39 + for (var i = 0; i < OBJECT_COUNT; i ++) {
  40 + record = createRandomObject();
  41 + collection.write(record, function(err) {
  42 + if (err) {
  43 + throw err;
  44 + }
  45 + });
  46 + }
  47 + benchmark.end();
  48 +
  49 + // wait for flush
  50 + setTimeout(function() {
  51 + var index = 0;
  52 + benchmark.start('reading collection', OBJECT_COUNT);
  53 + collection.read(function(error, record) {
  54 + assert.equal(error, null);
  55 + if (record === null) {
  56 + benchmark.end();
  57 + collection.end();
  58 + next();
  59 + } else {
  60 + index ++;
  61 + }
  62 + });
  63 + }, 2000);
  64 +
  65 + });
  66 +
  67 +
54 68 });
55 69
56 70 }
109 benchmarks/benchmark_collection_filter.js
... ... @@ -1,60 +1,69 @@
1 1 module.exports.run = function(benchmark, next) {
2   -
3   - var collection = require(__dirname + '/../lib/alfred/collection.js').open(__dirname + '/../tmp/collection_filter_benchmark.alf');
  2 +
4 3 var assert = require('assert');
5 4
6   - var OBJECT_COUNT = 100000;
7   -
8   - collection.on('error', function(error) {
9   - throw error;
10   - });
  5 + require(__dirname + '/../lib/alfred/collection.js').open(__dirname + '/../tmp/collection_filter_benchmark.alf', function(err, collection) {
  6 + if (err) {
  7 + throw err;
  8 + }
  9 + var OBJECT_COUNT = 100000;
11 10
12   - var createRandomString = function(string_length) {
13   - var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
14   - var randomstring = '';
15   - for (var i=0; i<string_length; i++) {
16   - var rnum = Math.floor(Math.random() * chars.length);
17   - randomstring += chars.substring(rnum,rnum+1);
18   - }
19   - return randomstring;
20   - };
21   -
22   - var createRandomObject = function(c_value) {
23   - return {
24   - a: createRandomString(10),
25   - b: createRandomString(50),
26   - c: c_value
  11 + var createRandomString = function(string_length) {
  12 + var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
  13 + var randomstring = '';
  14 + for (var i=0; i<string_length; i++) {
  15 + var rnum = Math.floor(Math.random() * chars.length);
  16 + randomstring += chars.substring(rnum,rnum+1);
  17 + }
  18 + return randomstring;
27 19 };
28   - };
29 20
30   - var compareObjects = function(a, b) {
31   - return a.a === b.a && a.b === b.b && a.c === b.c;
32   - };
  21 + var createRandomObject = function(c_value) {
  22 + return {
  23 + a: createRandomString(10),
  24 + b: createRandomString(50),
  25 + c: c_value
  26 + };
  27 + };
33 28
34   - var c_values = [createRandomString(50), createRandomString(50), createRandomString(50)]
35   -
36   - collection.prune(function() {
37   - for (var i = 0; i < OBJECT_COUNT; i ++) {
38   - record = createRandomObject(c_values[i % c_values.length]);
39   - collection.write(record);
40   - }
  29 + var compareObjects = function(a, b) {
  30 + return a.a === b.a && a.b === b.b && a.c === b.c;
  31 + };
  32 +
  33 + var c_values = [createRandomString(50), createRandomString(50), createRandomString(50)]
  34 +
  35 + collection.clear(function(err) {
  36 + if (err) {
  37 + throw err;
  38 + }
  39 + for (var i = 0; i < OBJECT_COUNT; i ++) {
  40 + record = createRandomObject(c_values[i % c_values.length]);
  41 + collection.write(record, function(err) {
  42 + if (err)
  43 + throw err;
  44 + });
  45 + }
  46 +
  47 + // wait for flush
  48 + setTimeout(function() {
  49 + var index = 0;
  50 + benchmark.start('Collection filter', OBJECT_COUNT);
  51 + collection.filter(function(record) {
  52 + return record.c == c_values[1];
  53 + }, function(error, result) {
  54 + if (error) {
  55 + throw error;
  56 + }
  57 + if (result === null) {
  58 + benchmark.end();
  59 + next();
  60 + }
  61 + });
  62 + }, 2000);
  63 +
  64 + });
41 65
42   - // wait for flush
43   - setTimeout(function() {
44   - var index = 0;
45   - benchmark.start('Collection filter', OBJECT_COUNT);
46   - collection.filter(function(record) {
47   - return record.c == c_values[1];
48   - }, function(error, result) {
49   - if (error) {
50   - throw error;
51   - }
52   - benchmark.end();
53   - next();
54   - });
55   - }, 2000);
56   -
57 66 });
  67 +
58 68
59   -}
60   -
  69 +}
87 benchmarks/benchmark_key_map.js
... ... @@ -0,0 +1,87 @@
  1 +module.exports.run = function(benchmark, next) {
  2 +
  3 + var assert = require('assert');
  4 +
  5 + require(__dirname + '/../lib/alfred/key_map.js').open(__dirname + '/../tmp/key_map_benchmark.alf', function(err, key_map) {
  6 + if (err) {
  7 + throw err;
  8 + }
  9 +
  10 + var KEY_COUNT = 50000;
  11 + var RETRIEVE_RANDOM_COUNT = 10;
  12 +
  13 + var createRandomString = function(string_length) {
  14 + var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
  15 + var randomstring = '';
  16 + for (var i=0; i<string_length; i++) {
  17 + var rnum = Math.floor(Math.random() * chars.length);
  18 + randomstring += chars.substring(rnum,rnum+1);
  19 + }
  20 + return randomstring;
  21 + };
  22 +
  23 + var createRandomObject = function() {
  24 + return {
  25 + a: createRandomString(10),
  26 + b: createRandomString(50),
  27 + c: createRandomString(50)
  28 + };
  29 + };
  30 +
  31 + var compareObjects = function(a, b) {
  32 + return a.a === b.a && a.b === b.b && a.c === b.c;
  33 + };
  34 +
  35 + var keys = [];
  36 + var map = {};
  37 +
  38 + key_map.clear(function(err) {
  39 + if (err) {
  40 + throw err;
  41 + }
  42 + benchmark.start('Insert object into key map', KEY_COUNT);
  43 + for (var i = 0; i < KEY_COUNT; i ++) {
  44 + var value = createRandomObject();
  45 + var key = createRandomString(16);
  46 + keys.push(key);
  47 + map[key] = value;
  48 + key_map.put(key, value, function(err) {
  49 + if (err) {
  50 + throw err;
  51 + }
  52 + });
  53 + }
  54 + benchmark.end();
  55 +
  56 + // wait for flush
  57 + setTimeout(function() {
  58 +
  59 + var sample_key_index = Math.floor(Math.random() * KEY_COUNT)
  60 + var key = keys[sample_key_index];
  61 + var retrieved_keys = 0;
  62 +
  63 + benchmark.start('Retrieve a random object by key from a ' + KEY_COUNT + ' hash map', RETRIEVE_RANDOM_COUNT);
  64 + for(var i = 0; i < RETRIEVE_RANDOM_COUNT; i++) {
  65 + key_map.get(key, function(err, record) {
  66 + if (err) {
  67 + throw err;
  68 + }
  69 + retrieved_keys ++;
  70 + if (retrieved_keys == RETRIEVE_RANDOM_COUNT) {
  71 + benchmark.end();
  72 + next();
  73 + }
  74 + });
  75 + }
  76 +
  77 + // test than when we try to retrieve with a non-existing ket, we get null back
  78 +
  79 + }, 5000);
  80 +
  81 + });
  82 +
  83 + });
  84 +
  85 +
  86 +}
  87 +
110 lib/alfred/collection.js
... ... @@ -1,48 +1,81 @@
1   -var fs = require('fs');
2   -var carrier = require('carrier');
  1 +var fs = require('fs'),
  2 + carrier = require('carrier'),
  3 + assert = require('assert'),
  4 + path = require('path');
3 5
4   -var open = function(file_path) {
5   - return fs.createWriteStream(file_path, {
6   - flags: 'a',
7   - encoding: 'utf-8'
8   - });
9   -}
10   -
11   -var Collection = function(file_path) {
  6 +var Collection = function(file_path, callback) {
12 7 var self = this;
13   - this.file_path = file_path;
14   - this.ws = open(file_path);
15   - this.ws.on('error', function(error) {
16   - self.emit('error', error);
  8 + file_path = path.normalize(file_path);
  9 + fs.stat(file_path, function(err, stats) {
  10 + if (err) {
  11 + callback(err);
  12 + }
  13 + self.file_path = file_path;
  14 + self.write_pos = stats.size;
  15 + callback();
17 16 });
18 17 };
19 18
20   -(require('sys') || require('util')).inherits(Collection, require('events').EventEmitter);
21   -
22   -module.exports.open = function(file_path) {
23   - return new Collection(file_path);
  19 +module.exports.open = function(file_path, callback) {
  20 + var coll = new Collection(file_path, function(err) {
  21 + if (err) {
  22 + callback(err);
  23 + } else {
  24 + coll.openFile(function(err) {
  25 + callback(err, coll);
  26 + });
  27 + }
  28 + });
24 29 }
25 30
26   -Collection.prototype.reopen = function(callback) {
27   - this.ws = open(this.file_path);
28   - if (callback) {
29   - callback();
30   - }
  31 +Collection.prototype.openFile = function(callback) {
  32 + var self = this;
  33 + fs.open(self.file_path, 'a+', 0666, function(err, fd) {
  34 + if (err) {
  35 + callback(err);
  36 + } else {
  37 + self.fd = fd;
  38 + callback(null);
  39 + }
  40 + });
31 41 };
32 42
33   -Collection.prototype.write = function(record) {
34   - this.ws.write(JSON.stringify(record) + "\n");
  43 +Collection.prototype.write = function(record, callback) {
  44 + var str = JSON.stringify(record) + "\n";
  45 + var length = Buffer.byteLength(str);
  46 + var buffer = new Buffer(str);
  47 + fs.write(this.fd, buffer, 0, length, this.write_pos, function(err, written) {
  48 + if (err) {
  49 + callback(err);
  50 + } else {
  51 + assert.equal(length, written, 'Written length is not the same as predicted length');
  52 + callback(null);
  53 + }
  54 + });
  55 + this.write_pos += length;
35 56 };
36 57
37   -Collection.prototype.end = function() {
38   - this.ws.end();
  58 +Collection.prototype.end = function(callback) {
  59 + fs.close(this.fd, callback);
39 60 };
40 61
41   -Collection.prototype.prune = function(callback) {
  62 +Collection.prototype.clear = function(callback) {
42 63 var self = this;
43   - self.end();
44   - fs.unlink(self.file_path, function() {
45   - self.reopen(callback);
  64 + self.end(function(err) {
  65 + if (err) {
  66 + callback(err);
  67 + } else {
  68 + fs.unlink(self.file_path, function(err) {
  69 + if (err) {
  70 + callback(err);
  71 + } else {
  72 + self.openFile(function(err) {
  73 + self.write_pos = 0;
  74 + callback(err);
  75 + });
  76 + }
  77 + });
  78 + }
46 79 });
47 80 };
48 81
@@ -52,7 +85,11 @@ Collection.prototype.read = function(record_handler) {
52 85 record_handler(error);
53 86 });
54 87 rs.on('end', function() {
55   - rs = null;
  88 + try {
  89 +
  90 + } catch (excp) {
  91 + rs.destroy();
  92 + }
56 93 record_handler(null, null);
57 94 });
58 95 carrier.carry(rs, function(line) {
@@ -62,18 +99,17 @@ Collection.prototype.read = function(record_handler) {
62 99 };
63 100
64 101 Collection.prototype.filter = function(filter_function, callback) {
65   - var records = [];
66 102 this.read(function(error, record) {
67 103 if (error) {
68 104 callback(error);
69 105 } else {
70   - if (record === null) { // reached the end
71   - callback(null, records);
  106 + if (record === null) { // reached the end
  107 + callback(null, null);
72 108 } else {
73 109 if (filter_function(record)) {
74   - records.push(record);
  110 + callback(null, record);
75 111 }
76 112 }
77 113 }
78 114 });
79   -}
  115 +};
48 lib/alfred/key_map.js
... ... @@ -0,0 +1,48 @@
  1 +var collection = require('./collection');
  2 +
  3 +var KeyMap = function() {
  4 +};
  5 +
  6 +(require('sys') || require('util')).inherits(KeyMap, require('events').EventEmitter);
  7 +
  8 +module.exports.open = function(file_path, callback) {
  9 + var key_map = new KeyMap();
  10 + collection.open(file_path, function(err, coll) {
  11 + if (err) {
  12 + callback(err);
  13 + } else {
  14 + key_map.collection = coll;
  15 + callback(null, key_map);
  16 + }
  17 + });
  18 +};
  19 +
  20 +KeyMap.prototype.put = function(key, value, callback) {
  21 + this.collection.write({
  22 + key: key,
  23 + value: value,
  24 + created: (new Date()).getTime()
  25 + },
  26 + callback);
  27 +};
  28 +
  29 +KeyMap.prototype.get = function(key, callback) {
  30 + var value = null;
  31 + this.collection.filter(function(record) {
  32 + return record.key === key;
  33 + }, function(error, record) {
  34 + if (error) {
  35 + callback(error);
  36 + return;
  37 + }
  38 + if (record === null) {
  39 + callback(null, value);
  40 + } else {
  41 + value = record.value;
  42 + }
  43 + });
  44 +};
  45 +
  46 +KeyMap.prototype.clear = function(callback) {
  47 + this.collection.clear(callback);
  48 +};
28 package.json
... ... @@ -0,0 +1,28 @@
  1 +{ "name" : "alfred"
  2 +, "description" : "Top Secret Stuff"
  3 +, "version" : "0.0.1"
  4 +, "homepage" : "http://github.com/pgte/alfred"
  5 +, "author" : "Pedro Teixeira <pedro.teixeira@gmail.com> (http://www.metaduck.com)"
  6 +, "contributors" :
  7 + [ "Pedro Teixeira <pedro.teixeira@gmail.com>"
  8 + ]
  9 +, "repository" :
  10 + { "type" : "git"
  11 + , "url" : "http://github.com/pgte/alfred.git"
  12 + }
  13 +, "bugs" :
  14 + { "mail" : "pedro.teixeira@gmail.com"
  15 + , "web" : "http://github.com/pgte/alfred/issues"
  16 + }
  17 +, "directories" : { "lib" : "./lib" }
  18 +, "engines" : { "node" : ">=0.2.5" }
  19 +, "scripts" : {}
  20 +, "dependencies" :
  21 + { "carrier" : ">=0.0.4"
  22 + }
  23 +, "licenses" :
  24 + [ { "type" : "MIT"
  25 + , "url" : "http://github.com/pgte/alfred/raw/master/LICENSE"
  26 + }
  27 + ]
  28 +}
105 test/test_collection.js
... ... @@ -1,61 +1,66 @@
1 1 module.exports.run = function(next) {
2 2
3   - var collection = require(__dirname + '/../lib/alfred/collection.js').open(__dirname + '/../tmp/collection_test.alf');
4 3 var assert = require('assert');
5   -
6   - collection.on('error', function(error) {
7   - throw error;
8   - });
9   -
10   - var createRandomString = function(string_length) {
11   - var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
12   - var randomstring = '';
13   - for (var i=0; i<string_length; i++) {
14   - var rnum = Math.floor(Math.random() * chars.length);
15   - randomstring += chars.substring(rnum,rnum+1);
16   - }
17   - return randomstring;
18   - };
  4 + require(__dirname + '/../lib/alfred/collection.js').open(__dirname + '/../tmp/collection_test.alf', function(err, collection) {
  5 + if (err) {
  6 + throw err;
  7 + }
  8 +
  9 + var createRandomString = function(string_length) {
  10 + var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
  11 + var randomstring = '';
  12 + for (var i=0; i<string_length; i++) {
  13 + var rnum = Math.floor(Math.random() * chars.length);
  14 + randomstring += chars.substring(rnum,rnum+1);
  15 + }
  16 + return randomstring;
  17 + };
19 18
20   - var createRandomObject = function() {
21   - return {
22   - a: createRandomString(10),
23   - b: createRandomString(100),
24   - c: createRandomString(100)
  19 + var createRandomObject = function() {
  20 + return {
  21 + a: createRandomString(10),
  22 + b: createRandomString(100),
  23 + c: createRandomString(100)
  24 + };
25 25 };
26   - };
27 26
28   - var compareObjects = function(a, b) {
29   - return a.a === b.a && a.b === b.b && a.c === b.c;
30   - };
  27 + var records = [];
31 28
32   - var records = [];
33   -
34   - collection.prune(function() {
35   - for (var i = 0; i < 1000; i ++) {
36   - record = createRandomObject()
37   - records.push(record);
38   - collection.write(record);
39   - }
40   -
41   - // wait for flush
42   - setTimeout(function() {
43   - var index = 0;
44   - collection.read(function(error, record) {
45   - assert.equal(error, null);
46   - if(record === null) {
47   - // reached the end
48   - assert.equal(records.length, index);
49   - collection.end();
50   - next();
51   - } else {
52   - assert.ok(compareObjects(record, records[index], "Object at index " + index + ' differs.'));
53   - index ++;
54   - }
55   - });
56   - }, 2000);
57   -
  29 + collection.clear(function(err) {
  30 + if (err) {
  31 + throw err;
  32 + }
  33 + for (var i = 0; i < 1000; i ++) {
  34 + var record = createRandomObject();
  35 + records.push(record);
  36 + collection.write(record, function(err) {
  37 + if (err) {
  38 + throw err
  39 + }
  40 + });
  41 + }
  42 +
  43 + // wait for flush
  44 + setTimeout(function() {
  45 + var index = 0;
  46 + collection.read(function(error, record) {
  47 + assert.equal(error, null);
  48 + if(record === null) {
  49 + // reached the end
  50 + assert.equal(records.length, index);
  51 + collection.end();
  52 + next();
  53 + } else {
  54 + assert.deepEqual(record, records[index], "Object at index " + index + ' differs.');
  55 + index ++;
  56 + }
  57 + });
  58 + }, 2000);
  59 +
  60 + });
  61 +
58 62 });
  63 +
59 64
60 65 }
61 66
107 test/test_collection_filter.js
... ... @@ -1,58 +1,73 @@
1 1 module.exports.run = function(next) {
2   -
3   - var collection = require(__dirname + '/../lib/alfred/collection.js').open(__dirname + '/../tmp/collection_filter_test.alf');
4 2 var assert = require('assert');
5 3
6   - var OBJECT_COUNT = 900;
7   -
8   - collection.on('error', function(error) {
9   - throw error;
10   - });
  4 + require(__dirname + '/../lib/alfred/collection.js').open(__dirname + '/../tmp/collection_filter_test.alf', function(err, collection) {
11 5
12   - var createRandomString = function(string_length) {
13   - var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
14   - var randomstring = '';
15   - for (var i=0; i<string_length; i++) {
16   - var rnum = Math.floor(Math.random() * chars.length);
17   - randomstring += chars.substring(rnum,rnum+1);
18   - }
19   - return randomstring;
20   - };
21   -
22   - var createRandomObject = function(c_value) {
23   - return {
24   - a: createRandomString(10),
25   - b: createRandomString(100),
26   - c: c_value
  6 + if (err) {
  7 + throw err;
  8 + }
  9 +
  10 + var OBJECT_COUNT = 900;
  11 +
  12 + var createRandomString = function(string_length) {
  13 + var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
  14 + var randomstring = '';
  15 + for (var i=0; i<string_length; i++) {
  16 + var rnum = Math.floor(Math.random() * chars.length);
  17 + randomstring += chars.substring(rnum,rnum+1);
  18 + }
  19 + return randomstring;
27 20 };
28   - };
29 21
30   - var compareObjects = function(a, b) {
31   - return a.a === b.a && a.b === b.b && a.c === b.c;
32   - };
  22 + var createRandomObject = function(c_value) {
  23 + return {
  24 + a: createRandomString(10),
  25 + b: createRandomString(100),
  26 + c: c_value
  27 + };
  28 + };
33 29
34   - var c_values = [createRandomString(100), createRandomString(100), createRandomString(100)]
35   -
36   - collection.prune(function() {
37   - for (var i = 0; i < OBJECT_COUNT; i ++) {
38   - record = createRandomObject(c_values[i % c_values.length]);
39   - collection.write(record);
40   - }
  30 + var compareObjects = function(a, b) {
  31 + return a.a === b.a && a.b === b.b && a.c === b.c;
  32 + };
  33 +
  34 + var c_values = [createRandomString(100), createRandomString(100), createRandomString(100)]
  35 +
  36 + collection.clear(function(err) {
  37 + if (err) {
  38 + throw err;
  39 + }
  40 + for (var i = 0; i < OBJECT_COUNT; i ++) {
  41 + var record = createRandomObject(c_values[i % c_values.length]);
  42 + collection.write(record, function(err) {
  43 + if (err) {
  44 + throw err;
  45 + }
  46 + });
  47 + }
  48 +
  49 + // wait for flush
  50 + setTimeout(function() {
  51 + var result_count = 0;
  52 + collection.filter(function(record) {
  53 + return record.c == c_values[1];
  54 + }, function(error, record) {
  55 + if (error) {
  56 + throw error;
  57 + }
  58 + if (record === null) {
  59 + assert.equal(result_count, OBJECT_COUNT / 3);
  60 + } else {
  61 + result_count ++;
  62 + }
  63 +
  64 + });
  65 + }, 2000);
  66 +
  67 + });
41 68
42   - // wait for flush
43   - setTimeout(function() {
44   - var index = 0;
45   - collection.filter(function(record) {
46   - return record.c == c_values[1];
47   - }, function(error, result) {
48   - if (error) {
49   - throw error;
50   - }
51   - assert.equal(result.length, OBJECT_COUNT / 3);
52   - });
53   - }, 2000);
54   -
55 69 });
  70 +
56 71
57 72 }
58 73
91 test/test_key_map.js
... ... @@ -0,0 +1,91 @@
  1 +module.exports.run = function(next) {
  2 +
  3 + var assert = require('assert');
  4 +
  5 + require(__dirname + '/../lib/alfred/key_map.js').open(__dirname + '/../tmp/key_map_test.alf', function(err, key_map) {
  6 +
  7 + if (err) {
  8 + throw err;
  9 + }
  10 +
  11 + var createRandomString = function(string_length) {
  12 + var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
  13 + var randomstring = '';
  14 + for (var i=0; i<string_length; i++) {
  15 + var rnum = Math.floor(Math.random() * chars.length);
  16 + randomstring += chars.substring(rnum,rnum+1);
  17 + }
  18 + return randomstring;
  19 + };
  20 +
  21 + var createRandomObject = function() {
  22 + return {
  23 + a: createRandomString(10),
  24 + b: createRandomString(100),
  25 + c: createRandomString(100)
  26 + };
  27 + };
  28 +
  29 + var map = {};
  30 + var key_count = 0;
  31 +
  32 + key_map.clear(function(err) {
  33 + if (err) {
  34 + throw err;
  35 + }
  36 + for (var i = 0; i < 100; i ++) {
  37 + var value = createRandomObject();
  38 + var key = createRandomString(16);
  39 + map[key] = value;
  40 + key_map.put(key, value, function(err) {
  41 + if (err) {
  42 + throw err;
  43 + }
  44 + });
  45 + key_count ++;
  46 + }
  47 +
  48 + // wait for flush
  49 + setTimeout(function() {
  50 +
  51 + // test if we can retrieve all keys
  52 + var tested_keys = 0;
  53 + var tested_null = false;
  54 + for(var key in map) {
  55 + (function(key) {
  56 + key_map.get(key, function(error, result) {
  57 + if (error) {
  58 + throw error;
  59 + }
  60 + assert.deepEqual(map[key], result);
  61 + tested_keys ++;
  62 + });
  63 + })(key);
  64 + }
  65 +
  66 + // test null
  67 + key_map.get(createRandomString(20), function(error, result) {
  68 + if (error) {
  69 + throw error;
  70 + }
  71 + assert.equal(null, result);
  72 + tested_null = true;
  73 + });
  74 +
  75 + setTimeout(function() {
  76 + assert.equal(key_count, tested_keys, 'tested keys is not equal to original key count');
  77 + assert.equal(true, tested_null, 'did not reach the test null');
  78 + next();
  79 + }, 3000);
  80 +
  81 + // test than when we try to retrieve with a non-existing ket, we get null back
  82 + }, 2000);
  83 +
  84 + });
  85 +
  86 +
  87 + });
  88 +
  89 +
  90 +}
  91 +

0 comments on commit 30d78f6

Please sign in to comment.
Something went wrong with that request. Please try again.