1 //  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2 //  This source code is licensed under both the GPLv2 (found in the
3 //  COPYING file in the root directory) and Apache 2.0 License
4 //  (found in the LICENSE.Apache file in the root directory).
5 
6 #ifndef ROCKSDB_LITE
7 
8 #include "rocksdb/utilities/debug.h"
9 
10 #include "db/db_impl/db_impl.h"
11 
12 namespace ROCKSDB_NAMESPACE {
13 
GetAllKeyVersions(DB * db,Slice begin_key,Slice end_key,size_t max_num_ikeys,std::vector<KeyVersion> * key_versions)14 Status GetAllKeyVersions(DB* db, Slice begin_key, Slice end_key,
15                          size_t max_num_ikeys,
16                          std::vector<KeyVersion>* key_versions) {
17   if (nullptr == db) {
18     return Status::InvalidArgument("db cannot be null.");
19   }
20   return GetAllKeyVersions(db, db->DefaultColumnFamily(), begin_key, end_key,
21                            max_num_ikeys, key_versions);
22 }
23 
GetAllKeyVersions(DB * db,ColumnFamilyHandle * cfh,Slice begin_key,Slice end_key,size_t max_num_ikeys,std::vector<KeyVersion> * key_versions)24 Status GetAllKeyVersions(DB* db, ColumnFamilyHandle* cfh, Slice begin_key,
25                          Slice end_key, size_t max_num_ikeys,
26                          std::vector<KeyVersion>* key_versions) {
27   if (nullptr == db) {
28     return Status::InvalidArgument("db cannot be null.");
29   }
30   if (nullptr == cfh) {
31     return Status::InvalidArgument("Column family handle cannot be null.");
32   }
33   if (nullptr == key_versions) {
34     return Status::InvalidArgument("key_versions cannot be null.");
35   }
36   key_versions->clear();
37 
38   DBImpl* idb = static_cast<DBImpl*>(db->GetRootDB());
39   auto icmp = InternalKeyComparator(idb->GetOptions(cfh).comparator);
40   ReadOptions read_options;
41   ReadRangeDelAggregator range_del_agg(&icmp,
42                                        kMaxSequenceNumber /* upper_bound */);
43   Arena arena;
44   ScopedArenaIterator iter(idb->NewInternalIterator(
45       read_options, &arena, &range_del_agg, kMaxSequenceNumber, cfh));
46 
47   if (!begin_key.empty()) {
48     InternalKey ikey;
49     ikey.SetMinPossibleForUserKey(begin_key);
50     iter->Seek(ikey.Encode());
51   } else {
52     iter->SeekToFirst();
53   }
54 
55   size_t num_keys = 0;
56   for (; iter->Valid(); iter->Next()) {
57     ParsedInternalKey ikey;
58     Status pik_status =
59         ParseInternalKey(iter->key(), &ikey, true /* log_err_key */);  // TODO
60     if (!pik_status.ok()) {
61       return pik_status;
62     }
63 
64     if (!end_key.empty() &&
65         icmp.user_comparator()->Compare(ikey.user_key, end_key) > 0) {
66       break;
67     }
68 
69     key_versions->emplace_back(ikey.user_key.ToString() /* _user_key */,
70                                iter->value().ToString() /* _value */,
71                                ikey.sequence /* _sequence */,
72                                static_cast<int>(ikey.type) /* _type */);
73     if (++num_keys >= max_num_ikeys) {
74       break;
75     }
76   }
77   return Status::OK();
78 }
79 
80 }  // namespace ROCKSDB_NAMESPACE
81 
82 #endif  // ROCKSDB_LITE
83