Home

Awesome

range

level based range index

NPM version Build Status Dependency Status Coverage Status

install

npm install [--save/--save-dev] range-index

examples

data set

IDNAMEAGESEX
1Divina Ventimiglia7F
2Nakisha Robuck54F
3Amira Markus18F
4Mohamed Kincannon60M
5Juana Ardon23F
6Hyon Davie60F
7Estell Cromer18F
8Jacob Neeley13M
9Carlene Weatherman42F
10Carie Markland7F

age/id range index

AGEID
71, 10
138
183, 7
235
429
542
604, 6

as seen in Database Indexes for The Inquisitive Mind

data set

DOCUMENTCOUNTRIES
AAustralia, Canada, Portugal
BCanada, Portugal, Togo
CAlgeria, Canada, Portugal, Togo
DAlgeria, Canada, Togo
ECanada, Togo
FTogo

country/document range index

COUNTRYDOCUMENTS
AlgeriaC, D
AustraliaA
CanadaA, B, C, D, E
PortugalA, B, C
TogoB, C, D, E, F

api

var range = require('range-index');

index range(level: db)

var db = level('path/to/location', {
  createIfMissing: true,
  valueEncoding: 'json',
  keyEncoding: 'binary' 
})

var age = range(db);

void index.put(string/number: value, *: key, function: callback)

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');
});

stream index.get(string/number: index)

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');
}));

stream index.all()

age.all().pipe(cursor.each(function (key, value, data) {
  console.log('Age: ', key, 'ID\'s: ', value);
}, function () {
  // end
}));

stream index.from(start)

age.from(54).pipe(cursor.each(function (key, value, data) {
  console.log('Age: ', key, 'ID\'s: ', value);
}, function () {
  // end
}));

returned indexes:

AGEID
542
604, 6

countries.from('P').pipe(cursor.each(function (key, value, data) {
  console.log('Country: ', key, 'Documents: ', value);
}, function () {
  // end
}));

returned indexes:

COUNTRYDOCUMENTS
PortugalA, B, C
TogoB, C, D, E, F

stream index.between(start, end)

age.between(13, 23).pipe(cursor.each(function (key, value, data) {
  console.log('Age: ', key, 'ID\'s: ', value);
}, function () {
  // end
}));

returned indexes:

AGEID
138
183, 7
235

countries.between('A', 'C').pipe(cursor.each(function (key, value, data) {
  console.log('Country: ', key, 'Documents: ', value);
}, function () {
  // end
}));

returned indexes:

COUNTRYDOCUMENTS
AlgeriaC, D
AustraliaA

stream index.until(end)

age.until(18).pipe(cursor.each(function (key, value, data) {
  console.log('Age: ', key, 'ID\'s: ', value);
}, function () {
  // end
}));

returned indexes:

AGEID
71, 10
138
183, 7

countries.until('P').pipe(cursor.each(function (key, value, data) {
  console.log('Country: ', key, 'Documents: ', value);
}, function () {
  // end
}));

returned indexes:

COUNTRYDOCUMENTS
AlgeriaC, D
AustraliaA
CanadaA, B, C, D, E

void index.del(string/number: value, *: key, function: callback)

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');
});

license

MIT