CachedBackend
Implements:
This is an intermediate layer caching the results of a backend. While simple get/put queries make use of the cache, more advanced queries will be forwarded to the backend.
Constructor Summary
Public Constructor | ||
public |
constructor(backend: IBackend) Creates a new instance of the cached layer using the specified backend. |
Member Summary
Public Members | ||
public get |
connected: boolean |
|
public get |
A map of index names to indices as defined by the underlying backend. |
Method Summary
Public Methods | ||
public abstract |
async applyCombined(tx: Transaction): function() Returns the necessary information in order to flush a combined transaction. |
|
public |
close(): Promise Closes the object store and potential connections. |
|
public |
Returns the count of entries in the given range. |
|
public |
createIndex(indexName: string, keyPath: string | Array<string>, options: IndexConfig): * Creates a new secondary index on the object store. |
|
public |
decode(value: *, key: string): * Method called to decode a single value. |
|
public |
deleteIndex(indexName: *, options: function(oldVersion: number, newVersion: number): boolean}): * Deletes a secondary index from the object store. |
|
public |
encode(value: *): * Method called to encode a single value. |
|
public |
async get(key: string, options: RetrievalConfig): Promise<*> Returns a promise of the object stored under the given primary key. |
|
public abstract |
getSync(key: string, options: SyncRetrievalConfig): * Returns the object stored under the given primary key. |
|
public |
Returns the index of the given name. |
|
public |
isCached(key: string): boolean A check whether a certain key is cached. |
|
public |
isSynchronous(): boolean Checks whether an object store implements the ISynchronousObjectStore interface. |
|
public |
Iterates over the keys in a given range and direction. |
|
public |
Returns a promise of a set of keys fulfilling the given query by querying the backend. |
|
public |
Returns a promise of the key being maximal for the given range. |
|
public |
Returns a promise of the object whose primary key is maximal for the given range. |
|
public |
Returns a promise of the key being minimal for the given range. |
|
public |
Returns a promise of the object whose primary key is minimal for the given range. |
|
public |
async truncate(): Promise Empties the object store. |
|
public |
valueStream(callback: function(value: *, key: string): boolean, ascending: boolean, query: KeyRange): * Iterates over the keys and values in a given range and direction. |
|
public |
Returns a promise of an array of objects whose primary keys fulfill the given query by relying on the backend. |
Protected Methods | ||
protected |
_apply(tx: Transaction): Promise Internally applies a transaction to the cache's and backend's state. |
|
protected |
_applyLocally(tx: Transaction) Internally applies a transaction to the cache's state. |
|
protected |
async _retrieveValues(keys: Set<string>): Promise<Array<*>> A helper method to retrieve the values corresponding to a set of keys. |
Public Constructors
Public Members
public get connected: boolean source
Public Methods
public abstract async applyCombined(tx: Transaction): function() source
Returns the necessary information in order to flush a combined transaction.
Params:
Name | Type | Attribute | Description |
tx | Transaction | The transaction that should be applied to this backend. |
Return:
function() | For non-persistent backends: a function that effectively applies the transaction. Native backends otherwise specify their own information as needed by their JungleDB instance. |
public close(): Promise source
Closes the object store and potential connections.
Return:
Promise | The promise resolves after closing the object store. |
public count(query: KeyRange): Promise<number> source
Returns the count of entries in the given range. If the optional query is not given, it returns the count of entries in the object store. If the query is of type KeyRange, it returns the count of entries within the given range.
Params:
Name | Type | Attribute | Description |
query | KeyRange |
|
Return:
Promise<number> |
public createIndex(indexName: string, keyPath: string | Array<string>, options: IndexConfig): * source
Creates a new secondary index on the object store. Currently, all secondary indices are non-unique. They are 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' } } Secondary indices may be multiEntry, i.e., if the keyPath resolves to an iterable object, each item within can be used to find this entry. If a new object does not possess the key path associated with that index, it is simply ignored.
This function may only be called before the database is connected. Moreover, it is only executed on database version updates or on first creation.
Params:
Name | Type | Attribute | Description |
indexName | string | The name of the index. |
|
keyPath | string | Array<string> |
|
The path to the key within the object. May be an array for multiple levels. |
options | IndexConfig |
|
An options object. |
Return:
* |
public decode(value: *, key: string): * source
Method called to decode a single value.
Params:
Name | Type | Attribute | Description |
value | * | Value to be decoded. |
|
key | string | Key corresponding to the value. |
Return:
* | The decoded value. |
public deleteIndex(indexName: *, options: function(oldVersion: number, newVersion: number): boolean}): * source
Deletes a secondary index from the object store.
Params:
Name | Type | Attribute | Description |
indexName | * | ||
options | function(oldVersion: number, newVersion: number): boolean} |
|
Return:
* |
public encode(value: *): * source
Method called to encode a single value.
Params:
Name | Type | Attribute | Description |
value | * | Value to be encoded. |
Return:
* | The encoded value. |
public async get(key: string, options: RetrievalConfig): Promise<*> source
Returns a promise of the object stored under the given primary key. If the item is in the cache, the cached value will be returned. Otherwise, the value will be fetched from the backend object store.. Resolves to undefined if the key is not present in the object store.
Params:
Name | Type | Attribute | Description |
key | string | The primary key to look for. |
|
options | RetrievalConfig |
|
Advanced retrieval options. |
Return:
Promise<*> | A promise of the object stored under the given key, or undefined if not present. |
public abstract getSync(key: string, options: SyncRetrievalConfig): * source
Returns the object stored under the given primary key. Resolves to undefined if the key is not present in the object store.
Params:
Name | Type | Attribute | Description |
key | string | The primary key to look for. |
|
options | SyncRetrievalConfig |
|
Advanced retrieval options. |
Return:
* | The object stored under the given key, or undefined if not present. |
public index(indexName: string): IIndex source
Returns the index of the given name. If the index does not exist, it returns undefined.
Params:
Name | Type | Attribute | Description |
indexName | string | The name of the requested index. |
public isCached(key: string): boolean source
A check whether a certain key is cached.
Params:
Name | Type | Attribute | Description |
key | string | The key to check. |
Return:
boolean | A boolean indicating whether the key is already in the cache. |
public isSynchronous(): boolean source
Checks whether an object store implements the ISynchronousObjectStore interface.
Return:
boolean | The transaction object. |
public keyStream(callback: function(key: string): boolean, ascending: boolean, query: KeyRange): * source
Iterates over the keys in a given range and direction. The callback is called for each primary key fulfilling the query until it returns false and stops the iteration.
Params:
Name | Type | Attribute | Description |
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:
* |
public keys(query: Query | KeyRange, limit: number): Promise<Set<string>> source
Returns a promise of a set of keys fulfilling the given query by querying the backend. If the optional query is not given, it returns all keys in the object store. If the query is of type KeyRange, it returns all keys of the object store being within this range. If the query is of type Query, it returns all keys fulfilling the query.
Return:
Promise<Set<string>> | A promise of the set of keys relevant to the query. |
public maxKey(query: KeyRange): Promise<string> source
Returns a promise of the key being maximal for the given range. If the optional query is not given, it returns the maximal key. If the query is of type KeyRange, it returns the key being maximal for the given range.
Params:
Name | Type | Attribute | Description |
query | KeyRange |
|
Optional query to check keys against. |
Return:
Promise<string> | A promise of the key relevant to the query. |
public maxValue(query: KeyRange): Promise<*> source
Returns a promise of the object whose primary key is maximal for the given range. If the optional query is not given, it returns the object whose key is maximal. If the query is of type KeyRange, it returns the object whose primary key is maximal for the given range.
Params:
Name | Type | Attribute | Description |
query | KeyRange |
|
Optional query to check keys against. |
Return:
Promise<*> | A promise of the object relevant to the query. |
public minKey(query: KeyRange): Promise<string> source
Returns a promise of the key being minimal for the given range. If the optional query is not given, it returns the minimal key. If the query is of type KeyRange, it returns the key being minimal for the given range.
Params:
Name | Type | Attribute | Description |
query | KeyRange |
|
Optional query to check keys against. |
Return:
Promise<string> | A promise of the key relevant to the query. |
public minValue(query: KeyRange): Promise<*> source
Returns a promise of the object whose primary key is minimal for the given range. If the optional query is not given, it returns the object whose key is minimal. If the query is of type KeyRange, it returns the object whose primary key is minimal for the given range.
Params:
Name | Type | Attribute | Description |
query | KeyRange |
|
Optional query to check keys against. |
Return:
Promise<*> | A promise of the object relevant to the query. |
public async truncate(): Promise source
Empties the object store.
Return:
Promise | The promise resolves after emptying the object store. |
public valueStream(callback: function(value: *, key: string): boolean, ascending: boolean, query: KeyRange): * source
Iterates over the keys and values in a given range and direction. The callback is called for each value and primary key fulfilling the query until it returns false and stops the iteration.
Params:
Name | Type | Attribute | Description |
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:
* |
public values(query: Query | KeyRange, limit: number): Promise<Array<*>> source
Returns a promise of an array of objects whose primary keys fulfill the given query by relying on the backend. If the optional query is not given, it returns all objects in the object store. If the query is of type KeyRange, it returns all objects whose primary keys are within this range. If the query is of type Query, it returns all objects whose primary keys fulfill the query.
Return:
Promise<Array<*>> | A promise of the array of objects relevant to the query. |
Protected Methods
protected _apply(tx: Transaction): Promise source
Internally applies a transaction to the cache's and backend's state. This needs to be done in batch (as a db level transaction), i.e., either the full state is updated or no changes are applied.
Params:
Name | Type | Attribute | Description |
tx | Transaction | The transaction to apply. |
Return:
Promise | The promise resolves after applying the transaction. |
protected _applyLocally(tx: Transaction) source
Internally applies a transaction to the cache's state.
Params:
Name | Type | Attribute | Description |
tx | Transaction | The transaction to apply. |
protected async _retrieveValues(keys: Set<string>): Promise<Array<*>> source
A helper method to retrieve the values corresponding to a set of keys.
Params:
Name | Type | Attribute | Description |
keys | Set<string> | The set of keys to get the corresponding values for. |
Return:
Promise<Array<*>> | A promise of the array of values. |