-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShard.h
136 lines (113 loc) · 3.8 KB
/
Shard.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#pragma once
#include "BloomFilter.h"
#include "KeyValueTypes.h"
#include <optional>
#include <string>
namespace kvs::shard {
using namespace kvs::utils;
/**
* @brief A single part of KVS after sharding. Contains a BloomFilter and loads StorageHashTable into RAM from disk when necessary.
*
* Instantiated via ShardBuilder class static method.
*
*/
class Shard final {
public:
/**
* @brief Get the index of a shard that contains the Key.
*
*/
static shard_index_t getShardIndex(const Key& key) noexcept;
/**
* @brief Read a value from this shard.
*
* @return pair.first - the Entry corresponding to the Key. Used to update the CacheMap. \n pair.second - the actual Value or nothing, if none is present.
*/
std::pair<Entry, std::optional<Value>> readValue(shard_index_t shardIndex,
const Key& key) const;
/**
* @brief Write a Value to this shard.
*
* @return Either the old or the new Entry that corresponds to given Key.
*/
Entry writeValue(shard_index_t shardIndex, const Key& key,
const Value& value);
/**
* @brief Remove a Value from this shard and decrease aliveValuesCnt.
*
* Can handle delayed removals.
*
* @return The new Entry that corresponds to given Key.
*/
Entry removeEntry(shard_index_t shardIndex, const Key& key);
/**
* @brief Push the delayed removal operation onto disk.
*
* Basically same as removeEntry() except this one doesn't decrement the aliveValuesCnt.
*
*/
Entry pushRemoveEntry(shard_index_t shardIndex, const Key& key);
/**
* @brief Read a Value directly from disk storage. Used when CacheMap entry is hit.
*
* Attention: this nethod does not update the internal alive values counter, it has to be done using the increment / decrement methods.
*
* @return The read Value. If the Ptr points to a nonexistent Value, the behavior is undefined.
*/
Value readValueDirectly(shard_index_t shardIndex, Ptr ptr) const;
/**
* @brief Write a Value directly to disk storage. Used when CacheMap entry is hit.
*
* Attention: this nethod does not update the internal alive values counter, it has to be done using the increment / decrement methods.
*
*/
void writeValueDirectly(shard_index_t shardIndex, Ptr ptr,
const Value& value);
/**
* @brief Increase the counter of non-deleted elements in this shard.
*
* Used by KVS when a deleted element in CacheMap is added again.
*
*/
void incrementAliveValuesCnt() noexcept;
/**
* @brief Decrease the counter of non-deleted elements in this shard.
*
* Used by KVS when an entry in CacheMap is marked as deleted.
*
*/
void decrementAliveValuesCnt() noexcept;
/**
* @brief Check if a rebuild needs to be called.
*
*/
bool isRebuildRequired(shard_index_t shardIndex) const;
/**
* @brief The path to the directory that contains the internal files.
*
*/
static std::string storageDirectoryPath;
static std::string getShardDirectoryPath(shard_index_t shardIndex) noexcept;
static std::string getValuesFilePath(shard_index_t shardIndex) noexcept;
static std::string
getStorageHashTableFilePath(shard_index_t shardIndex) noexcept;
private:
/**
* @brief Disallow to create Shard objects with constructors. Use ShardBuilder::createShard instead.
*
*/
explicit Shard() noexcept;
explicit Shard(const std::vector<Entry>& storageHashTableEntries) noexcept;
/**
* @brief Number of values that are stored on disk, but not deleted yet.
*
*/
values_cnt_t aliveValuesCnt;
/**
* @brief A filter.
*
*/
bloom_filter::BloomFilter filter;
friend class ShardBuilder;
};
} // namespace kvs::shard