symisc / unqlite

An Embedded NoSQL, Transactional Database Engine
https://unqlite.symisc.net
Other
2.11k stars 164 forks source link

Differing initial cursor validity for in-mem vs. on-disk storage (v1.1.6 amalgamation) #27

Closed csaftoiu closed 7 years ago

csaftoiu commented 8 years ago

When using an in-memory database, a just-initialized cursor is valid:

unqlite *pDb;
unqlite_kv_cursor *pCursor;

const char key[] = "thekey";
const char data[] = "thedata";

unqlite_open(&pDb, ":mem:", UNQLITE_OPEN_CREATE);

unqlite_kv_store(pDb, key, sizeof(key), data, sizeof(data));
unqlite_commit(pDb);

unqlite_kv_cursor_init(pDb, &pCursor);

printf("No sought key, valid=%d\n", unqlite_kv_cursor_valid_entry(pCursor));

This prints valid=1, and loading the key gives you "thekey".

However, if it's an on-disk database instead (unqlite_open(&pDb, "some_file.tmp", UNQLITE_OPEN_CREATE)), then the initial cursor is not valid, and points to no element.

I think the behavior should be consistent, and in both cases the cursor should start off invalid. Either that, or specify explicitly in the docs that a just-initialized cursor may be either valid or invalid, and to get an invalid cursor on purpose you have to do something like:

unqlite_kv_cursor_last_entry(pCursor);
unqlite_kv_cursor_next_entry(pCursor);

(Note this may not even work, see #29).

symisc commented 7 years ago

Source Code (Amalgamation & Github) Update with the 1.1.7 release which fixes all the known bugs.

https://unqlite.org/downloads.html