Home Reference Source Test
public class | source

InMemoryIndex

Direct Subclass:

TransactionIndex

Implements:

This is a BTree based index, which is generally stored in memory. It is used by transactions.

Constructor Summary

Public Constructor
public

constructor(objectStore: IObjectStore, keyPath: string | Array<string>, multiEntry: boolean, unique: boolean)

Creates a new InMemoryIndex for a given object store.

Member Summary

Public Members
public get

keyPath: string | Array<string>

The key path associated with this index.

public get

multiEntry: boolean

This value determines whether the index supports multiple secondary keys per entry.

public get

unique: boolean

This value determines whether the index is a unique constraint.

Method Summary

Public Methods
public

async count(query: KeyRange): Promise<number>

Returns the count of entries, whose secondary key is in the given range.

public

keyStream(callback: function(key: string): boolean, ascending: boolean, query: KeyRange): Promise

Iterates over the primary keys in a given range of secondary keys and direction.

public

async keys(query: KeyRange, limit: number): Promise<Set<string>>

Returns a promise of a set of primary keys, whose associated objects' secondary keys are in the given range. If the optional query is not given, it returns all primary keys in the index. If the query is of type KeyRange, it returns all primary keys for which the secondary key is within this range.

public

async maxKeys(query: KeyRange): Promise<Set<*>>

Returns a promise of a set of primary keys, whose associated secondary keys are maximal for the given range.

public

async maxValues(query: KeyRange): Promise<Array<*>>

Returns a promise of an array of objects whose secondary key is maximal for the given range.

public

async minKeys(query: KeyRange): Promise<Set<*>>

Returns a promise of a set of primary keys, whose associated secondary keys are minimal for the given range.

public

async minValues(query: KeyRange): Promise<Array<*>>

Returns a promise of an array of objects whose secondary key is minimal for the given range.

public

put(key: string, value: *, oldValue: *)

Inserts a new key-value pair into the index.

public

remove(key: string, oldValue: *)

Removes a key-value pair from the index.

public

Reinitialises the index.

public

async valueStream(callback: function(value: *, key: string): boolean, ascending: boolean, query: KeyRange): Promise

Iterates over the values of the store in a given range of secondary keys and direction.

public

async values(query: KeyRange, limit: number): Promise<Array<*>>

Returns a promise of an array of objects whose secondary keys fulfill the given query.

Protected Methods
protected

async _retrieveValues(keys: Set<string>): Promise<Array<*>>

A helper method to retrieve the values corresponding to a set of keys.

Public Constructors

public constructor(objectStore: IObjectStore, keyPath: string | Array<string>, multiEntry: boolean, unique: boolean) source

Creates a new InMemoryIndex for a given object store. The key path describes the path of the secondary key within the stored objects. Only objects for which the key path exists are part of the secondary index.

A key path is defined by a key within the object or alternatively a path through the object to a specific subkey. For example, ['a', 'b'] could be used to use 'key' as the key in the following object: { 'a': { 'b': 'key' } }

If a secondary index is a multi entry index, and the value at the key path is iterable, every item of the iterable value will be associated with the object.

Params:

NameTypeAttributeDescription
objectStore IObjectStore

The underlying object store to use.

keyPath string | Array<string>
  • optional

The key path of the indexed attribute. If the keyPath is not given, this is a primary index.

multiEntry boolean
  • optional

Whether the indexed attribute is considered to be iterable or not.

unique boolean
  • optional

Whether there is a unique constraint on the attribute.

Public Members

public get keyPath: string | Array<string> source

The key path associated with this index. A key path is defined by a key within the object or alternatively a path through the object to a specific subkey. For example, ['a', 'b'] could be used to use 'key' as the key in the following object: { 'a': { 'b': 'key' } } If the keyPath is undefined, this index uses the primary key of the key-value store.

public get multiEntry: boolean source

This value determines whether the index supports multiple secondary keys per entry. If so, the value at the key path is considered to be an iterable.

public get unique: boolean source

This value determines whether the index is a unique constraint.

Public Methods

public async count(query: KeyRange): Promise<number> source

Returns the count of entries, whose secondary key is in the given range. If the optional query is not given, it returns the count of entries in the index. If the query is of type KeyRange, it returns the count of entries, whose secondary key is within the given range.

Params:

NameTypeAttributeDescription
query KeyRange
  • optional

Return:

Promise<number>

public keyStream(callback: function(key: string): boolean, ascending: boolean, query: KeyRange): Promise source

Iterates over the primary keys in a given range of secondary keys and direction. The order is determined by the secondary keys first and by the primary keys second. The callback is called for each primary key fulfilling the query until it returns false and stops the iteration.

Params:

NameTypeAttributeDescription
callback function(key: string): boolean

A predicate called for each key until returning false.

ascending boolean

Determines the direction of traversal.

query KeyRange

An optional KeyRange to narrow down the iteration space.

Return:

Promise

The promise resolves after all elements have been streamed.

public async keys(query: KeyRange, limit: number): Promise<Set<string>> source

Returns a promise of a set of primary keys, whose associated objects' secondary keys are in the given range. If the optional query is not given, it returns all primary keys in the index. If the query is of type KeyRange, it returns all primary keys for which the secondary key is within this range.

Params:

NameTypeAttributeDescription
query KeyRange
  • optional

Optional query to check the secondary keys against.

limit number
  • optional

Limits the number of results if given.

Return:

Promise<Set<string>>

A promise of the set of primary keys relevant to the query.

public async maxKeys(query: KeyRange): Promise<Set<*>> source

Returns a promise of a set of primary keys, whose associated secondary keys are maximal for the given range. If the optional query is not given, it returns the set of primary keys, whose associated secondary key is maximal within the index. If the query is of type KeyRange, it returns the set of primary keys, whose associated secondary key is maximal for the given range.

Params:

NameTypeAttributeDescription
query KeyRange
  • optional

Optional query to check keys against.

Return:

Promise<Set<*>>

A promise of the key relevant to the query.

public async maxValues(query: KeyRange): Promise<Array<*>> source

Returns a promise of an array of objects whose secondary key is maximal for the given range. If the optional query is not given, it returns the objects whose secondary key is maximal within the index. If the query is of type KeyRange, it returns the objects whose secondary key is maximal for the given range.

Params:

NameTypeAttributeDescription
query KeyRange
  • optional

Optional query to check keys against.

Return:

Promise<Array<*>>

A promise of array of objects relevant to the query.

public async minKeys(query: KeyRange): Promise<Set<*>> source

Returns a promise of a set of primary keys, whose associated secondary keys are minimal for the given range. If the optional query is not given, it returns the set of primary keys, whose associated secondary key is minimal within the index. If the query is of type KeyRange, it returns the set of primary keys, whose associated secondary key is minimal for the given range.

Params:

NameTypeAttributeDescription
query KeyRange
  • optional

Optional query to check keys against.

Return:

Promise<Set<*>>

A promise of the key relevant to the query.

public async minValues(query: KeyRange): Promise<Array<*>> source

Returns a promise of an array of objects whose secondary key is minimal for the given range. If the optional query is not given, it returns the objects whose secondary key is minimal within the index. If the query is of type KeyRange, it returns the objects whose secondary key is minimal for the given range.

Params:

NameTypeAttributeDescription
query KeyRange
  • optional

Optional query to check keys against.

Return:

Promise<Array<*>>

A promise of array of objects relevant to the query.

public put(key: string, value: *, oldValue: *) source

Inserts a new key-value pair into the index. For replacing an existing pair, the old value has to be passed as well.

Params:

NameTypeAttributeDescription
key string

The primary key of the pair.

value *

The value of the pair. The indexed key will be extracted from this.

oldValue *
  • optional

The old value associated with the primary key.

public remove(key: string, oldValue: *) source

Removes a key-value pair from the index.

Params:

NameTypeAttributeDescription
key string

The primary key of the pair.

oldValue *

The old value of the pair. The indexed key will be extracted from this.

public truncate() source

Reinitialises the index.

public async valueStream(callback: function(value: *, key: string): boolean, ascending: boolean, query: KeyRange): Promise source

Iterates over the values of the store in a given range of secondary keys and direction. The order is determined by the secondary keys first and by the primary keys second. The callback is called for each value and primary key fulfilling the query until it returns false and stops the iteration.

Params:

NameTypeAttributeDescription
callback function(value: *, key: string): boolean

A predicate called for each value and key until returning false.

ascending boolean

Determines the direction of traversal.

query KeyRange

An optional KeyRange to narrow down the iteration space.

Return:

Promise

The promise resolved after all elements have been streamed.

public async values(query: KeyRange, limit: number): Promise<Array<*>> source

Returns a promise of an array of objects whose secondary keys fulfill the given query. If the optional query is not given, it returns all objects in the index. If the query is of type KeyRange, it returns all objects whose secondary keys are within this range.

Params:

NameTypeAttributeDescription
query KeyRange
  • optional

Optional query to check secondary keys against.

limit number
  • optional

Limits the number of results if given.

Return:

Promise<Array<*>>

A promise of the array of objects relevant to the query.

Protected Methods

protected async _retrieveValues(keys: Set<string>): Promise<Array<*>> source

A helper method to retrieve the values corresponding to a set of keys.

Params:

NameTypeAttributeDescription
keys Set<string>

The set of keys to get the corresponding values for.

Return:

Promise<Array<*>>

A promise of the array of values.