-
Notifications
You must be signed in to change notification settings - Fork 4
/
TableHashStore.cpp
97 lines (75 loc) · 2.87 KB
/
TableHashStore.cpp
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
#include "TableHashStore.hpp"
TableHashStore::TableHashStore(const string &tablefile, const string &tmptablefile){
int error;
this->ptable = NULL;
this->ptmptable = NULL;
this->tablefile = tablefile;
this->tmptablefile = tmptablefile;
if (!tablefile.empty()){
this->ptable = table_read(tablefile.c_str(), &error);
if (this->ptable == NULL){
throw TableException("unable to read table file");
}
}
if (!tmptablefile.empty()){
this->ptmptable = table_read(tmptablefile.c_str(), &error);
if (this->ptmptable == NULL){
this->ptmptable = table_alloc(1<<26, &error);
if (this->ptmptable == NULL){
throw TableException("unable to allocate tmp table");
}
}
}
}
TableHashStore::~TableHashStore(){}
void TableHashStore::PutHashKeyValue(const struct ImgHash *pimghash, ClipTBLEntry *pentry, bool overwrite){
int over = (overwrite) ? 1 : 0;
if (ptable)
table_insert_kd(ptable, (void*)pimghash, sizeof(struct ImgHash), (void*)pentry, sizeof(ClipTBLEntry), 0, 0, over);
}
void TableHashStore::GetHashValue(const struct ImgHash *pimghash, ClipTBLEntry **pentry){
int entrySize;
if (ptable)
table_retrieve(ptable, (void*)pimghash, sizeof(struct ImgHash), (void**)pentry, &entrySize);
}
void TableHashStore::PutTmpHashKeyValue(const struct ImgHash *pimghash, TmpClipTBLEntry *pentry, bool overwrite){
int over = (overwrite) ? 1 : 0;
if (ptmptable)
table_insert_kd(ptmptable, (void*)pimghash, sizeof(struct ImgHash), (void*)pentry, sizeof(TmpClipTBLEntry), 0, 0, over);
}
void TableHashStore::GetTmpHashValue(const struct ImgHash *pimghash, TmpClipTBLEntry **pentry){
int entrySize;
if (ptmptable)
table_retrieve(ptmptable, (void*)pimghash, sizeof(struct ImgHash), (void**)pentry, &entrySize);
}
void TableHashStore::DeleteTmpHashValue(const struct ImgHash *pimghash){
if (ptmptable)
table_delete(ptmptable, pimghash, sizeof(struct ImgHash), NULL, NULL);
}
static const string countKey = "NumberEntriesKey";
int64_t TableHashStore::GetCount() {
int64_t *num = NULL;
int size_num;
int error = table_retrieve(ptable, countKey.c_str(),
countKey.length(), (void **)&num, &size_num);
if(error != TABLE_ERROR_NONE && num == NULL
&& size_num != sizeof(int64_t))
return -1;
return *num;
}
int TableHashStore::SetCount(int64_t count) {
int error = table_insert_kd(ptable, countKey.c_str(), countKey.length(),
&count, sizeof(count), 0, 0, 1);
if (error != TABLE_ERROR_NONE)
return -1;
return 0;
}
void TableHashStore::Shutdown(){
int error = TABLE_ERROR_NONE;
if (!tmptablefile.empty() && ptmptable != NULL){
error = table_write(ptmptable, tmptablefile.c_str(), 00755);
}
if (ptable != NULL) table_free(ptable);
if (ptmptable != NULL) table_free(ptmptable);
if (error != TABLE_ERROR_NONE) throw TableException("unable to write tmp table");
}