Skip to content

Commit 6b1dfca

Browse files
committed
fix: keep preserved duplicate paths idempotent
Keep the stored-hash duplicate check active when preserve_duplicate_paths is enabled so adding the same path/content tuple twice remains a no-op instead of hitting the unique path constraint. Different paths with identical content still receive path-scoped hashes and remain preserved. Added a regression test for repeating memory_add_content('path/api4.md', '') with preserve_duplicate_paths=1.
1 parent 5dd04fc commit 6b1dfca

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/sqlite-memory.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2713,7 +2713,7 @@ static int dbmem_process_buffer (dbmem_context *ctx, const char *buffer, int64_t
27132713
}
27142714
dbmem_database_delete_stale_path(db, ctx->path, hash);
27152715

2716-
if (!ctx->preserve_duplicate_paths && dbmem_database_check_if_stored(ctx->db, hash, len)) {
2716+
if (dbmem_database_check_if_stored(ctx->db, hash, len)) {
27172717
if (ctx->source_path) {
27182718
char *stored_path = dbmem_database_path_for_hash_copy(ctx->db, hash);
27192719
if (!stored_path) {

test/unittest.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3537,6 +3537,30 @@ TEST(sqlite_memory_add_content_preserves_duplicate_empty_paths_when_enabled) {
35373537
sqlite3_close(db);
35383538
}
35393539

3540+
TEST(sqlite_memory_add_content_keeps_same_empty_path_idempotent_when_preserving_duplicates) {
3541+
sqlite3 *db = open_test_db();
3542+
ASSERT(db != NULL);
3543+
3544+
sqlite3_int64 result = 0;
3545+
int rc = exec_get_int(db, "SELECT memory_set_option('preserve_duplicate_paths', 1);", &result);
3546+
ASSERT_EQ(rc, SQLITE_OK);
3547+
3548+
rc = exec_get_int(db, "SELECT memory_add_content('docs/empty-idempotent.md', '');", &result);
3549+
ASSERT_EQ(rc, SQLITE_OK);
3550+
rc = exec_get_int(db, "SELECT memory_add_content('docs/empty-idempotent.md', '');", &result);
3551+
ASSERT_EQ(rc, SQLITE_OK);
3552+
3553+
rc = exec_get_int(db, "SELECT COUNT(*) FROM dbmem_content;", &result);
3554+
ASSERT_EQ(rc, SQLITE_OK);
3555+
ASSERT_EQ(result, 1);
3556+
3557+
rc = exec_get_int(db, "SELECT COUNT(*) FROM dbmem_content WHERE path = 'docs/empty-idempotent.md' AND length = 0;", &result);
3558+
ASSERT_EQ(rc, SQLITE_OK);
3559+
ASSERT_EQ(result, 1);
3560+
3561+
sqlite3_close(db);
3562+
}
3563+
35403564
TEST(sqlite_memory_add_content_preserves_duplicate_nonempty_paths_when_enabled) {
35413565
sqlite3 *db = open_test_db();
35423566
ASSERT(db != NULL);
@@ -4815,6 +4839,7 @@ int main(int argc, char *argv[]) {
48154839
RUN_TEST(sqlite_memory_preserve_duplicate_paths_option_defaults_to_zero);
48164840
RUN_TEST(sqlite_memory_add_content_stores_empty_content);
48174841
RUN_TEST(sqlite_memory_add_content_preserves_duplicate_empty_paths_when_enabled);
4842+
RUN_TEST(sqlite_memory_add_content_keeps_same_empty_path_idempotent_when_preserving_duplicates);
48184843
RUN_TEST(sqlite_memory_add_content_preserves_duplicate_nonempty_paths_when_enabled);
48194844
RUN_TEST(sqlite_memory_add_file_reads_disk_and_stores_context);
48204845
RUN_TEST(sqlite_memory_add_file_stores_empty_file);

0 commit comments

Comments
 (0)