Skip to content

Instantly share code, notes, and snippets.

@gflarity
Created March 29, 2011 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gflarity/891587 to your computer and use it in GitHub Desktop.
Save gflarity/891587 to your computer and use it in GitHub Desktop.
node-index issues
// You can run this file again and again and it will:
// 1) Keep inserting the same keys over and over again
// 2) You can't no longer trust the variables outside of the callbacks passed to index.set, context is needed
var generate_uuid = require('node-uuid');
var index_file = 'data/test.index';
var index;
console.log('run it twice, it should fail the second time as it keeps adding keys as if they were new');
var on_index_created = function(err) {
if ( err ) {
console.log(err);
process.exit(1);
}
var key = '6D22000D-5D4E-4EC3-9685-2323EF25B487';
var value = 'foo';
index.set( key, value, function(err) {
if ( err ) {
console.log( err );
process.exit(0);
}
if ( key == 'E18EFDFD-ACC4-42CB-8261-E8139886DA1C') {
console.log('I see key ' + key + ' but I\'m really ' + '6D22000D-5D4E-4EC3-9685-2323EF25B487!');
}
});
var key = 'E18EFDFD-ACC4-42CB-8261-E8139886DA1C';
var value = 'foo';
index.set( key, value, function(err) {
if ( err ) {
console.log( err );
process.exit(0);
}
} );
};
string_sort = function(a, b) {
return (a === null || a < b) ? -1 : a == b ? 0 : -1;
};
index = require('index').createIndex({
order: 32, // Maximum number of items in page
// Tree's height depends on that
storage: require('index').storage.file // Place where all tree data will be stored
.createStorage({filename:index_file}, on_index_created), // (see more description below)
sort: string_sort
// Function that will be used to compare keys
// Note that null is a system value and sort should always return negative
// result if first argument is null
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment