Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Compact() function to gtreap store. #1317

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions index/store/gtreap/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,41 @@ func (s *Store) Writer() (store.KVWriter, error) {
return &Writer{s: s}, nil
}

// Compact removes DictionaryTerm entries with a count of zero.
// This is a workaround for github issue #374.
func (s *Store) Compact() (err error) {
kvreader, err := s.Reader()
if err != nil {
return err
}

defer func() {
if cerr := kvreader.Close(); err == nil && cerr != nil {
err = cerr
}
}()

prefix := []byte("d")

s.m.Lock()
defer s.m.Unlock()
it := kvreader.PrefixIterator(prefix)
defer func() {
if cerr := it.Close(); err == nil && cerr != nil {
err = cerr
}
}()

for ; it.Valid(); it.Next() {
k, v, _ := it.Current()
if bytes.Equal(v, []byte{0}) {
s.t = s.t.Delete(&Item{k: k})
}
}

return
}

func init() {
registry.RegisterKVStore(Name, New)
}
65 changes: 65 additions & 0 deletions index/store/gtreap/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package gtreap

import (
"bytes"
"testing"

"github.com/blevesearch/bleve/index/store"
Expand Down Expand Up @@ -91,3 +92,67 @@ func TestGTreapMerge(t *testing.T) {
defer cleanup(t, s)
test.CommonTestMerge(t, s)
}

func TestGTreapCompact(t *testing.T) {
s := open(t, nil)
defer cleanup(t, s)

writer, err := s.Writer()
if err != nil {
t.Error(err)
}

batch := writer.NewBatch()
// Should preserve non-dictionary row (key doesn't start with 'd').
batch.Set([]byte("a1"), []byte{0})
// Should delete for dictionary row with zero reference.
batch.Set([]byte("d1"), []byte{0})
batch.Set([]byte("d2"), []byte{0})
// Should preserve for dictionary row with non-zero reference.
batch.Set([]byte("d3"), []byte{1})

err = writer.ExecuteBatch(batch)
if err != nil {
t.Fatal(err)
}

err = writer.Close()
if err != nil {
t.Fatal(err)
}

validate(t, s, []byte("a1"), []byte{0})
validate(t, s, []byte("d1"), []byte{0})
validate(t, s, []byte("d2"), []byte{0})
validate(t, s, []byte("d3"), []byte{1})

if err := s.(*Store).Compact(); err != nil {
t.Fatal(err)
}

validate(t, s, []byte("a1"), []byte{0})
validate(t, s, []byte("d1"), nil)
validate(t, s, []byte("d2"), nil)
validate(t, s, []byte("d3"), []byte{1})
}

func validate(t *testing.T, s store.KVStore, key []byte, value []byte) {
reader, err := s.Reader()
if err != nil {
t.Error(err)
}
defer func() {
err := reader.Close()
if err != nil {
t.Fatal(err)
}
}()

got, err := reader.Get(key)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, value) {
t.Errorf("key %v value got %v, want %v", string(key), got, value)
}
}