3 Star 0 Fork 0

Gitee 极速下载 / mindex

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/internalfx/mindex
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
ISC

Mindex

A small simple javascript compound index that borrows heavily from LokiJS.

Useful for finding objects in collections, Mindex is able to search huge arrays of data almost instantly. It can return ranges of the data, and the results are always sorted. It also has a very intuitive query syntax and even supports skip and offset so you can quickly paginate results.

Mindex makes finding information fast.

Features

  • Small - Under 500 lines of code.
  • Fast - Records can be found in O(log n) time.
  • Powerful - Supports compound keys and allows for a simple query syntax.

npm version license Build Status

NPM


Special thanks to Arthur Andrew Medical for sponsoring this project.

Arthur Andrew Medical manufactures products with ingredients that have extensive clinical research for safety and efficacy. We specialize in Enzymes, Probiotics and Antioxidants.


Some Biased Benchmarks

I tried to compare using Mindex to simply searching/sorting an array of the records. This uses a database of 50,000 records for the testing.

Not surprisingly, inserts are slower. But everything else is a landslide.

***********************
Test Mindex performance
***********************

Testing insertRecord(record)

Mindex 16.80 ops/sec, Native Array 45.51 ops/sec
Mindex is 63% slower


Testing get(key)

Mindex 3485998.20 ops/sec, Native Array 642.11 ops/sec
Mindex is 542799% faster


Testing getAll(), get all records

Mindex 374.92 ops/sec, Native Array 14.41 ops/sec
Mindex is 2502% faster


Testing removeRecord(key, value)

Mindex 1955971.50 ops/sec, Native Array 220.43 ops/sec
Mindex is 887260% faster

What makes Mindex so fast?

A very simple (and old) idea, Binary Search. Mindex populates two arrays, an array of keys and an array of values. What makes Mindex useful is how it abstracts all the complexity of managing the arrays from you, and the simple querying capabilities it provides.

If Mindex is just arrays of keys and values, how is it compound?

After searching through an index it is possible for Mindex to find....another Mindex. This complexity is also handled for you. Mindex will create as many nested indexes as is necessary to hold your data. There is no limit on the depth of compound keys.

Installation

Supports node v4.0+

npm install --save mindex

TL;DR

var Mindex = require('mindex')

var index = Mindex(['age'])

index.insertRecord({
  id: 'John',
  age: 25
})
index.insertRecord({
  id: 'Darcy',
  age: 28
})
index.insertRecord({
  id: 'Jim',
  age: 29
})
index.insertRecord({
  id: 'Betty',
  age: 25
})

// Get IDs by key
console.log(index.get(25)) // [ 'Betty', 'John' ]

// Get all IDs sorted by key (age)
console.log(index.getAll()) // [ 'Betty', 'John', 'Darcy', 'Jim' ]

// Get all IDs within a given range
console.log(index.query({'>': [22], '<': [29]})) // [ 'Betty', 'John', 'Darcy' ]

Constructor(keyList)

Parameters
key default type description
keyList [] Array The array of keys to index records by
returns

mindex instance

Description

Creates a new index.

Example
var Mindex = require('mindex')

var mindex = Mindex(['city', 'age'])

insertRecord(data)

Parameters
key default type description
data undefined Object The object to index. Object must have an id property
returns

undefined

Description

Inserts a new object into the index. This is a convenience method for set().

Example
mindex.insertRecord({
  id: 25,
  name: 'John',
  city: 'Denver',
  age: 35
})

removeRecord(data)

Parameters
key default type description
data undefined Object The object to remove. Object must have an id property
returns

undefined

Description

Removes an object from the index. This is a convenience method for remove().

Example
mindex.removeRecord({
  id: 25,
  name: 'John',
  city: 'Denver',
  age: 35
})

updateRecord(data)

Parameters
key default type description
data undefined Object The object to update. Object must have an id property
returns

undefined

Description

Replaces an object in the index with new data. This is a convenience method for remove() and set().

Example
mindex.updateRecord({
  id: 25,
  name: 'John',
  city: 'Denver',
  age: 35
})

set(keyList, value)

Parameters
key default type description
keyList undefined Array, String, Integer An array of keys to search for in the index. If the index is not compound a string or integer may be used.
value undefined String, Integer The value to insert at the given key
returns

undefined

Description

Inserts the given value at the given key.

Example

var mindex = Mindex(['city', 'age'])

// insertRecord and set produce equivalent results in the example below.
mindex.insertRecord({
  id: 1,
  name: 'John',
  city: 'Denver',
  age: 35
})
mindex.set(['Denver', 35], 1)

// Get all 35 year olds in the city of Denver
mindex.get(['Denver', 35]) // returns [1]

get(keyList)

Parameters
key default type description
keyList undefined Array, String, Integer An array of keys to search for in the index. If the index is not compound a string or integer may be used.
returns

An array of found record IDs.

Description

Retrieves an array of matching IDs from the index.

Example

mindex.insertRecord({
  id: 1,
  name: 'John',
  city: 'Denver',
  age: 35
})

mindex.insertRecord({
  id: 2,
  name: 'Betty',
  city: 'Denver',
  age: 37
})

// Get all 35 year olds in the city of Denver
mindex.get(['Denver', 35]) // returns [1]

// If you are only looking for 'Denver' you can omit the array.
mindex.get('Denver') // returns [1, 2]

remove(keyList, value)

Parameters
key default type description
keyList undefined Array, String, Integer An array of keys to search for in the index. If the index is not compound a string or integer may be used.
value undefined String, Integer The value to remove at the given key
returns

undefined

Description

Removes the given value from the index at the given key.

Example

var mindex = Mindex(['city', 'age'])

mindex.insertRecord({
  id: 1,
  name: 'John',
  city: 'Denver',
  age: 35
})

mindex.get(['Denver', 35]) // returns [1]

// removeRecord and remove produce equivalent results in the example below.
mindex.removeRecord({
  id: 1,
  name: 'John',
  city: 'Denver',
  age: 35
})
// -- or --
mindex.remove(['Denver', 35], 1)

// Get all 35 year olds in the city of Denver
mindex.get(['Denver', 35]) // returns []

getAll(options)

Parameters
key default type description
options undefined Object An options object.
Options
key default type description
order 'asc' String Return results in ascending or descending order
returns

An array of all IDs in the index, sorted by key(s).

Description

Retrieves an array of all IDs.

Example

mindex.insertRecord({
  id: 1,
  name: 'John',
  city: 'Denver',
  age: 35
})

mindex.insertRecord({
  id: 2,
  name: 'Betty',
  city: 'Denver',
  age: 37
})

mindex.getAll() // returns [1, 2]

mindex.getAll({order: 'desc'}) // returns [2, 1]

query(query)

Parameters
key default type description
query undefined Object An object representing the query.
Query Options
key default type description
> undefined Array An array of keys to search for. Mutually exclusive with >=
>= undefined Array An array of keys to search for. Mutually exclusive with >
< undefined Array An array of keys to search for. Mutually exclusive with <=
<= undefined Array An array of keys to search for. Mutually exclusive with <
offset 0 Integer Number of IDs to skip, can be used for pagination.
limit undefined Integer Maximum number of IDs to return
order 'asc' String Return results in ascending or descending order
returns

An array of found record IDs.

Description

Retrieves an array of matching IDs from the index. When used with limit and order, you can quickly get the highest or lowest value in the index.

Example

var mindex = Mindex(['city', 'age'])

mindex.insertRecord({
  id: 1,
  name: 'John',
  city: 'Denver',
  age: 35
})

mindex.insertRecord({
  id: 2,
  name: 'Betty',
  city: 'Denver',
  age: 37
})

mindex.insertRecord({
  id: 3,
  name: 'Jen',
  city: 'Phoenix',
  age: 36
})

mindex.insertRecord({
  id: 4,
  name: 'Jim',
  city: 'Denver',
  age: 37
})

index.query({'>': ['Denver', 35], '<=': ['Denver', 37]}) // returns [2,4]

index.query({'>=': ['Denver', 35], '<=': ['Denver', 37], offset: 1}) // returns [2,4]

index.query({'>=': ['Denver', 35], '<=': ['Denver', 37], limit: 1}) // returns [1,2]

index.query({'>': ['Denver'], '<=': ['Phoenix']}) // returns [3]

index.query({'>=': ['Denver'], '<=': ['Phoenix']}) // returns [1,2,4,3]

index.query({'>=': ['Denver'], '<': ['Phoenix']}) // returns [1,2,4]

index.query({'>=': ['Denver']}) // returns [1,2,4,3]

index.query({limit: 1, order: 'desc'}) // returns [3]

index.query({limit: 1, order: 'asc'}) // returns [1]

clear()

returns

undefined

Description

Clears the index.

Example

mindex.insertRecord({
  id: 1,
  name: 'John',
  city: 'Denver',
  age: 35
})

mindex.insertRecord({
  id: 2,
  name: 'Betty',
  city: 'Denver',
  age: 37
})

mindex.clear()

mindex.getAll() // returns []
ISC License Copyright (c) 2015, InternalFX. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

简介

暂无描述 展开 收起
JavaScript
ISC
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/mirrors/mindex.git
git@gitee.com:mirrors/mindex.git
mirrors
mindex
mindex
master

搜索帮助