Skip to content

Commit 7b0beea

Browse files
committed
API: valueAvailable() - the number of (value) bytes available for reading, recordsNumber() - total number of records in CDB
1 parent 1c7539e commit 7b0beea

File tree

8 files changed

+49
-11
lines changed

8 files changed

+49
-11
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ int readValue();
1919

2020
int readValue(void *buff, unsigned int byteNum);
2121

22+
unsigned long recordsNumber();
23+
24+
unsigned long valueAvailable();
25+
2226
cdbResult close();
2327
```
2428

examples/airports/airports.ino

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void query(const void *key, unsigned long keyLen) {
6969

7070
void setup() {
7171
const char fileName[] = "airports.cdb";
72-
const char *air[] = {"SBGL", "00AR", "PG-TFI", "US-0480"};
72+
const char *air[] = {"SBGL", "00AR", "PG-TFI", "US-0480", "ZYGH"};
7373

7474
Serial.begin(9600);
7575
while (!Serial) {
@@ -79,6 +79,8 @@ void setup() {
7979
SD.begin(10);
8080

8181
if (ucdb.open(fileName) == CDB_OK) {
82+
Serial.print("Total records number: ");
83+
Serial.println(ucdb.recordsNumber());
8284
// Find some existing codes.
8385
for (unsigned int i = 0; i < sizeof (air) / sizeof (const char *); i++) {
8486
query(air[i], strlen(air[i]));

examples/airports/airports.png

7.14 KB
Loading

examples/benchmark/benchmark.ino

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ void loop() {
4848
return;
4949
}
5050

51+
Serial.print("Total records number: ");
52+
Serial.println(ucdb.recordsNumber());
53+
5154
Serial.println("Querying 1000 random keys from interval [0, 5000000)...");
5255
startMillis = millis();
5356
for (int i = 0; i < 1000; ++i) {
@@ -144,6 +147,8 @@ void loop() {
144147
rt = ucdb.findKey(str, strlen(str));
145148

146149
if (rt == KEY_FOUND) {
150+
Serial.print("Value length in bytes: ");
151+
Serial.println(ucdb.valueAvailable());
147152
br = ucdb.readValue(str, 15);
148153
if (br >= 0) {
149154
str[br] = '\0';

examples/benchmark/benchmark.png

6.25 KB
Loading

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=uCDB
2-
version=0.4.2
2+
version=0.4.3
33
author=Ioulianos Kakoulidis
44
maintainer=Ioulianos Kakoulidis <ioulianos.kakoulidis@hotmail.com>
55
sentence=API for querying Constant DataBase file store.

src/uCDB.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ uCDB::uCDB() {
2323
cdbResult uCDB::open(const char *fileName, unsigned long (*userHashFunc)(const void *key, unsigned long keyLen)) {
2424
unsigned long htPos;
2525
unsigned long htSlotsNum;
26+
27+
unsigned long dend;
28+
unsigned long snum;
29+
2630
byte buff[CDB_DESCRIPTOR_SIZE];
2731

2832
zero();
@@ -43,8 +47,8 @@ cdbResult uCDB::open(const char *fileName, unsigned long (*userHashFunc)(const v
4347
return (state = CDB_ERROR);
4448
}
4549

46-
dataEndPos = cdb.size();
47-
slotsNum = 0;
50+
dend = cdb.size();
51+
snum = 0;
4852

4953
for (unsigned long pos = 0; pos < CDB_HEADER_SIZE; pos += CDB_DESCRIPTOR_SIZE) {
5054
if (!readDescriptor(buff, pos)) {
@@ -65,20 +69,22 @@ cdbResult uCDB::open(const char *fileName, unsigned long (*userHashFunc)(const v
6569
}
6670

6771
// Adjust data end position and total slots number
68-
if (htPos < dataEndPos) {
69-
dataEndPos = htPos;
72+
if (htPos < dend) {
73+
dend = htPos;
7074
}
71-
slotsNum += htSlotsNum;
75+
snum += htSlotsNum;
7276

73-
if (((cdb.size() - dataEndPos) >> 3) < slotsNum) {
77+
if (((cdb.size() - dend) >> 3) < snum) {
7478
return (state = CDB_ERROR); // Critical CDB format or data integrity error
7579
}
7680
}
7781
// Check total
78-
if ((cdb.size() - dataEndPos) != 8 * slotsNum){
82+
if ((cdb.size() - dend) != 8 * snum){
7983
return (state = CDB_ERROR); // Critical CDB format or data integrity error
8084
}
8185

86+
dataEndPos = dend;
87+
slotsNum = snum;
8288
hashFunc = userHashFunc;
8389
return (state = CDB_OK);
8490
}
@@ -219,6 +225,14 @@ int uCDB::readValue(void *buff, unsigned int byteNum) {
219225
return -1;
220226
}
221227

228+
unsigned long uCDB::recordsNumber() {
229+
return (slotsNum >> 1);
230+
}
231+
232+
unsigned long uCDB::valueAvailable() {
233+
return ((state == KEY_FOUND) ? valueBytesAvail : 0);
234+
}
235+
222236
cdbResult uCDB::close() {
223237
zero();
224238
cdb.close();
@@ -267,6 +281,9 @@ bool uCDB::readDescriptor(byte *buff, unsigned long pos) {
267281
}
268282

269283
void uCDB::zero() {
284+
dataEndPos = 0;
285+
slotsNum = 0;
286+
270287
slotsToScan = 0;
271288
nextSlotPos = 0;
272289
}

src/uCDB.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ class uCDB
5959
after successful finKey() or findNextValue() call
6060
*/
6161
int readValue(void *buff, unsigned int byteNum);
62+
63+
/**
64+
Total records number in CDB
65+
*/
66+
unsigned long recordsNumber();
67+
68+
/**
69+
The number of `value' bytes available for reading
70+
*/
71+
unsigned long valueAvailable();
6272

6373
/**
6474
Close CDB
@@ -73,8 +83,8 @@ class uCDB
7383
unsigned long keyLen_;
7484
unsigned long keyHash;
7585

76-
unsigned long dataEndPos;
77-
unsigned long slotsNum;
86+
unsigned long dataEndPos; ///< Data end position
87+
unsigned long slotsNum; ///< Total slots number in CDB.
7888

7989
/// @name Hash table descriptor (HEADER section)
8090
/// @{

0 commit comments

Comments
 (0)