## Index

Indexes on field names, with atomic operations and which can optionally enforce a unique constraint or allow indexed fields to be undefined

**Kind**: global class * [Index](#Index) * [new Index(options)](#new_Index_new) * [.fieldName](#Index+fieldName) : string * [.unique](#Index+unique) : boolean * [.sparse](#Index+sparse) : boolean * [.treeOptions](#Index+treeOptions) : Object * [.tree](#Index+tree) : AVLTree * [.reset([newData])](#Index+reset) * [.insert(doc)](#Index+insert) * [.remove(doc)](#Index+remove) * [.update(oldDoc, [newDoc])](#Index+update) * [.revertUpdate(oldDoc, [newDoc])](#Index+revertUpdate) * [.getMatching(value)](#Index+getMatching) ⇒ [Array.<document>](#document) * [.getBetweenBounds(query)](#Index+getBetweenBounds) ⇒ [Array.<document>](#document) * [.getAll()](#Index+getAll) ⇒ [Array.<document>](#document) ### new Index(options)

Create a new index All methods on an index guarantee that either the whole operation was successful and the index changed or the operation was unsuccessful and an error is thrown while the index is unchanged

| Param | Type | Default | Description | | --- | --- | --- | --- | | options | object | | | | options.fieldName | string | |

On which field should the index apply (can use dot notation to index on sub fields)

| | [options.unique] | boolean | false |

Enforces a unique constraint

| | [options.sparse] | boolean | false |

Allows a sparse index (we can have documents for which fieldName is undefined)

| ### index.fieldName : string

On which field the index applies to (may use dot notation to index on sub fields).

**Kind**: instance property of [Index](#Index) ### index.unique : boolean

Defines if the index enforces a unique constraint for this index.

**Kind**: instance property of [Index](#Index) ### index.sparse : boolean

Defines if we can have documents for which fieldName is undefined

**Kind**: instance property of [Index](#Index) ### index.treeOptions : Object

Options object given to the underlying BinarySearchTree.

**Kind**: instance property of [Index](#Index) ### index.tree : AVLTree

Underlying BinarySearchTree for this index. Uses an AVLTree for optimization.

**Kind**: instance property of [Index](#Index) ### index.reset([newData])

Reset an index

**Kind**: instance method of [Index](#Index) | Param | Type | Description | | --- | --- | --- | | [newData] | [document](#document) \| [?Array.<document>](#document) |

Data to initialize the index with. If an error is thrown during insertion, the index is not modified.

| ### index.insert(doc)

Insert a new document in the index If an array is passed, we insert all its elements (if one insertion fails the index is not modified) O(log(n))

**Kind**: instance method of [Index](#Index) | Param | Type | Description | | --- | --- | --- | | doc | [document](#document) \| [Array.<document>](#document) |

The document, or array of documents, to insert.

| ### index.remove(doc)

Removes a document from the index. If an array is passed, we remove all its elements The remove operation is safe with regards to the 'unique' constraint O(log(n))

**Kind**: instance method of [Index](#Index) | Param | Type | Description | | --- | --- | --- | | doc | [Array.<document>](#document) \| [document](#document) |

The document, or Array of documents, to remove.

| ### index.update(oldDoc, [newDoc])

Update a document in the index If a constraint is violated, changes are rolled back and an error thrown Naive implementation, still in O(log(n))

**Kind**: instance method of [Index](#Index) | Param | Type | Description | | --- | --- | --- | | oldDoc | [document](#document) \| Array.<{oldDoc: document, newDoc: document}> |

Document to update, or an Array of {oldDoc, newDoc} pairs.

| | [newDoc] | [document](#document) |

Document to replace the oldDoc with. If the first argument is an Array of {oldDoc, newDoc} pairs, this second argument is ignored.

| ### index.revertUpdate(oldDoc, [newDoc])

Revert an update

**Kind**: instance method of [Index](#Index) | Param | Type | Description | | --- | --- | --- | | oldDoc | [document](#document) \| Array.<{oldDoc: document, newDoc: document}> |

Document to revert to, or an Array of {oldDoc, newDoc} pairs.

| | [newDoc] | [document](#document) |

Document to revert from. If the first argument is an Array of {oldDoc, newDoc}, this second argument is ignored.

| ### index.getMatching(value) ⇒ [Array.<document>](#document)

Get all documents in index whose key match value (if it is a Thing) or one of the elements of value (if it is an array of Things)

**Kind**: instance method of [Index](#Index) | Param | Type | Description | | --- | --- | --- | | value | Array.<\*> \| \* |

Value to match the key against

| ### index.getBetweenBounds(query) ⇒ [Array.<document>](#document)

Get all documents in index whose key is between bounds are they are defined by query Documents are sorted by key

**Kind**: instance method of [Index](#Index) | Param | Type | Description | | --- | --- | --- | | query | object |

An object with at least one matcher among $gt, $gte, $lt, $lte.

| | [query.$gt] | \* |

Greater than matcher.

| | [query.$gte] | \* |

Greater than or equal matcher.

| | [query.$lt] | \* |

Lower than matcher.

| | [query.$lte] | \* |

Lower than or equal matcher.

| ### index.getAll() ⇒ [Array.<document>](#document)

Get all elements in the index

**Kind**: instance method of [Index](#Index)