Skip to content

Commit 80578ff

Browse files
committed
Converted INTEGER primary key to TEXT
1 parent 1e011b7 commit 80578ff

6 files changed

Lines changed: 1399 additions & 142 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ This makes all sync functions safe to call repeatedly - for example, on a cron s
168168

169169
Multiple agents can share and merge knowledge without any coordination. Each agent works independently with its own local SQLite database, syncing through a shared [SQLiteCloud](https://sqlitecloud.io/) managed database when connectivity is available.
170170

171+
> Upgrade note: if sync was enabled with a sqlite-memory version earlier than `1.0.0`, you must set it up again from scratch after upgrading. Version `1.0.0` changed the internal table declarations used by sync, so existing pre-`1.0.0` synced databases are not compatible with the new schema.
172+
171173
Enable sync on a database connection before ingesting content:
172174

173175
```sql

src/dbmem-search.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "sqlite3.h"
1616
#endif
1717

18+
#include <stdlib.h>
1819
#include <string.h>
1920
#include <stdio.h>
2021
#include <float.h>
@@ -266,10 +267,12 @@ static void vMemorySearchUpdateAccess(sqlite3 *db, vMemorySearchCursor *c) {
266267
if (rc != SQLITE_OK) return;
267268

268269
sqlite3_int64 now = (sqlite3_int64)time(NULL);
270+
char hash_hex[17];
269271

270272
for (int i = 0; i < c->count; i++) {
273+
snprintf(hash_hex, sizeof(hash_hex), "%016llx", (unsigned long long)(uint64_t)c->hash[i]);
271274
sqlite3_bind_int64(vm, 1, now);
272-
sqlite3_bind_int64(vm, 2, c->hash[i]);
275+
sqlite3_bind_text(vm, 2, hash_hex, -1, SQLITE_STATIC);
273276
sqlite3_step(vm);
274277
sqlite3_reset(vm);
275278
}
@@ -346,8 +349,9 @@ static int dbmem_fts_search (sqlite3 *db, vMemorySearchCursor *c, const char *in
346349
if (rank < rank_min) rank_min = rank;
347350
if (rank > rank_max) rank_max = rank;
348351

352+
const char *hash_text = (const char *)sqlite3_column_text(vm, 1);
349353
c->fts.rank[count] = rank;
350-
c->fts.hash[count] = sqlite3_column_int64(vm, 1);
354+
c->fts.hash[count] = (sqlite3_int64)(uint64_t)strtoull(hash_text ? hash_text : "0", NULL, 16);
351355
c->fts.seq[count] = sqlite3_column_int64(vm, 2);
352356
c->fts.count++;
353357

@@ -408,8 +412,9 @@ static int dbmem_semantic_search (sqlite3 *db, vMemorySearchCursor *c, float *em
408412
else if (rc != SQLITE_ROW) break;
409413

410414
// SQLITE_ROW
415+
const char *hash_text = (const char *)sqlite3_column_text(vm, 1);
411416
c->semantic.rank[count] = sqlite3_column_double(vm, 0);
412-
c->semantic.hash[count] = sqlite3_column_int64(vm, 1);
417+
c->semantic.hash[count] = (sqlite3_int64)(uint64_t)strtoull(hash_text ? hash_text : "0", NULL, 16);
413418
c->semantic.seq[count] = sqlite3_column_int64(vm, 2);
414419
c->semantic.count++;
415420

@@ -528,25 +533,28 @@ static int vMemorySearchCursorColumn (sqlite3_vtab_cursor *cur, sqlite3_context
528533
vMemorySearchCursor *c = (vMemorySearchCursor *)cur;
529534
sqlite3 *db = ((vMemorySearchTable *)cur->pVtab)->db;
530535

536+
char hash_hex[17];
537+
snprintf(hash_hex, sizeof(hash_hex), "%016llx", (unsigned long long)(uint64_t)c->hash[c->index]);
538+
531539
switch (iCol) {
532540
case SEARCH_COLUMN_HASH:
533-
sqlite3_result_int64(context, c->hash[c->index]);
541+
sqlite3_result_text(context, hash_hex, 16, SQLITE_TRANSIENT);
534542
break;
535-
543+
536544
case SEARCH_COLUMN_SEQ:
537545
sqlite3_result_int64(context, c->seq[c->index]);
538546
break;
539-
547+
540548
case SEARCH_COLUMN_RANKING:
541549
sqlite3_result_double(context, c->rank[c->index]);
542550
break;
543-
551+
544552
case SEARCH_COLUMN_PATH:
545553
case SEARCH_COLUMN_SNIPPET:{
546554
const char *sql = (iCol == SEARCH_COLUMN_PATH) ? path_sql : snippet_sql;
547555
sqlite3_stmt *vm = NULL;
548556
if (sqlite3_prepare_v2(db, sql, -1, &vm, NULL) == SQLITE_OK) {
549-
sqlite3_bind_int64(vm, 1, c->hash[c->index]);
557+
sqlite3_bind_text(vm, 1, hash_hex, -1, SQLITE_STATIC);
550558
if (iCol == SEARCH_COLUMN_SNIPPET) sqlite3_bind_int64(vm, 2, c->seq[c->index]);
551559
if (sqlite3_step(vm) == SQLITE_ROW) sqlite3_result_value(context, sqlite3_column_value(vm, 0));
552560
}

0 commit comments

Comments
 (0)