2014-04-17 22:55:53 +02:00
|
|
|
// Copyright (c) 2014 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.
|
2014-09-02 16:54:50 +02:00
|
|
|
|
2014-04-17 22:55:53 +02:00
|
|
|
package upside_down
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2014-08-28 21:38:57 +02:00
|
|
|
"github.com/blevesearch/bleve/document"
|
|
|
|
"github.com/blevesearch/bleve/index"
|
2014-09-02 19:56:35 +02:00
|
|
|
"github.com/blevesearch/bleve/index/store/boltdb"
|
2014-04-17 22:55:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestIndexReader(t *testing.T) {
|
2015-10-19 20:27:03 +02:00
|
|
|
defer func() {
|
|
|
|
err := DestroyTest()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2014-04-17 22:55:53 +02:00
|
|
|
|
2015-09-02 19:12:08 +02:00
|
|
|
analysisQueue := index.NewAnalysisQueue(1)
|
2015-09-23 20:25:47 +02:00
|
|
|
idx, err := NewUpsideDownCouch(boltdb.Name, boltTestConfig, analysisQueue)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = idx.Open()
|
2014-04-17 22:55:53 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error opening index: %v", err)
|
|
|
|
}
|
2015-04-08 00:05:41 +02:00
|
|
|
defer func() {
|
|
|
|
err := idx.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2014-04-17 22:55:53 +02:00
|
|
|
|
2014-09-04 00:47:02 +02:00
|
|
|
var expectedCount uint64
|
2014-04-17 22:55:53 +02:00
|
|
|
doc := document.NewDocument("1")
|
2014-08-19 14:58:26 +02:00
|
|
|
doc.AddField(document.NewTextField("name", []uint64{}, []byte("test")))
|
2014-04-17 22:55:53 +02:00
|
|
|
err = idx.Update(doc)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error updating index: %v", err)
|
|
|
|
}
|
2014-09-04 00:47:02 +02:00
|
|
|
expectedCount++
|
2014-04-17 22:55:53 +02:00
|
|
|
|
|
|
|
doc = document.NewDocument("2")
|
2014-08-19 14:58:26 +02:00
|
|
|
doc.AddField(document.NewTextFieldWithAnalyzer("name", []uint64{}, []byte("test test test"), testAnalyzer))
|
2014-09-03 19:02:10 +02:00
|
|
|
doc.AddField(document.NewTextFieldCustom("desc", []uint64{}, []byte("eat more rice"), document.IndexField|document.IncludeTermVectors, testAnalyzer))
|
2014-04-17 22:55:53 +02:00
|
|
|
err = idx.Update(doc)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error updating index: %v", err)
|
|
|
|
}
|
2014-09-04 00:47:02 +02:00
|
|
|
expectedCount++
|
2014-04-17 22:55:53 +02:00
|
|
|
|
2014-10-31 14:40:23 +01:00
|
|
|
indexReader, err := idx.Reader()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2015-04-08 00:05:41 +02:00
|
|
|
defer func() {
|
|
|
|
err := indexReader.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2014-09-12 23:21:35 +02:00
|
|
|
|
2014-12-18 18:43:12 +01:00
|
|
|
// first look for a term that doesn't exist
|
2016-07-30 16:26:42 +02:00
|
|
|
reader, err := indexReader.TermFieldReader([]byte("nope"), "name", true, true, true)
|
2014-04-17 22:55:53 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error accessing term field reader: %v", err)
|
|
|
|
}
|
|
|
|
count := reader.Count()
|
|
|
|
if count != 0 {
|
|
|
|
t.Errorf("Expected doc count to be: %d got: %d", 0, count)
|
|
|
|
}
|
2015-04-07 20:52:00 +02:00
|
|
|
err = reader.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-04-17 22:55:53 +02:00
|
|
|
|
2016-07-30 16:26:42 +02:00
|
|
|
reader, err = indexReader.TermFieldReader([]byte("test"), "name", true, true, true)
|
2014-04-17 22:55:53 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error accessing term field reader: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedCount = 2
|
|
|
|
count = reader.Count()
|
|
|
|
if count != expectedCount {
|
|
|
|
t.Errorf("Exptected doc count to be: %d got: %d", expectedCount, count)
|
|
|
|
}
|
|
|
|
|
|
|
|
var match *index.TermFieldDoc
|
|
|
|
var actualCount uint64
|
2016-07-21 01:53:30 +02:00
|
|
|
match, err = reader.Next(nil)
|
2014-04-17 22:55:53 +02:00
|
|
|
for err == nil && match != nil {
|
2016-07-21 01:53:30 +02:00
|
|
|
match, err = reader.Next(nil)
|
2014-04-17 22:55:53 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error reading next")
|
|
|
|
}
|
2014-09-04 00:47:02 +02:00
|
|
|
actualCount++
|
2014-04-17 22:55:53 +02:00
|
|
|
}
|
|
|
|
if actualCount != count {
|
|
|
|
t.Errorf("count was 2, but only saw %d", actualCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedMatch := &index.TermFieldDoc{
|
2016-08-01 20:26:50 +02:00
|
|
|
ID: index.IndexInternalID("2"),
|
2014-04-17 22:55:53 +02:00
|
|
|
Freq: 1,
|
|
|
|
Norm: 0.5773502588272095,
|
|
|
|
Vectors: []*index.TermFieldVector{
|
2016-04-03 03:54:33 +02:00
|
|
|
{
|
2014-04-17 22:55:53 +02:00
|
|
|
Field: "desc",
|
|
|
|
Pos: 3,
|
|
|
|
Start: 9,
|
|
|
|
End: 13,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-07-30 16:26:42 +02:00
|
|
|
tfr, err := indexReader.TermFieldReader([]byte("rice"), "desc", true, true, true)
|
2014-04-17 22:55:53 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
2016-07-21 01:53:30 +02:00
|
|
|
match, err = tfr.Next(nil)
|
2014-04-17 22:55:53 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(expectedMatch, match) {
|
|
|
|
t.Errorf("got %#v, expected %#v", match, expectedMatch)
|
|
|
|
}
|
2015-04-07 20:52:00 +02:00
|
|
|
err = reader.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-04-20 15:43:02 +02:00
|
|
|
|
|
|
|
// now test usage of advance
|
2016-07-30 16:26:42 +02:00
|
|
|
reader, err = indexReader.TermFieldReader([]byte("test"), "name", true, true, true)
|
2014-04-20 15:43:02 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error accessing term field reader: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-08-01 20:26:50 +02:00
|
|
|
match, err = reader.Advance(index.IndexInternalID("2"), nil)
|
2014-04-20 15:43:02 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
2014-05-09 22:37:04 +02:00
|
|
|
if match == nil {
|
|
|
|
t.Fatalf("Expected match, got nil")
|
|
|
|
}
|
2016-08-01 20:26:50 +02:00
|
|
|
if !match.ID.Equals(index.IndexInternalID("2")) {
|
2014-04-20 15:43:02 +02:00
|
|
|
t.Errorf("Expected ID '2', got '%s'", match.ID)
|
|
|
|
}
|
2016-08-01 20:26:50 +02:00
|
|
|
match, err = reader.Advance(index.IndexInternalID("3"), nil)
|
2014-04-20 15:43:02 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if match != nil {
|
|
|
|
t.Errorf("expected nil, got %v", match)
|
|
|
|
}
|
2015-04-07 20:52:00 +02:00
|
|
|
err = reader.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-04-20 15:43:02 +02:00
|
|
|
|
2014-04-22 19:57:13 +02:00
|
|
|
// now test creating a reader for a field that doesn't exist
|
2016-07-30 16:26:42 +02:00
|
|
|
reader, err = indexReader.TermFieldReader([]byte("water"), "doesnotexist", true, true, true)
|
2014-04-22 19:57:13 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error accessing term field reader: %v", err)
|
|
|
|
}
|
|
|
|
count = reader.Count()
|
|
|
|
if count != 0 {
|
|
|
|
t.Errorf("expected count 0 for reader of non-existant field")
|
|
|
|
}
|
2016-07-21 01:53:30 +02:00
|
|
|
match, err = reader.Next(nil)
|
2014-04-22 19:57:13 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if match != nil {
|
|
|
|
t.Errorf("expected nil, got %v", match)
|
|
|
|
}
|
2016-08-01 20:26:50 +02:00
|
|
|
match, err = reader.Advance(index.IndexInternalID("anywhere"), nil)
|
2014-04-22 19:57:13 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if match != nil {
|
|
|
|
t.Errorf("expected nil, got %v", match)
|
|
|
|
}
|
|
|
|
|
2014-04-17 22:55:53 +02:00
|
|
|
}
|
2014-07-11 20:24:28 +02:00
|
|
|
|
|
|
|
func TestIndexDocIdReader(t *testing.T) {
|
2015-10-19 20:27:03 +02:00
|
|
|
defer func() {
|
|
|
|
err := DestroyTest()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2014-07-11 20:24:28 +02:00
|
|
|
|
2015-09-02 19:12:08 +02:00
|
|
|
analysisQueue := index.NewAnalysisQueue(1)
|
2015-09-23 20:25:47 +02:00
|
|
|
idx, err := NewUpsideDownCouch(boltdb.Name, boltTestConfig, analysisQueue)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = idx.Open()
|
2014-07-11 20:24:28 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error opening index: %v", err)
|
|
|
|
}
|
2015-04-08 00:05:41 +02:00
|
|
|
defer func() {
|
|
|
|
err := idx.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2014-07-11 20:24:28 +02:00
|
|
|
|
2014-09-04 00:47:02 +02:00
|
|
|
var expectedCount uint64
|
2014-07-11 20:24:28 +02:00
|
|
|
doc := document.NewDocument("1")
|
2014-08-19 14:58:26 +02:00
|
|
|
doc.AddField(document.NewTextField("name", []uint64{}, []byte("test")))
|
2014-07-11 20:24:28 +02:00
|
|
|
err = idx.Update(doc)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error updating index: %v", err)
|
|
|
|
}
|
2014-09-04 00:47:02 +02:00
|
|
|
expectedCount++
|
2014-07-11 20:24:28 +02:00
|
|
|
|
|
|
|
doc = document.NewDocument("2")
|
2014-08-19 14:58:26 +02:00
|
|
|
doc.AddField(document.NewTextField("name", []uint64{}, []byte("test test test")))
|
2014-09-03 19:02:10 +02:00
|
|
|
doc.AddField(document.NewTextFieldWithIndexingOptions("desc", []uint64{}, []byte("eat more rice"), document.IndexField|document.IncludeTermVectors))
|
2014-07-11 20:24:28 +02:00
|
|
|
err = idx.Update(doc)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error updating index: %v", err)
|
|
|
|
}
|
2014-09-04 00:47:02 +02:00
|
|
|
expectedCount++
|
2014-07-11 20:24:28 +02:00
|
|
|
|
2014-10-31 14:40:23 +01:00
|
|
|
indexReader, err := idx.Reader()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2015-04-08 00:05:41 +02:00
|
|
|
defer func() {
|
|
|
|
err := indexReader.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
2014-09-12 23:21:35 +02:00
|
|
|
|
2014-07-11 20:24:28 +02:00
|
|
|
// first get all doc ids
|
2014-09-12 23:21:35 +02:00
|
|
|
reader, err := indexReader.DocIDReader("", "")
|
2014-07-11 20:24:28 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error accessing doc id reader: %v", err)
|
|
|
|
}
|
2015-04-08 00:05:41 +02:00
|
|
|
defer func() {
|
|
|
|
err := reader.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2014-07-11 20:24:28 +02:00
|
|
|
|
|
|
|
id, err := reader.Next()
|
|
|
|
count := uint64(0)
|
2016-07-31 19:46:18 +02:00
|
|
|
for id != nil {
|
2014-07-11 20:24:28 +02:00
|
|
|
count++
|
|
|
|
id, err = reader.Next()
|
|
|
|
}
|
|
|
|
if count != expectedCount {
|
|
|
|
t.Errorf("expected %d, got %d", expectedCount, count)
|
|
|
|
}
|
2014-08-15 19:12:55 +02:00
|
|
|
|
|
|
|
// try it again, but jump to the second doc this time
|
2015-04-08 00:05:41 +02:00
|
|
|
reader2, err := indexReader.DocIDReader("", "")
|
2014-08-15 19:12:55 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error accessing doc id reader: %v", err)
|
|
|
|
}
|
2015-04-08 00:05:41 +02:00
|
|
|
defer func() {
|
|
|
|
err := reader2.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
2014-08-15 19:12:55 +02:00
|
|
|
|
2016-08-01 20:26:50 +02:00
|
|
|
id, err = reader2.Advance(index.IndexInternalID("2"))
|
2014-08-15 19:12:55 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2016-08-01 20:26:50 +02:00
|
|
|
if !id.Equals(index.IndexInternalID("2")) {
|
2014-08-15 19:12:55 +02:00
|
|
|
t.Errorf("expected to find id '2', got '%s'", id)
|
|
|
|
}
|
|
|
|
|
2016-08-01 20:26:50 +02:00
|
|
|
id, err = reader2.Advance(index.IndexInternalID("3"))
|
2014-08-15 19:12:55 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2016-07-31 19:46:18 +02:00
|
|
|
if id != nil {
|
2014-08-15 19:12:55 +02:00
|
|
|
t.Errorf("expected to find id '', got '%s'", id)
|
|
|
|
}
|
2014-07-11 20:24:28 +02:00
|
|
|
}
|
2015-07-13 21:22:54 +02:00
|
|
|
|
|
|
|
func TestCrashBadBackIndexRow(t *testing.T) {
|
|
|
|
br, err := NewBackIndexRowKV([]byte{byte('b'), byte('a'), ByteSeparator}, []byte{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(br.doc) != "a" {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|