2 * Copyright (C) 2017 Cisco Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 // @author Changxue Deng <chadeng@cisco.com>
27 #include "rollable_file.h"
29 #include "lock_free.h"
35 typedef struct _AsyncNode AsyncNode;
39 // This is the work horse class for basic db operations (add, find and remove).
40 class Dict : public DRMBase
43 Dict(const std::string &mbdir, bool init_header, int datasize,
44 int db_options, size_t memsize_index, size_t memsize_data,
45 uint32_t block_sz_index, uint32_t block_sz_data,
46 int max_num_index_blk, int max_num_data_blk,
47 int64_t entry_per_bucket, uint32_t queue_size);
51 // Called by writer only
52 int Init(uint32_t id);
54 int Add(const uint8_t *key, int len, MBData &data, bool overwrite);
56 int Find(const uint8_t *key, int len, MBData &data);
57 // Find value by key using prefix match
58 int FindPrefix(const uint8_t *key, int len, MBData &data);
59 // Delete entry by key
60 int Remove(const uint8_t *key, int len);
61 // Delete entry by key
62 int Remove(const uint8_t *key, int len, MBData &data);
68 // multiple-process updates using shared memory queue
69 int SHMQ_Add(const char *key, int key_len, const char *data, int data_len,
71 int SHMQ_Remove(const char *key, int len);
73 int SHMQ_Backup(const char *backup_dir);
74 int SHMQ_CollectResource(int64_t m_index_rc_size, int64_t m_data_rc_size,
75 int64_t max_dbsz, int64_t max_dbcnt);
76 bool SHMQ_Busy() const;
79 void ReserveData(const uint8_t* buff, int size, size_t &offset);
80 void WriteData(const uint8_t *buff, unsigned len, size_t offset) const;
82 // Print dictinary stats
83 void PrintStats(std::ostream *out_stream) const;
84 void PrintStats(std::ostream &out_stream) const;
86 int64_t Count() const;
87 size_t GetRootOffset() const;
88 size_t GetStartDataOffset() const;
90 DictMem *GetMM() const;
92 LockFree* GetLockFreePtr();
94 // Used for DB iterator
95 int ReadNextEdge(const uint8_t *node_buff, EdgePtrs &edge_ptrs, int &match,
96 MBData &data, std::string &match_str, size_t &node_off,
97 bool rd_kv = true) const;
98 int ReadNode(size_t node_off, uint8_t *node_buff, EdgePtrs &edge_ptrs,
99 int &match, MBData &data, bool rd_kv = true) const;
100 void ReadNodeHeader(size_t node_off, int &node_size, int &match,
101 size_t &data_offset, size_t &data_link_offset);
102 int ReadRootNode(uint8_t *node_buff, EdgePtrs &edge_ptrs, int &match,
105 // Shared memory objects
106 int InitShmObjects();
107 pthread_rwlock_t* GetShmLockPtrs() const;
109 void UpdateNumReader(int delta) const;
110 int UpdateNumWriter(int delta) const;
112 void ResetSlidingWindow() const;
114 int ExceptionRecovery();
117 int Find_Internal(size_t root_off, const uint8_t *key, int len, MBData &data);
118 int FindPrefix_Internal(size_t root_off, const uint8_t *key, int len, MBData &data);
119 int ReleaseBuffer(size_t offset);
120 int UpdateDataBuffer(EdgePtrs &edge_ptrs, bool overwrite, const uint8_t *buff,
121 int len, bool &inc_count);
122 int ReadDataFromEdge(MBData &data, const EdgePtrs &edge_ptrs) const;
123 int ReadDataFromNode(MBData &data, const uint8_t *node_ptr) const;
124 int DeleteDataFromEdge(MBData &data, EdgePtrs &edge_ptrs);
125 int ReadNodeMatch(size_t node_off, int &match, MBData &data) const;
127 int SHMQ_PrepareSlot(AsyncNode *node_ptr) const;
128 AsyncNode* SHMQ_AcquireSlot() const;
131 // DB access permission
141 size_t reader_rc_off;