11use log:: debug;
22use serde_json:: { to_string, Value } ;
33use std:: io:: { Error , ErrorKind } ;
4- use std:: sync:: RwLock ;
54use tantivy:: aggregation:: agg_req:: Aggregations ;
65use tantivy:: aggregation:: agg_result:: AggregationResults ;
76use 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
482436fn 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
502454fn 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.
788738fn 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 (
0 commit comments