0
0
Fork 0

don't allow operations on empty doc id

fixes #239
This commit is contained in:
Marty Schoch 2015-09-28 17:00:08 -04:00
parent 1c9feaf792
commit cddf90c0ee
4 changed files with 55 additions and 1 deletions

View File

@ -25,6 +25,7 @@ const (
ErrorAliasMulti
ErrorAliasEmpty
ErrorUnknownIndexType
ErrorEmptyID
)
// Error represents a more strongly typed bleve error for detecting
@ -50,4 +51,5 @@ var errorMessages = map[int]string{
int(ErrorAliasMulti): "cannot perform single index operation on multiple index alias",
int(ErrorAliasEmpty): "cannot perform operation on empty alias",
int(ErrorUnknownIndexType): "unknown index type",
int(ErrorEmptyID): "document ID cannot be empty",
}

View File

@ -30,6 +30,9 @@ type Batch struct {
// batch. NOTE: the bleve Index is not updated
// until the batch is executed.
func (b *Batch) Index(id string, data interface{}) error {
if id == "" {
return ErrorEmptyID
}
doc := document.NewDocument(id)
err := b.index.Mapping().mapDocument(doc, data)
if err != nil {
@ -43,7 +46,9 @@ func (b *Batch) Index(id string, data interface{}) error {
// batch. NOTE: the bleve Index is not updated until
// the batch is executed.
func (b *Batch) Delete(id string) {
b.internal.Delete(id)
if id != "" {
b.internal.Delete(id)
}
}
// SetInternal adds the specified set internal

View File

@ -283,6 +283,10 @@ func (i *indexImpl) Mapping() *IndexMapping {
// The IndexMapping for this index will determine
// how the object is indexed.
func (i *indexImpl) Index(id string, data interface{}) error {
if id == "" {
return ErrorEmptyID
}
i.mutex.RLock()
defer i.mutex.RUnlock()
@ -305,6 +309,10 @@ func (i *indexImpl) Index(id string, data interface{}) error {
// Delete entries for the specified identifier from
// the index.
func (i *indexImpl) Delete(id string) error {
if id == "" {
return ErrorEmptyID
}
i.mutex.RLock()
defer i.mutex.RUnlock()

View File

@ -1038,3 +1038,42 @@ func TestTermVectorArrayPositions(t *testing.T) {
t.Fatal(err)
}
}
func TestIndexEmptyDocId(t *testing.T) {
defer func() {
err := os.RemoveAll("testidx")
if err != nil {
t.Fatal(err)
}
}()
index, err := New("testidx", NewIndexMapping())
if err != nil {
t.Fatal(err)
}
doc := map[string]interface{}{
"body": "nodocid",
}
err = index.Index("", doc)
if err != ErrorEmptyID {
t.Errorf("expect index empty doc id to fail")
}
err = index.Delete("")
if err != ErrorEmptyID {
t.Errorf("expect delete empty doc id to fail")
}
batch := index.NewBatch()
err = batch.Index("", doc)
if err != ErrorEmptyID {
t.Errorf("expect index empty doc id in batch to fail")
}
batch.Delete("")
if batch.Size() > 0 {
t.Errorf("expect delete empty doc id in batch to be ignored")
}
}