2014-08-15 19:12:55 +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-08-15 19:12:55 +02:00
|
|
|
package upside_down
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-08-25 20:52:42 +02:00
|
|
|
"github.com/blevesearch/bleve/index"
|
|
|
|
"github.com/blevesearch/bleve/index/store/boltdb"
|
|
|
|
|
2014-08-28 21:38:57 +02:00
|
|
|
"github.com/blevesearch/bleve/document"
|
2014-08-15 19:12:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDump(t *testing.T) {
|
2015-04-07 21:39:56 +02:00
|
|
|
defer func() {
|
|
|
|
err := os.RemoveAll("test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2014-08-15 19:12:55 +02:00
|
|
|
|
2015-04-24 23:11:47 +02:00
|
|
|
s := boltdb.New("test", "bleve")
|
|
|
|
s.SetMergeOperator(&mergeOperator)
|
2015-08-25 20:52:42 +02:00
|
|
|
analysisQueue := index.NewAnalysisQueue(1)
|
2015-04-24 23:11:47 +02:00
|
|
|
idx := NewUpsideDownCouch(s, analysisQueue)
|
|
|
|
err := idx.Open()
|
2014-08-15 19:12:55 +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-08-15 19:12:55 +02:00
|
|
|
|
2014-09-04 00:47:02 +02:00
|
|
|
var expectedCount uint64
|
2014-10-31 14:40:23 +01:00
|
|
|
docCount, err := idx.DocCount()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2014-08-15 19:12:55 +02:00
|
|
|
if docCount != expectedCount {
|
|
|
|
t.Errorf("Expected document count to be %d got %d", expectedCount, docCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
doc := document.NewDocument("1")
|
2014-09-03 19:02:10 +02:00
|
|
|
doc.AddField(document.NewTextFieldWithIndexingOptions("name", []uint64{}, []byte("test"), document.IndexField|document.StoreField))
|
|
|
|
doc.AddField(document.NewNumericFieldWithIndexingOptions("age", []uint64{}, 35.99, document.IndexField|document.StoreField))
|
|
|
|
dateField, err := document.NewDateTimeFieldWithIndexingOptions("unixEpoch", []uint64{}, time.Unix(0, 0), document.IndexField|document.StoreField)
|
2014-08-19 16:02:33 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
doc.AddField(dateField)
|
2014-08-15 19:12:55 +02:00
|
|
|
err = idx.Update(doc)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error updating index: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
doc = document.NewDocument("2")
|
2014-09-03 19:02:10 +02:00
|
|
|
doc.AddField(document.NewTextFieldWithIndexingOptions("name", []uint64{}, []byte("test2"), document.IndexField|document.StoreField))
|
|
|
|
doc.AddField(document.NewNumericFieldWithIndexingOptions("age", []uint64{}, 35.99, document.IndexField|document.StoreField))
|
|
|
|
dateField, err = document.NewDateTimeFieldWithIndexingOptions("unixEpoch", []uint64{}, time.Unix(0, 0), document.IndexField|document.StoreField)
|
2014-08-19 16:02:33 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
doc.AddField(dateField)
|
2014-08-15 19:12:55 +02:00
|
|
|
err = idx.Update(doc)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error updating index: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fieldsCount := 0
|
|
|
|
fieldsRows := idx.DumpFields()
|
|
|
|
for _ = range fieldsRows {
|
|
|
|
fieldsCount++
|
|
|
|
}
|
|
|
|
if fieldsCount != 3 {
|
|
|
|
t.Errorf("expected 3 fields, got %d", fieldsCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1 text term
|
|
|
|
// 16 numeric terms
|
|
|
|
// 16 date terms
|
|
|
|
// 3 stored fields
|
2014-09-03 19:02:10 +02:00
|
|
|
expectedDocRowCount := int(1 + (2 * (64 / document.DefaultPrecisionStep)) + 3)
|
2014-08-15 19:12:55 +02:00
|
|
|
docRowCount := 0
|
|
|
|
docRows := idx.DumpDoc("1")
|
|
|
|
for _ = range docRows {
|
|
|
|
docRowCount++
|
|
|
|
}
|
|
|
|
if docRowCount != expectedDocRowCount {
|
|
|
|
t.Errorf("expected %d rows for document, got %d", expectedDocRowCount, docRowCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
docRowCount = 0
|
|
|
|
docRows = idx.DumpDoc("2")
|
|
|
|
for _ = range docRows {
|
|
|
|
docRowCount++
|
|
|
|
}
|
|
|
|
if docRowCount != expectedDocRowCount {
|
|
|
|
t.Errorf("expected %d rows for document, got %d", expectedDocRowCount, docRowCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1 version
|
|
|
|
// fieldsCount field rows
|
|
|
|
// 2 docs * expectedDocRowCount
|
|
|
|
// 2 back index rows
|
|
|
|
// 2 text term row count (2 different text terms)
|
|
|
|
// 16 numeric term row counts (shared for both docs, same numeric value)
|
|
|
|
// 16 date term row counts (shared for both docs, same date value)
|
2014-09-03 19:02:10 +02:00
|
|
|
expectedAllRowCount := int(1 + fieldsCount + (2 * expectedDocRowCount) + 2 + 2 + int((2 * (64 / document.DefaultPrecisionStep))))
|
2014-08-15 19:12:55 +02:00
|
|
|
allRowCount := 0
|
|
|
|
allRows := idx.DumpAll()
|
|
|
|
for _ = range allRows {
|
|
|
|
allRowCount++
|
|
|
|
}
|
|
|
|
if allRowCount != expectedAllRowCount {
|
|
|
|
t.Errorf("expected %d rows for all, got %d", expectedAllRowCount, allRowCount)
|
|
|
|
}
|
|
|
|
}
|