added String method for Batch
also changed Batch methods to pointer receiver closes #180
This commit is contained in:
parent
683c0a7a54
commit
8581e73cef
18
index.go
18
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.
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue