Skip to content
This repository was archived by the owner on Jan 23, 2026. It is now read-only.

Commit 374fb96

Browse files
authored
Remove locking from mgcxx side (#17)
* remove locking from mgcxx side * fix error message
1 parent a01f99c commit 374fb96

3 files changed

Lines changed: 33 additions & 142 deletions

File tree

text_search/src/lib.rs

Lines changed: 33 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use log::debug;
22
use serde_json::{to_string, Value};
33
use std::io::{Error, ErrorKind};
4-
use std::sync::RwLock;
54
use tantivy::aggregation::agg_req::Aggregations;
65
use tantivy::aggregation::agg_result::AggregationResults;
76
use tantivy::aggregation::AggregationCollector;
@@ -95,7 +94,7 @@ pub struct TantivyContext {
9594
pub index_path: std::path::PathBuf,
9695
pub schema: Schema,
9796
pub index: Index,
98-
pub index_writer: RwLock<IndexWriter>,
97+
pub index_writer: IndexWriter,
9998
pub index_reader: IndexReader,
10099
}
101100

@@ -348,7 +347,7 @@ fn create_index(path: &String, config: &ffi::IndexConfig) -> Result<ffi::Context
348347
index_path: path,
349348
schema,
350349
index,
351-
index_writer: RwLock::new(index_writer),
350+
index_writer,
352351
index_reader,
353352
}),
354353
})
@@ -373,44 +372,20 @@ fn add_document(
373372
));
374373
}
375374
};
376-
377-
if skip_commit {
378-
// Use shared (read) lock for just adding the document
379-
let index_writer_guard = context.tantivyContext.index_writer.read().unwrap();
380-
match index_writer_guard.add_document(document) {
381-
Ok(_) => Ok(()),
382-
Err(e) => {
383-
Err(Error::new(
384-
ErrorKind::Other,
385-
format!("Unable to add document -> {}", e),
386-
))
375+
let index_writer = &mut context.tantivyContext.index_writer;
376+
match index_writer.add_document(document) {
377+
Ok(_) => {
378+
if skip_commit {
379+
return Ok(());
380+
} else {
381+
commit(context)
387382
}
388383
}
389-
} else {
390-
// Use exclusive (write) lock for add + commit
391-
let mut index_writer_guard = context.tantivyContext.index_writer.write().unwrap();
392-
match index_writer_guard.add_document(document) {
393-
Ok(_) => {
394-
// Commit while holding the write lock
395-
match index_writer_guard.commit() {
396-
Ok(_) => Ok(()),
397-
Err(e) => {
398-
Err(Error::new(
399-
ErrorKind::Other,
400-
format!(
401-
"Unable to commit text search index at {:?} -> {}",
402-
index_path, e
403-
),
404-
))
405-
}
406-
}
407-
}
408-
Err(e) => {
409-
Err(Error::new(
410-
ErrorKind::Other,
411-
format!("Unable to add document -> {}", e),
412-
))
413-
}
384+
Err(e) => {
385+
return Err(Error::new(
386+
ErrorKind::Other,
387+
format!("Unable to add document -> {}", e),
388+
));
414389
}
415390
}
416391
}
@@ -437,53 +412,30 @@ fn delete_document(
437412
));
438413
}
439414
};
440-
441-
if skip_commit {
442-
// Use shared (read) lock for just deleting the document
443-
let index_writer_guard = context.tantivyContext.index_writer.read().unwrap();
444-
match index_writer_guard.delete_query(query) {
445-
Ok(_) => Ok(()),
446-
Err(e) => {
447-
Err(Error::new(
448-
ErrorKind::Other,
449-
format!("Unable to delete document -> {}", e),
450-
))
415+
let index_writer = &mut context.tantivyContext.index_writer;
416+
match index_writer.delete_query(query) {
417+
Ok(_) => {
418+
if skip_commit {
419+
return Ok(());
420+
} else {
421+
commit(context)
451422
}
452423
}
453-
} else {
454-
// Use exclusive (write) lock for delete + commit
455-
let mut index_writer_guard = context.tantivyContext.index_writer.write().unwrap();
456-
match index_writer_guard.delete_query(query) {
457-
Ok(_) => {
458-
// Commit while holding the write lock
459-
match index_writer_guard.commit() {
460-
Ok(_) => Ok(()),
461-
Err(e) => {
462-
Err(Error::new(
463-
ErrorKind::Other,
464-
format!(
465-
"Unable to commit text search index at {:?} -> {}",
466-
index_path, e
467-
),
468-
))
469-
}
470-
}
471-
}
472-
Err(e) => {
473-
Err(Error::new(
474-
ErrorKind::Other,
475-
format!("Unable to delete document -> {}", e),
476-
))
477-
}
424+
Err(e) => {
425+
return Err(Error::new(
426+
ErrorKind::Other,
427+
format!(
428+
"Unable to delete document from text search index at {:?} -> {}",
429+
index_path, e
430+
),
431+
));
478432
}
479433
}
480434
}
481435

482436
fn commit(context: &mut ffi::Context) -> Result<(), std::io::Error> {
483437
let index_path = &context.tantivyContext.index_path;
484-
// Use exclusive (write) lock for commit operations
485-
let mut index_writer_guard = context.tantivyContext.index_writer.write().unwrap();
486-
match index_writer_guard.commit() {
438+
match context.tantivyContext.index_writer.commit() {
487439
Ok(_) => {
488440
return Ok(());
489441
}
@@ -501,9 +453,7 @@ fn commit(context: &mut ffi::Context) -> Result<(), std::io::Error> {
501453

502454
fn rollback(context: &mut ffi::Context) -> Result<(), std::io::Error> {
503455
let index_path = &context.tantivyContext.index_path;
504-
// Use exclusive (write) lock for rollback operations
505-
let mut index_writer_guard = context.tantivyContext.index_writer.write().unwrap();
506-
match index_writer_guard.rollback() {
456+
match context.tantivyContext.index_writer.rollback() {
507457
Ok(_) => {
508458
return Ok(());
509459
}
@@ -786,9 +736,8 @@ fn get_num_docs(context: &mut ffi::Context) -> Result<u64, std::io::Error> {
786736
/// This will remove the entire directory and all its contents.
787737
/// NOTE: This function takes ownership of the context.
788738
fn drop_index(context: ffi::Context) -> Result<(), std::io::Error> {
789-
// Extract the IndexWriter from the RwLock by taking ownership
790-
let index_writer = context.tantivyContext.index_writer.into_inner().unwrap();
791-
739+
let index_writer = context.tantivyContext.index_writer;
740+
792741
// Wait for all merging threads to finish before dropping the index.
793742
if let Err(e) = index_writer.wait_merging_threads() {
794743
return Err(Error::new(

text_search/test_bench.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include <iostream>
22
#include <memory>
3-
#include <thread>
43

54
#include <benchmark/benchmark.h>
65

text_search/test_unit.cpp

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "gtest/gtest.h"
2-
#include <mutex>
32
#include <thread>
43

54
#include "test_util.hpp"
@@ -116,62 +115,6 @@ TEST(text_search_test_case, mappings) {
116115
}
117116
}
118117

119-
TEST(text_search_test_case, drop_index_stress_test) {
120-
try {
121-
constexpr auto index_name = "tantivy_index_stress_drop";
122-
123-
nlohmann::json mappings = {};
124-
mappings["properties"] = {};
125-
mappings["properties"]["data"] = {
126-
{"type", "text"}, {"stored", true}, {"text", true}, {"fast", true}};
127-
128-
auto context = mgcxx::text_search::create_index(
129-
index_name,
130-
mgcxx::text_search::IndexConfig{.mappings = mappings.dump()});
131-
132-
// Use multiple threads to create maximum merging pressure
133-
constexpr auto thread_count = 10;
134-
constexpr auto docs_per_thread = 50;
135-
{
136-
std::vector<std::jthread> threads;
137-
std::mutex mutex;
138-
threads.reserve(thread_count);
139-
140-
for (auto t = 0; t < thread_count; t++) {
141-
threads.emplace_back([&context, &mutex, t, docs_per_thread]() {
142-
for (auto i = 0; i < docs_per_thread; i++) {
143-
nlohmann::json doc_data = {};
144-
doc_data["data"] = "Thread " + std::to_string(t) + " document " + std::to_string(i) +
145-
" with substantial content to create larger segments that require merging " +
146-
"when multiple threads are adding documents simultaneously creating pressure";
147-
148-
mgcxx::text_search::DocumentInput doc = {
149-
.data = doc_data.dump()
150-
};
151-
152-
std::lock_guard lock(mutex);
153-
// Commit every few documents to create many small segments
154-
bool skip_commit = (i % 9 != 0);
155-
mgcxx::text_search::add_document(context, doc, skip_commit);
156-
}
157-
});
158-
}
159-
}
160-
// Final commit to ensure all documents are processed
161-
mgcxx::text_search::commit(context);
162-
163-
auto num_of_attempts = 10;
164-
while (num_of_attempts > 0 &&
165-
mgcxx::text_search::get_num_docs(context) < thread_count * docs_per_thread) {
166-
std::this_thread::sleep_for(std::chrono::milliseconds(100));
167-
num_of_attempts--;
168-
}
169-
mgcxx::text_search::drop_index(std::move(context));
170-
} catch (const ::rust::Error &error) {
171-
FAIL() << "Stress drop test failed: " << error.what();
172-
}
173-
}
174-
175118
// TODO(gitbuda): Make a gtest main lib and link agains other test binaries.
176119
int main(int argc, char *argv[]) {
177120
// init tantivy engine (actually logging setup, should be called once per

0 commit comments

Comments
 (0)