@@ -12,16 +12,26 @@ import (
1212// Implementations might differ in how the index is created and how the
1313// index is gotten (reused, created on the fly, etc).
1414//
15+ // The GetIndex method returns a function that must be called to close the index.
1516// Some implementations might require the index to be kept opened, meaning
1617// the index should be closed only when the application is shutting down. In
17- // this case, IndexCanBeClosed should return false. If the index can be
18- // closed and reopened safely at any time, IndexCanBeClosed should
19- // return true.
18+ // this case, the returned function to close the index should do nothing (not
19+ // closing the index). If the index can be closed and reopened safely at any
20+ // time, the returned function should close the index.
21+ // Calling the returned function to close the index is fine regardless of the
22+ // implementation, and it will act as a no-op if the index should be kept opened.
2023type IndexGetter interface {
21- GetIndex (opts ... GetIndexOption ) (bleve.Index , error )
22- IndexCanBeClosed () bool
24+ GetIndex (opts ... GetIndexOption ) (bleve.Index , func (), error )
2325}
2426
27+ // IndexGetterMemory is an implementation of IndexGetter that uses an in-memory
28+ // index. The implementation caches the index and returns the same index every
29+ // time GetIndex is called.
30+ // The data won't be persisted between runs, and closing the index will wipe
31+ // the data.
32+ // The close function returned by GetIndex won't do anything. The index should
33+ // be kept opened until the application is shutting down.
34+ // This is useful for testing and small datasets.
2535type IndexGetterMemory struct {
2636 mapping mapping.IndexMapping
2737 index bleve.Index
@@ -39,25 +49,26 @@ func NewIndexGetterMemory(mapping mapping.IndexMapping) *IndexGetterMemory {
3949
4050// GetIndex creates a new in-memory index every time it is called.
4151// The options are ignored in this implementation.
42- func (i * IndexGetterMemory ) GetIndex (opts ... GetIndexOption ) (bleve.Index , error ) {
52+ func (i * IndexGetterMemory ) GetIndex (opts ... GetIndexOption ) (bleve.Index , func (), error ) {
53+ closeFn := func () {} // no-op
4354 if i .index != nil {
44- return i .index , nil
55+ return i .index , closeFn , nil
4556 }
4657
4758 index , err := bleve .NewMemOnly (i .mapping )
4859 if err != nil {
49- return nil , err
60+ return nil , closeFn , err
5061 }
5162
5263 i .index = index
53- return i .index , nil
54- }
55-
56- // IndexCanBeClosed returns false, meaning the index must be kept opened.
57- func (i * IndexGetterMemory ) IndexCanBeClosed () bool {
58- return false
64+ return i .index , closeFn , nil
5965}
6066
67+ // IndexGetterPersistent is an implementation of IndexGetter that persists the
68+ // index on the filesystem. The implementation caches the index and returns the
69+ // same index every time GetIndex is called.
70+ // The close function returned by GetIndex won't do anything. The index should
71+ // be kept opened until the application is shutting down.
6172type IndexGetterPersistent struct {
6273 rootDir string
6374 mapping mapping.IndexMapping
@@ -79,29 +90,30 @@ func NewIndexGetterPersistent(rootDir string, mapping mapping.IndexMapping) *Ind
7990
8091// GetIndex returns the cached index. The options are ignored in this
8192// implementation.
82- func (i * IndexGetterPersistent ) GetIndex (opts ... GetIndexOption ) (bleve.Index , error ) {
93+ func (i * IndexGetterPersistent ) GetIndex (opts ... GetIndexOption ) (bleve.Index , func (), error ) {
94+ closeFn := func () {} // no-op
8395 if i .index != nil {
84- return i .index , nil
96+ return i .index , closeFn , nil
8597 }
8698
8799 destination := filepath .Join (i .rootDir , "bleve" )
88100 index , err := bleve .Open (destination )
89101 if errors .Is (bleve .ErrorIndexPathDoesNotExist , err ) {
90102 index , err = bleve .New (destination , i .mapping )
91103 if err != nil {
92- return nil , err
104+ return nil , closeFn , err
93105 }
94106 }
95107
96108 i .index = index
97- return i .index , nil
98- }
99-
100- // IndexCanBeClosed returns false, meaning the index must be kept opened.
101- func (i * IndexGetterPersistent ) IndexCanBeClosed () bool {
102- return false
109+ return i .index , closeFn , nil
103110}
104111
112+ // IndexGetterPersistentScale is an implementation of IndexGetter that persists
113+ // the index on the filesystem. The implementation does not cache the index and
114+ // creates a new connection to the index every time GetIndex is called.
115+ // The close function returned by GetIndex must be called to close the index, as
116+ // soon as you the operations on the index are done.
105117type IndexGetterPersistentScale struct {
106118 rootDir string
107119 mapping mapping.IndexMapping
@@ -124,7 +136,7 @@ func NewIndexGetterPersistentScale(rootDir string, mapping mapping.IndexMapping)
124136// allow read-only operations to be performed in parallel.
125137// In order to avoid blocking write operations, you should close the index
126138// as soon as you are done with it.
127- func (i * IndexGetterPersistentScale ) GetIndex (opts ... GetIndexOption ) (bleve.Index , error ) {
139+ func (i * IndexGetterPersistentScale ) GetIndex (opts ... GetIndexOption ) (bleve.Index , func (), error ) {
128140 options := newGetIndexOptions (opts ... )
129141 destination := filepath .Join (i .rootDir , "bleve" )
130142 params := map [string ]interface {}{
@@ -134,17 +146,12 @@ func (i *IndexGetterPersistentScale) GetIndex(opts ...GetIndexOption) (bleve.Ind
134146 if errors .Is (bleve .ErrorIndexPathDoesNotExist , err ) {
135147 index , err = bleve .New (destination , i .mapping )
136148 if err != nil {
137- return nil , err
149+ closeFn := func () {} // no-op
150+ return nil , closeFn , err
138151 }
139152
140- return index , nil
153+ return index , func () { index . Close () }, nil
141154 }
142155
143- return index , err
144- }
145-
146- // IndexCanBeClosed returns true, meaning the index can be closed and
147- // reopened. You should close the index as soon as you are done with it.
148- func (i * IndexGetterPersistentScale ) IndexCanBeClosed () bool {
149- return true
156+ return index , func () { index .Close () }, err
150157}
0 commit comments