Tune and test Go bindings for DocReader.
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/c1cdb4d7 Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/c1cdb4d7 Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/c1cdb4d7 Branch: refs/heads/master Commit: c1cdb4d7912d0105ad5e00491769111eb8991d0b Parents: 38bea5a Author: Marvin Humphrey <[email protected]> Authored: Tue Dec 8 19:17:24 2015 -0800 Committer: Marvin Humphrey <[email protected]> Committed: Thu Dec 10 18:22:53 2015 -0800 ---------------------------------------------------------------------- go/build.go | 6 ++++++ go/lucy/index.go | 23 +++++++++++++++++++++++ go/lucy/index_test.go | 23 +++++++++++++++++++++++ go/lucy/lucy.go | 21 ++------------------- go/lucy/search.go | 2 +- 5 files changed, 55 insertions(+), 20 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy/blob/c1cdb4d7/go/build.go ---------------------------------------------------------------------- diff --git a/go/build.go b/go/build.go index 2bdab37..801654a 100644 --- a/go/build.go +++ b/go/build.go @@ -191,6 +191,12 @@ func specClasses(parcel *cfc.Parcel) { dataReaderBinding.SpecMethod("Close", "Close() error") dataReaderBinding.Register() + + docReaderBinding := cfc.NewGoClass(parcel, "Lucy::Index::DocReader") + docReaderBinding.SpecMethod("", "ReadDoc(int32, interface{}) error") + docReaderBinding.SpecMethod("Fetch_Doc", "FetchDoc(int32) (HitDoc, error)") + docReaderBinding.Register() + bgMergerBinding := cfc.NewGoClass(parcel, "Lucy::Index::BackgroundMerger") bgMergerBinding.SpecMethod("Prepare_Commit", "PrepareCommit() error") bgMergerBinding.SpecMethod("Commit", "Commit() error") http://git-wip-us.apache.org/repos/asf/lucy/blob/c1cdb4d7/go/lucy/index.go ---------------------------------------------------------------------- diff --git a/go/lucy/index.go b/go/lucy/index.go index defa9f2..18d5ba1 100644 --- a/go/lucy/index.go +++ b/go/lucy/index.go @@ -19,6 +19,7 @@ package lucy /* #include "Lucy/Index/Indexer.h" #include "Lucy/Index/DataReader.h" +#include "Lucy/Index/DocReader.h" #include "Lucy/Index/IndexManager.h" #include "Lucy/Index/BackgroundMerger.h" #include "Lucy/Index/TermVector.h" @@ -459,3 +460,25 @@ func (d *DataReaderIMP) Close() error { C.LUCY_DataReader_Close(self) }) } + +func (d *DocReaderIMP) ReadDoc(docID int32, doc interface{}) error { + self := (*C.lucy_DocReader)(clownfish.Unwrap(d, "d")) + class := clownfish.GetClass(d) + classC := ((*C.cfish_Class)(clownfish.Unwrap(class, "class"))) + if classC == C.LUCY_DEFAULTDOCREADER { + return doReadDocData((*C.lucy_DefaultDocReader)(self), docID, doc) + } else if classC == C.LUCY_POLYDOCREADER { + return readDocPolyDR((*C.lucy_PolyDocReader)(self), docID, doc) + } else { + panic(clownfish.NewErr(fmt.Sprintf("Unexpected type: %s", class.GetName))) + } +} + +func (d *DocReaderIMP) FetchDoc(docID int32) (doc HitDoc, err error) { + err = clownfish.TrapErr(func() { + self := (*C.lucy_DocReader)(clownfish.Unwrap(d, "d")) + docC := C.LUCY_DocReader_Fetch_Doc(self, C.int32_t(docID)) + doc = WRAPHitDoc(unsafe.Pointer(docC)) + }) + return doc, err +} http://git-wip-us.apache.org/repos/asf/lucy/blob/c1cdb4d7/go/lucy/index_test.go ---------------------------------------------------------------------- diff --git a/go/lucy/index_test.go b/go/lucy/index_test.go index 40609e6..8934167 100644 --- a/go/lucy/index_test.go +++ b/go/lucy/index_test.go @@ -750,3 +750,26 @@ func TestIndexReaderMisc(t *testing.T) { reader := searcher.GetReader() runDataReaderCommon(t, reader, false) } + +func TestDefaultDocReaderMisc(t *testing.T) { + folder := createTestIndex("a", "b", "c") + searcher, _ := OpenIndexSearcher(folder) + segReaders := searcher.GetReader().SegReaders() + reader := segReaders[0].(SegReader).Obtain("Lucy::Index::DocReader").(DefaultDocReader) + doc := make(map[string]interface{}) + if err := reader.ReadDoc(2, doc); err != nil { + t.Errorf("ReadDoc: %v", err) + } + runDataReaderCommon(t, reader, true) +} + +func TestPolyDocReaderMisc(t *testing.T) { + folder := createTestIndex("a", "b", "c") + searcher, _ := OpenIndexSearcher(folder) + reader := searcher.GetReader().Obtain("Lucy::Index::DocReader").(PolyDocReader) + doc := make(map[string]interface{}) + if err := reader.ReadDoc(2, doc); err != nil { + t.Errorf("ReadDoc: %v", err) + } + runDataReaderCommon(t, reader, true) +} http://git-wip-us.apache.org/repos/asf/lucy/blob/c1cdb4d7/go/lucy/lucy.go ---------------------------------------------------------------------- diff --git a/go/lucy/lucy.go b/go/lucy/lucy.go index 03307bb..175ceee 100644 --- a/go/lucy/lucy.go +++ b/go/lucy/lucy.go @@ -383,20 +383,8 @@ func fetchEntry(ivars *C.lucy_InverterIVARS, fieldGo string) *C.lucy_InverterEnt return (*C.lucy_InverterEntry)(unsafe.Pointer(entry)) } -func fetchDocFromDocReader(dr DocReader, docID int32, doc interface{}) error { - switch v := dr.(type) { - case *DefaultDocReaderIMP: - return v.readDoc(docID, doc) - case *PolyDocReaderIMP: - return v.readDoc(docID, doc) - default: - panic(clownfish.NewErr(fmt.Sprintf("Unexpected type: %T", v))) - } -} - -func (pdr *PolyDocReaderIMP) readDoc(docID int32, doc interface{}) error { - self := (*C.lucy_PolyDocReader)(clownfish.Unwrap(pdr, "pdr")) - ivars := C.lucy_PolyDocReader_IVARS(self) +func readDocPolyDR(pdr *C.lucy_PolyDocReader, docID int32, doc interface{}) error { + ivars := C.lucy_PolyDocReader_IVARS(pdr) segTick := C.lucy_PolyReader_sub_tick(ivars.offsets, C.int32_t(docID)) offset := C.LUCY_I32Arr_Get(ivars.offsets, segTick) defDocReader := (*C.lucy_DefaultDocReader)(C.CFISH_Vec_Fetch(ivars.readers, C.size_t(segTick))) @@ -414,11 +402,6 @@ func (pdr *PolyDocReaderIMP) readDoc(docID int32, doc interface{}) error { return err } -func (ddr *DefaultDocReaderIMP) readDoc(docID int32, doc interface{}) error { - self := (*C.lucy_DefaultDocReader)(clownfish.Unwrap(ddr, "ddr")) - return doReadDocData(self, docID, doc) -} - func setMapField(store interface{}, field string, val interface{}) error { m := store.(map[string]interface{}) m[field] = val http://git-wip-us.apache.org/repos/asf/lucy/blob/c1cdb4d7/go/lucy/search.go ---------------------------------------------------------------------- diff --git a/go/lucy/search.go b/go/lucy/search.go index e202b9d..3f7845c 100644 --- a/go/lucy/search.go +++ b/go/lucy/search.go @@ -88,7 +88,7 @@ func (s *SearcherIMP) ReadDoc(docID int32, doc interface{}) error { return clownfish.NewErr("No DocReader available") } docReaderGo := clownfish.WRAPAny(unsafe.Pointer(C.cfish_incref(unsafe.Pointer(docReader)))).(DocReader) - return fetchDocFromDocReader(docReaderGo, docID, doc) + return docReaderGo.ReadDoc(docID, doc) } else { return clownfish.NewErr("Support for ReadDoc not implemented") }
