Skip to content

Commit

Permalink
Add interfaces for searching with index. (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yata committed Dec 1, 2014
1 parent 4c96709 commit c2d3e07
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 55 deletions.
122 changes: 67 additions & 55 deletions include/grnxx/index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,64 +12,52 @@ namespace grnxx {

class Column;

//enum EndPointType {
// INCLUSIVE_END_POINT,
// EXCLUSIVE_END_POINT
//};

//struct EndPoint {
// Datum value;
// EndPointType type;
//};

//class IndexRange {
// public:
// IndexRange()
// : has_lower_bound_(false),
// has_upper_bound_(false),
// lower_bound_(),
// upper_bound_() {}

// bool has_lower_bound() const {
// return has_lower_bound_;
// }
// bool has_upper_bound() const {
// return has_upper_bound_;
// }

// const EndPoint &lower_bound() const {
// return lower_bound_;
// }
// const EndPoint &upper_bound() const {
// return upper_bound_;
// }
enum EndPointType {
INCLUSIVE_END_POINT,
EXCLUSIVE_END_POINT
};

// void set_lower_bound(const Datum &value,
// EndPointType type = INCLUSIVE_END_POINT) {
// has_lower_bound_ = true;
// lower_bound_.value = value;
// lower_bound_.type = type;
// }
// void set_upper_bound(const Datum &value,
// EndPointType type = INCLUSIVE_END_POINT) {
// has_upper_bound_ = true;
// upper_bound_.value = value;
// upper_bound_.type = type;
// }
struct EndPoint {
Datum value;
EndPointType type;

// void unset_lower_bound() {
// has_lower_bound_ = false;
// }
// void unset_upper_bound() {
// has_lower_bound_ = false;
// }
EndPoint() : value(NA()), type(INCLUSIVE_END_POINT) {}
};

// private:
// bool has_lower_bound_;
// bool has_upper_bound_;
// EndPoint lower_bound_;
// EndPoint upper_bound_;
//};
class IndexRange {
public:
IndexRange() = default;
~IndexRange() = default;

const EndPoint &lower_bound() const {
return lower_bound_;
}
const EndPoint &upper_bound() const {
return upper_bound_;
}

void set_lower_bound(const Datum &value,
EndPointType type = INCLUSIVE_END_POINT) {
lower_bound_.value = value;
lower_bound_.type = type;
}
void set_upper_bound(const Datum &value,
EndPointType type = INCLUSIVE_END_POINT) {
upper_bound_.value = value;
upper_bound_.type = type;
}

void unset_lower_bound() {
lower_bound_.value = NA();
}
void unset_upper_bound() {
upper_bound_.value = NA();
}

private:
EndPoint lower_bound_;
EndPoint upper_bound_;
};

enum IndexType {
// TODO: Tree indexes support range search.
Expand Down Expand Up @@ -118,6 +106,30 @@ class Index {
const Datum &value,
const CursorOptions &options = CursorOptions()) const = 0;

// Create a cursor to get records.
//
// On success, returns the cursor.
// On failure, throws an exception.
virtual std::unique_ptr<Cursor> find_in_range(
const IndexRange &range = IndexRange(),
const CursorOptions &options = CursorOptions()) const = 0;

// Create a cursor to get records.
//
// On success, returns the cursor.
// On failure, throws an exception.
virtual std::unique_ptr<Cursor> find_starts_with(
const EndPoint &prefix,
const CursorOptions &options = CursorOptions()) const = 0;

// Create a cursor to get records.
//
// On success, returns the cursor.
// On failure, throws an exception.
virtual std::unique_ptr<Cursor> find_prefixes(
const Datum &datum,
const CursorOptions &options = CursorOptions()) const = 0;

protected:
virtual ~Index() = default;
};
Expand Down
18 changes: 18 additions & 0 deletions lib/grnxx/impl/index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,24 @@ std::unique_ptr<Cursor> Index::find(
throw "Not supported yet"; // TODO
}

std::unique_ptr<Cursor> Index::find_in_range(
const IndexRange &range,
const CursorOptions &options) const {
throw "Not supported yet"; // TODO
}

std::unique_ptr<Cursor> Index::find_starts_with(
const EndPoint &prefix,
const CursorOptions &options) const {
throw "Not supported yet"; // TODO
}

std::unique_ptr<Cursor> Index::find_prefixes(
const Datum &datum,
const CursorOptions &options) const {
throw "Not supported yet"; // TODO
}

Index *Index::create(ColumnBase *column,
const String &name,
IndexType type,
Expand Down
9 changes: 9 additions & 0 deletions lib/grnxx/impl/index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ class Index : public IndexInterface {
virtual std::unique_ptr<Cursor> find(
const Datum &value,
const CursorOptions &options = CursorOptions()) const;
virtual std::unique_ptr<Cursor> find_in_range(
const IndexRange &range = IndexRange(),
const CursorOptions &options = CursorOptions()) const;
virtual std::unique_ptr<Cursor> find_starts_with(
const EndPoint &prefix,
const CursorOptions &options = CursorOptions()) const;
virtual std::unique_ptr<Cursor> find_prefixes(
const Datum &datum,
const CursorOptions &options = CursorOptions()) const;

// -- Internal API --

Expand Down

0 comments on commit c2d3e07

Please sign in to comment.