2016-09-08 20:25:52 +08:00
|
|
|
|
2016-08-18 22:34:36 +08:00
|
|
|
#pragma once
|
|
|
|
|
2016-08-25 22:29:45 +08:00
|
|
|
#include <atomic>
|
2016-08-18 22:34:36 +08:00
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
2016-08-25 22:29:45 +08:00
|
|
|
#include "mvcc/id.hpp"
|
|
|
|
|
|
|
|
// #include "storage/indexes/index_record.hpp"
|
2016-11-29 11:08:08 +08:00
|
|
|
#include "logging/loggable.hpp"
|
2016-08-25 22:29:45 +08:00
|
|
|
#include "storage/garbage/delete_sensitive.hpp"
|
2016-09-08 20:25:52 +08:00
|
|
|
#include "storage/indexes/index_definition.hpp"
|
2016-08-18 22:34:36 +08:00
|
|
|
#include "utils/border.hpp"
|
2016-08-31 01:13:23 +08:00
|
|
|
#include "utils/iterator/virtual_iter.hpp"
|
2016-08-18 22:34:36 +08:00
|
|
|
|
2016-08-25 22:29:45 +08:00
|
|
|
template <class TG, class K>
|
2016-08-18 22:34:36 +08:00
|
|
|
class IndexRecord;
|
|
|
|
|
2016-08-25 22:29:45 +08:00
|
|
|
class DbTransaction;
|
|
|
|
class DbAccessor;
|
|
|
|
namespace tx
|
2016-08-18 22:34:36 +08:00
|
|
|
{
|
2016-08-25 22:29:45 +08:00
|
|
|
class Transaction;
|
|
|
|
}
|
2016-08-18 22:34:36 +08:00
|
|
|
|
|
|
|
// Interface for all indexes.
|
2016-08-25 22:29:45 +08:00
|
|
|
// TG type group
|
2016-08-18 22:34:36 +08:00
|
|
|
// K type of key on which records are ordered
|
2016-08-25 22:29:45 +08:00
|
|
|
template <class TG, class K>
|
2016-11-29 11:08:08 +08:00
|
|
|
class IndexBase : public DeleteSensitive, public Loggable
|
2016-08-18 22:34:36 +08:00
|
|
|
{
|
|
|
|
public:
|
2016-08-25 22:29:45 +08:00
|
|
|
// Created with the database
|
2016-11-29 11:08:08 +08:00
|
|
|
IndexBase(IndexDefinition &&it, std::string &&logger_name = "IndexBase");
|
2016-08-25 22:29:45 +08:00
|
|
|
|
2016-11-29 11:08:08 +08:00
|
|
|
IndexBase(IndexDefinition &&it, const tx::Transaction &t,
|
|
|
|
std::string &&logger_name = "IndexBase");
|
2016-08-18 22:34:36 +08:00
|
|
|
|
2016-08-28 22:47:13 +08:00
|
|
|
virtual ~IndexBase(){};
|
|
|
|
|
2016-08-18 22:34:36 +08:00
|
|
|
// Insert's value.
|
|
|
|
// unique => returns false if there is already valid equal value.
|
|
|
|
// nonunique => always succeds.
|
2016-08-25 22:29:45 +08:00
|
|
|
virtual bool insert(IndexRecord<TG, K> &&value) = 0;
|
2016-08-18 22:34:36 +08:00
|
|
|
|
2016-08-23 02:03:45 +08:00
|
|
|
// Returns iterator which returns valid filled records in range.
|
2016-08-18 22:34:36 +08:00
|
|
|
// order==noe => doesn't guarantee any order of returned records.
|
|
|
|
// order==Ascending => guarantees order of returnd records will be from
|
|
|
|
// smallest to largest.
|
|
|
|
// order==Descending => guarantees order of returned records will be from
|
|
|
|
// largest to smallest.
|
|
|
|
// Range must be from<=to
|
2016-08-31 01:13:23 +08:00
|
|
|
virtual iter::Virtual<const typename TG::accessor_t>
|
2016-08-18 22:34:36 +08:00
|
|
|
for_range(DbAccessor &, Border<K> from = Border<K>(),
|
|
|
|
Border<K> to = Border<K>()) = 0;
|
|
|
|
|
|
|
|
// Removes for all transactions obsolete Records.
|
|
|
|
// Cleaner has to call this method when he decideds that it is time for
|
2016-08-30 07:45:07 +08:00
|
|
|
// cleaning. Id must be id of oldest active transaction.
|
|
|
|
virtual void clean(const Id &id) = 0;
|
2016-08-18 22:34:36 +08:00
|
|
|
|
2016-08-25 22:29:45 +08:00
|
|
|
// Activates index for readers.
|
|
|
|
void activate();
|
|
|
|
|
|
|
|
// True if index is ready for reading.
|
|
|
|
bool can_read();
|
|
|
|
|
|
|
|
// True if transaction is obliged to insert T into index.
|
|
|
|
bool is_obliged_to_insert(const tx::Transaction &t);
|
|
|
|
|
2016-09-08 20:25:52 +08:00
|
|
|
IndexType type() const { return it.type; }
|
2016-08-28 22:47:13 +08:00
|
|
|
|
2016-09-08 20:25:52 +08:00
|
|
|
const IndexDefinition &definition() const { return it; }
|
2016-08-25 22:29:45 +08:00
|
|
|
|
2016-11-29 11:08:08 +08:00
|
|
|
protected:
|
|
|
|
Logger logger;
|
|
|
|
|
2016-08-25 22:29:45 +08:00
|
|
|
private:
|
2016-09-08 20:25:52 +08:00
|
|
|
const IndexDefinition it;
|
2016-08-25 22:29:45 +08:00
|
|
|
// Id of transaction which created this index.
|
|
|
|
const Id created;
|
|
|
|
// Active state
|
|
|
|
std::atomic_bool active = {false};
|
2016-08-18 22:34:36 +08:00
|
|
|
};
|