github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/c-deps/libroach/db.h (about) 1 // Copyright 2017 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 #pragma once 12 13 #include <libroach.h> 14 #include <memory> 15 #include <rocksdb/comparator.h> 16 #include <rocksdb/db.h> 17 #include <rocksdb/env.h> 18 #include <rocksdb/iterator.h> 19 #include <rocksdb/metadata.h> 20 #include <rocksdb/status.h> 21 #include <rocksdb/write_batch.h> 22 23 namespace cockroach { 24 25 struct EnvManager; 26 27 typedef rocksdb::Status(DBOpenHook)(std::shared_ptr<rocksdb::Logger> info_log, 28 const std::string& db_dir, const DBOptions opts, 29 EnvManager* env_mgr); 30 31 DBOpenHook DBOpenHookOSS; 32 33 // ToDBSlice returns a DBSlice from a rocksdb::Slice 34 inline DBSlice ToDBSlice(const rocksdb::Slice& s) { 35 DBSlice result; 36 result.data = const_cast<char*>(s.data()); 37 result.len = s.size(); 38 return result; 39 } 40 41 inline DBSlice ToDBSlice(const DBString& s) { 42 DBSlice result; 43 result.data = s.data; 44 result.len = s.len; 45 return result; 46 } 47 48 // ToDBString converts a rocksdb::Slice to a DBString. 49 inline DBString ToDBString(const rocksdb::Slice& s) { 50 DBString result; 51 result.len = s.size(); 52 result.data = static_cast<char*>(malloc(result.len)); 53 memcpy(result.data, s.data(), s.size()); 54 return result; 55 } 56 57 // ToDBKey converts a rocksb::Slice to a DBKey. 58 DBKey ToDBKey(const rocksdb::Slice& s); 59 60 // ToString converts a DBSlice/DBString to a C++ string. 61 inline std::string ToString(DBSlice s) { return std::string(s.data, s.len); } 62 inline std::string ToString(DBString s) { return std::string(s.data, s.len); } 63 64 // ToSlice converts a DBSlice/DBString to a rocksdb::Slice. 65 // Unfortunately, rocksdb::Slice represents empty slice with an 66 // empty string (rocksdb::Slice("", 0)), as opposed to nullptr. 67 // This is problematic since rocksdb has various 68 // assertions checking for slice.data != nullptr. 69 inline rocksdb::Slice ToSlice(DBSlice s) { return rocksdb::Slice(s.data == nullptr ? "" : s.data, s.len); } 70 inline rocksdb::Slice ToSlice(DBString s) { return rocksdb::Slice(s.data == nullptr ? "" : s.data, s.len); } 71 72 // MVCCComputeStatsInternal returns the mvcc stats of the data in an iterator. 73 // Stats are only computed for keys between the given range. 74 MVCCStatsResult MVCCComputeStatsInternal(::rocksdb::Iterator* const iter_rep, DBKey start, 75 DBKey end, int64_t now_nanos); 76 77 // ScopedStats wraps an iterator and, if that iterator has the stats 78 // member populated, aggregates a subset of the RocksDB perf counters 79 // into it (while the ScopedStats is live). 80 class ScopedStats { 81 public: 82 ScopedStats(DBIterator*); 83 ~ScopedStats(); 84 85 private: 86 DBIterator* const iter_; 87 uint64_t internal_delete_skipped_count_base_; 88 }; 89 90 // BatchSStables batches the supplied sstable metadata into chunks of 91 // sstables that are target_size. An empty start or end key indicates 92 // that the a compaction from the beginning (or end) of the key space 93 // should be provided. The sstable metadata must already be sorted by 94 // smallest key. 95 void BatchSSTablesForCompaction(const std::vector<rocksdb::SstFileMetaData>& sst, 96 rocksdb::Slice start_key, rocksdb::Slice end_key, 97 uint64_t target_size, std::vector<rocksdb::Range>* ranges); 98 99 } // namespace cockroach