2015-08-25 20:52:42 +02:00
|
|
|
// Copyright (c) 2015 Couchbase, Inc.
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
// except in compliance with the License. You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software distributed under the
|
|
|
|
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
|
|
|
// either express or implied. See the License for the specific language governing permissions
|
|
|
|
// and limitations under the License.
|
|
|
|
|
|
|
|
package firestorm
|
|
|
|
|
|
|
|
import (
|
2015-12-31 20:07:59 +01:00
|
|
|
"runtime"
|
2015-08-25 20:52:42 +02:00
|
|
|
"testing"
|
2016-01-07 17:06:18 +01:00
|
|
|
"time"
|
2015-08-25 20:52:42 +02:00
|
|
|
|
|
|
|
"github.com/blevesearch/bleve/index"
|
2015-10-28 16:26:01 +01:00
|
|
|
"github.com/blevesearch/bleve/index/store/gtreap"
|
2015-08-25 20:52:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDictUpdater(t *testing.T) {
|
2015-10-28 16:26:01 +01:00
|
|
|
aq := index.NewAnalysisQueue(1)
|
|
|
|
f, err := NewFirestorm(gtreap.Name, nil, aq)
|
2015-08-25 20:52:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-28 16:26:01 +01:00
|
|
|
err = f.Open()
|
2015-08-25 20:52:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dictBatch := map[string]int64{
|
|
|
|
string([]byte{'d', 1, 0, 'c', 'a', 't'}): 3,
|
|
|
|
}
|
|
|
|
dictExpect := map[string]int64{
|
|
|
|
string([]byte{'d', 1, 0, 'c', 'a', 't'}): 3,
|
|
|
|
}
|
|
|
|
|
2015-10-28 16:26:01 +01:00
|
|
|
f.(*Firestorm).dictUpdater.NotifyBatch(dictBatch)
|
2015-08-25 20:52:42 +02:00
|
|
|
|
|
|
|
// invoke updater manually
|
2015-12-31 20:07:59 +01:00
|
|
|
for len(f.(*Firestorm).dictUpdater.incoming) > 0 {
|
|
|
|
runtime.Gosched()
|
|
|
|
}
|
2016-01-07 17:06:18 +01:00
|
|
|
f.(*Firestorm).dictUpdater.waitTasksDone(5 * time.Second)
|
2015-08-25 20:52:42 +02:00
|
|
|
|
|
|
|
// assert that dictionary rows are correct
|
2015-10-28 16:26:01 +01:00
|
|
|
reader, err := f.(*Firestorm).store.Reader()
|
2015-08-25 20:52:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, _ := range dictBatch {
|
|
|
|
v, err := reader.Get([]byte(key))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-01-07 16:45:20 +01:00
|
|
|
if v == nil {
|
|
|
|
t.Fatal("unexpected dictionary value missing")
|
|
|
|
}
|
2015-08-25 20:52:42 +02:00
|
|
|
dr, err := NewDictionaryRowKV([]byte(key), v)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
expect := dictExpect[key]
|
|
|
|
if int64(dr.Count()) != expect {
|
|
|
|
t.Errorf("expected %d, got %d", expect, dr.Count())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = reader.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// update it again
|
|
|
|
dictBatch = map[string]int64{
|
|
|
|
string([]byte{'d', 1, 0, 'c', 'a', 't'}): 1,
|
|
|
|
}
|
|
|
|
dictExpect = map[string]int64{
|
|
|
|
string([]byte{'d', 1, 0, 'c', 'a', 't'}): 4,
|
|
|
|
}
|
|
|
|
|
2015-10-28 16:26:01 +01:00
|
|
|
f.(*Firestorm).dictUpdater.NotifyBatch(dictBatch)
|
2015-08-25 20:52:42 +02:00
|
|
|
|
|
|
|
// invoke updater manually
|
2015-12-31 20:07:59 +01:00
|
|
|
for len(f.(*Firestorm).dictUpdater.incoming) > 0 {
|
|
|
|
runtime.Gosched()
|
|
|
|
}
|
2015-10-28 16:26:01 +01:00
|
|
|
f.(*Firestorm).dictUpdater.update()
|
2015-08-25 20:52:42 +02:00
|
|
|
|
|
|
|
// assert that dictionary rows are correct
|
2015-10-28 16:26:01 +01:00
|
|
|
reader, err = f.(*Firestorm).store.Reader()
|
2015-08-25 20:52:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, _ := range dictBatch {
|
|
|
|
v, err := reader.Get([]byte(key))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
dr, err := NewDictionaryRowKV([]byte(key), v)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
expect := dictExpect[key]
|
|
|
|
if int64(dr.Count()) != expect {
|
|
|
|
t.Errorf("expected %d, got %d", expect, dr.Count())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = reader.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// update it again (decrement this time)
|
|
|
|
dictBatch = map[string]int64{
|
|
|
|
string([]byte{'d', 1, 0, 'c', 'a', 't'}): -2,
|
|
|
|
}
|
|
|
|
dictExpect = map[string]int64{
|
|
|
|
string([]byte{'d', 1, 0, 'c', 'a', 't'}): 2,
|
|
|
|
}
|
|
|
|
|
2015-10-28 16:26:01 +01:00
|
|
|
f.(*Firestorm).dictUpdater.NotifyBatch(dictBatch)
|
2015-08-25 20:52:42 +02:00
|
|
|
|
|
|
|
// invoke updater manually
|
2015-12-31 20:07:59 +01:00
|
|
|
for len(f.(*Firestorm).dictUpdater.incoming) > 0 {
|
|
|
|
runtime.Gosched()
|
|
|
|
}
|
2015-10-28 16:26:01 +01:00
|
|
|
f.(*Firestorm).dictUpdater.update()
|
2015-08-25 20:52:42 +02:00
|
|
|
|
|
|
|
// assert that dictionary rows are correct
|
2015-10-28 16:26:01 +01:00
|
|
|
reader, err = f.(*Firestorm).store.Reader()
|
2015-08-25 20:52:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, _ := range dictBatch {
|
|
|
|
v, err := reader.Get([]byte(key))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
dr, err := NewDictionaryRowKV([]byte(key), v)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
expect := dictExpect[key]
|
|
|
|
if int64(dr.Count()) != expect {
|
|
|
|
t.Errorf("expected %d, got %d", expect, dr.Count())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = reader.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|