#ifndef DGRIDD_DATASTORE_TABLE_INDEX_H #define DGRIDD_DATASTORE_TABLE_INDEX_H #include #include #include #include "dgrid.h" #include "dgrid_req_structs.h" namespace DGrid { class TableData; class TableRowData; class TableRowKeyItem { public: TableRowKeyItem(const TableRowKeyItem& o); TableRowKeyItem(const std::string& val, const FieldType data_type, const SortOrder sort_order); ~TableRowKeyItem(); std::string val; FieldType data_type; SortOrder sort_order; static bool equal(const FieldType data_type, const std::string& a, const std::string& b); static bool compare(const FieldType data_type, const std::string& a, const std::string& b); inline bool operator==(const TableRowKeyItem& b) const { return equal(data_type, val, b.val); }; inline bool operator==(const std::string& b) const { return equal(data_type, val, b); }; inline bool operator>(const TableRowKeyItem& b) const { return compare(data_type, val, b.val); }; inline bool operator>(const std::string& b) const { return compare(data_type, val, b); }; inline bool operator<(const TableRowKeyItem& b) const { return compare(data_type, b.val, val); }; inline bool operator<(const std::string& b) const { return compare(data_type, b, val); }; template inline bool operator!=(const ITEM& b) const { return !(*this == b); } }; typedef std::vector TableRowKey; struct TableRowKeyHasher { uint64_t operator()(const TableRowKey& k) const; }; // hashed map for row indexing typedef std::unordered_map HashedRows; class DataIndex { public: DataIndex(TableData* table, const TableKeyInfo&); ~DataIndex(); virtual const std::string& type() const = 0; const std::string& name() const; bool checkOrRebuild(); virtual bool rebuild() = 0; virtual void reset() = 0; virtual void removeRow(TableRowData*) = 0; virtual bool search(const std::vector& values, uint64_t& index) = 0; bool isValid(); bool checkComponents(const std::vector& components) const; protected: TableData* table; TableKeyInfo key_info; bool valid; }; typedef std::pair sortedKeyItem; class DataIndex_sorted : public DataIndex { public: DataIndex_sorted(TableData* table, const TableKeyInfo&); ~DataIndex_sorted(); const std::string& type() const override { return _type; }; bool rebuild() override; void reset() override; void removeRow(TableRowData* row) override; bool search(const std::vector& values, uint64_t& index) override; private: std::vector rows_index; bool bsearch(const TableRowKey& key, uint64_t& index); private: static const std::string _type; }; class DataIndex_hashed : public DataIndex { public: DataIndex_hashed(TableData* table, const TableKeyInfo&); ~DataIndex_hashed(); const std::string& type() const override { return _type; }; bool rebuild() override; void reset() override; void removeRow(TableRowData* row) override; bool search(const std::vector& values, uint64_t& index) override; private: HashedRows rows_index; private: static const std::string _type; }; } // namespace DGrid #endif // DGRIDD_DATASTORE_TABLE_INDEX_H