33 #ifndef _IDENTT_STORE_STORE_TABLE_HPP_ 34 #define _IDENTT_STORE_STORE_TABLE_HPP_ 37 #include <google/protobuf/reflection.h> 38 #include <google/protobuf/map.h> 46 using StoreLevel::dbpointer;
47 using KeySetT=std::set<KeyTypeE>;
48 using MapT=google::protobuf::Map<uint64_t,T>;
66 StoreTable(dbpointer trydb,
const KeyTypeE pkey,
const KeySetT ukeys,
const KeySetT ikeys)
104 bool key_found=
false;
105 std::string key,value;
108 if (keytype==PrimaryKey) {
109 key =
GetKey(record,PrimaryKey,
false);
112 }
else if ( unique_keys.find(keytype)!=unique_keys.end() || index_keys.find(keytype)!=index_keys.end()) {
113 s =
getDB()->Get(usemydb::ReadOptions(),
GetKey(record,keytype,
false), &value);
116 if ( node.ParseFromString(value) ) {
117 record->set_id( node.id() );
118 key =
GetKey(record,PrimaryKey,
false);
128 s =
getDB()->Get(usemydb::ReadOptions(), key, &value);
133 if (!record->ParseFromString(value))
161 bool GetMany(T* refer, MapT* records, KeyTypeE keytype,
size_t skip,
size_t limit)
163 if (keytype==PrimaryKey) {
168 if (!key_found)
return false;
169 std::string key,value;
173 for (
size_t i=0; i<nodes.node_size(); ++i) {
174 if (nodes.mutable_node(i)->keytype()!=PrimaryKey)
176 record.set_id( nodes.mutable_node(i)->id() );
177 key =
GetKey(&record,PrimaryKey,
false);
178 s =
getDB()->Get(usemydb::ReadOptions(), key, &value);
181 if (!record.ParseFromString(value))
183 (*records)[record.id()]=record;
205 void AddTrans(TransactionT* trans, std::string key, std::string& value,
bool to_del)
207 TransItemT* item = trans->add_item();
209 if (!to_del) item->set_value(value);
210 item->set_to_del(to_del);
229 virtual std::string
GetKey(T* record, KeyTypeE keytype,
bool pre)=0;
248 if ( unique_keys.find(keytype)==unique_keys.end() && index_keys.find(keytype)==index_keys.end() )
250 std::string key =
GetKey(record,keytype,
false);
252 usemydb::Status s =
getDB()->Get(usemydb::ReadOptions(), key, &value);
253 if (!s.ok())
return false;
254 return node->ParseFromString(value);
272 bool AddRecord(T* record, TransactionT* trans,
bool upsert)
275 if (record->notfound())
return false;
276 if (record->id()==0)
return false;
279 old.set_id(record->id());
280 bool old_record_exists =
GetOne(&old,PrimaryKey);
281 if (old_record_exists && !upsert)
return false;
287 for (
auto& keytype : unique_keys) {
289 if (status && ( record->id() != node.id()) )
return false;
294 node.set_id(record->id());
295 node.set_keytype(PrimaryKey);
296 node.SerializeToString(&dumpid);
299 for (
auto& keytype : unique_keys) {
300 std::string u_new =
GetKey(record,keytype,
false);
301 if (old_record_exists) {
302 std::string u_old =
GetKey(&old,keytype,
false);
313 for (
auto& keytype : index_keys) {
314 std::string u_new =
GetKey(record,keytype,
false);
315 if (old_record_exists) {
316 std::string u_old =
GetKey(&old,keytype,
false);
327 record->SerializeToString(&dumpid);
328 std::string pkey =
GetKey(record,PrimaryKey,
false);
348 if (record->notfound())
return false;
353 for (
auto& keytype : unique_keys) {
356 for (
auto& keytype : index_keys) {
383 bool GetKeysFromIndex(KeyTypeE keytype, T* record, NodeListT* nodes,
size_t skip,
size_t limit)
385 if ( unique_keys.find(keytype)==unique_keys.end() && index_keys.find(keytype)==index_keys.end() )
387 std::string key =
GetKey(record,keytype,
true);
388 if (key.length() <= IDENTT_KEYID_LEN )
return false;
389 std::shared_ptr<usemydb::Iterator> it(
getDB()->NewIterator(usemydb::ReadOptions()));
390 usemydb::Slice start = usemydb::Slice ( key );
391 usemydb::Slice match = usemydb::Slice ( key );
393 bool at_least_one=
false;
394 for (it->Seek(start); it->Valid() && it->key().starts_with(match) ; it->Next()) {
395 if (++counter<=skip)
continue;
396 NodeT* node = nodes->add_node();
397 node->ParseFromString(it->value().ToString());
399 if (counter==skip+limit)
break;
422 void ScanTable(uint64_t startid,
size_t limit, std::function<
void(T*)> Func)
424 std::shared_ptr<usemydb::Iterator> it(
getDB()->NewIterator(usemydb::ReadOptions()));
426 usemydb::Slice match = usemydb::Slice ( keymatch );
427 startid = (startid>0) ? startid : 1;
429 usemydb::Slice start = usemydb::Slice ( keystart );
432 for (it->Seek(start); it->Valid() && it->key().starts_with(match) ; it->Next()) {
434 if (!record.ParseFromString(it->value().ToString()) )
437 if (++counter==limit)
break;
442 const KeyTypeE PrimaryKey;
443 const KeySetT unique_keys;
444 const KeySetT index_keys;
Definition: StoreLevel.hpp:49
bool DelRecord(T *record, TransactionT *trans)
DelRecord : get delete transaction item.
Definition: StoreTable.hpp:346
bool GetOne(T *record, KeyTypeE keytype)
GetOne: get a record by primary or unique key.
Definition: StoreTable.hpp:102
void AddTrans(TransactionT *trans, std::string key, std::string &value, bool to_del)
AddTrans : add transactions.
Definition: StoreTable.hpp:205
bool GetSecondaryValue(T *record, KeyTypeE keytype, NodeT *node)
GetSecondaryValue: get a secondary key value.
Definition: StoreTable.hpp:246
std::string EncodePrimaryKey(KeyTypeE keytype, ::google::protobuf::uint64 id)
EncodePrimaryKey : make primary key with id only.
Definition: StoreBase.cc:124
Definition: StoreTable.hpp:44
bool GetKeysFromIndex(KeyTypeE keytype, T *record, NodeListT *nodes, size_t skip, size_t limit)
GetKeysFromIndex : get primary keys from secondary.
Definition: StoreTable.hpp:383
StoreTable(dbpointer trydb, const KeyTypeE pkey, const KeySetT ukeys, const KeySetT ikeys)
Constructor.
Definition: StoreTable.hpp:66
Definition: CryptoBase.hpp:49
bool AddRecord(T *record, TransactionT *trans, bool upsert)
AddRecord : get transaction item.
Definition: StoreTable.hpp:272
Definition: BaseUtils.hpp:89
std::string EncodeKeyType(KeyTypeE keytype)
EncodeKeyType : make prefix key with keytype.
Definition: StoreBase.cc:113
virtual ~StoreTable()
Destructor.
Definition: StoreTable.hpp:78
virtual std::string GetKey(T *record, KeyTypeE keytype, bool pre)=0
GetKey: get a key.
StoreTable()=delete
make noncopyable and remove default
dbpointer getDB()
getDB: Get shared pointer to DB
Definition: StoreLevel.cc:155
void ScanTable(uint64_t startid, size_t limit, std::function< void(T *)> Func)
ScanTable : scan by primary key.
Definition: StoreTable.hpp:422
Definition: BaseUtils.hpp:95
bool GetMany(T *refer, MapT *records, KeyTypeE keytype, size_t skip, size_t limit)
GetMany: get all values by non unique key.
Definition: StoreTable.hpp:161