Skip to content

Commit bf33454

Browse files
committedJan 21, 2022
findKey(): reuse last accessed hash table info if possible
1 parent eb88c98 commit bf33454

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed
 

‎library.properties

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
name=uCDB
2-
version=0.5.2
2+
version=0.5.3
33
author=Ioulianos Kakoulidis
44
maintainer=Ioulianos Kakoulidis <ioulianos.kakoulidis@hotmail.com>
55
sentence=API for querying Constant DataBase file store.
66
paragraph=Simple and fast solution for constant (key, value) dictionary. Supports files up to 4 gigabyte.
77
category=Data Storage
88
url=https://github.com/JulStrat/uCDB
9-
architectures=*
9+
architectures=*
10+
depends=SD

‎src/uCDB.hpp

+18-7
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848
4949
*/
5050

51+
#define UCDB_VERSION_MAJOR 0
52+
#define UCDB_VERSION_MINOR 5
53+
#define UCDB_VERSION_PATCH 3
54+
5155
#ifdef TRACE_CDB
5256
#ifndef TracePrinter
5357
#define TracePrinter Serial
@@ -144,6 +148,7 @@ class uCDB
144148
unsigned long dataEndPos; ///< Data end position
145149
unsigned long slotsNum; ///< Total slots number in CDB.
146150

151+
unsigned int hashTabID_; ///< Last accessed hash table
147152
/// @name Hash table descriptor (HEADER section)
148153
/// @{
149154
unsigned long hashTabStartPos; ///< Hash table position
@@ -201,6 +206,7 @@ cdbResult uCDB<TFileSystem, TFile>::open(const char *fileName, unsigned long (*u
201206
// Close previously opened CDB file
202207
// State - CDB_CLOSED
203208
close();
209+
hashTabID_ = -1;
204210

205211
if (!fs_.exists(fileName)) {
206212
return CDB_NOT_FOUND;
@@ -263,6 +269,7 @@ cdbResult uCDB<TFileSystem, TFile>::open(const char *fileName, unsigned long (*u
263269
template <class TFileSystem, class TFile>
264270
cdbResult uCDB<TFileSystem, TFile>::findKey(const void *key, unsigned long keyLen) {
265271
byte buff[CDB_DESCRIPTOR_SIZE];
272+
unsigned int hashTabID;
266273

267274
zero();
268275
// Check CDB state
@@ -274,17 +281,21 @@ cdbResult uCDB<TFileSystem, TFile>::findKey(const void *key, unsigned long keyLe
274281
;
275282
}
276283

277-
keyHash = hashFunc(key, keyLen);
278284
key_ = static_cast<const byte *>(key);
279285
keyLen_ = keyLen;
286+
keyHash = hashFunc(key, keyLen);
287+
hashTabID = keyHash & 255;
288+
289+
if (hashTabID != hashTabID_) {
290+
if (!readDescriptor<TFile>(cdb, buff, hashTabID << 3)) {
291+
RETURN(state = FILE_ERROR, FILE_ERROR);
292+
}
280293

281-
if (!readDescriptor<TFile>(cdb, buff, (keyHash & 255) << 3)) {
282-
RETURN(state = FILE_ERROR, FILE_ERROR);
294+
hashTabStartPos = unpack(buff);
295+
hashTabSlotsNum = unpack(buff + 4);
296+
hashTabEndPos = hashTabStartPos + hashTabSlotsNum * CDB_DESCRIPTOR_SIZE;
297+
hashTabID_ = hashTabID;
283298
}
284-
285-
hashTabStartPos = unpack(buff);
286-
hashTabSlotsNum = unpack(buff + 4);
287-
hashTabEndPos = hashTabStartPos + hashTabSlotsNum * CDB_DESCRIPTOR_SIZE;
288299
slotsToScan = hashTabSlotsNum;
289300
nextSlotPos = hashTabStartPos + ((keyHash >> 8) % hashTabSlotsNum) * 8;
290301

0 commit comments

Comments
 (0)
Please sign in to comment.