File tree 3 files changed +44
-0
lines changed
3 files changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -91,6 +91,18 @@ while (rc == TKVDB_OK) {
91
91
cursor->free(cursor);
92
92
```
93
93
94
+ ` while ` loop can be written in alternative way
95
+
96
+ ```
97
+ while (rc == TKVDB_OK) {
98
+ tkvdb_datum key = cursor->key_datum(cursor);
99
+ tkvdb_datum value = cursor->val_datum(cursor);
100
+
101
+ rc = cursor->next(cursor);
102
+ }
103
+ ```
104
+
105
+
94
106
To iterate in reverse order use ` cursor->last() ` and ` cursor->prev() ` .
95
107
96
108
If you want to search a key-value pair in database by prefix use ` cursor->seek(cursor, &key, TKVDB_SEEK) `
Original file line number Diff line number Diff line change @@ -554,6 +554,30 @@ tkvdb_cursor_valsize(tkvdb_cursor *c)
554
554
return cdata -> val_size ;
555
555
}
556
556
557
+ static tkvdb_datum
558
+ tkvdb_cursor_key_datum (tkvdb_cursor * c )
559
+ {
560
+ tkvdb_datum dat ;
561
+ tkvdb_cursor_data * cdata = c -> data ;
562
+
563
+ dat .data = cdata -> prefix ;
564
+ dat .size = cdata -> prefix_size ;
565
+
566
+ return dat ;
567
+ }
568
+
569
+ static tkvdb_datum
570
+ tkvdb_cursor_val_datum (tkvdb_cursor * c )
571
+ {
572
+ tkvdb_datum dat ;
573
+ tkvdb_cursor_data * cdata = c -> data ;
574
+
575
+ dat .data = cdata -> val ;
576
+ dat .size = cdata -> val_size ;
577
+
578
+ return dat ;
579
+ }
580
+
557
581
static int
558
582
tkvdb_cursor_resize_prefix (tkvdb_cursor * c , size_t n , int grow )
559
583
{
@@ -895,6 +919,9 @@ tkvdb_cursor_create(tkvdb_tr *tr)
895
919
c -> val = & tkvdb_cursor_val ;
896
920
c -> valsize = & tkvdb_cursor_valsize ;
897
921
922
+ c -> key_datum = & tkvdb_cursor_key_datum ;
923
+ c -> val_datum = & tkvdb_cursor_val_datum ;
924
+
898
925
c -> free = & tkvdb_cursor_free ;
899
926
900
927
if (trdata -> params .alignval > 1 ) {
Original file line number Diff line number Diff line change @@ -110,6 +110,10 @@ struct tkvdb_cursor
110
110
void * (* val )(tkvdb_cursor * c );
111
111
size_t (* valsize )(tkvdb_cursor * c );
112
112
113
+ /* alternate way to get key/value */
114
+ tkvdb_datum (* key_datum )(tkvdb_cursor * c );
115
+ tkvdb_datum (* val_datum )(tkvdb_cursor * c );
116
+
113
117
TKVDB_RES (* seek )(tkvdb_cursor * c ,
114
118
const tkvdb_datum * key , TKVDB_SEEK seek );
115
119
TKVDB_RES (* first )(tkvdb_cursor * c );
@@ -120,6 +124,7 @@ struct tkvdb_cursor
120
124
121
125
void (* free )(tkvdb_cursor * c );
122
126
127
+
123
128
void * data ;
124
129
};
125
130
You can’t perform that action at this time.
0 commit comments