0
0
Fork 0

changed batch behavior

now created through the index itself
mapping problems reported early at the time
data is added to the batch, previously these
were not reported until the batch was executed
This commit is contained in:
Marty Schoch 2015-03-03 13:18:20 -05:00
parent eaccd74c93
commit af356acff0
5 changed files with 56 additions and 45 deletions

View File

@ -19,48 +19,46 @@ import (
// operations you would like performed at the same
// time.
type Batch struct {
indexOps map[string]interface{}
internalOps map[string][]byte
}
// NewBatch creates a new empty batch.
func NewBatch() *Batch {
return &Batch{
indexOps: make(map[string]interface{}),
internalOps: make(map[string][]byte),
}
index Index
internal *index.Batch
}
// Index adds the specified index operation to the
// batch. NOTE: the bleve Index is not updated
// until the batch is executed.
func (b Batch) Index(id string, data interface{}) {
b.indexOps[id] = data
func (b Batch) Index(id string, data interface{}) error {
doc := document.NewDocument(id)
err := b.index.Mapping().mapDocument(doc, data)
if err != nil {
return err
}
b.internal.Update(doc)
return nil
}
// Delete adds the specified delete operation to the
// batch. NOTE: the bleve Index is not updated until
// the batch is executed.
func (b Batch) Delete(id string) {
b.indexOps[id] = nil
b.internal.Delete(id)
}
// SetInternal adds the specified set internal
// operation to the batch. NOTE: the bleve Index is
// not updated until the batch is executed.
func (b Batch) SetInternal(key, val []byte) {
b.internalOps[string(key)] = val
b.internal.SetInternal(key, val)
}
// SetInternal adds the specified delete internal
// operation to the batch. NOTE: the bleve Index is
// not updated until the batch is executed.
func (b Batch) DeleteInternal(key []byte) {
b.internalOps[string(key)] = nil
b.internal.DeleteInternal(key)
}
func (b Batch) Size() int {
return len(b.indexOps) + len(b.internalOps)
return len(b.internal.IndexOps) + len(b.internal.InternalOps)
}
// An Index implements all the indexing and searching
@ -70,6 +68,7 @@ type Index interface {
Index(id string, data interface{}) error
Delete(id string) error
NewBatch() *Batch
Batch(b *Batch) error
Document(id string) (*document.Document, error)

View File

@ -466,3 +466,19 @@ func MultiSearch(req *SearchRequest, indexes ...Index) (*SearchResult, error) {
return sr, nil
}
func (i *indexAliasImpl) NewBatch() *Batch {
i.mutex.RLock()
defer i.mutex.RUnlock()
if !i.open {
return nil
}
err := i.isAliasToSingleIndex()
if err != nil {
return nil
}
return i.indexes[0].NewBatch()
}

View File

@ -30,7 +30,8 @@ func TestIndexAliasSingle(t *testing.T) {
t.Errorf("expected %v, got %v", expectedError, err)
}
err = alias.Batch(NewBatch())
batch := alias.NewBatch()
err = alias.Batch(batch)
if err != expectedError {
t.Errorf("expected %v, got %v", expectedError, err)
}
@ -116,7 +117,7 @@ func TestIndexAliasSingle(t *testing.T) {
t.Errorf("expected %v, got %v", expectedError2, err)
}
err = alias.Batch(NewBatch())
err = alias.Batch(batch)
if err != expectedError2 {
t.Errorf("expected %v, got %v", expectedError2, err)
}
@ -200,7 +201,7 @@ func TestIndexAliasSingle(t *testing.T) {
t.Errorf("expected %v, got %v", expectedError3, err)
}
err = alias.Batch(NewBatch())
err = alias.Batch(batch)
if err != expectedError3 {
t.Errorf("expected %v, got %v", expectedError3, err)
}
@ -281,7 +282,8 @@ func TestIndexAliasClosed(t *testing.T) {
t.Errorf("expected %v, got %v", ErrorIndexClosed, err)
}
err = alias.Batch(NewBatch())
batch := alias.NewBatch()
err = alias.Batch(batch)
if err != ErrorIndexClosed {
t.Errorf("expected %v, got %v", ErrorIndexClosed, err)
}
@ -362,7 +364,8 @@ func TestIndexAliasEmpty(t *testing.T) {
t.Errorf("expected %v, got %v", ErrorAliasEmpty, err)
}
err = alias.Batch(NewBatch())
batch := alias.NewBatch()
err = alias.Batch(batch)
if err != ErrorAliasEmpty {
t.Errorf("expected %v, got %v", ErrorAliasEmpty, err)
}
@ -472,7 +475,8 @@ func TestIndexAliasMulti(t *testing.T) {
t.Errorf("expected %v, got %v", ErrorAliasMulti, err)
}
err = alias.Batch(NewBatch())
batch := alias.NewBatch()
err = alias.Batch(batch)
if err != ErrorAliasMulti {
t.Errorf("expected %v, got %v", ErrorAliasMulti, err)
}
@ -765,3 +769,7 @@ func (i *stubIndex) DeleteInternal(key []byte) error {
func (i *stubIndex) Advanced() (index.Index, store.KVStore, error) {
return nil, nil, nil
}
func (i *stubIndex) NewBatch() *Batch {
return &Batch{}
}

View File

@ -296,27 +296,7 @@ func (i *indexImpl) Batch(b *Batch) error {
return ErrorIndexClosed
}
ib := index.NewBatch()
for bk, bd := range b.indexOps {
if bd == nil {
ib.Delete(bk)
} else {
doc := document.NewDocument(bk)
err := i.m.mapDocument(doc, bd)
if err != nil {
return err
}
ib.Update(doc)
}
}
for ik, iv := range b.internalOps {
if iv == nil {
ib.DeleteInternal([]byte(ik))
} else {
ib.SetInternal([]byte(ik), iv)
}
}
return i.i.Batch(ib)
return i.i.Batch(b.internal)
}
// Document is used to find the values of all the
@ -598,3 +578,11 @@ func (i *indexImpl) DeleteInternal(key []byte) error {
return i.i.DeleteInternal(key)
}
// NewBatch creates a new empty batch.
func (i *indexImpl) NewBatch() *Batch {
return &Batch{
index: i,
internal: index.NewBatch(),
}
}

View File

@ -66,7 +66,7 @@ func TestCrud(t *testing.T) {
"name": "steve",
"desc": "cbft master",
}
batch := NewBatch()
batch := index.NewBatch()
batch.Index("b", docb)
batch.Delete("x")
batch.SetInternal([]byte("batchi"), []byte("batchv"))
@ -255,7 +255,7 @@ func TestClosedIndex(t *testing.T) {
t.Errorf("expected error index closed, got %v", err)
}
b := NewBatch()
b := index.NewBatch()
err = index.Batch(b)
if err != ErrorIndexClosed {
t.Errorf("expected error index closed, got %v", err)