Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
CascadingRadium committed Jul 30, 2024
1 parent c677ab7 commit 2ac9741
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 33 deletions.
16 changes: 3 additions & 13 deletions index.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,11 @@ func (b *Batch) Index(id string, data interface{}) error {
if id == "" {
return ErrorEmptyID
}
// notify handlers, if defined, that an index operation is about to be performed
// and that it would require additional memory estimated to be equal to the size
// of the last document indexed.
intIndex, err := b.index.Advanced()
if err == nil {
if eventIndex, ok := intIndex.(index.EventIndex); ok {
eventIndex.OnIndexStart()
defer func() {
eventIndex.OnIndex()
}()
}
if eventIndex, ok := b.index.(index.EventIndex); ok {
eventIndex.FireEvent(index.EventKindIndex)
}

doc := document.NewDocument(id)
err = b.index.Mapping().MapDocument(doc, data)
err := b.index.Mapping().MapDocument(doc, data)
if err != nil {
return err
}
Expand Down
3 changes: 0 additions & 3 deletions index/scorch/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,3 @@ var EventKindPreMergeCheck = EventKind(9)
// EventKindIndexStart is fired when Index() is invoked which
// creates a new Document object from an interface using the index mapping.
var EventKindIndexStart = EventKind(10)

// EventKindIndex is fired when Index() completes.
var EventKindIndex = EventKind(11)
18 changes: 12 additions & 6 deletions index/scorch/scorch.go
Original file line number Diff line number Diff line change
Expand Up @@ -878,10 +878,16 @@ func (s *Scorch) CopyReader() index.CopyReader {
return rv
}

func (s *Scorch) OnIndexStart() {
s.fireEvent(EventKindIndexStart, 0)
}

func (s *Scorch) OnIndex() {
s.fireEvent(EventKindIndex, 0)
// external API to fire a scorch event externally from bleve
func (s *Scorch) FireEvent(eventKind index.EventKind) {

Check failure on line 882 in index/scorch/scorch.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, ubuntu-latest)

undefined: index.EventKind

Check failure on line 882 in index/scorch/scorch.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, macos-latest)

undefined: index.EventKind

Check failure on line 882 in index/scorch/scorch.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, ubuntu-latest)

undefined: index.EventKind

Check failure on line 882 in index/scorch/scorch.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, macos-latest)

undefined: index.EventKind

Check failure on line 882 in index/scorch/scorch.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

undefined: index.EventKind

Check failure on line 882 in index/scorch/scorch.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, macos-latest)

undefined: index.EventKind
// switch on bleve the event kind and fire the corresponding scorch event
switch eventKind {
case index.EventKindIndex:

Check failure on line 885 in index/scorch/scorch.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, ubuntu-latest)

undefined: index.EventKindIndex

Check failure on line 885 in index/scorch/scorch.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, macos-latest)

undefined: index.EventKindIndex

Check failure on line 885 in index/scorch/scorch.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, ubuntu-latest)

undefined: index.EventKindIndex

Check failure on line 885 in index/scorch/scorch.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, macos-latest)

undefined: index.EventKindIndex

Check failure on line 885 in index/scorch/scorch.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

undefined: index.EventKindIndex

Check failure on line 885 in index/scorch/scorch.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, macos-latest)

undefined: index.EventKindIndex
// bleve is about to index a document using the Index() API
// fire the corresponding scorch event
s.fireEvent(EventKindIndexStart, 0)
default:
// do nothing
return
}
}
25 changes: 14 additions & 11 deletions index_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,17 +255,7 @@ func (i *indexImpl) Index(id string, data interface{}) (err error) {
if !i.open {
return ErrorIndexClosed
}

// notify handlers, if defined, that an index operation is about to be performed
intIndex, err := i.Advanced()
if err == nil {
if eventIndex, ok := intIndex.(index.EventIndex); ok {
eventIndex.OnIndexStart()
defer func() {
eventIndex.OnIndex()
}()
}
}
i.FireEvent(index.EventKindIndex)
doc := document.NewDocument(id)
err = i.m.MapDocument(doc, data)
if err != nil {
Expand Down Expand Up @@ -1122,3 +1112,16 @@ func (f FileSystemDirectory) GetWriter(filePath string) (io.WriteCloser,
return os.OpenFile(filepath.Join(string(f), dir, file),
os.O_RDWR|os.O_CREATE, 0600)
}

func (i *indexImpl) FireEvent(eventKind index.EventKind) {
// get the internal index implementation
intIndex, err := i.Advanced()
if err != nil {
return
}
// check if the internal index implementation supports events
if eventIntIndex, ok := intIndex.(index.EventIndex); ok {
// fire the event
eventIntIndex.FireEvent(eventKind)
}
}

0 comments on commit 2ac9741

Please sign in to comment.