Options
All
  • Public
  • Public/Protected
  • All
Menu

Class IndexedStore<IndexedRecord>

IndexedStore represents a storage of records that are indexed over multiple properties.
Records ar stored in a multilevel map, with the following structure:


    PrimaryIndexName     --> IndexValue --> [document]
                        --> IndexValue --> [document]
    SecondaryIndexName   --> IndexValue --> [document, document]

Indexed properties are allowed to be nullable (i.e. have null or undefined as their values).
This is a low-level class and exposes raw primitives. It needs to be used by higher level abstractions.

Type parameters

  • IndexedRecord: Recordable

    Type of the indexed record.

Hierarchy

  • IndexedStore

Implements

  • Iterable<IndexedRecord>

Constructors

constructor

Accessors

indexes

  • Get names of indexed properties.

    Returns IndexName<IndexedRecord>[]

size

  • get size(): number
  • Get number of records present in the storage.

    Returns number

values

  • get values(): IndexedRecord[]
  • Get a view of all records from storage.

    Returns IndexedRecord[]

    Array of all records. Array can be modified by client, as it is created on each method call.

Methods

[Symbol.iterator]

  • [Symbol.iterator](): Iterator<IndexedRecord, any, undefined>
  • Iterate over records from storage.

    Returns Iterator<IndexedRecord, any, undefined>

clear

  • clear(): void
  • Removes all records from storage and leaves it empty.
    Notice that indexes are not removed, only their associated records are deleted.

    Returns void

contains

  • Check whether indexName contains records indexed with indexValue.

    Parameters

    • indexName: IndexName<IndexedRecord>

      Name of the index.

    • indexValue: IndexValue

      Value of that index.

    Returns boolean

containsIndex

  • containsIndex(indexName: IndexName<IndexedRecord>): boolean
  • Check whether index with indexName exists.

    Parameters

    • indexName: IndexName<IndexedRecord>

      Name of the index.

    Returns boolean

createIndexes

  • createIndexes(newIndexProperties: IndexName<IndexedRecord>[]): void
  • Create new indexes for a set of record properties.
    When storage already contains records, they will be indexed for newly defined indexes.

    Parameters

    • newIndexProperties: IndexName<IndexedRecord>[]

      Name of properties that need to be indexed.

    Returns void

dropIndex

  • dropIndex(indexName: IndexName<IndexedRecord>): boolean
  • Remove index from storage.
    Notice that records are not removed, and can be found by another existing indexes.

    Parameters

    • indexName: IndexName<IndexedRecord>

      Name of the index.

    Returns boolean

    Whether index was removed or not.

dropIndexes

  • dropIndexes(): void
  • Remove all indexes, except the primary one.

    Returns void

filter

  • filter(predicate: UnaryPredicate<IndexedRecord>, onIndex?: IndexName<IndexedRecord>, withValue?: IndexValue): IndexedRecord[]
  • Filter a set of documents.
    When index related params are not specified, will apply predicate over all records.

    Parameters

    • predicate: UnaryPredicate<IndexedRecord>

      Predicate function.

    • Optional onIndex: IndexName<IndexedRecord>

      Index from were documents need to be retrieved.

    • Optional withValue: IndexValue

      Value of that index.

    Returns IndexedRecord[]

    List of filtered records.

find

  • find(predicate: UnaryPredicate<IndexedRecord>, onIndex?: IndexName<IndexedRecord>, withValue?: IndexValue): Optional<IndexedRecord>
  • Find a single record from storage.
    When index related params are not specified, will apply predicate over all records.

    Parameters

    • predicate: UnaryPredicate<IndexedRecord>

      Predicate function.

    • Optional onIndex: IndexName<IndexedRecord>

      Index from were documents need to be retrieved.

    • Optional withValue: IndexValue

      Value of that index.

    Returns Optional<IndexedRecord>

    Record matching search criteria, if found.

getIndexRecordsCount

  • getIndexRecordsCount(indexName: IndexName<IndexedRecord>): number
  • Get number of records stored under indexName.

    Parameters

    • indexName: IndexName<IndexedRecord>

      Name of the index.

    Returns number

insert

  • insert(records: IndexedRecord[]): void
  • Inserts records into storage.
    After insertion is completed, will index records by properties that are indexable.

    Parameters

    • records: IndexedRecord[]

      List of documents to be inserted.

    Returns void

map

  • map<MappedType>(mapper: Mapper<IndexedRecord, MappedType>, onIndex?: IndexName<IndexedRecord>, withValue?: IndexValue): MappedType[]
  • Map a set of records.
    When index related params are not specified, will map all of the records.

    Type parameters

    • MappedType

    Parameters

    • mapper: Mapper<IndexedRecord, MappedType>

      Mapping function.

    • Optional onIndex: IndexName<IndexedRecord>

      Index from were documents need to be retrieved.

    • Optional withValue: IndexValue

      Value of that index.

    Returns MappedType[]

    List of mapped records.

read

  • Read documents under indexName having it's value equal to indexValue.
    Reference to internal index structure is returned, and therefore the caller should not alter it.

    Parameters

    • indexName: IndexName<IndexedRecord>

      Name of the index.

    • indexValue: IndexValue

      Value of that index.

    Returns IndexedRecord[]

    List of documents having indexName equal to indexValue.

readIndex

  • readIndex(indexName: IndexName<IndexedRecord>): Index<IndexedRecord>
  • Read all documents stored in the indexName.
    Reference to internal index structure is returned, and therefore the caller should not alter it.

    Parameters

    • indexName: IndexName<IndexedRecord>

      Name of the index.

    Returns Index<IndexedRecord>

    The internal index for indexName.

reindex

  • Reindex document after it's indexName property has been changed.
    This method will also set the new value of indexName to record.
    Caller needs to call this method everytime value for one of the indexed properties changes.
    Notice that primary key should remain immutable, it's change is forbidden.

    example

    Index record that wasn't indexed before

    // insert a record that wasn't indexed by 'birthYear', because it was null
    const record = { id: 1, birthYear: null };
    storage.insert([record]);
    
    // (re)index it by birth year
    storage.reindex('birthYear', record.birthYear, 1997, record.id);
    console.log(record.birthYear) // 1997
    

    Reindex record that was indexed before


    // insert a record that is indexed by 'birthYear'
    const record = { id: 1, birthYear: 2000 };
    storage.insert([record]);
    
    // reindex it by birth year
    storage.reindex('birthYear', record.birthYear, 1997, record.id);
    console.log(record.birthYear) // 1997
    

    De-index record that was indexed before


    // insert a record that is indexed by 'birthYear'
    const record = { id: 1, birthYear: 2000 };
    storage.insert([record]);
    
    // de-index it by birth year
    storage.reindex('birthYear', record.birthYear, null, record.id);
    console.log(record.birthYear) // null
    

    Parameters

    • indexName: IndexName<IndexedRecord>

      Name of the index.

    • oldValue: IndexValue

      Old value of the index. It is used for record retrieval.

    • newValue: IndexValue

      The updated value of the index. Used for actual reindexing.

    • matcher: IndexValue | UnaryPredicate<IndexedRecord>

      Predicate that matches record, indexed property of which has been changed.

    Returns void

remove

  • remove(indexName: IndexName<IndexedRecord>, indexValue: IndexValue, predicate?: UnaryPredicate<IndexedRecord>): Optional<IndexedRecord>
  • Remove from storage record that match search criteria.
    Search criteria is expressed as name of the index, it's value, and an optional predicate for filtering records from that index.
    Notice that only the first record that matched the predicate will be removed.

    example

    Delete record by primary index

    storage.remove('id', 'value-of-id');

    Delete record by secondary index

    // removes record having `fullName` equal to 'John', and `age` equal to 18
    storage.remove('fullName', 'John', record => record.age === 18);
    

    Parameters

    • indexName: IndexName<IndexedRecord>

      Name of the index.

    • indexValue: IndexValue

      Value of that index.

    • Optional predicate: UnaryPredicate<IndexedRecord>

      Predicate used for record filtering.
      When indexName is the primary one, this parameter is optional.

    Returns Optional<IndexedRecord>

    Removed document, if found.