# IndexType reference

Very much like the Perl API, the IndexType is an abstract class, in which you can't create the objects directly, you have to create the objects with its concrete sub-classes. It has the methods common for all the index types, it is defined in

    type/IndexType.h

and like all types it's an Mtarget.

The index type id, defined as enum IndexType::IndexId, allows to find out the subclass of the actual object and cast it if desired. The supported index types are:

- IT_HASHED,
- IT_FIFO,
- IT_SORTED,
- IT_ORDERED.

There also are some special semi-hidden helper index types. IT_ROOT is created in every table as the root of its index tree. IT_NONE is used to construct the special object NO_INDEX_TYPE that is used as the return value of failed search for a nested index. And the value IT_LAST is defined past the last actual type, so if you ever need to iterate through the types, you can do it as

    for (int i = 0; i < IndexType::IT_LAST; i++) { ... }

The conversion between the index type id and name can be done with the methods:

    static const char *indexIdString(int enval, const char *def = "???");
    static int stringIndexId(const char *str);

As usual with the contant-and-name conversions, if the numeric id

    enval

is invalid, the string

    def

is returned, by default ???. If the string name

    str

is unknown, -1 is returned.

    IndexId getIndexId() const;

Returns the type id of this index type. The id can't be changed, it gets hardcoded in the subclass constructor.

    IndexType *addSubIndex(const string &name, Onceref index);

Add a sub-index, in exactly the same way as adding an index type to the TableType.

In works like TableType in all the other ways as well: it adds a copy of the argument, not the argument itself, and it designed for chaining. For example:

    Autoref tt = initializeOrThrow( TableType::make(rt1)
    	->addSubIndex("primary", HashedIndexType::make(
    		NameSet::make()->add("b")->add("c"))
    	)->addSubIndex("limit", FifoIndexType::make()
    		->setLimit(2) // will policy-delete 2 rows
    	)
    );

Not all index types allow sub-indexes but this method works silently for all of them. Any mistakes will be detected and reported at the initialization time. The sub-indexes may be added only until the index type is initialized. Any modification attempts after that will throw an Exception.

    bool isLeaf() const;

Returns true if this index type has no sub-indexes. Of course, if this type is not initialized yet, more sub-types can be added to it to make it non-leaf later.

    IndexType *findSubIndex(const string &name) const;
    IndexType *findSubIndexById(IndexId it) const;

Find the sub-index by name or id, works in the same way as for TableType. If the index is not found, returns the special value NO_INDEX_TYPE. The idea here is to allow the safe chaining of

    findSubIndex()

for the look-ups of the nested types:

    Autoref it = tt->findSubIndex("level1")->findSubIndex("level2");
    if (it.eq(NO_INDEX_TYPE)) {
    	// not found
    }

If any of the elements in the path are missing, the end result will be

    NO_INDEX_TYPE

, conveniently. But it won't tell you, which one was missing, inconveniently. Even though the object pointed by

    NO_INDEX_TYPE

is a static one, it's safe to use an AutoRef on it, because its reference count gets initialized to 1, and AutoRef would never free it.

    const IndexTypeVec &getSubIndexes() const;

Returns back the whole set of sub-indexes.

    IndexType *getFirstLeaf() const;

Returns the first leaf index type (if a leaf itself, will return itself).

    const NameSet *getKey() const;

Get the key information from the index. It will work with any kind of index, but will return a NULL if the index doesn't support the key. There is no matching method to set it, setting the key is up to the subclasses.

    const NameSet *getKeyExpr() const;

Get the key information from the index in the way it was used to construct the index. So for the Ordered index the names of descending fields will be prepended by a !. It will work with any kind of index, but will return a NULL if the index doesn't support the key.

    IndexType *setAggregator(Onceref agg);
    const AggregatorType *getAggregator() const;

Set or get an aggregator type for this index type. As usual, any setting can be done only until the index type is initialized. Any modification attempts after that will throw an Exception.

    bool isInitialized() const;

Check whether the index type has been initialized. The index types are initialized as a part of the table type initialization, there is no method to initialize an index type directly.

    TableType *getTabtype() const;

Returns the table type, to which this index type is tied. The tying-together happens at the initialization time, so for an initialized index type this method will return NULL.

    IndexType *copy(bool flat = false) const;

Create an un-initialized copy of this index. If

    flat

is

    false

then the copy is done with all the sub-index and aggregator types also copied, but the row types shared. This method is used by

    addSubIndex()

to copy its argument before adding. By the way, the usual copy constructor could theoretically be used on the index types but usually doesn't make a whole lot of a sense because the sub-types and such will end up shared by reference.

If

    flat

is true, the copy is flat, with the sub-index types and aggregator types simply not included. It's the &Cpp; analog of the Perl method

    flatCopy()

. This allows to re-create the partial index hierarchies, recursively flat-copying the parts that need to be included.

    IndexType *deepCopy(HoldRowTypes *holder) const;

Create a deep copy, up to and including the row types. See the &xrsp; and &xrsp; for the detailed explanation. When you deep-copy a table type, the

    holder

argument propagates recurively to all the index types in it.

There are great many more methods on the IndexType, that are used to maintain the index trees, but you don't need to look at them unless you are interested in the inner workings of the Triceps tables.

IndexType
Exception