From f95a4662b1551a6bd1a5c64a5b89a9b27d620a89 Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Mon, 21 Sep 2015 20:18:33 +0200 Subject: [PATCH 1/3] Use Error type correctly --- error.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/error.go b/error.go index 48b570ce..6e71d593 100644 --- a/error.go +++ b/error.go @@ -32,22 +32,22 @@ const ( type Error int func (e Error) Error() string { - return errorMessages[int(e)] + return errorMessages[e] } -var errorMessages = map[int]string{ - int(ErrorIndexPathExists): "cannot create new index, path already exists", - int(ErrorIndexPathDoesNotExist): "cannot open index, path does not exist", - int(ErrorIndexMetaMissing): "cannot open index, metadata missing", - int(ErrorIndexMetaCorrupt): "cannot open index, metadata corrupt", - int(ErrorDisjunctionFewerThanMinClauses): "disjunction query has fewer than the minimum number of clauses to satisfy", - int(ErrorBooleanQueryNeedsMustOrShouldOrNotMust): "boolean query must contain at least one must or should or not must clause", - int(ErrorNumericQueryNoBounds): "numeric range query must specify min or max", - int(ErrorPhraseQueryNoTerms): "phrase query must contain at least one term", - int(ErrorUnknownQueryType): "unknown query type", - int(ErrorUnknownStorageType): "unknown storage type", - int(ErrorIndexClosed): "index is closed", - int(ErrorAliasMulti): "cannot perform single index operation on multiple index alias", - int(ErrorAliasEmpty): "cannot perform operation on empty alias", - int(ErrorUnknownIndexType): "unknown index type", +var errorMessages = map[Error]string{ + ErrorIndexPathExists: "cannot create new index, path already exists", + ErrorIndexPathDoesNotExist: "cannot open index, path does not exist", + ErrorIndexMetaMissing: "cannot open index, metadata missing", + ErrorIndexMetaCorrupt: "cannot open index, metadata corrupt", + ErrorDisjunctionFewerThanMinClauses: "disjunction query has fewer than the minimum number of clauses to satisfy", + ErrorBooleanQueryNeedsMustOrShouldOrNotMust: "boolean query must contain at least one must or should or not must clause", + ErrorNumericQueryNoBounds: "numeric range query must specify min or max", + ErrorPhraseQueryNoTerms: "phrase query must contain at least one term", + ErrorUnknownQueryType: "unknown query type", + ErrorUnknownStorageType: "unknown storage type", + ErrorIndexClosed: "index is closed", + ErrorAliasMulti: "cannot perform single index operation on multiple index alias", + ErrorAliasEmpty: "cannot perform operation on empty alias", + ErrorUnknownIndexType: "unknown index type", } From 3414701fcab42782cf41bd087b183557cfa69bda Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Mon, 21 Sep 2015 20:20:27 +0200 Subject: [PATCH 2/3] Simplify returns --- index_impl.go | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/index_impl.go b/index_impl.go index 256a13f6..c1fb742d 100644 --- a/index_impl.go +++ b/index_impl.go @@ -276,7 +276,7 @@ func (i *indexImpl) Mapping() *IndexMapping { // Index the object with the specified identifier. // The IndexMapping for this index will determine // how the object is indexed. -func (i *indexImpl) Index(id string, data interface{}) error { +func (i *indexImpl) Index(id string, data interface{}) (err error) { i.mutex.RLock() defer i.mutex.RUnlock() @@ -285,20 +285,17 @@ func (i *indexImpl) Index(id string, data interface{}) error { } doc := document.NewDocument(id) - err := i.m.mapDocument(doc, data) + err = i.m.mapDocument(doc, data) if err != nil { return err } err = i.i.Update(doc) - if err != nil { - return err - } - return nil + return } // Delete entries for the specified identifier from // the index. -func (i *indexImpl) Delete(id string) error { +func (i *indexImpl) Delete(id string) (err error) { i.mutex.RLock() defer i.mutex.RUnlock() @@ -306,11 +303,8 @@ func (i *indexImpl) Delete(id string) error { return ErrorIndexClosed } - err := i.i.Delete(id) - if err != nil { - return err - } - return nil + err = i.i.Delete(id) + return } // Batch executes multiple Index and Delete From 650d48427d7ba2f220f75238182d825a37fd3445 Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Mon, 21 Sep 2015 20:46:33 +0200 Subject: [PATCH 3/3] Refactor AddFieldValue method Removing one level of nesting makes the method easier to read. --- search/search.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/search/search.go b/search/search.go index eef47406..aabb0c76 100644 --- a/search/search.go +++ b/search/search.go @@ -50,19 +50,20 @@ func (dm *DocumentMatch) AddFieldValue(name string, value interface{}) { dm.Fields = make(map[string]interface{}) } existingVal, ok := dm.Fields[name] - if ok { - valSlice, ok := existingVal.([]interface{}) - if ok { - // already a slice, append to it - valSlice = append(valSlice, value) - } else { - // create a slice - valSlice = []interface{}{existingVal, value} - } - dm.Fields[name] = valSlice - } else { + if !ok { dm.Fields[name] = value + return } + + valSlice, ok := existingVal.([]interface{}) + if ok { + // already a slice, append to it + valSlice = append(valSlice, value) + } else { + // create a slice + valSlice = []interface{}{existingVal, value} + } + dm.Fields[name] = valSlice } type DocumentMatchCollection []*DocumentMatch