Skip to content

Commit

Permalink
GO-4350 Ensure concurrency safety
Browse files Browse the repository at this point in the history
  • Loading branch information
fat-fellow committed Oct 24, 2024
1 parent 5ac39d5 commit 9668f1b
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion tantivycontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import "C"
import (
"errors"
"fmt"
"sync"
"unsafe"
)

type TantivyContext struct{ ptr *C.TantivyContext }
type TantivyContext struct {
ptr *C.TantivyContext
lock sync.Mutex // tantivy writer commits should be executed exclusively
}

// NewTantivyContextWithSchema creates a new instance of TantivyContext with the provided schema.
//
Expand Down Expand Up @@ -39,6 +43,8 @@ func NewTantivyContextWithSchema(path string, schema *Schema) (*TantivyContext,
// Returns:
// - error: An error if adding and consuming the documents fails.
func (tc *TantivyContext) AddAndConsumeDocuments(docs ...*Document) error {
tc.lock.Lock()
defer tc.lock.Unlock()
if len(docs) == 0 {
return nil
}
Expand All @@ -63,6 +69,8 @@ func (tc *TantivyContext) AddAndConsumeDocuments(docs ...*Document) error {
// Returns:
// - error: An error if deleting the documents fails.
func (tc *TantivyContext) DeleteDocuments(field string, deleteIds ...string) error {
tc.lock.Lock()
defer tc.lock.Unlock()
if len(deleteIds) == 0 {
return nil
}
Expand Down

0 comments on commit 9668f1b

Please sign in to comment.