Terracotta 10.5 | TCStore API Developer Guide | Reference | Indexes
 
Indexes
The records stored in a dataset are accessed for CRUD operations using the key against which the record is held. However, for stream queries there is an option to use secondary indexes for better query performance. Secondary indexes can be created on a specific Cell, thus all the records having that cell will be indexed. The queries on the indexed cell will try to use the index for optimized results.
Creating Secondary Indexes
The code snippet provided below depicts how to create/destroy indexes.

DatasetManager datasetManager =
DatasetManager.clustered(clusterUri).build();

DatasetConfiguration configuration = datasetManager.datasetConfiguration()
.offheap(offHeapResourceId)
.index(CellDefinition.define("orderId", Type.STRING),
IndexSettings.BTREE) // 1
.build();

datasetManager.newDataset("indexedOrders", Type.LONG, configuration);
Dataset<Long> dataset =
datasetManager.getDataset("indexedOrders", Type.LONG);

Indexing indexing = dataset.getIndexing(); // 2

Operation<Index<Integer>> indexOperation = indexing.createIndex(
CellDefinition.define("invoiceId", Type.INT),
IndexSettings.BTREE); // 3

Index<Integer> invoiceIdIndex = indexOperation.get(); // 4
1
An Index can be created while the dataset is being created. The DatasetConfigurationBuilder.index method takes a CellDefinition and an IndexSettings. Currently only IndexSettings.BTREE is supported for secondary indexes.
2
In case there is a need to index a cell after dataset is created, that can be done as well. For that, Indexing is provided by Dataset.getIndexing to create/delete indexes on a dataset.
3
The Indexing.createIndex method again takes a CellDefinition and an IndexSettings, to return an Operation of Index. Operation represents the asynchronous execution of the long running indexing operation.
4
You get an Index when the operation completes.
Getting Index Status
The code snippet depicts how to determine the status of indexes.
Collection<Index<?>> allIndexes = indexing.getAllIndexes(); // 1
Collection<Index<?>> liveIndexes = indexing.getLiveIndexes(); // 2
1
Indexing.getAllIndexes returns all the indexes created on the dataset, regardless of their status.
2
Indexing.getLiveIndexes returns only those indexes whose Status is LIVE.
Destroying Indexes
The code snippet depicts how to determine the status of indexes.
indexing.destroyIndex(invoiceIdIndex); // 1
1
An existing Index can be destroyed using Indexing.destroyIndex.
Indexes in HA setup
Creating an index is a long running operation. With an HA setup, indexes are created asynchronously on the mirrors. This implies that if an index creation has completed and the status is LIVE, the index creation might still be in progress on mirrors which might complete eventually. Also when a new mirror comes up, the records on the active are synced to mirror, but they are indexed only when syncing of data is complete. Thus indexing on a new mirror is done asynchronously.
Please refer to API documentation for more details.

Copyright © 2010-2019 | Software AG, Darmstadt, Germany and/or Software AG USA, Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.