@@ -12,16 +12,26 @@ import (
12
12
// Implementations might differ in how the index is created and how the
13
13
// index is gotten (reused, created on the fly, etc).
14
14
//
15
+ // The GetIndex method returns a function that must be called to close the index.
15
16
// Some implementations might require the index to be kept opened, meaning
16
17
// 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.
20
23
type IndexGetter interface {
21
- GetIndex (opts ... GetIndexOption ) (bleve.Index , error )
22
- IndexCanBeClosed () bool
24
+ GetIndex (opts ... GetIndexOption ) (bleve.Index , func (), error )
23
25
}
24
26
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.
25
35
type IndexGetterMemory struct {
26
36
mapping mapping.IndexMapping
27
37
index bleve.Index
@@ -39,25 +49,26 @@ func NewIndexGetterMemory(mapping mapping.IndexMapping) *IndexGetterMemory {
39
49
40
50
// GetIndex creates a new in-memory index every time it is called.
41
51
// 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
43
54
if i .index != nil {
44
- return i .index , nil
55
+ return i .index , closeFn , nil
45
56
}
46
57
47
58
index , err := bleve .NewMemOnly (i .mapping )
48
59
if err != nil {
49
- return nil , err
60
+ return nil , closeFn , err
50
61
}
51
62
52
63
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
59
65
}
60
66
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.
61
72
type IndexGetterPersistent struct {
62
73
rootDir string
63
74
mapping mapping.IndexMapping
@@ -79,29 +90,30 @@ func NewIndexGetterPersistent(rootDir string, mapping mapping.IndexMapping) *Ind
79
90
80
91
// GetIndex returns the cached index. The options are ignored in this
81
92
// 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
83
95
if i .index != nil {
84
- return i .index , nil
96
+ return i .index , closeFn , nil
85
97
}
86
98
87
99
destination := filepath .Join (i .rootDir , "bleve" )
88
100
index , err := bleve .Open (destination )
89
101
if errors .Is (bleve .ErrorIndexPathDoesNotExist , err ) {
90
102
index , err = bleve .New (destination , i .mapping )
91
103
if err != nil {
92
- return nil , err
104
+ return nil , closeFn , err
93
105
}
94
106
}
95
107
96
108
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
103
110
}
104
111
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.
105
117
type IndexGetterPersistentScale struct {
106
118
rootDir string
107
119
mapping mapping.IndexMapping
@@ -124,7 +136,7 @@ func NewIndexGetterPersistentScale(rootDir string, mapping mapping.IndexMapping)
124
136
// allow read-only operations to be performed in parallel.
125
137
// In order to avoid blocking write operations, you should close the index
126
138
// 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 ) {
128
140
options := newGetIndexOptions (opts ... )
129
141
destination := filepath .Join (i .rootDir , "bleve" )
130
142
params := map [string ]interface {}{
@@ -134,17 +146,12 @@ func (i *IndexGetterPersistentScale) GetIndex(opts ...GetIndexOption) (bleve.Ind
134
146
if errors .Is (bleve .ErrorIndexPathDoesNotExist , err ) {
135
147
index , err = bleve .New (destination , i .mapping )
136
148
if err != nil {
137
- return nil , err
149
+ closeFn := func () {} // no-op
150
+ return nil , closeFn , err
138
151
}
139
152
140
- return index , nil
153
+ return index , func () { index . Close () }, nil
141
154
}
142
155
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
150
157
}
0 commit comments