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

Remove dictionary rows when no document is referring to it #1314

Open
wants to merge 3 commits 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
15 changes: 14 additions & 1 deletion index/store/boltdb/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,23 @@ func (w *Writer) ExecuteBatch(batch store.KVBatch) (err error) {
err = fmt.Errorf("merge operator returned failure")
return
}
err = bucket.Put(kb, mergedVal)

var decodedVal uint64
decodedVal, err = w.store.mo.DecodeMergedVal(mergedVal)
if err != nil {
return
}

if decodedVal > 0 {
if err = bucket.Put(kb, mergedVal); err != nil {
return
}
} else {
// Delete the shared node when its count reaches 0.
if err = bucket.Delete(kb); err != nil {
return
}
}
}

for _, op := range emulatedBatch.Ops {
Expand Down
15 changes: 13 additions & 2 deletions index/store/goleveldb/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,19 @@ func (w *Writer) ExecuteBatch(b store.KVBatch) error {
if !fullMergeOk {
return fmt.Errorf("merge operator returned failure")
}
// add the final merge to this batch
batch.batch.Put(kb, mergedVal)

decodedVal, err := w.store.mo.DecodeMergedVal(mergedVal)
if err != nil {
return err
}

if decodedVal > 0 {
// Add the final merge to this batch.
batch.batch.Put(kb, mergedVal)
} else {
// Remove this record when its frequency drops to zero.
batch.batch.Delete(kb)
}
}

// now execute the batch
Expand Down
13 changes: 12 additions & 1 deletion index/store/gtreap/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,18 @@ func (w *Writer) ExecuteBatch(batch store.KVBatch) error {
if !fullMergeOk {
return fmt.Errorf("merge operator returned failure")
}
w.s.t = w.s.t.Upsert(&Item{k: kb, v: mergedVal}, rand.Int())

decodedVal, err := w.s.mo.DecodeMergedVal(mergedVal)
if err != nil {
return err
}

if decodedVal > 0 {
w.s.t = w.s.t.Upsert(&Item{k: kb, v: mergedVal}, rand.Int())
} else {
// Delete the shared node when its count reaches 0.
w.s.t = w.s.t.Delete(&Item{k: kb})
}
}

for _, op := range emulatedBatch.Ops {
Expand Down
3 changes: 3 additions & 0 deletions index/store/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type MergeOperator interface {
// all processing until the FullMerge is done.
PartialMerge(key, leftOperand, rightOperand []byte) ([]byte, bool)

// DecodeMergeVal decodes a merged value.
DecodeMergedVal(val []byte) (uint64, error)

// Name returns an identifier for the operator
Name() string
}
Expand Down
31 changes: 27 additions & 4 deletions index/store/test/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,25 @@ func encodeUint64(in uint64) []byte {
return rv
}

func encodeInt64(in int64) []byte {
rv := make([]byte, 8)
binary.LittleEndian.PutUint64(rv, uint64(in))
return rv
}

func CommonTestMerge(t *testing.T, s store.KVStore) {

testKey := []byte("k1")
textKey1 := []byte("k1")
testKey2 := []byte("k2")

data := []struct {
key []byte
val []byte
}{
{testKey, encodeUint64(1)},
{testKey, encodeUint64(1)},
{textKey1, encodeUint64(1)},
{textKey1, encodeUint64(1)},
{testKey2, encodeInt64(1)},
{testKey2, encodeInt64(-1)},
}

// open a writer
Expand Down Expand Up @@ -70,7 +79,7 @@ func CommonTestMerge(t *testing.T, s store.KVStore) {
}

// read key
returnedVal, err := reader.Get(testKey)
returnedVal, err := reader.Get(textKey1)
if err != nil {
t.Fatal(err)
}
Expand All @@ -81,6 +90,16 @@ func CommonTestMerge(t *testing.T, s store.KVStore) {
t.Errorf("expected 2, got %d", mergedval)
}

returnedVal, err = reader.Get(testKey2)
if err != nil {
t.Fatal(err)
}

// The node is not expected to be inserted when the count is zero.
if returnedVal != nil {
t.Errorf("expected nil, got %v", binary.LittleEndian.Uint64(returnedVal))
}

// close the reader
err = reader.Close()
if err != nil {
Expand Down Expand Up @@ -117,6 +136,10 @@ func (mc *TestMergeCounter) PartialMerge(key, leftOperand, rightOperand []byte)
return rv, true
}

func (mc *TestMergeCounter) DecodeMergedVal(val []byte) (uint64, error) {
return binary.LittleEndian.Uint64(val), nil
}

func (mc *TestMergeCounter) Name() string {
return "test_merge_counter"
}
4 changes: 4 additions & 0 deletions index/upsidedown/row_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func (m *upsideDownMerge) PartialMerge(key, leftOperand, rightOperand []byte) ([
return rv, true
}

func (m *upsideDownMerge) DecodeMergedVal(val []byte) (uint64, error) {
return dictionaryRowParseV(val)
}

func (m *upsideDownMerge) Name() string {
return "upsideDownMerge"
}
Loading