Skip to content

Commit 17dd28e

Browse files
committed
Changes for python binding
1 parent 25d448b commit 17dd28e

5 files changed

Lines changed: 137 additions & 133 deletions

File tree

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ test_sqlite_blaster: test_sqlite_blaster.cpp src/*.h
1616

1717
clean:
1818
rm test_sqlite_blaster
19+
rm *.db
20+
rm *.txt
1921

2022
#build/.o: src/imain.cpp src/*.h
2123
# $(CXX) $(CXXFLAGS) $(INCLUDES) -c src/imain.cpp -o build/imain.o

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ Valentine's Day,Comedy,Warner Bros.
215215
- Deletes are not implemented yet. This library is intended primarily for fast inserts.
216216
- Support for concurrent inserts not implemented yet.
217217
- The regular ROWID table of Sqlite is not implemented.
218+
- Only the equivalent of memcmp is used to index records. The order in which keys are ordered may not match with official Sqlite lib for non-ASCII char sets.
218219
- Key lengths are limited depending on page size as shown in the table below. This is just because the source code does not implement support for longer keys. However, this is considered sufficient for most practical purposes.
219220

220221
| **Page Size** | **Max Key Length** |

src/btree_handler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ class btree_handler {
197197
int search_result = traverse_to_leaf(ctx);
198198
if (search_result < 0)
199199
return false;
200+
if (in_size_out_val_len != NULL)
201+
*in_size_out_val_len = key_at_len;
200202
if (val != NULL)
201203
descendant->copy_value(val, in_size_out_val_len);
202204
return true;

src/lru_cache.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class lru_cache {
4949
size_t disk_to_cache_map_size;
5050
dbl_lnklst *llarr;
5151
std::set<int> new_pages;
52-
char filename[100];
52+
std::string filename;
5353
#if USE_FOPEN == 1
5454
FILE *fp;
5555
#else
@@ -197,15 +197,14 @@ if (page_size == 4096) {
197197
lru_cache(int pg_size, int cache_size_kb, const char *fname,
198198
bool (*is_changed)(uint8_t *, int), void (*set_changed)(uint8_t *, int, bool),
199199
int init_page_count = 0, void *(*alloc_fn)(size_t) = NULL)
200-
: is_changed_fn (is_changed), set_changed_fn (set_changed) {
200+
: filename (fname), is_changed_fn (is_changed), set_changed_fn (set_changed) {
201201
if (alloc_fn == NULL)
202202
alloc_fn = malloc;
203203
malloc_fn = alloc_fn;
204204
page_size = pg_size;
205205
cache_size_in_pages = cache_size_kb * 1024 / page_size;
206206
cache_occupied_size = 0;
207207
lnklst_first_entry = lnklst_last_entry = NULL;
208-
strcpy(filename, fname);
209208
page_cache = (uint8_t *) alloc_fn(pg_size * cache_size_in_pages);
210209
root_block = (uint8_t *) alloc_fn(pg_size);
211210
llarr = (dbl_lnklst *) alloc_fn(cache_size_in_pages * sizeof(dbl_lnklst));

src/sqlite_index_blaster.h

Lines changed: 130 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -148,110 +148,6 @@ class sqlite_index_blaster : public btree_handler<sqlite_index_blaster> {
148148
return *ptr;
149149
}
150150

151-
// Reads and returns big-endian uint16_t
152-
// at a given memory location
153-
uint16_t read_uint16(const uint8_t *ptr) {
154-
return (*ptr << 8) + ptr[1];
155-
}
156-
157-
// Reads and returns big-endian int24_t
158-
// at a given memory location
159-
int32_t read_int24(const uint8_t *ptr) {
160-
uint32_t ret;
161-
ret = ((uint32_t)(*ptr & 0x80)) << 24;
162-
ret |= ((uint32_t)(*ptr++ & 0x7F)) << 16;
163-
ret |= ((uint32_t)*ptr++) << 8;
164-
ret += *ptr;
165-
return ret;
166-
}
167-
168-
// Reads and returns big-endian uint24_t
169-
// at a given memory location
170-
uint32_t read_uint24(const uint8_t *ptr) {
171-
uint32_t ret;
172-
ret = ((uint32_t)*ptr++) << 16;
173-
ret += ((uint32_t)*ptr++) << 8;
174-
ret += *ptr;
175-
return ret;
176-
}
177-
178-
// Reads and returns big-endian uint32_t
179-
// at a given memory location
180-
uint32_t read_uint32(const uint8_t *ptr) {
181-
uint32_t ret;
182-
ret = ((uint32_t)*ptr++) << 24;
183-
ret += ((uint32_t)*ptr++) << 16;
184-
ret += ((uint32_t)*ptr++) << 8;
185-
ret += *ptr;
186-
return ret;
187-
}
188-
189-
// Reads and returns big-endian int48_t
190-
// at a given memory location
191-
int64_t read_int48(const uint8_t *ptr) {
192-
uint64_t ret;
193-
ret = ((uint64_t)(*ptr & 0x80)) << 56;
194-
ret |= ((uint64_t)(*ptr++ & 0x7F)) << 48;
195-
ret |= ((uint64_t)*ptr++) << 32;
196-
ret |= ((uint64_t)*ptr++) << 24;
197-
ret |= ((uint64_t)*ptr++) << 16;
198-
ret |= ((uint64_t)*ptr++) << 8;
199-
ret += *ptr;
200-
return ret;
201-
}
202-
203-
// Reads and returns big-endian uint48_t :)
204-
// at a given memory location
205-
uint64_t read_uint48(const uint8_t *ptr) {
206-
uint64_t ret = 0;
207-
int len = 6;
208-
while (len--)
209-
ret += (*ptr++ << (8 * len));
210-
return ret;
211-
}
212-
213-
// Reads and returns big-endian uint64_t
214-
// at a given memory location
215-
uint64_t read_uint64(const uint8_t *ptr) {
216-
uint64_t ret = 0;
217-
int len = 8;
218-
while (len--)
219-
ret += (*ptr++ << (8 * len));
220-
return ret;
221-
}
222-
223-
// Reads and returns variable integer
224-
// from given location as uint16_t
225-
// Also returns the length of the varint
226-
uint16_t read_vint16(const uint8_t *ptr, int8_t *vlen) {
227-
uint16_t ret = 0;
228-
int8_t len = 3; // read max 3 bytes
229-
do {
230-
ret <<= 7;
231-
ret += *ptr & 0x7F;
232-
len--;
233-
} while ((*ptr++ & 0x80) == 0x80 && len);
234-
if (vlen)
235-
*vlen = 3 - len;
236-
return ret;
237-
}
238-
239-
// Reads and returns variable integer
240-
// from given location as uint32_t
241-
// Also returns the length of the varint
242-
uint32_t read_vint32(const uint8_t *ptr, int8_t *vlen) {
243-
uint32_t ret = 0;
244-
int8_t len = 5; // read max 5 bytes
245-
do {
246-
ret <<= 7;
247-
ret += *ptr & 0x7F;
248-
len--;
249-
} while ((*ptr++ & 0x80) == 0x80 && len);
250-
if (vlen)
251-
*vlen = 5 - len;
252-
return ret;
253-
}
254-
255151
// Returns type of column based on given value and length
256152
// See https://www.sqlite.org/fileformat.html#record_format
257153
uint32_t derive_col_type_or_len(int type, const void *val, int len) {
@@ -317,28 +213,6 @@ class sqlite_index_blaster : public btree_handler<sqlite_index_blaster> {
317213
return len;
318214
}
319215

320-
// See .h file for API description
321-
uint32_t derive_data_len(uint32_t col_type_or_len) {
322-
if (col_type_or_len >= 12) {
323-
if (col_type_or_len % 2)
324-
return (col_type_or_len - 13)/2;
325-
return (col_type_or_len - 12)/2;
326-
} else if (col_type_or_len < 10)
327-
return col_data_lens[col_type_or_len];
328-
return 0;
329-
}
330-
331-
// See .h file for API description
332-
uint32_t derive_col_type(uint32_t col_type_or_len) {
333-
if (col_type_or_len >= 12) {
334-
if (col_type_or_len % 2)
335-
return SQLT_TYPE_TEXT;
336-
return SQLT_TYPE_BLOB;
337-
} else if (col_type_or_len < 10)
338-
return col_type_or_len;
339-
return 0;
340-
}
341-
342216
// Initializes the buffer as a B-Tree Leaf Index
343217
void init_bt_idx_interior(uint8_t *ptr) {
344218
ptr[0] = 2; // Interior index b-tree page
@@ -580,10 +454,6 @@ class sqlite_index_blaster : public btree_handler<sqlite_index_blaster> {
580454
| ((int64_t)(bytes & 0x7FFFFF) << (52-23) );
581455
}
582456

583-
double read_double(const uint8_t *ptr) {
584-
return (double) *ptr; // TODO: assuming little endian?
585-
}
586-
587457
int64_t cvt_to_int64(const uint8_t *ptr, int type) {
588458
switch (type) {
589459
case SQLT_TYPE_NULL:
@@ -727,6 +597,136 @@ class sqlite_index_blaster : public btree_handler<sqlite_index_blaster> {
727597
return write_new_rec(-1, 0, col_count, values, value_lens, types, ptr);
728598
}
729599

600+
// Reads and returns big-endian uint16_t
601+
// at a given memory location
602+
uint16_t read_uint16(const uint8_t *ptr) {
603+
return (*ptr << 8) + ptr[1];
604+
}
605+
606+
// Reads and returns big-endian int24_t
607+
// at a given memory location
608+
int32_t read_int24(const uint8_t *ptr) {
609+
uint32_t ret;
610+
ret = ((uint32_t)(*ptr & 0x80)) << 24;
611+
ret |= ((uint32_t)(*ptr++ & 0x7F)) << 16;
612+
ret |= ((uint32_t)*ptr++) << 8;
613+
ret += *ptr;
614+
return ret;
615+
}
616+
617+
// Reads and returns big-endian uint24_t
618+
// at a given memory location
619+
uint32_t read_uint24(const uint8_t *ptr) {
620+
uint32_t ret;
621+
ret = ((uint32_t)*ptr++) << 16;
622+
ret += ((uint32_t)*ptr++) << 8;
623+
ret += *ptr;
624+
return ret;
625+
}
626+
627+
// Reads and returns big-endian uint32_t
628+
// at a given memory location
629+
uint32_t read_uint32(const uint8_t *ptr) {
630+
uint32_t ret;
631+
ret = ((uint32_t)*ptr++) << 24;
632+
ret += ((uint32_t)*ptr++) << 16;
633+
ret += ((uint32_t)*ptr++) << 8;
634+
ret += *ptr;
635+
return ret;
636+
}
637+
638+
// Reads and returns big-endian int48_t
639+
// at a given memory location
640+
int64_t read_int48(const uint8_t *ptr) {
641+
uint64_t ret;
642+
ret = ((uint64_t)(*ptr & 0x80)) << 56;
643+
ret |= ((uint64_t)(*ptr++ & 0x7F)) << 48;
644+
ret |= ((uint64_t)*ptr++) << 32;
645+
ret |= ((uint64_t)*ptr++) << 24;
646+
ret |= ((uint64_t)*ptr++) << 16;
647+
ret |= ((uint64_t)*ptr++) << 8;
648+
ret += *ptr;
649+
return ret;
650+
}
651+
652+
// Reads and returns big-endian uint48_t :)
653+
// at a given memory location
654+
uint64_t read_uint48(const uint8_t *ptr) {
655+
uint64_t ret = 0;
656+
int len = 6;
657+
while (len--)
658+
ret += (*ptr++ << (8 * len));
659+
return ret;
660+
}
661+
662+
// Reads and returns big-endian uint64_t
663+
// at a given memory location
664+
uint64_t read_uint64(const uint8_t *ptr) {
665+
uint64_t ret = 0;
666+
int len = 8;
667+
while (len--)
668+
ret += (*ptr++ << (8 * len));
669+
return ret;
670+
}
671+
672+
// Reads and returns variable integer
673+
// from given location as uint16_t
674+
// Also returns the length of the varint
675+
uint16_t read_vint16(const uint8_t *ptr, int8_t *vlen) {
676+
uint16_t ret = 0;
677+
int8_t len = 3; // read max 3 bytes
678+
do {
679+
ret <<= 7;
680+
ret += *ptr & 0x7F;
681+
len--;
682+
} while ((*ptr++ & 0x80) == 0x80 && len);
683+
if (vlen)
684+
*vlen = 3 - len;
685+
return ret;
686+
}
687+
688+
// Reads and returns variable integer
689+
// from given location as uint32_t
690+
// Also returns the length of the varint
691+
uint32_t read_vint32(const uint8_t *ptr, int8_t *vlen) {
692+
uint32_t ret = 0;
693+
int8_t len = 5; // read max 5 bytes
694+
do {
695+
ret <<= 7;
696+
ret += *ptr & 0x7F;
697+
len--;
698+
} while ((*ptr++ & 0x80) == 0x80 && len);
699+
if (vlen)
700+
*vlen = 5 - len;
701+
return ret;
702+
}
703+
704+
double read_double(const uint8_t *ptr) {
705+
return (double) *ptr; // TODO: assuming little endian?
706+
}
707+
708+
// See .h file for API description
709+
uint32_t derive_data_len(uint32_t col_type_or_len) {
710+
if (col_type_or_len >= 12) {
711+
if (col_type_or_len % 2)
712+
return (col_type_or_len - 13)/2;
713+
return (col_type_or_len - 12)/2;
714+
} else if (col_type_or_len < 10)
715+
return col_data_lens[col_type_or_len];
716+
return 0;
717+
}
718+
719+
// See .h file for API description
720+
uint32_t derive_col_type(uint32_t col_type_or_len) {
721+
if (col_type_or_len >= 12) {
722+
if (col_type_or_len % 2)
723+
return SQLT_TYPE_TEXT;
724+
return SQLT_TYPE_BLOB;
725+
} else if (col_type_or_len < 10)
726+
return col_type_or_len;
727+
return 0;
728+
}
729+
730730
uint8_t *locate_col(int which_col, uint8_t *rec, int& col_type_or_len, int& col_len, int& col_type) {
731731
int8_t vlen;
732732
int hdr_len = read_vint32(rec, &vlen);

0 commit comments

Comments
 (0)