7 #include "db/dbformat.h"
8 #include "rocksdb/iterator.h"
9 #include "rocksdb/slice.h"
10 #include "table/internal_iterator.h"
14 // Iterator over a vector of keys/values
15 class VectorIterator
: public InternalIterator
{
17 VectorIterator(std::vector
<std::string
> keys
, std::vector
<std::string
> values
,
18 const InternalKeyComparator
* icmp
)
19 : keys_(std::move(keys
)),
20 values_(std::move(values
)),
21 indexed_cmp_(icmp
, &keys_
),
22 current_(keys
.size()) {
23 assert(keys_
.size() == values_
.size());
25 indices_
.reserve(keys_
.size());
26 for (size_t i
= 0; i
< keys_
.size(); i
++) {
27 indices_
.push_back(i
);
29 std::sort(indices_
.begin(), indices_
.end(), indexed_cmp_
);
32 virtual bool Valid() const override
{
33 return !indices_
.empty() && current_
< indices_
.size();
36 virtual void SeekToFirst() override
{ current_
= 0; }
37 virtual void SeekToLast() override
{ current_
= indices_
.size() - 1; }
39 virtual void Seek(const Slice
& target
) override
{
40 current_
= std::lower_bound(indices_
.begin(), indices_
.end(), target
,
45 virtual void SeekForPrev(const Slice
& target
) override
{
46 current_
= std::lower_bound(indices_
.begin(), indices_
.end(), target
,
56 virtual void Next() override
{ current_
++; }
57 virtual void Prev() override
{ current_
--; }
59 virtual Slice
key() const override
{
60 return Slice(keys_
[indices_
[current_
]]);
62 virtual Slice
value() const override
{
63 return Slice(values_
[indices_
[current_
]]);
66 virtual Status
status() const override
{ return Status::OK(); }
68 virtual bool IsKeyPinned() const override
{ return true; }
69 virtual bool IsValuePinned() const override
{ return true; }
72 struct IndexedKeyComparator
{
73 IndexedKeyComparator(const InternalKeyComparator
* c
,
74 const std::vector
<std::string
>* ks
)
77 bool operator()(size_t a
, size_t b
) const {
78 return cmp
->Compare((*keys
)[a
], (*keys
)[b
]) < 0;
81 bool operator()(size_t a
, const Slice
& b
) const {
82 return cmp
->Compare((*keys
)[a
], b
) < 0;
85 bool operator()(const Slice
& a
, size_t b
) const {
86 return cmp
->Compare(a
, (*keys
)[b
]) < 0;
89 const InternalKeyComparator
* cmp
;
90 const std::vector
<std::string
>* keys
;
93 std::vector
<std::string
> keys_
;
94 std::vector
<std::string
> values_
;
95 IndexedKeyComparator indexed_cmp_
;
96 std::vector
<size_t> indices_
;
100 } // namespace rocksdb