From 8581e73cef03129e4effcb4fd569b755b2ae1300 Mon Sep 17 00:00:00 2001 From: Marty Schoch Date: Wed, 8 Apr 2015 10:41:42 -0400 Subject: [PATCH] added String method for Batch also changed Batch methods to pointer receiver closes #180 --- index.go | 18 +++++++++++++----- index/index.go | 28 ++++++++++++++++++++++++---- index_test.go | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 9 deletions(-) diff --git a/index.go b/index.go index 4fbd107b..9514c972 100644 --- a/index.go +++ b/index.go @@ -26,7 +26,7 @@ type Batch struct { // 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{}) error { +func (b *Batch) Index(id string, data interface{}) error { doc := document.NewDocument(id) err := b.index.Mapping().mapDocument(doc, data) if err != nil { @@ -39,28 +39,36 @@ func (b Batch) Index(id string, data interface{}) error { // 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) { +func (b *Batch) Delete(id string) { 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) { +func (b *Batch) SetInternal(key, val []byte) { 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) { +func (b *Batch) DeleteInternal(key []byte) { b.internal.DeleteInternal(key) } -func (b Batch) Size() int { +// Size returns the total number of operations inside the batch +// including normal index operations and internal operations. +func (b *Batch) Size() int { return len(b.internal.IndexOps) + len(b.internal.InternalOps) } +// String prints a user friendly string represenation of what +// is inside this batch. +func (b *Batch) String() string { + return b.internal.String() +} + // An Index implements all the indexing and searching // capabilities of bleve. An Index can be created // using the New() and Open() methods. diff --git a/index/index.go b/index/index.go index 6ffd7909..72c3eaa4 100644 --- a/index/index.go +++ b/index/index.go @@ -11,6 +11,7 @@ package index import ( "encoding/json" + "fmt" "github.com/blevesearch/bleve/document" ) @@ -109,18 +110,37 @@ func NewBatch() *Batch { } } -func (b Batch) Update(doc *document.Document) { +func (b *Batch) Update(doc *document.Document) { b.IndexOps[doc.ID] = doc } -func (b Batch) Delete(id string) { +func (b *Batch) Delete(id string) { b.IndexOps[id] = nil } -func (b Batch) SetInternal(key, val []byte) { +func (b *Batch) SetInternal(key, val []byte) { b.InternalOps[string(key)] = val } -func (b Batch) DeleteInternal(key []byte) { +func (b *Batch) DeleteInternal(key []byte) { b.InternalOps[string(key)] = nil } + +func (b *Batch) String() string { + rv := fmt.Sprintf("Batch (%d ops, %d internal ops)\n", len(b.IndexOps), len(b.InternalOps)) + for k, v := range b.IndexOps { + if v != nil { + rv += fmt.Sprintf("\tINDEX - '%s'\n", k) + } else { + rv += fmt.Sprintf("\tDELETE - '%s'\n", k) + } + } + for k, v := range b.InternalOps { + if v != nil { + rv += fmt.Sprintf("\tSET INTERNAL - '%s'\n", k) + } else { + rv += fmt.Sprintf("\tDELETE INTERNAL - '%s'\n", k) + } + } + return rv +} diff --git a/index_test.go b/index_test.go index 744d2f32..f1e6b770 100644 --- a/index_test.go +++ b/index_test.go @@ -565,3 +565,37 @@ func TestDict(t *testing.T) { t.Fatal(err) } } + +func TestBatchString(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) + } + + batch := index.NewBatch() + err = batch.Index("a", []byte("{}")) + if err != nil { + t.Fatal(err) + } + batch.Delete("b") + batch.SetInternal([]byte("c"), []byte{}) + batch.DeleteInternal([]byte("d")) + + expectedBatchStr := `Batch (2 ops, 2 internal ops) + INDEX - 'a' + DELETE - 'b' + SET INTERNAL - 'c' + DELETE INTERNAL - 'd' +` + batchStr := batch.String() + if batchStr != expectedBatchStr { + t.Errorf("expected: %s\ngot: %s", expectedBatchStr, batchStr) + } +}