Skip to content

Commit 7e7b570

Browse files
committed
fix: rekey renamed duplicate-path content
Recompute path-scoped hashes when memory_rename_file renames rows with preserve_duplicate_paths=1, and carry the new hash through dbmem_content, dbmem_vault, and dbmem_vault_fts in the same transaction. Reject renames for preserve_duplicate_paths rows created with save_content=0 because the original content is unavailable, so the new path-scoped hash cannot be recomputed safely. Document the save_content interaction, add regression coverage for empty content and indexed vault/FTS rows, and bump SQLITE_DBMEMORY_VERSION to 1.3.4.
1 parent 30d37bc commit 7e7b570

4 files changed

Lines changed: 219 additions & 6 deletions

File tree

‎API.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,9 @@ Renames an indexed file path in memory without reprocessing content.
337337
**Notes:**
338338
- Updates `dbmem_content.path` and keeps any local `dbmem_content_source` metadata attached to the renamed logical path
339339
- It does not rename the file on disk or change the stored `source_path` value
340-
- Does not change `hash`, `value`, embeddings, or FTS entries
340+
- With `preserve_duplicate_paths=1`, recomputes the path-scoped `hash` and updates related embedding and FTS rows
341+
- With `preserve_duplicate_paths=1`, rename requires saved content; if `save_content=0` was used for the row, `memory_rename_file()` returns an error because the path-scoped hash cannot be recomputed safely
342+
- Does not change `value` or stored embedding/FTS content
341343
- Fails if `new_path` already exists because `dbmem_content.path` is unique
342344
- Explicit directory markers can be renamed only to another trailing-slash marker path; this renames only the marker row, not child paths
343345
- Fails if `old_path` matches more than one row across `path` and local `dbmem_content_source.source_path`; pass a unique logical path or exact local source path
@@ -829,7 +831,7 @@ sqlite3_memory_register_provider(db, "my-engine", &provider);
829831
| `max_tokens` | INTEGER | 400 | Maximum tokens per chunk |
830832
| `overlay_tokens` | INTEGER | 80 | Token overlap between consecutive chunks |
831833
| `chars_per_tokens` | INTEGER | 4 | Estimated characters per token |
832-
| `save_content` | INTEGER | 1 | Store original content (1=yes, 0=no) |
834+
| `save_content` | INTEGER | 1 | Store original content (1=yes, 0=no). Required for renaming rows created with `preserve_duplicate_paths=1` |
833835
| `skip_semantic` | INTEGER | 0 | Skip markdown parsing, treat as raw text |
834836
| `skip_html` | INTEGER | 1 | Strip HTML tags when parsing |
835837
| `extensions` | TEXT | "md,mdx" | Comma-separated file extensions to process |

‎src/sqlite-memory.c‎

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,12 +1584,15 @@ static void dbmem_rename_file (sqlite3_context *context, int argc, sqlite3_value
15841584
}
15851585

15861586
sqlite3 *db = sqlite3_context_db_handle(context);
1587+
dbmem_context *ctx = (dbmem_context *)sqlite3_user_data(context);
15871588
const char *old_path = (const char *)sqlite3_value_text(argv[0]);
15881589
const char *new_path = (const char *)sqlite3_value_text(argv[1]);
15891590
uint64_t hash = 0;
1591+
uint64_t new_hash = 0;
15901592
int matches = 0;
15911593
char *resolved_path = NULL;
15921594
char *new_storage_path = NULL;
1595+
const char *result_error = NULL;
15931596

15941597
int rc = dbmem_resolve_content_hash_for_path(db, old_path, &hash, &matches);
15951598
if (rc != SQLITE_OK) {
@@ -1659,11 +1662,66 @@ static void dbmem_rename_file (sqlite3_context *context, int argc, sqlite3_value
16591662
rc = dbmem_database_begin_transaction(db);
16601663
if (rc != SQLITE_OK) goto cleanup;
16611664

1662-
rc = sqlite3_prepare_v2(db, "UPDATE dbmem_content SET path = ?2 WHERE hash = ?1;", -1, &vm, NULL);
1665+
new_hash = hash;
1666+
if (ctx->preserve_duplicate_paths) {
1667+
rc = sqlite3_prepare_v2(db, "SELECT value FROM dbmem_content WHERE hash = ?1;", -1, &vm, NULL);
1668+
if (rc != SQLITE_OK) goto rollback;
1669+
rc = dbmem_bind_hash(vm, 1, hash);
1670+
if (rc != SQLITE_OK) goto rollback;
1671+
rc = sqlite3_step(vm);
1672+
if (rc == SQLITE_ROW) {
1673+
if (sqlite3_column_type(vm, 0) == SQLITE_NULL) {
1674+
result_error = "memory_rename_file cannot rekey preserve_duplicate_paths content when save_content=0";
1675+
sqlite3_finalize(vm);
1676+
vm = NULL;
1677+
rc = SQLITE_ERROR;
1678+
goto rollback;
1679+
}
1680+
const char *value = (const char *)sqlite3_column_text(vm, 0);
1681+
int value_len = sqlite3_column_bytes(vm, 0);
1682+
new_hash = dbmem_storage_hash_compute(value, (size_t)value_len, new_storage_path, true);
1683+
}
1684+
if (rc == SQLITE_ROW || rc == SQLITE_DONE) rc = SQLITE_OK;
1685+
if (rc != SQLITE_OK) goto rollback;
1686+
sqlite3_finalize(vm);
1687+
vm = NULL;
1688+
}
1689+
1690+
if (new_hash != hash) {
1691+
rc = sqlite3_prepare_v2(db, "UPDATE dbmem_vault SET hash = ?2 WHERE hash = ?1;", -1, &vm, NULL);
1692+
if (rc != SQLITE_OK) goto rollback;
1693+
rc = dbmem_bind_hash(vm, 1, hash);
1694+
if (rc != SQLITE_OK) goto rollback;
1695+
rc = dbmem_bind_hash(vm, 2, new_hash);
1696+
if (rc != SQLITE_OK) goto rollback;
1697+
rc = sqlite3_step(vm);
1698+
if (rc == SQLITE_DONE) rc = SQLITE_OK;
1699+
if (rc != SQLITE_OK) goto rollback;
1700+
sqlite3_finalize(vm);
1701+
vm = NULL;
1702+
1703+
if (fts5_is_available) {
1704+
rc = sqlite3_prepare_v2(db, "UPDATE dbmem_vault_fts SET hash = ?2 WHERE hash = ?1;", -1, &vm, NULL);
1705+
if (rc != SQLITE_OK) goto rollback;
1706+
rc = dbmem_bind_hash(vm, 1, hash);
1707+
if (rc != SQLITE_OK) goto rollback;
1708+
rc = dbmem_bind_hash(vm, 2, new_hash);
1709+
if (rc != SQLITE_OK) goto rollback;
1710+
rc = sqlite3_step(vm);
1711+
if (rc == SQLITE_DONE) rc = SQLITE_OK;
1712+
if (rc != SQLITE_OK) goto rollback;
1713+
sqlite3_finalize(vm);
1714+
vm = NULL;
1715+
}
1716+
}
1717+
1718+
rc = sqlite3_prepare_v2(db, "UPDATE dbmem_content SET hash = ?2, path = ?3 WHERE hash = ?1;", -1, &vm, NULL);
16631719
if (rc != SQLITE_OK) goto rollback;
16641720
rc = dbmem_bind_hash(vm, 1, hash);
16651721
if (rc != SQLITE_OK) goto rollback;
1666-
rc = sqlite3_bind_text(vm, 2, new_storage_path, -1, SQLITE_STATIC);
1722+
rc = dbmem_bind_hash(vm, 2, new_hash);
1723+
if (rc != SQLITE_OK) goto rollback;
1724+
rc = sqlite3_bind_text(vm, 3, new_storage_path, -1, SQLITE_STATIC);
16671725
if (rc != SQLITE_OK) goto rollback;
16681726

16691727
rc = sqlite3_step(vm);
@@ -1697,7 +1755,7 @@ static void dbmem_rename_file (sqlite3_context *context, int argc, sqlite3_value
16971755
if (vm) sqlite3_finalize(vm);
16981756
dbmemory_free(resolved_path);
16991757
dbmemory_free(new_storage_path);
1700-
sqlite3_result_error(context, sqlite3_errmsg(db), -1);
1758+
sqlite3_result_error(context, result_error ? result_error : sqlite3_errmsg(db), -1);
17011759
}
17021760

17031761
// MARK: - Path Listing -

‎src/sqlite-memory.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
extern "C" {
2727
#endif
2828

29-
#define SQLITE_DBMEMORY_VERSION "1.3.3"
29+
#define SQLITE_DBMEMORY_VERSION "1.3.4"
3030

3131
// public API
3232
SQLITE_DBMEMORY_API int sqlite3_memory_init (sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi);

‎test/unittest.c‎

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3680,6 +3680,156 @@ TEST(sqlite_memory_add_content_preserves_duplicate_empty_paths_when_enabled) {
36803680
sqlite3_close(db);
36813681
}
36823682

3683+
TEST(sqlite_memory_rename_file_rekeys_preserved_empty_path_hash) {
3684+
sqlite3 *db = open_test_db();
3685+
ASSERT(db != NULL);
3686+
3687+
sqlite3_int64 result = 0;
3688+
int rc = exec_get_int(db, "SELECT memory_set_option('preserve_duplicate_paths', 1);", &result);
3689+
ASSERT_EQ(rc, SQLITE_OK);
3690+
3691+
rc = exec_get_int(db, "SELECT memory_add_content('untitled-1.md', '');", &result);
3692+
ASSERT_EQ(rc, SQLITE_OK);
3693+
3694+
rc = exec_get_int(db, "SELECT memory_rename_file('untitled-1.md', '1.md');", &result);
3695+
ASSERT_EQ(rc, SQLITE_OK);
3696+
ASSERT_EQ(result, 1);
3697+
3698+
rc = exec_get_int(db, "SELECT memory_add_content('untitled-1.md', '');", &result);
3699+
ASSERT_EQ(rc, SQLITE_OK);
3700+
3701+
rc = exec_get_int(db, "SELECT COUNT(*) FROM dbmem_content WHERE path IN ('1.md', 'untitled-1.md');", &result);
3702+
ASSERT_EQ(rc, SQLITE_OK);
3703+
ASSERT_EQ(result, 2);
3704+
3705+
rc = exec_get_int(db, "SELECT COUNT(DISTINCT hash) FROM dbmem_content WHERE path IN ('1.md', 'untitled-1.md');", &result);
3706+
ASSERT_EQ(rc, SQLITE_OK);
3707+
ASSERT_EQ(result, 2);
3708+
3709+
sqlite3_close(db);
3710+
}
3711+
3712+
TEST(sqlite_memory_rename_file_rekeys_preserved_index_hashes) {
3713+
sqlite3 *db = open_test_db();
3714+
ASSERT(db != NULL);
3715+
3716+
dbmem_provider_t prov = { .init = dummy_init, .compute = dummy_compute, .free = dummy_free };
3717+
int rc = sqlite3_memory_register_provider(db, "dummy", &prov);
3718+
ASSERT_EQ(rc, SQLITE_OK);
3719+
3720+
sqlite3_int64 result = 0;
3721+
rc = exec_get_int(db, "SELECT memory_set_model('dummy', 'test-model');", &result);
3722+
ASSERT_EQ(rc, SQLITE_OK);
3723+
3724+
rc = exec_get_int(db, "SELECT memory_set_option('preserve_duplicate_paths', 1);", &result);
3725+
ASSERT_EQ(rc, SQLITE_OK);
3726+
3727+
rc = exec_get_int(db, "SELECT memory_add_content('untitled-1.md', '# Heading\nIndexed body text.');", &result);
3728+
ASSERT_EQ(rc, SQLITE_OK);
3729+
3730+
char old_hash[DBMEM_HASH_STR_MAXLEN];
3731+
rc = exec_get_text(db, "SELECT hash FROM dbmem_content WHERE path = 'untitled-1.md';", old_hash, sizeof(old_hash));
3732+
ASSERT_EQ(rc, SQLITE_OK);
3733+
3734+
char *sql = sqlite3_mprintf("SELECT COUNT(*) FROM dbmem_vault WHERE hash = '%q';", old_hash);
3735+
ASSERT(sql != NULL);
3736+
rc = exec_get_int(db, sql, &result);
3737+
sqlite3_free(sql);
3738+
ASSERT_EQ(rc, SQLITE_OK);
3739+
ASSERT(result > 0);
3740+
3741+
sql = sqlite3_mprintf("SELECT COUNT(*) FROM dbmem_vault_fts WHERE hash = '%q';", old_hash);
3742+
ASSERT(sql != NULL);
3743+
rc = exec_get_int(db, sql, &result);
3744+
sqlite3_free(sql);
3745+
ASSERT_EQ(rc, SQLITE_OK);
3746+
ASSERT(result > 0);
3747+
3748+
rc = exec_get_int(db, "SELECT memory_rename_file('untitled-1.md', '1.md');", &result);
3749+
ASSERT_EQ(rc, SQLITE_OK);
3750+
ASSERT_EQ(result, 1);
3751+
3752+
char new_hash[DBMEM_HASH_STR_MAXLEN];
3753+
rc = exec_get_text(db, "SELECT hash FROM dbmem_content WHERE path = '1.md';", new_hash, sizeof(new_hash));
3754+
ASSERT_EQ(rc, SQLITE_OK);
3755+
ASSERT(strcmp(old_hash, new_hash) != 0);
3756+
3757+
sql = sqlite3_mprintf("SELECT COUNT(*) FROM dbmem_vault WHERE hash = '%q';", old_hash);
3758+
ASSERT(sql != NULL);
3759+
rc = exec_get_int(db, sql, &result);
3760+
sqlite3_free(sql);
3761+
ASSERT_EQ(rc, SQLITE_OK);
3762+
ASSERT_EQ(result, 0);
3763+
3764+
sql = sqlite3_mprintf("SELECT COUNT(*) FROM dbmem_vault WHERE hash = '%q';", new_hash);
3765+
ASSERT(sql != NULL);
3766+
rc = exec_get_int(db, sql, &result);
3767+
sqlite3_free(sql);
3768+
ASSERT_EQ(rc, SQLITE_OK);
3769+
ASSERT(result > 0);
3770+
3771+
sql = sqlite3_mprintf("SELECT COUNT(*) FROM dbmem_vault_fts WHERE hash = '%q';", old_hash);
3772+
ASSERT(sql != NULL);
3773+
rc = exec_get_int(db, sql, &result);
3774+
sqlite3_free(sql);
3775+
ASSERT_EQ(rc, SQLITE_OK);
3776+
ASSERT_EQ(result, 0);
3777+
3778+
sql = sqlite3_mprintf("SELECT COUNT(*) FROM dbmem_vault_fts WHERE hash = '%q';", new_hash);
3779+
ASSERT(sql != NULL);
3780+
rc = exec_get_int(db, sql, &result);
3781+
sqlite3_free(sql);
3782+
ASSERT_EQ(rc, SQLITE_OK);
3783+
ASSERT(result > 0);
3784+
3785+
rc = exec_get_int(db, "SELECT memory_add_content('untitled-1.md', '# Heading\nIndexed body text.');", &result);
3786+
ASSERT_EQ(rc, SQLITE_OK);
3787+
3788+
rc = exec_get_int(db, "SELECT COUNT(*) FROM dbmem_content WHERE path IN ('1.md', 'untitled-1.md');", &result);
3789+
ASSERT_EQ(rc, SQLITE_OK);
3790+
ASSERT_EQ(result, 2);
3791+
3792+
sqlite3_close(db);
3793+
}
3794+
3795+
TEST(sqlite_memory_rename_file_rejects_preserved_path_without_saved_content) {
3796+
sqlite3 *db = open_test_db();
3797+
ASSERT(db != NULL);
3798+
3799+
dbmem_provider_t prov = { .init = dummy_init, .compute = dummy_compute, .free = dummy_free };
3800+
int rc = sqlite3_memory_register_provider(db, "dummy", &prov);
3801+
ASSERT_EQ(rc, SQLITE_OK);
3802+
3803+
sqlite3_int64 result = 0;
3804+
rc = exec_get_int(db, "SELECT memory_set_model('dummy', 'test-model');", &result);
3805+
ASSERT_EQ(rc, SQLITE_OK);
3806+
3807+
rc = exec_get_int(db, "SELECT memory_set_option('preserve_duplicate_paths', 1);", &result);
3808+
ASSERT_EQ(rc, SQLITE_OK);
3809+
rc = exec_get_int(db, "SELECT memory_set_option('save_content', 0);", &result);
3810+
ASSERT_EQ(rc, SQLITE_OK);
3811+
3812+
rc = exec_get_int(db, "SELECT memory_add_content('untitled-1.md', '# Heading\nUnsaved body text.');", &result);
3813+
ASSERT_EQ(rc, SQLITE_OK);
3814+
3815+
sqlite3_stmt *stmt = NULL;
3816+
rc = sqlite3_prepare_v2(db, "SELECT memory_rename_file('untitled-1.md', '1.md');", -1, &stmt, NULL);
3817+
ASSERT_EQ(rc, SQLITE_OK);
3818+
rc = sqlite3_step(stmt);
3819+
ASSERT_EQ(rc, SQLITE_ERROR);
3820+
ASSERT(strstr(sqlite3_errmsg(db), "save_content=0") != NULL);
3821+
sqlite3_finalize(stmt);
3822+
3823+
rc = exec_get_int(db, "SELECT COUNT(*) FROM dbmem_content WHERE path = 'untitled-1.md';", &result);
3824+
ASSERT_EQ(rc, SQLITE_OK);
3825+
ASSERT_EQ(result, 1);
3826+
rc = exec_get_int(db, "SELECT COUNT(*) FROM dbmem_content WHERE path = '1.md';", &result);
3827+
ASSERT_EQ(rc, SQLITE_OK);
3828+
ASSERT_EQ(result, 0);
3829+
3830+
sqlite3_close(db);
3831+
}
3832+
36833833
TEST(sqlite_memory_add_content_keeps_same_empty_path_idempotent_when_preserving_duplicates) {
36843834
sqlite3 *db = open_test_db();
36853835
ASSERT(db != NULL);
@@ -5217,6 +5367,9 @@ int main(int argc, char *argv[]) {
52175367
RUN_TEST(sqlite_memory_preserve_duplicate_paths_option_defaults_to_zero);
52185368
RUN_TEST(sqlite_memory_add_content_stores_empty_content);
52195369
RUN_TEST(sqlite_memory_add_content_preserves_duplicate_empty_paths_when_enabled);
5370+
RUN_TEST(sqlite_memory_rename_file_rekeys_preserved_empty_path_hash);
5371+
RUN_TEST(sqlite_memory_rename_file_rekeys_preserved_index_hashes);
5372+
RUN_TEST(sqlite_memory_rename_file_rejects_preserved_path_without_saved_content);
52205373
RUN_TEST(sqlite_memory_add_content_keeps_same_empty_path_idempotent_when_preserving_duplicates);
52215374
RUN_TEST(sqlite_memory_add_content_requires_preserve_for_directory_marker);
52225375
RUN_TEST(sqlite_memory_add_content_creates_directory_marker);

0 commit comments

Comments
 (0)