lucy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mar...@apache.org
Subject [4/5] lucy git commit: Adapt to changes from CLOWNFISH-28 take 2.
Date Tue, 07 Apr 2015 00:03:58 GMT
Adapt to changes from CLOWNFISH-28 take 2.

*   Each non-inert class gets mapped to a private struct and a public
    interface.
*   WRAP* and finalize functions autogenerated.


Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/85f3cd67
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/85f3cd67
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/85f3cd67

Branch: refs/heads/master
Commit: 85f3cd677bad093f4f570c38fcfc59c7a5dc96ec
Parents: 81e861c
Author: Marvin Humphrey <marvin@rectangular.com>
Authored: Sat Mar 28 21:12:02 2015 -0700
Committer: Marvin Humphrey <marvin@rectangular.com>
Committed: Sat Apr 4 17:51:29 2015 -0700

----------------------------------------------------------------------
 go/lucy/analysis.go          | 29 ++++++++++-----
 go/lucy/index.go             | 77 +++++++++++++++++++++++++--------------
 go/lucy/lucy_test.go         |  2 +-
 go/lucy/plan.go              | 62 +++++++++++++++++++++----------
 go/lucy/search.go            | 77 ++++++++++++++++++++++++++++-----------
 go/sample/getting_started.go |  4 +-
 6 files changed, 170 insertions(+), 81 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/85f3cd67/go/lucy/analysis.go
----------------------------------------------------------------------
diff --git a/go/lucy/analysis.go b/go/lucy/analysis.go
index 679643c..df3f341 100644
--- a/go/lucy/analysis.go
+++ b/go/lucy/analysis.go
@@ -31,28 +31,39 @@ type Analyzer interface {
 	ToAnalyzerPtr() uintptr
 }
 
-type EasyAnalyzer struct {
+type implAnalyzer struct {
+	ref *C.lucy_Analyzer
+}
+
+type EasyAnalyzer interface {
+	Analyzer
+}
+
+type implEasyAnalyzer struct {
 	ref *C.lucy_EasyAnalyzer
 }
 
-func NewEasyAnalyzer(language string) *EasyAnalyzer {
+func NewEasyAnalyzer(language string) EasyAnalyzer {
 	lang := clownfish.NewString(language)
-	obj := &EasyAnalyzer{
-		C.lucy_EasyAnalyzer_new((*C.cfish_String)(unsafe.Pointer(lang.ToPtr()))),
-	}
-	runtime.SetFinalizer(obj, (*EasyAnalyzer).finalize)
+	cfObj := C.lucy_EasyAnalyzer_new((*C.cfish_String)(unsafe.Pointer(lang.ToPtr())))
+	return WRAPEasyAnalyzer(unsafe.Pointer(cfObj))
+}
+
+func WRAPEasyAnalyzer(ptr unsafe.Pointer) EasyAnalyzer {
+	obj := &implEasyAnalyzer{(*C.lucy_EasyAnalyzer)(ptr)}
+	runtime.SetFinalizer(obj, (*implEasyAnalyzer).finalize)
 	return obj
 }
 
-func (obj *EasyAnalyzer) finalize() {
+func (obj *implEasyAnalyzer) finalize() {
 	C.cfish_dec_refcount(unsafe.Pointer(obj.ref))
 	obj.ref = nil
 }
 
-func (obj *EasyAnalyzer) ToPtr() uintptr {
+func (obj *implEasyAnalyzer) ToPtr() uintptr {
 	return uintptr(unsafe.Pointer(obj.ref))
 }
 
-func (obj *EasyAnalyzer) ToAnalyzerPtr() uintptr {
+func (obj *implEasyAnalyzer) ToAnalyzerPtr() uintptr {
 	return obj.ToPtr()
 }

http://git-wip-us.apache.org/repos/asf/lucy/blob/85f3cd67/go/lucy/index.go
----------------------------------------------------------------------
diff --git a/go/lucy/index.go b/go/lucy/index.go
index 7440d8b..b72e487 100644
--- a/go/lucy/index.go
+++ b/go/lucy/index.go
@@ -34,27 +34,38 @@ import "strings"
 import "unsafe"
 import "git-wip-us.apache.org/repos/asf/lucy-clownfish.git/runtime/go/clownfish"
 
-type Indexer struct {
+type Indexer interface {
+	clownfish.Obj
+	Close() error
+	AddDoc(doc interface{}) error
+	Commit() error
+}
+
+type implIndexer struct {
 	ref        *C.lucy_Indexer
-	fieldNames map[string]*clownfish.String
+	fieldNames map[string]clownfish.String
+}
+
+type IndexManager interface {
+	clownfish.Obj
 }
 
-type IndexManager struct {
+type implIndexManager struct {
 	ref *C.lucy_IndexManager
 }
 
 type OpenIndexerArgs struct {
-	Schema   *Schema
+	Schema   Schema
 	Index    interface{}
-	Manager  *IndexManager
+	Manager  IndexManager
 	Create   bool
 	Truncate bool
 }
 
-func OpenIndexer(args *OpenIndexerArgs) (obj *Indexer, err error) {
+func OpenIndexer(args *OpenIndexerArgs) (obj Indexer, err error) {
 	var schemaC *C.lucy_Schema = nil
 	if args.Schema != nil {
-		schemaC = args.Schema.ref
+		schemaC = (*C.lucy_Schema)(unsafe.Pointer(args.Schema.ToPtr()))
 	}
 	switch args.Index.(type) {
 	case string:
@@ -64,7 +75,7 @@ func OpenIndexer(args *OpenIndexerArgs) (obj *Indexer, err error) {
 	ixLoc := clownfish.NewString(args.Index.(string))
 	var managerC *C.lucy_IndexManager = nil
 	if args.Manager != nil {
-		managerC = args.Manager.ref
+		managerC = (*C.lucy_IndexManager)(unsafe.Pointer(args.Manager.ToPtr()))
 	}
 	var flags int32
 	if args.Create {
@@ -74,22 +85,25 @@ func OpenIndexer(args *OpenIndexerArgs) (obj *Indexer, err error) {
 		flags = flags | int32(C.lucy_Indexer_TRUNCATE)
 	}
 	err = clownfish.TrapErr(func() {
-		obj = &Indexer{
-			C.lucy_Indexer_new(schemaC,
-				(*C.cfish_Obj)(unsafe.Pointer(ixLoc.ToPtr())),
-				managerC, C.int32_t(flags)),
-			nil,
-		}
-		runtime.SetFinalizer(obj, (*Indexer).finalize)
+		cfObj := C.lucy_Indexer_new(schemaC,
+			(*C.cfish_Obj)(unsafe.Pointer(ixLoc.ToPtr())),
+			managerC, C.int32_t(flags))
+		obj = WRAPIndexer(unsafe.Pointer(cfObj))
 	})
 	return obj, err
 }
 
-func (obj *Indexer) finalize() {
+func WRAPIndexer(ptr unsafe.Pointer) Indexer {
+	obj := &implIndexer{(*C.lucy_Indexer)(ptr), nil}
+	runtime.SetFinalizer(obj, (*implIndexer).finalize)
+	return obj
+}
+
+func (obj *implIndexer) finalize() {
 	obj.Close()
 }
 
-func (obj *Indexer) Close() error {
+func (obj *implIndexer) Close() error {
 	// TODO: implement Close in core Lucy rather than bindings.
 	if obj.ref != nil {
 		C.cfish_dec_refcount(unsafe.Pointer(obj.ref))
@@ -98,7 +112,7 @@ func (obj *Indexer) Close() error {
 	return nil // TODO catch errors
 }
 
-func (obj *Indexer) AddDoc(doc interface{}) error {
+func (obj *implIndexer) AddDoc(doc interface{}) error {
 	stockDoc := C.LUCY_Indexer_Get_Stock_Doc(obj.ref)
 	docFields := (*C.cfish_Hash)(C.LUCY_Doc_Get_Fields(stockDoc))
 	C.CFISH_Hash_Clear(docFields)
@@ -116,7 +130,7 @@ func (obj *Indexer) AddDoc(doc interface{}) error {
 	if docValue == (reflect.Value{}) {
 		mess := fmt.Sprintf("Doc not struct pointer: %v",
 			reflect.TypeOf(doc))
-		return clownfish.NewError(mess)
+		return clownfish.NewErr(mess)
 	}
 	docType := docValue.Type()
 
@@ -126,7 +140,7 @@ func (obj *Indexer) AddDoc(doc interface{}) error {
 		fieldC := obj.findFieldC(field)
 		valueC := clownfish.NewString(value)
 		C.CFISH_Hash_Store(docFields,
-			(*C.cfish_Obj)(unsafe.Pointer(fieldC.ToPtr())),
+			(*C.cfish_Obj)(unsafe.Pointer(fieldC)),
 			C.cfish_inc_refcount(unsafe.Pointer(valueC.ToPtr())))
 	}
 
@@ -140,11 +154,11 @@ func (obj *Indexer) AddDoc(doc interface{}) error {
 	return err
 }
 
-func (obj *Indexer) findFieldC(name string) *clownfish.String {
+func (obj *implIndexer) findFieldC(name string) *C.cfish_String {
 	if obj.fieldNames == nil {
-		obj.fieldNames = make(map[string]*clownfish.String)
+		obj.fieldNames = make(map[string]clownfish.String)
 	}
-	fieldC, ok := obj.fieldNames[name]
+	f, ok := obj.fieldNames[name]
 	if !ok {
 		schema := C.LUCY_Indexer_Get_Schema(obj.ref)
 		fieldList := C.LUCY_Schema_All_Fields(schema)
@@ -153,16 +167,25 @@ func (obj *Indexer) findFieldC(name string) *clownfish.String {
 			cfString := unsafe.Pointer(C.CFISH_VA_Fetch(fieldList, C.uint32_t(i)))
 			field := clownfish.CFStringToGo(cfString)
 			if strings.EqualFold(name, field) {
-				obj.fieldNames[name] = clownfish.NewString(field)
-				fieldC = obj.fieldNames[name]
+				C.cfish_inc_refcount(cfString)
+				f = clownfish.WRAPString(cfString)
+				obj.fieldNames[name] = f
 			}
 		}
 	}
-	return fieldC
+	return (*C.cfish_String)(unsafe.Pointer(f.ToPtr()))
 }
 
-func (obj *Indexer) Commit() error {
+func (obj *implIndexer) Commit() error {
 	return clownfish.TrapErr(func() {
 		C.LUCY_Indexer_Commit(obj.ref)
 	})
 }
+
+func (obj *implIndexer) ToPtr() uintptr {
+	return uintptr(unsafe.Pointer(obj.ref))
+}
+
+func (obj *implIndexManager) ToPtr() uintptr {
+	return uintptr(unsafe.Pointer(obj.ref))
+}

http://git-wip-us.apache.org/repos/asf/lucy/blob/85f3cd67/go/lucy/lucy_test.go
----------------------------------------------------------------------
diff --git a/go/lucy/lucy_test.go b/go/lucy/lucy_test.go
index 19b13bf..e436c2a 100644
--- a/go/lucy/lucy_test.go
+++ b/go/lucy/lucy_test.go
@@ -26,7 +26,7 @@ func TestStuff(t *testing.T) {
 
 func TestOpenIndexer(t *testing.T) {
 	_, err := lucy.OpenIndexer(&lucy.OpenIndexerArgs{Index: "notalucyindex"})
-	if _, ok := err.(*clownfish.Err); !ok {
+	if _, ok := err.(clownfish.Err); !ok {
 		t.Error("Didn't catch exception opening indexer")
 	}
 }

http://git-wip-us.apache.org/repos/asf/lucy/blob/85f3cd67/go/lucy/plan.go
----------------------------------------------------------------------
diff --git a/go/lucy/plan.go b/go/lucy/plan.go
index dbc9d5f..e8dff12 100644
--- a/go/lucy/plan.go
+++ b/go/lucy/plan.go
@@ -26,7 +26,12 @@ import "unsafe"
 
 import "git-wip-us.apache.org/repos/asf/lucy-clownfish.git/runtime/go/clownfish"
 
-type Schema struct {
+type Schema interface {
+	clownfish.Obj
+	SpecField(field string, fieldType FieldType)
+}
+
+type implSchema struct {
 	ref *C.lucy_Schema
 }
 
@@ -35,49 +40,66 @@ type FieldType interface {
 	ToFieldTypePtr() uintptr
 }
 
-type FullTextType struct {
+type implFieldType struct {
+	ref *C.lucy_FieldType
+}
+
+type FullTextType interface {
+	FieldType
+}
+
+type implFullTextType struct {
 	ref *C.lucy_FullTextType
 }
 
-func NewSchema() *Schema {
-	obj := &Schema{
-		C.lucy_Schema_new(),
-	}
-	runtime.SetFinalizer(obj, (*Schema).finalize)
+func NewSchema() Schema {
+	cfObj := C.lucy_Schema_new()
+	return WRAPSchema(unsafe.Pointer(cfObj))
+}
+
+func WRAPSchema(ptr unsafe.Pointer) Schema {
+	obj := &implSchema{(*C.lucy_Schema)(ptr)}
+	runtime.SetFinalizer(obj, (*implSchema).finalize)
 	return obj
 }
 
-func (obj *Schema) finalize() {
+func (obj *implSchema) finalize() {
 	C.cfish_dec_refcount(unsafe.Pointer(obj.ref))
 	obj.ref = nil
 }
 
-func (obj *Schema) SpecField(field string, fieldType FieldType) {
+func (obj *implSchema) SpecField(field string, fieldType FieldType) {
 	fieldCF := clownfish.NewString(field)
 	C.LUCY_Schema_Spec_Field(obj.ref,
 		(*C.cfish_String)(unsafe.Pointer(fieldCF.ToPtr())),
-		(*C.lucy_FieldType)(unsafe.Pointer(fieldType.ToFieldTypePtr())))
+		(*C.lucy_FieldType)(unsafe.Pointer(fieldType.ToPtr())))
+}
+
+func (obj *implSchema) ToPtr() uintptr {
+	return uintptr(unsafe.Pointer(obj.ref))
+}
+
+func NewFullTextType(analyzer Analyzer) FullTextType {
+	cfObj := C.lucy_FullTextType_new(
+		(*C.lucy_Analyzer)(unsafe.Pointer(analyzer.ToPtr())))
+	return WRAPFullTextType(unsafe.Pointer(cfObj))
 }
 
-func NewFullTextType(analyzer Analyzer) *FullTextType {
-	obj := &FullTextType{
-		C.lucy_FullTextType_new(
-			(*C.lucy_Analyzer)(unsafe.Pointer(analyzer.ToAnalyzerPtr())),
-		),
-	}
-	runtime.SetFinalizer(obj, (*FullTextType).finalize)
+func WRAPFullTextType(ptr unsafe.Pointer) FullTextType {
+	obj := &implFullTextType{(*C.lucy_FullTextType)(ptr)}
+	runtime.SetFinalizer(obj, (*implFullTextType).finalize)
 	return obj
 }
 
-func (obj *FullTextType) finalize() {
+func (obj *implFullTextType) finalize() {
 	C.cfish_dec_refcount(unsafe.Pointer(obj.ref))
 	obj.ref = nil
 }
 
-func (obj *FullTextType) ToPtr() uintptr {
+func (obj *implFullTextType) ToPtr() uintptr {
 	return uintptr(unsafe.Pointer(obj.ref))
 }
 
-func (obj *FullTextType) ToFieldTypePtr() uintptr {
+func (obj *implFullTextType) ToFieldTypePtr() uintptr {
 	return obj.ToPtr()
 }

http://git-wip-us.apache.org/repos/asf/lucy/blob/85f3cd67/go/lucy/search.go
----------------------------------------------------------------------
diff --git a/go/lucy/search.go b/go/lucy/search.go
index cf3c3e6..aa68cec 100644
--- a/go/lucy/search.go
+++ b/go/lucy/search.go
@@ -40,26 +40,45 @@ type Query interface {
 	ToQueryPtr() uintptr
 }
 
+type implQuery struct {
+	ref *C.lucy_Query
+}
+
 type Searcher interface {
 	clownfish.Obj
 	ToSearcherPtr() uintptr
-	Hits(query interface{}, offset uint32, numWanted uint32, sortSpec *SortSpec) (*Hits, error)
+	Hits(query interface{}, offset uint32, numWanted uint32, sortSpec SortSpec) (Hits, error)
+	Close() error
+}
+
+type Hits interface {
+	clownfish.Obj
+	Next(hit interface{}) bool
+	Error() error
 }
 
-type Hits struct {
+type implHits struct {
 	ref *C.lucy_Hits
 	err error
 }
 
-type SortSpec struct {
+type SortSpec interface {
+	clownfish.Obj
+}
+
+type implSortSpec struct {
 	ref *C.lucy_SortSpec
 }
 
-type IndexSearcher struct {
+type IndexSearcher interface {
+	Searcher
+}
+
+type implIndexSearcher struct {
 	ref *C.lucy_IndexSearcher
 }
 
-func OpenIndexSearcher(index interface{}) (obj *IndexSearcher, err error) {
+func OpenIndexSearcher(index interface{}) (obj IndexSearcher, err error) {
 	var indexC *C.cfish_Obj
 	switch index.(type) {
 	case string:
@@ -69,55 +88,69 @@ func OpenIndexSearcher(index interface{}) (obj *IndexSearcher, err error)
{
 		panic("TODO: support Folder")
 	}
 	err = clownfish.TrapErr(func() {
-		obj = &IndexSearcher{C.lucy_IxSearcher_new(indexC)}
+		cfObj := C.lucy_IxSearcher_new(indexC)
+		obj = WRAPIndexSearcher(unsafe.Pointer(cfObj))
 	})
-	runtime.SetFinalizer(obj, (*IndexSearcher).finalize)
 	return obj, err
 }
 
-func (obj *IndexSearcher) finalize() {
+func WRAPIndexSearcher(ptr unsafe.Pointer) IndexSearcher {
+	obj := &implIndexSearcher{(*C.lucy_IndexSearcher)(ptr)}
+	runtime.SetFinalizer(obj, (*implIndexSearcher).finalize)
+	return obj
+}
+
+func (obj *implIndexSearcher) finalize() {
 	C.cfish_dec_refcount(unsafe.Pointer(obj.ref))
 	obj.ref = nil
 }
 
-func (obj *IndexSearcher) Close() error {
+func (obj *implIndexSearcher) Close() error {
 	return clownfish.TrapErr(func() {
 		C.LUCY_IxSearcher_Close(obj.ref)
 	})
 }
 
-func (obj *IndexSearcher) ToPtr() uintptr {
+func (obj *implIndexSearcher) ToPtr() uintptr {
 	return uintptr(unsafe.Pointer(obj.ref))
 }
 
-func (obj *IndexSearcher) ToSearcherPtr() uintptr {
+func (obj *implIndexSearcher) ToSearcherPtr() uintptr {
 	return obj.ToPtr()
 }
 
-func (obj *IndexSearcher) Hits(query interface{}, offset uint32, numWanted uint32,
-	sortSpec *SortSpec) (hits *Hits, err error) {
-	var hitsC *C.lucy_Hits
+func (obj *implIndexSearcher) Hits(query interface{}, offset uint32, numWanted uint32,
+	sortSpec SortSpec) (hits Hits, err error) {
 	var sortSpecC *C.lucy_SortSpec
 	if sortSpec != nil {
-		sortSpecC = sortSpec.ref
+		sortSpecC = (*C.lucy_SortSpec)(unsafe.Pointer(sortSpec.ToPtr()))
 	}
 	switch query.(type) {
 	case string:
 		queryStringC := clownfish.NewString(query.(string))
 		err = clownfish.TrapErr(func() {
-			hitsC = C.LUCY_IxSearcher_Hits(obj.ref,
+			hitsC := C.LUCY_IxSearcher_Hits(obj.ref,
 				(*C.cfish_Obj)(unsafe.Pointer(queryStringC.ToPtr())),
 				C.uint32_t(offset), C.uint32_t(numWanted), sortSpecC)
+			hits = WRAPHits(unsafe.Pointer(hitsC))
 		})
 	default:
 		panic("TODO: support Query objects")
 	}
-	hits = &Hits{hitsC, nil}
-	runtime.SetFinalizer(hits, (*Hits).finalize)
 	return hits, err
 }
 
-func (obj *Hits) Next(hit interface{}) bool {
+func WRAPHits(ptr unsafe.Pointer) Hits {
+	obj := &implHits{(*C.lucy_Hits)(ptr), nil}
+	runtime.SetFinalizer(obj, (*implHits).finalize)
+	return obj
+}
+
+func (obj *implHits) ToPtr() uintptr {
+	return uintptr(unsafe.Pointer(obj.ref))
+}
+
+func (obj *implHits) Next(hit interface{}) bool {
 	// TODO: accept a HitDoc object and populate score.
 
 	// Get reflection value and type for the supplied struct.
@@ -133,7 +166,7 @@ func (obj *Hits) Next(hit interface{}) bool {
 	if hitValue == (reflect.Value{}) {
 		mess := fmt.Sprintf("Arg not writeable struct pointer: %v",
 			reflect.TypeOf(hit))
-		obj.err = clownfish.NewError(mess)
+		obj.err = clownfish.NewErr(mess)
 		return false
 	}
 
@@ -169,11 +202,11 @@ func (obj *Hits) Next(hit interface{}) bool {
 	return true
 }
 
-func (obj *Hits) finalize() {
+func (obj *implHits) finalize() {
 	C.cfish_dec_refcount(unsafe.Pointer(obj.ref))
 	obj.ref = nil
 }
 
-func (obj *Hits) Error() error {
+func (obj *implHits) Error() error {
 	return obj.err
 }

http://git-wip-us.apache.org/repos/asf/lucy/blob/85f3cd67/go/sample/getting_started.go
----------------------------------------------------------------------
diff --git a/go/sample/getting_started.go b/go/sample/getting_started.go
index fb69b74..a817b63 100644
--- a/go/sample/getting_started.go
+++ b/go/sample/getting_started.go
@@ -41,7 +41,7 @@ func main() {
 	performSearch(searcher, `"fugiat nulla"`)
 }
 
-func createSchema() *lucy.Schema {
+func createSchema() lucy.Schema {
 	// Create a new schema.
 	schema := lucy.NewSchema()
 
@@ -84,7 +84,7 @@ var docs []MyDoc = []MyDoc{
 	},
 }
 
-func indexDocuments(schema *lucy.Schema, index string) {
+func indexDocuments(schema lucy.Schema, index string) {
 	indexerArgs := &lucy.OpenIndexerArgs{
 		Schema:   schema,
 		Index:    index,


Mime
View raw message