| 121 | } |
| 122 | |
| 123 | DBlmdb::Cursor::Cursor( |
| 124 | lmdb::Cur &&cur, const std::string &prefix, const std::string &middle, size_t max_key_size, bool forward) |
| 125 | : db_cur(std::move(cur)), prefix(prefix), forward(forward) { |
| 126 | std::string start = prefix + middle; |
| 127 | lmdb::Val itkey(start); |
| 128 | if (forward) |
| 129 | is_end = !db_cur.get(itkey, data, start.empty() ? MDB_FIRST : MDB_SET_RANGE); |
| 130 | else { |
| 131 | if (start.empty()) |
| 132 | is_end = !db_cur.get(itkey, data, MDB_LAST); |
| 133 | else { |
| 134 | if (start.size() < max_key_size) |
| 135 | start += std::string(max_key_size - start.size(), char(0xff)); |
| 136 | itkey = lmdb::Val(start); |
| 137 | is_end = !db_cur.get(itkey, data, MDB_SET_RANGE); |
| 138 | is_end = !db_cur.get(itkey, data, |
| 139 | is_end ? MDB_LAST : MDB_PREV); // If failed to find a key >= prefix, then it should be last in db |
| 140 | } |
| 141 | } |
| 142 | check_prefix(itkey); |
| 143 | } |
| 144 | |
| 145 | void DBlmdb::Cursor::next() { |
| 146 | lmdb::Val itkey; |