@@ -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,25 @@ 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 ) {
43
53
if i .index != nil {
44
- return i .index , nil
54
+ return i .index , func () {}, nil
45
55
}
46
56
47
57
index , err := bleve .NewMemOnly (i .mapping )
48
58
if err != nil {
49
- return nil , err
59
+ return nil , func () {}, err
50
60
}
51
61
52
62
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
63
+ return i .index , func () {}, nil
59
64
}
60
65
66
+ // IndexGetterPersistent is an implementation of IndexGetter that persists the
67
+ // index on the filesystem. The implementation caches the index and returns the
68
+ // same index every time GetIndex is called.
69
+ // The close function returned by GetIndex won't do anything. The index should
70
+ // be kept opened until the application is shutting down.
61
71
type IndexGetterPersistent struct {
62
72
rootDir string
63
73
mapping mapping.IndexMapping
@@ -79,29 +89,29 @@ func NewIndexGetterPersistent(rootDir string, mapping mapping.IndexMapping) *Ind
79
89
80
90
// GetIndex returns the cached index. The options are ignored in this
81
91
// implementation.
82
- func (i * IndexGetterPersistent ) GetIndex (opts ... GetIndexOption ) (bleve.Index , error ) {
92
+ func (i * IndexGetterPersistent ) GetIndex (opts ... GetIndexOption ) (bleve.Index , func (), error ) {
83
93
if i .index != nil {
84
- return i .index , nil
94
+ return i .index , func () {}, nil
85
95
}
86
96
87
97
destination := filepath .Join (i .rootDir , "bleve" )
88
98
index , err := bleve .Open (destination )
89
99
if errors .Is (bleve .ErrorIndexPathDoesNotExist , err ) {
90
100
index , err = bleve .New (destination , i .mapping )
91
101
if err != nil {
92
- return nil , err
102
+ return nil , func () {}, err
93
103
}
94
104
}
95
105
96
106
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
107
+ return i .index , func () {}, nil
103
108
}
104
109
110
+ // IndexGetterPersistentScale is an implementation of IndexGetter that persists
111
+ // the index on the filesystem. The implementation does not cache the index and
112
+ // creates a new connection to the index every time GetIndex is called.
113
+ // The close function returned by GetIndex must be called to close the index, as
114
+ // soon as you the operations on the index are done.
105
115
type IndexGetterPersistentScale struct {
106
116
rootDir string
107
117
mapping mapping.IndexMapping
@@ -124,7 +134,7 @@ func NewIndexGetterPersistentScale(rootDir string, mapping mapping.IndexMapping)
124
134
// allow read-only operations to be performed in parallel.
125
135
// In order to avoid blocking write operations, you should close the index
126
136
// as soon as you are done with it.
127
- func (i * IndexGetterPersistentScale ) GetIndex (opts ... GetIndexOption ) (bleve.Index , error ) {
137
+ func (i * IndexGetterPersistentScale ) GetIndex (opts ... GetIndexOption ) (bleve.Index , func (), error ) {
128
138
options := newGetIndexOptions (opts ... )
129
139
destination := filepath .Join (i .rootDir , "bleve" )
130
140
params := map [string ]interface {}{
@@ -134,17 +144,11 @@ func (i *IndexGetterPersistentScale) GetIndex(opts ...GetIndexOption) (bleve.Ind
134
144
if errors .Is (bleve .ErrorIndexPathDoesNotExist , err ) {
135
145
index , err = bleve .New (destination , i .mapping )
136
146
if err != nil {
137
- return nil , err
147
+ return nil , func () {}, err
138
148
}
139
149
140
- return index , nil
150
+ return index , func () { index . Close () }, nil
141
151
}
142
152
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
153
+ return index , func () { index .Close () }, err
150
154
}
0 commit comments