forked from OpenAtomFoundation/pika
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpika_partition.h
130 lines (104 loc) · 3.04 KB
/
pika_partition.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
// Copyright (c) 2018-present, Qihoo, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
#ifndef PIKA_PARTITION_H_
#define PIKA_PARTITION_H_
#include "pstd/include/scope_record_lock.h"
#include "storage/backupable.h"
#include "storage/storage.h"
#include "include/pika_binlog.h"
class Cmd;
/*
*Keyscan used
*/
struct KeyScanInfo {
time_t start_time = 0;
std::string s_start_time;
int32_t duration = -3;
std::vector<storage::KeyInfo> key_infos; // the order is strings, hashes, lists, zsets, sets
bool key_scaning_ = false;
KeyScanInfo()
: start_time(0),
s_start_time("1970-01-01 08:00:00"),
duration(-3),
key_infos({{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}),
key_scaning_(false) {}
};
struct BgSaveInfo {
bool bgsaving = false;
time_t start_time = 0;
std::string s_start_time;
std::string path;
LogOffset offset;
BgSaveInfo() : bgsaving(false), offset() {}
void Clear() {
bgsaving = false;
path.clear();
offset = LogOffset();
}
};
class Partition : public std::enable_shared_from_this<Partition> {
public:
Partition(const std::string& table_name, uint32_t partition_id, const std::string& table_db_path);
virtual ~Partition();
std::string GetTableName() const;
uint32_t GetPartitionId() const;
std::string GetPartitionName() const;
std::shared_ptr<storage::Storage> db() const;
void Compact(const storage::DataType& type);
void DbRWLockWriter();
void DbRWLockReader();
void DbRWUnLock();
pstd::lock::LockMgr* LockMgr();
void PrepareRsync();
bool TryUpdateMasterOffset();
bool ChangeDb(const std::string& new_path);
void Leave();
void Close();
void MoveToTrash();
// BgSave use;
bool IsBgSaving();
void BgSavePartition();
BgSaveInfo bgsave_info();
// FlushDB & FlushSubDB use
bool FlushDB();
bool FlushSubDB(const std::string& db_name);
// key scan info use
Status GetKeyNum(std::vector<storage::KeyInfo>* key_info);
KeyScanInfo GetKeyScanInfo();
private:
std::string table_name_;
uint32_t partition_id_ = 0;
std::string db_path_;
std::string bgsave_sub_path_;
std::string dbsync_path_;
std::string partition_name_;
bool opened_ = false;
pthread_rwlock_t db_rwlock_;
pstd::lock::LockMgr* lock_mgr_ = nullptr;
std::shared_ptr<storage::Storage> db_;
bool full_sync_ = false;
pstd::Mutex key_info_protector_;
KeyScanInfo key_scan_info_;
/*
* BgSave use
*/
static void DoBgSave(void* arg);
bool RunBgsaveEngine();
bool InitBgsaveEnv();
bool InitBgsaveEngine();
void ClearBgsave();
void FinishBgsave();
BgSaveInfo bgsave_info_;
pstd::Mutex bgsave_protector_;
storage::BackupEngine* bgsave_engine_;
// key scan info use
void InitKeyScan();
/*
* No allowed copy and copy assign
*/
Partition(const Partition&);
void operator=(const Partition&);
};
#endif