@@ -57,8 +57,6 @@ type indexImpl struct {
5757
5858const storePath = "store"
5959
60- var mappingInternalKey = []byte ("_mapping" )
61-
6260const SearchQueryStartCallbackKey = "_search_query_start_callback_key"
6361const SearchQueryEndCallbackKey = "_search_query_end_callback_key"
6462
@@ -129,7 +127,7 @@ func newIndexUsing(path string, mapping mapping.IndexMapping, indexType string,
129127 if err != nil {
130128 return nil , err
131129 }
132- err = rv .i .SetInternal (mappingInternalKey , mappingBytes )
130+ err = rv .i .SetInternal (scorch . MappingInternalKey , mappingBytes )
133131 if err != nil {
134132 return nil , err
135133 }
@@ -164,25 +162,110 @@ func openIndexUsing(path string, runtimeConfig map[string]interface{}) (rv *inde
164162 storeConfig = map [string ]interface {}{}
165163 }
166164
165+ storeConfig ["path" ] = indexStorePath (path )
166+ storeConfig ["create_if_missing" ] = false
167+ storeConfig ["error_if_exists" ] = false
168+ for rck , rcv := range runtimeConfig {
169+ storeConfig [rck ] = rcv
170+ }
171+
172+ // open the index
173+ indexTypeConstructor := registry .IndexTypeConstructorByName (rv .meta .IndexType )
174+ if indexTypeConstructor == nil {
175+ return nil , ErrorUnknownIndexType
176+ }
177+
178+ rv .i , err = indexTypeConstructor (rv .meta .Storage , storeConfig , Config .analysisQueue )
179+ if err != nil {
180+ return nil , err
181+ }
182+ err = rv .i .Open ()
183+ if err != nil {
184+ return nil , err
185+ }
186+ defer func (rv * indexImpl ) {
187+ if ! rv .open {
188+ rv .i .Close ()
189+ }
190+ }(rv )
191+
192+ // now load the mapping
193+ indexReader , err := rv .i .Reader ()
194+ if err != nil {
195+ return nil , err
196+ }
197+ defer func () {
198+ if cerr := indexReader .Close (); cerr != nil && err == nil {
199+ err = cerr
200+ }
201+ }()
202+
203+ mappingBytes , err := indexReader .GetInternal (scorch .MappingInternalKey )
204+ if err != nil {
205+ return nil , err
206+ }
207+
208+ var im * mapping.IndexMappingImpl
209+ err = util .UnmarshalJSON (mappingBytes , & im )
210+ if err != nil {
211+ return nil , fmt .Errorf ("error parsing mapping JSON: %v\n mapping contents:\n %s" , err , string (mappingBytes ))
212+ }
213+
214+ // mark the index as open
215+ rv .mutex .Lock ()
216+ defer rv .mutex .Unlock ()
217+ rv .open = true
218+
219+ // validate the mapping
220+ err = im .Validate ()
221+ if err != nil {
222+ // note even if the mapping is invalid
223+ // we still return an open usable index
224+ return rv , err
225+ }
226+
227+ rv .m = im
228+ indexStats .Register (rv )
229+ return rv , err
230+ }
231+
232+ func updateIndexUsing (path string , runtimeConfig map [string ]interface {}, newParams string ) (rv * indexImpl , err error ) {
233+ rv = & indexImpl {
234+ path : path ,
235+ name : path ,
236+ }
237+ rv .stats = & IndexStat {i : rv }
238+
239+ rv .meta , err = openIndexMeta (path )
240+ if err != nil {
241+ return nil , err
242+ }
243+
244+ // backwards compatibility if index type is missing
245+ if rv .meta .IndexType == "" {
246+ rv .meta .IndexType = upsidedown .Name
247+ }
248+
249+ storeConfig := rv .meta .Config
250+ if storeConfig == nil {
251+ storeConfig = map [string ]interface {}{}
252+ }
253+
167254 var um * mapping.IndexMappingImpl
168- var umBytes []byte
255+
256+ if len (newParams ) == 0 {
257+ return nil , fmt .Errorf (("updated mapping is empty" ))
258+ }
259+
260+ err = util .UnmarshalJSON ([]byte (newParams ), & um )
261+ if err != nil {
262+ return nil , fmt .Errorf ("error parsing updated mapping JSON: %v\n mapping contents:\n %s" , err , newParams )
263+ }
169264
170265 storeConfig ["path" ] = indexStorePath (path )
171266 storeConfig ["create_if_missing" ] = false
172267 storeConfig ["error_if_exists" ] = false
173268 for rck , rcv := range runtimeConfig {
174- if rck == "mapping" {
175- if val , ok := rcv .([]byte ); ok {
176- err = util .UnmarshalJSON (val , & um )
177- if err != nil {
178- return nil , fmt .Errorf ("error parsing updated mapping JSON: %v\n mapping contents:\n %s" , err , val )
179- }
180- umBytes = val
181- } else {
182- return nil , fmt .Errorf ("error typecasting updated mapping JSON\n mapping contents: %v" , rcv )
183- }
184- continue
185- }
186269 storeConfig [rck ] = rcv
187270 }
188271
@@ -196,6 +279,7 @@ func openIndexUsing(path string, runtimeConfig map[string]interface{}) (rv *inde
196279 if err != nil {
197280 return nil , err
198281 }
282+
199283 err = rv .i .Open ()
200284 if err != nil {
201285 return nil , err
@@ -217,7 +301,7 @@ func openIndexUsing(path string, runtimeConfig map[string]interface{}) (rv *inde
217301 }
218302 }()
219303
220- mappingBytes , err := indexReader .GetInternal (mappingInternalKey )
304+ mappingBytes , err := indexReader .GetInternal (scorch . MappingInternalKey )
221305 if err != nil {
222306 return nil , err
223307 }
@@ -241,6 +325,7 @@ func openIndexUsing(path string, runtimeConfig map[string]interface{}) (rv *inde
241325 return rv , err
242326 }
243327
328+ // Validate and update the index with the new mapping
244329 if um != nil {
245330 ui , ok := rv .i .(index.UpdateIndex )
246331 if ! ok {
@@ -252,15 +337,16 @@ func openIndexUsing(path string, runtimeConfig map[string]interface{}) (rv *inde
252337 return rv , err
253338 }
254339
255- fieldInfo , err := deletedFields (im , um )
340+ fieldInfo , err := DeletedFields (im , um )
256341 if err != nil {
257342 return rv , err
258343 }
259344
260- err = ui .UpdateFields (fieldInfo , umBytes )
345+ err = ui .UpdateFields (fieldInfo , [] byte ( newParams ) )
261346 if err != nil {
262347 return rv , err
263348 }
349+ im = um
264350 }
265351
266352 rv .m = im
0 commit comments