## Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending#"""This module provides the ability to create, check, and retrieve DataIndex objects from Deephaven tables."""fromtypingimportList,OptionalimportjpyfromdeephavenimportDHErrorfromdeephaven._wrapperimportJObjectWrapperfromdeephaven.jcompatimportj_list_to_listfromdeephaven.tableimportTable_JDataIndexer=jpy.get_type("io.deephaven.engine.table.impl.indexer.DataIndexer")_JDataIndex=jpy.get_type("io.deephaven.engine.table.DataIndex")
[docs]classDataIndex(JObjectWrapper):"""A DataIndex is an index used to improve the speed of data access operations for a Deephaven table. The index applies to one or more indexed (key) column(s) of a Deephaven table. Note that a DataIndex itself is backed by a table."""j_object_type=_JDataIndexdef__init__(self,j_data_index:jpy.JType):self._j_data_index=j_data_index@propertydefj_object(self)->jpy.JType:returnself._j_data_index@propertydefkeys(self)->List[str]:"""The names of the columns indexed by the DataIndex. """returnj_list_to_list(self._j_data_index.keyColumnNames())@propertydeftable(self)->Table:"""The backing table of the DataIndex."""returnTable(self._j_data_index.table())
[docs]defhas_data_index(table:Table,key_cols:List[str])->bool:"""Checks if a table currently has a DataIndex for the given key columns. Args: table (Table): the table to check key_cols (List[str]): the names of the key columns indexed Returns: bool: True if the table has a DataIndex, False otherwise """return_JDataIndexer.hasDataIndex(table.j_table,key_cols)
def_get_data_index(table:Table,key_cols:List[str])->Optional[DataIndex]:"""Gets a DataIndex for the given key columns. Returns None if the DataIndex does not exist. Args: table (Table): the table to get the DataIndex from key_cols (List[str]): the names of the key columns indexed Returns: a DataIndex or None """j_di=_JDataIndexer.getDataIndex(table.j_table,key_cols)returnDataIndex(j_di)ifj_dielseNone
[docs]defdata_index(table:Table,key_cols:List[str],create_if_absent:bool=True)->Optional[DataIndex]:"""Gets the DataIndex for the given key columns on the provided table. When the DataIndex already exists, returns it. When the DataIndex doesn't already exist, if create_if_absent is True, creates the DataIndex first then returns it; otherwise returns None. Args: table (Table): the table to index key_cols (List[str]): the names of the key columns to index create_if_absent (bool): if True, create the DataIndex if it does not already exist, default is True Returns: a DataIndex or None Raises: DHError """try:ifnotcreate_if_absent:return_get_data_index(table,key_cols)returnDataIndex(_JDataIndexer.getOrCreateDataIndex(table.j_table,key_cols))exceptExceptionase:raiseDHError(e,"failed to create DataIndex.")frome