Tune and test Go bindings for TermVector.
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/85790f34 Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/85790f34 Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/85790f34 Branch: refs/heads/LUCY-282-test-index-go-pt1 Commit: 85790f34e03203a57360b1c00b6fb86c2561db4c Parents: e264f6f Author: Marvin Humphrey <[email protected]> Authored: Fri Oct 30 17:41:31 2015 -0700 Committer: Marvin Humphrey <[email protected]> Committed: Fri Oct 30 17:41:31 2015 -0700 ---------------------------------------------------------------------- go/build.go | 7 +++++++ go/lucy/index.go | 31 +++++++++++++++++++++++++++++++ go/lucy/index_test.go | 29 +++++++++++++++++++++++++++++ go/lucy/object.go | 9 +++++++++ 4 files changed, 76 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy/blob/85790f34/go/build.go ---------------------------------------------------------------------- diff --git a/go/build.go b/go/build.go index 52de435..75fe5b5 100644 --- a/go/build.go +++ b/go/build.go @@ -170,6 +170,13 @@ func specClasses(parcel *cfc.Parcel) { managerBinding.SpecMethod("Recycle", "Recycle(PolyReader, DeletionsWriter, int64, bool) ([]SegReader, error)") managerBinding.Register() + tvBinding := cfc.NewGoClass(parcel, "Lucy::Index::TermVector") + tvBinding.SpecMethod("Get_Positions", "GetPositions() []int32") + tvBinding.SpecMethod("Get_Start_Offsets", "GetStartOffsets() []int32") + tvBinding.SpecMethod("Get_End_Offsets", "GetEndOffsets() []int32") + tvBinding.SetSuppressCtor(true) + tvBinding.Register() + schemaBinding := cfc.NewGoClass(parcel, "Lucy::Plan::Schema") schemaBinding.SpecMethod("All_Fields", "AllFields() []string") schemaBinding.Register() http://git-wip-us.apache.org/repos/asf/lucy/blob/85790f34/go/lucy/index.go ---------------------------------------------------------------------- diff --git a/go/lucy/index.go b/go/lucy/index.go index ba26f95..43dc90c 100644 --- a/go/lucy/index.go +++ b/go/lucy/index.go @@ -20,6 +20,7 @@ package lucy #include "Lucy/Index/Indexer.h" #include "Lucy/Index/IndexManager.h" #include "Lucy/Index/BackgroundMerger.h" +#include "Lucy/Index/TermVector.h" #include "Lucy/Document/Doc.h" #include "Lucy/Plan/Schema.h" #include "Clownfish/Hash.h" @@ -307,3 +308,33 @@ func (im *IndexManagerIMP) Recycle(reader PolyReader, delWriter DeletionsWriter, }) return retval, err } + +func NewTermVector(field, text string, positions, startOffsets, endOffsets []int32) TermVector { + fieldC := (*C.cfish_String)(clownfish.GoToClownfish(field, unsafe.Pointer(C.CFISH_STRING), false)) + textC := (*C.cfish_String)(clownfish.GoToClownfish(text, unsafe.Pointer(C.CFISH_STRING), false)) + defer C.cfish_decref(unsafe.Pointer(fieldC)) + defer C.cfish_decref(unsafe.Pointer(textC)) + posits := NewI32Array(positions) + starts := NewI32Array(startOffsets) + ends := NewI32Array(endOffsets) + positsC := (*C.lucy_I32Array)(clownfish.Unwrap(posits, "posits")) + startsC := (*C.lucy_I32Array)(clownfish.Unwrap(starts, "starts")) + endsC := (*C.lucy_I32Array)(clownfish.Unwrap(ends, "ends")) + retvalC := C.lucy_TV_new(fieldC, textC, positsC, startsC, endsC) + return WRAPTermVector(unsafe.Pointer(retvalC)) +} + +func (tv *TermVectorIMP) GetPositions() []int32 { + self := (*C.lucy_TermVector)(clownfish.Unwrap(tv, "tv")) + return i32ArrayToSlice(C.LUCY_TV_Get_Positions(self)) +} + +func (tv *TermVectorIMP) GetStartOffsets() []int32 { + self := (*C.lucy_TermVector)(clownfish.Unwrap(tv, "tv")) + return i32ArrayToSlice(C.LUCY_TV_Get_Start_Offsets(self)) +} + +func (tv *TermVectorIMP) GetEndOffsets() []int32 { + self := (*C.lucy_TermVector)(clownfish.Unwrap(tv, "tv")) + return i32ArrayToSlice(C.LUCY_TV_Get_End_Offsets(self)) +} http://git-wip-us.apache.org/repos/asf/lucy/blob/85790f34/go/lucy/index_test.go ---------------------------------------------------------------------- diff --git a/go/lucy/index_test.go b/go/lucy/index_test.go index ba9a009..bdfe05a 100644 --- a/go/lucy/index_test.go +++ b/go/lucy/index_test.go @@ -21,6 +21,8 @@ import "os" import "reflect" import "strings" +import "git-wip-us.apache.org/repos/asf/lucy-clownfish.git/runtime/go/clownfish" + func TestIndexerAddDoc(t *testing.T) { schema := createTestSchema() index := NewRAMFolder("") @@ -294,3 +296,30 @@ func TestBitVecDelDocsMisc(t *testing.T) { t.Errorf("Get returned false") } } + +func TestTermVectorMisc(t *testing.T) { + + positions := []int32{0, 3} + startOffsets := []int32{0, 20} + endOffsets := []int32{2, 22} + tv := NewTermVector("content", "red yellow green red blue", positions, startOffsets, endOffsets) + if got := tv.GetPositions(); !reflect.DeepEqual(got, positions) { + t.Errorf("GetPositions: %v", got) + } + if got := tv.GetStartOffsets(); !reflect.DeepEqual(got, startOffsets) { + t.Errorf("GetStartOffsets: %v", got) + } + if got := tv.GetEndOffsets(); !reflect.DeepEqual(got, endOffsets) { + t.Errorf("GetEndOffsets: %v", got) + } + + folder := NewRAMFolder("") + out, _ := folder.OpenOut("dump") + tv.Serialize(out) + out.Close() + in, _ := folder.OpenIn("dump") + dupe := clownfish.GetClass(tv).MakeObj().(TermVector).Deserialize(in) + if !tv.Equals(dupe) { + t.Errorf("Unsuccessful serialization round trip") + } +} http://git-wip-us.apache.org/repos/asf/lucy/blob/85790f34/go/lucy/object.go ---------------------------------------------------------------------- diff --git a/go/lucy/object.go b/go/lucy/object.go index 78c86f5..bd0998b 100644 --- a/go/lucy/object.go +++ b/go/lucy/object.go @@ -49,3 +49,12 @@ func NewI32Array(nums []int32) I32Array { } return WRAPI32Array(unsafe.Pointer(obj)) } + +func i32ArrayToSlice(a *C.lucy_I32Array) []int32 { + size := int(C.LUCY_I32Arr_Get_Size(a)) + nums := make([]int32, size) + for i := 0; i < size; i++ { + nums[i] = int32(C.LUCY_I32Arr_Get(a, C.uint32_t(i))) + } + return nums +}
