level based range index
npm install [--save/--save-dev] range-index
ID | NAME | AGE | SEX |
---|---|---|---|
1 |
Divina Ventimiglia | 7 | F |
2 |
Nakisha Robuck | 54 | F |
3 |
Amira Markus | 18 | F |
4 |
Mohamed Kincannon | 60 | M |
5 |
Juana Ardon | 23 | F |
6 |
Hyon Davie | 60 | F |
7 |
Estell Cromer | 18 | F |
8 |
Jacob Neeley | 13 | M |
9 |
Carlene Weatherman | 42 | F |
10 |
Carie Markland | 7 | F |
AGE | ID |
---|---|
7 | 1 , 10 |
13 | 8 |
18 | 3 , 7 |
23 | 5 |
42 | 9 |
54 | 2 |
60 | 4 , 6 |
as seen in Database Indexes for The Inquisitive Mind
DOCUMENT | COUNTRIES |
---|---|
A |
Australia, Canada, Portugal |
B |
Canada, Portugal, Togo |
C |
Algeria, Canada, Portugal, Togo |
D |
Algeria, Canada, Togo |
E |
Canada, Togo |
F |
Togo |
COUNTRY | DOCUMENTS |
---|---|
Algeria | C , D |
Australia | A |
Canada | A , B , C , D , E |
Portugal | A , B , C |
Togo | B , C , D , E , F |
var range = require('range-index');
var db = level('path/to/location', {
createIfMissing: true,
valueEncoding: 'json',
keyEncoding: 'binary'
})
var age = range(db);
age.put(18, 7, function (e) {
if(e) throw e;
console.log('index saved successfully');
});
countries.put('Portugal', 'A', function (e) {
if(e) throw e;
console.log('index saved successfully');
});
var user = age.get(18);
user.on('data', function (data) {
assert(data.value == [3, 7]);
assert(data.key == 18);
});
user.on('error', function (e) {
throw e;
});
user.on('close', function () {
console.log('Stream closed')
});
user.on('end', function () {
console.log('Stream closed')
});
with kordon/cursor:
age.get(18).pipe(cursor.all(function (keys, values, data) {
assert.equal(e, null);
assert(values[0] == [3, 7]);
assert(keys[0] == 18);
}));
countries.get('Portugal').pipe(cursor.all(function (keys, values, data) {
assert.equal(e, null);
assert(values[0] == ['A', 'B', 'C']);
assert(key[0] == 'Portugal');
}));
age.all().pipe(cursor.each(function (key, value, data) {
console.log('Age: ', key, 'ID\'s: ', value);
}, function () {
// end
}));
age.from(54).pipe(cursor.each(function (key, value, data) {
console.log('Age: ', key, 'ID\'s: ', value);
}, function () {
// end
}));
returned indexes:
AGE | ID |
---|---|
54 | 2 |
60 | 4 , 6 |
countries.from('P').pipe(cursor.each(function (key, value, data) {
console.log('Country: ', key, 'Documents: ', value);
}, function () {
// end
}));
returned indexes:
COUNTRY | DOCUMENTS |
---|---|
Portugal | A , B , C |
Togo | B , C , D , E , F |
age.between(13, 23).pipe(cursor.each(function (key, value, data) {
console.log('Age: ', key, 'ID\'s: ', value);
}, function () {
// end
}));
returned indexes:
AGE | ID |
---|---|
13 | 8 |
18 | 3 , 7 |
23 | 5 |
countries.between('A', 'C').pipe(cursor.each(function (key, value, data) {
console.log('Country: ', key, 'Documents: ', value);
}, function () {
// end
}));
returned indexes:
COUNTRY | DOCUMENTS |
---|---|
Algeria | C , D |
Australia | A |
age.until(18).pipe(cursor.each(function (key, value, data) {
console.log('Age: ', key, 'ID\'s: ', value);
}, function () {
// end
}));
returned indexes:
AGE | ID |
---|---|
7 | 1 , 10 |
13 | 8 |
18 | 3 , 7 |
countries.until('P').pipe(cursor.each(function (key, value, data) {
console.log('Country: ', key, 'Documents: ', value);
}, function () {
// end
}));
returned indexes:
COUNTRY | DOCUMENTS |
---|---|
Algeria | C , D |
Australia | A |
Canada | A , B , C , D , E |
age.del(18, 7, function (e) {
if(e) throw e;
console.log('index deleted successfully');
});
countries.del('Portugal', 'A', function (e) {
if(e) throw e;
console.log('index deleted successfully');
});
MIT