Skip to content

Commit

Permalink
Release v0.5.0b3
Browse files Browse the repository at this point in the history
  • Loading branch information
pgte committed Feb 1, 2011
1 parent e524c61 commit dd78e2a
Show file tree
Hide file tree
Showing 9 changed files with 400 additions and 141 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -16,7 +16,7 @@ test: mkdirtmp
node tools/test.js test_collection test_collection_filter test_key_map test_key_map_reload test_key_map_each_with_pos test_indexed_key_map test_indexed_key_map_reload \ node tools/test.js test_collection test_collection_filter test_key_map test_key_map_reload test_key_map_each_with_pos test_indexed_key_map test_indexed_key_map_reload \
test_cached_key_map test_unordered_index test_unbuffered_collection test_flush_on_exit test_interleaved test_atomic test_compact test_nulls_on_keymap \ test_cached_key_map test_unordered_index test_unbuffered_collection test_flush_on_exit test_interleaved test_atomic test_compact test_nulls_on_keymap \
test_nulls_on_unordered_indexes test_nulls_on_ordered_indexes test_false_on_indexes test_ranges_on_ordered_indexes test_meta test_meta_indexes \ test_nulls_on_unordered_indexes test_nulls_on_ordered_indexes test_false_on_indexes test_ranges_on_ordered_indexes test_meta test_meta_indexes \
test_file_close test_stream test_exit \ test_file_close test_stream test_exit test_end_flush \
operators/test_gte operators/test_gt operators/test_lte operators/test_lt operators/test_eq operators/test_composed operators/test_optimization operators/test_in \ operators/test_gte operators/test_gt operators/test_lte operators/test_lt operators/test_eq operators/test_composed operators/test_optimization operators/test_in \
operators/test_nin operators/test_neq operators/test_or operators/test_global_or operators/test_order operators/test_desc_order operators/test_chainable \ operators/test_nin operators/test_neq operators/test_or operators/test_global_or operators/test_order operators/test_desc_order operators/test_chainable \
operators/test_find_stream operators/test_find_stream_chained \ operators/test_find_stream operators/test_find_stream_chained \
Expand Down
6 changes: 6 additions & 0 deletions docs/web/app.js
Expand Up @@ -51,6 +51,12 @@ app.get('/api/replication', function(req, res) {
app.get('/examples', function(req, res) { app.get('/examples', function(req, res) {
res.render('examples/index'); res.render('examples/index');
}); });
app.get('/examples/odm', function(req, res) {
res.render('examples/odm');
});
app.get('/examples/raw', function(req, res) {
res.render('examples/raw');
});


app.get('/features', function(req, res) { app.get('/features', function(req, res) {
res.render('features/index'); res.render('features/index');
Expand Down
3 changes: 2 additions & 1 deletion docs/web/views/api/key_map.jade
Expand Up @@ -114,11 +114,12 @@ p.note Everything here is experimental, and the API is no exception, so expect t
a(id='traversing') a(id='traversing')
h1 Traversing h1 Traversing


h2 key_map.scan (callback) h2 key_map.scan (callback [, nullOnEnd])
p Cycles through all records. p Cycles through all records.
p p
ul ul
li <b>callback</b> (err, key, value): Called when there is an error or a record is found. li <b>callback</b> (err, key, value): Called when there is an error or a record is found.
li <b>nullOnEnd</b>: Pass through if you want callback to be called when the last record is fetched. In this case, the first (err) and second (key) arguments on callback will be null.


a(id='filtering') a(id='filtering')
h1 Filtering h1 Filtering
Expand Down
143 changes: 5 additions & 138 deletions docs/web/views/examples/index.jade
@@ -1,142 +1,9 @@
#examples #examples
h1 Examples h1 Examples


h2 Create a Key Map
p p
pre ul
code li
| var alfred = require('alfred'); a(href="/examples/odm") Examples using the Object Data Model (ODM)
| alfred.open('path/to/db/dir', function(err, db) { li
| if (err) { throw err; } a(href="/examples/raw") Raw access
| db.ensure('users', function(err, users_key_map) {
| console.log('users key map attached');
| });
| });

h2 Populate
p Populate a Key Map with some values
pre
code
| var USERS = {
| 1: {name: 'Pedro', age: 35, sex: 'm'}
| , 2: {name: 'John', age: 32, sex: 'm'}
| , 3: {name: 'Bruno', age: 28, sex: 'm'}
| , 4: {name: 'Sandra', age: 35, sex: 'f'}
| , 5: {name: 'Patricia', age: 42, sex: 'f'}
| , 6: {name: 'Joana', age: 29, sex: 'f'}
| , 7: {name: 'Susana', age: 30, sex: 'f'}
| };
pre
code
| alfred.open('path/to/db/dir', function(err, db) {
| if (err) { throw err; }
| for (id in USERS) {
| if (USERS.hasOwnProperty(id)) {
| db.users.put(id, USERS[id], function(err) {
| if (err) { throw err; }
| console.log('user with id ' + id + ' inserted.');
| });
| }
| }
| });

h2 Fetch
p Fetch a value from a key map based on key.
p Assuming we have db open and db.users attached, let's find user with key 4
p
pre
code
| db.users.get(4, function(err, user) {
| if (err) { throw err; }
| console.log(user.name); // -> 'Sandra'
| });

h2 Create index
p Assuming we have db open and db.users attached, let's add the 'age' and 'sex' indexes':
p
pre
code
| // add 'age' index to users
| db.users.addIndex('age', function(user) {
| return user.age;
| }, function(err) {
| if (err) { throw err; }
| console.log('age index added to users');
| });

p You can add a 'sex' index in a similar way.

h2 Find
p Assuming we have db open and db.users attached, and that db.users has indexes 'age' and 'sex':
p
pre
code
| // find the users with age = 35
| db.users.find({age: {$eq: 35}}) (function(err, key, user) {
| if (err) { throw err; }
| console.log(user);
| });

p
pre
code
| // find the feminine users with age > 28 and <= 35
| db.users.find({age: {$gt: 28, $lte: 35}, sex: 'f'}) (function(err, key, user) {
| if (err) { throw err; }
| console.log(user);
| });

p
pre
code
| // find the with age > 28 and <= 35 OR that are female
| db.users.find({age: {$gt: 28, $lte: 35}}).or({sex: {$eq: 'f'}}) (function(err, key, user) {
| if (err) { throw err; }
| console.log(user);
| });

h2 Bulk find
p Do the same thing, but get all the results on the same callback.
p Assuming we have db open and db.users attached, and that db.users has indexes 'age' and 'sex':
p
pre
code
| // find the with age > 28 and <= 35 OR that are female
| db.users.find({age: {$gt: 28, $lte: 35}}).or({sex: {$eq: 'f'}}).all(function(err, users) {
| if (err) { throw err; }
| users.forEach(function(user) {
| console.log(user);
| });
| });

h2 Find stream
p Do the same thing, but get all the results as a stream.
p Assuming we have db open and db.users attached, and that db.users has indexes 'age' and 'sex':
p
pre
code
| // find the with age > 28 and <= 35 OR that are female
| var stream = db.users.find({age: {$gt: 28, $lte: 35}}).or({sex: {$eq: 'f'}}).stream();
| stream.on('record', function(user) {
| console.log(user);
| });
| stream.on('error' function(err) {
| throw err;
| });
| stream.on('end', function() {
| console.log('ended');
| };

h2 Activity stream
p Stream filtered <i>puts</i> on a key map. And let it run...
p Assuming we have db open and db.users attached:
p
pre
code
| var activity_stream = db.users.startStream(function(key, value) {
| return value.age <= 30;
| }, function(key, user) {
| console.log('here is a young user: ' + require('util').inspect(user));
| });
| // eventually, stop the stream
| db.users.stopStream(activity_stream);
53 changes: 53 additions & 0 deletions docs/web/views/examples/odm.jade
@@ -0,0 +1,53 @@
#examples
h1 Examples (raw accesss)

h2 Define a User model
p
pre
code
| var User = db.define('User');
| User.property('name');
| User.property('address');
| User.property('age', Number);
| User.property('married', 'boolean');
| User.property('email', 'string');
| User.index('age', function(user) {
| return user.age;
| });

h2 Create a new User document
p
pre
code
| var user = User.new(USER);
| user.save(function(validationErrors) {
| assert.ok(errors === null, 'errors is not null');
| console.log('User saved');
| });

h2 Find User document by ID
p
pre
code
| var id = 'abc';
| User.get(id, function(user) {
| console.log('found user: ' + user.inspect());
| });

h2 Find
p
pre
code
| User.find({age: {$lte: 30}}) (function(gotUser) {
| assert.ok(user.equal(gotUser), 'user ('+user.toString()+') and gotten user ('+gotUser.toString()+') are different');
| done();
| }).all(function(users) {
| assert.equal(1, users.length);
| assert.ok(user.equal(users[0]), 'user ('+user.toString()+') and gotten user ('+users[0].toString()+') are different');
| done();
| }).where({age: {$gt: 30}}).all(function(users) {
| assert.equal(0, users.length);
| done();
| });
| });

142 changes: 142 additions & 0 deletions docs/web/views/examples/raw.jade
@@ -0,0 +1,142 @@
#examples
h1 Examples (raw accesss)

h2 Create a Key Map
p
pre
code
| var alfred = require('alfred');
| alfred.open('path/to/db/dir', function(err, db) {
| if (err) { throw err; }
| db.ensure('users', function(err, users_key_map) {
| console.log('users key map attached');
| });
| });

h2 Populate
p Populate a Key Map with some values
pre
code
| var USERS = {
| 1: {name: 'Pedro', age: 35, sex: 'm'}
| , 2: {name: 'John', age: 32, sex: 'm'}
| , 3: {name: 'Bruno', age: 28, sex: 'm'}
| , 4: {name: 'Sandra', age: 35, sex: 'f'}
| , 5: {name: 'Patricia', age: 42, sex: 'f'}
| , 6: {name: 'Joana', age: 29, sex: 'f'}
| , 7: {name: 'Susana', age: 30, sex: 'f'}
| };
pre
code
| alfred.open('path/to/db/dir', function(err, db) {
| if (err) { throw err; }
| for (id in USERS) {
| if (USERS.hasOwnProperty(id)) {
| db.users.put(id, USERS[id], function(err) {
| if (err) { throw err; }
| console.log('user with id ' + id + ' inserted.');
| });
| }
| }
| });

h2 Fetch
p Fetch a value from a key map based on key.
p Assuming we have db open and db.users attached, let's find user with key 4
p
pre
code
| db.users.get(4, function(err, user) {
| if (err) { throw err; }
| console.log(user.name); // -> 'Sandra'
| });

h2 Create index
p Assuming we have db open and db.users attached, let's add the 'age' and 'sex' indexes':
p
pre
code
| // add 'age' index to users
| db.users.addIndex('age', function(user) {
| return user.age;
| }, function(err) {
| if (err) { throw err; }
| console.log('age index added to users');
| });

p You can add a 'sex' index in a similar way.

h2 Find
p Assuming we have db open and db.users attached, and that db.users has indexes 'age' and 'sex':
p
pre
code
| // find the users with age = 35
| db.users.find({age: {$eq: 35}}) (function(err, key, user) {
| if (err) { throw err; }
| console.log(user);
| });

p
pre
code
| // find the feminine users with age > 28 and <= 35
| db.users.find({age: {$gt: 28, $lte: 35}, sex: 'f'}) (function(err, key, user) {
| if (err) { throw err; }
| console.log(user);
| });

p
pre
code
| // find the with age > 28 and <= 35 OR that are female
| db.users.find({age: {$gt: 28, $lte: 35}}).or({sex: {$eq: 'f'}}) (function(err, key, user) {
| if (err) { throw err; }
| console.log(user);
| });

h2 Bulk find
p Do the same thing, but get all the results on the same callback.
p Assuming we have db open and db.users attached, and that db.users has indexes 'age' and 'sex':
p
pre
code
| // find the with age > 28 and <= 35 OR that are female
| db.users.find({age: {$gt: 28, $lte: 35}}).or({sex: {$eq: 'f'}}).all(function(err, users) {
| if (err) { throw err; }
| users.forEach(function(user) {
| console.log(user);
| });
| });

h2 Find stream
p Do the same thing, but get all the results as a stream.
p Assuming we have db open and db.users attached, and that db.users has indexes 'age' and 'sex':
p
pre
code
| // find the with age > 28 and <= 35 OR that are female
| var stream = db.users.find({age: {$gt: 28, $lte: 35}}).or({sex: {$eq: 'f'}}).stream();
| stream.on('record', function(user) {
| console.log(user);
| });
| stream.on('error' function(err) {
| throw err;
| });
| stream.on('end', function() {
| console.log('ended');
| };

h2 Activity stream
p Stream filtered <i>puts</i> on a key map. And let it run...
p Assuming we have db open and db.users attached:
p
pre
code
| var activity_stream = db.users.startStream(function(key, value) {
| return value.age <= 30;
| }, function(key, user) {
| console.log('here is a young user: ' + require('util').inspect(user));
| });
| // eventually, stop the stream
| db.users.stopStream(activity_stream);
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{ "name" : "alfred" { "name" : "alfred"
, "description" : "In-process key-value store" , "description" : "In-process key-value store"
, "version" : "0.5.0b2" , "version" : "0.5.0b3"
, "homepage" : "http://pgte.github.com/alfred" , "homepage" : "http://pgte.github.com/alfred"
, "author" : "Pedro Teixeira <pedro.teixeira@gmail.com> (http://metaduck.com)" , "author" : "Pedro Teixeira <pedro.teixeira@gmail.com> (http://metaduck.com)"
, "contributors" : , "contributors" :
Expand Down

0 comments on commit dd78e2a

Please sign in to comment.