GCC Code Coverage Report


Directory: src/
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 0.0% 0 / 0 / 36
Functions: 0.0% 0 / 0 / 8
Branches: 0.0% 0 / 0 / 44

storage/nvm/pet_kv/pet_kv.h
Line Branch Exec Source
1 #pragma once
2
3 #include <stdlib.h>
4
5 #include <algorithm>
6 #include <fstream>
7 #include <memory>
8 #include <string>
9 #include <tuple>
10 #include <utility>
11 #include <vector>
12
13 #include "base/array.h"
14 #include "base/async_time.h"
15 #include "base/base.h"
16 #include "base/hash.h"
17 #include "memory/allocators/persist_loop_slab_allocator.h"
18 #include "memory/shm_file.h"
19 #include "persistence.h"
20 #include "pet_hash.h"
21 #include "shm_common.h"
22
23 namespace base {
24
25 class PetKV {
26 typedef PetHash<uint64, PetKVData, true> ShmKDoubleDict;
27 static constexpr int valid_file_size = 4;
28 inline static const std::string valid_file_tag = "valid tag";
29
30 public:
31 explicit PetKV(const std::string& shm_dir,
32 int64 memory_size,
33 int capacity,
34 int pre_known_value_size = 0);
35 ~PetKV();
36
37 bool Update(uint64 key, const char* log, int log_size);
38
39 PetKVReadData Get(uint64 key) const {
40 PetKVReadData read_data;
41 auto [cache, exists] = dict_->Get(key);
42 if (!exists)
43 return read_data;
44 read_data.expire_timet = cache.expire_timet();
45 read_data.data = shm_malloc_->GetMallocData(cache.shm_malloc_offset());
46 if (pre_known_value_size_ == 0)
47 read_data.size = shm_malloc_->GetMallocSize(cache.shm_malloc_offset());
48 else
49 read_data.size = pre_known_value_size_;
50 return read_data;
51 }
52
53 inline void HintPrefetch(const uint64 key) const { dict_->HintPrefetch(key); }
54
55 bool Valid();
56
57 std::string GetInfo();
58 int64 key_num() const { return dict_->Size(); }
59
60 private:
61 uint64_t start_ts_ = 0;
62
63 ShmKDoubleDict* dict_ = nullptr;
64 MallocApi* shm_malloc_;
65 ShmBaseRecycle* shm_recycle_;
66
67 std::unique_ptr<ShmFile> dict_shm_file_;
68 std::unique_ptr<ShmFile> valid_shm_file_;
69
70 std::string shm_dir_;
71 TimestampGetter ts_getter_ = nullptr;
72
73 // 这个锁是保证写安全
74 base::Lock modify_lock_;
75
76 int pre_known_value_size_ = 0;
77
78 DISALLOW_COPY_AND_ASSIGN(PetKV);
79 };
80
81 class PetMultiKV {
82 public:
83 explicit PetMultiKV(const std::vector<std::string>& shm_dir,
84 int shard_num,
85 int64 shard_memory,
86 int shard_cache_capacity,
87 int pre_known_value_size = 0);
88 explicit PetMultiKV(const std::string& shm_dir,
89 int shard_num,
90 int64 shard_memory,
91 int shard_cache_capacity,
92 int pre_known_value_size = 0)
93 : PetMultiKV(std::vector<std::string>{shm_dir},
94 shard_num,
95 shard_memory,
96 shard_cache_capacity,
97 pre_known_value_size) {}
98 ~PetMultiKV() {
99 for (auto shm_kv : shm_kv_)
100 delete shm_kv;
101 }
102 int GetShard(uint64 key) const {
103 return GetHashWithLevel(key, 1) % shard_num_;
104 }
105
106 bool Update(uint64 key, const char* log, int log_size) {
107 return shm_kv_[GetShard(key)]->Update(key, log, log_size);
108 }
109
110 void BatchGet(base::ConstArray<uint64> keys,
111 std::vector<base::ConstArray<float>>* values) {
112 for (int i = 0; i < keys.Size(); i++) {
113 auto key = keys[i];
114 if (UNLIKELY(i != keys.Size() - 1)) {
115 auto prefetch_key = keys[i + 1];
116 shm_kv_[GetShard(prefetch_key)]->HintPrefetch(prefetch_key);
117 }
118 auto read_data = shm_kv_[GetShard(key)]->Get(key);
119 CHECK_NE(read_data.size, 0);
120 values->emplace_back(
121 (float*)read_data.data, read_data.size / sizeof(float));
122 }
123 }
124
125 PetKVReadData Get(uint64 key) const {
126 return shm_kv_[GetShard(key)]->Get(key);
127 }
128
129 std::string GetInfo();
130
131 int64 key_num() const {
132 int64 key_num = 0;
133 for (auto shm_kv : shm_kv_)
134 key_num += shm_kv->key_num();
135 return key_num;
136 }
137
138 int shard_num() const { return shard_num_; }
139 const std::string& shm_dir() const { return shm_dir_.front(); }
140
141 private:
142 std::string shm_dir(int shard_id);
143 void LoadShard(int shard);
144
145 std::vector<std::string> shm_dir_;
146 int shard_num_;
147 int64 shard_memory_;
148 int shard_cache_capacity_;
149 std::vector<PetKV*> shm_kv_;
150 int pre_known_value_size_ = 0;
151 DISALLOW_COPY_AND_ASSIGN(PetMultiKV);
152 };
153
154 } // namespace base
155