Repository: lucy
Updated Branches:
  refs/heads/master d460538f2 -> 45c870ba2


Adapt for v4 of Go proof-of-concept bindings.

See CLOWNFISH-28-Go-POC-v4.

*   Make structs public: `FooIMP` instead of `implFoo`.
*   Embed parent structs.
*   Make `ref` a `uintptr` instead of a pointer to a C type.
*   Add the INITOBJ method to ObjIMP.
*   Eliminate all downstream finalizers and TOPTR() implementations.


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

Branch: refs/heads/master
Commit: 9330b149f936c9431e1c8f4055b4b6366a552d91
Parents: be1b383
Author: Marvin Humphrey <[email protected]>
Authored: Fri May 1 19:16:04 2015 -0700
Committer: Marvin Humphrey <[email protected]>
Committed: Fri May 1 19:16:40 2015 -0700

----------------------------------------------------------------------
 go/lucy/analysis.go | 22 +++++------------
 go/lucy/index.go    | 48 +++++++++++++-----------------------
 go/lucy/plan.go     | 45 ++++++++++------------------------
 go/lucy/search.go   | 64 ++++++++++++++++++++----------------------------
 4 files changed, 62 insertions(+), 117 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/9330b149/go/lucy/analysis.go
----------------------------------------------------------------------
diff --git a/go/lucy/analysis.go b/go/lucy/analysis.go
index 1da55d6..6fbd522 100644
--- a/go/lucy/analysis.go
+++ b/go/lucy/analysis.go
@@ -21,7 +21,6 @@ package lucy
 #include "Lucy/Analysis/EasyAnalyzer.h"
 */
 import "C"
-import "runtime"
 import "unsafe"
 
 import 
"git-wip-us.apache.org/repos/asf/lucy-clownfish.git/runtime/go/clownfish"
@@ -30,16 +29,16 @@ type Analyzer interface {
        clownfish.Obj
 }
 
-type implAnalyzer struct {
-       ref *C.lucy_Analyzer
+type AnalyzerIMP struct {
+       clownfish.ObjIMP
 }
 
 type EasyAnalyzer interface {
        Analyzer
 }
 
-type implEasyAnalyzer struct {
-       ref *C.lucy_EasyAnalyzer
+type EasyAnalyzerIMP struct {
+       AnalyzerIMP
 }
 
 func NewEasyAnalyzer(language string) EasyAnalyzer {
@@ -49,16 +48,7 @@ func NewEasyAnalyzer(language string) EasyAnalyzer {
 }
 
 func WRAPEasyAnalyzer(ptr unsafe.Pointer) EasyAnalyzer {
-       obj := &implEasyAnalyzer{(*C.lucy_EasyAnalyzer)(ptr)}
-       runtime.SetFinalizer(obj, (*implEasyAnalyzer).finalize)
+       obj := &EasyAnalyzerIMP{}
+       obj.INITOBJ(ptr);
        return obj
 }
-
-func (obj *implEasyAnalyzer) finalize() {
-       C.cfish_dec_refcount(unsafe.Pointer(obj.ref))
-       obj.ref = nil
-}
-
-func (obj *implEasyAnalyzer) TOPTR() uintptr {
-       return uintptr(unsafe.Pointer(obj.ref))
-}

http://git-wip-us.apache.org/repos/asf/lucy/blob/9330b149/go/lucy/index.go
----------------------------------------------------------------------
diff --git a/go/lucy/index.go b/go/lucy/index.go
index 3a14281..9ccce23 100644
--- a/go/lucy/index.go
+++ b/go/lucy/index.go
@@ -29,7 +29,6 @@ package lucy
 import "C"
 import "fmt"
 import "reflect"
-import "runtime"
 import "strings"
 import "unsafe"
 import 
"git-wip-us.apache.org/repos/asf/lucy-clownfish.git/runtime/go/clownfish"
@@ -41,8 +40,8 @@ type Indexer interface {
        Commit() error
 }
 
-type implIndexer struct {
-       ref        *C.lucy_Indexer
+type IndexerIMP struct {
+       clownfish.ObjIMP
        fieldNames map[string]clownfish.String
 }
 
@@ -50,8 +49,8 @@ type IndexManager interface {
        clownfish.Obj
 }
 
-type implIndexManager struct {
-       ref *C.lucy_IndexManager
+type IndexManagerIMP struct {
+       clownfish.ObjIMP
 }
 
 type OpenIndexerArgs struct {
@@ -94,26 +93,19 @@ func OpenIndexer(args *OpenIndexerArgs) (obj Indexer, err 
error) {
 }
 
 func WRAPIndexer(ptr unsafe.Pointer) Indexer {
-       obj := &implIndexer{(*C.lucy_Indexer)(ptr), nil}
-       runtime.SetFinalizer(obj, (*implIndexer).finalize)
+       obj := &IndexerIMP{}
+       obj.INITOBJ(ptr);
        return obj
 }
 
-func (obj *implIndexer) finalize() {
-       obj.Close()
-}
-
-func (obj *implIndexer) Close() error {
+func (obj *IndexerIMP) Close() error {
        // TODO: implement Close in core Lucy rather than bindings.
-       if obj.ref != nil {
-               C.cfish_dec_refcount(unsafe.Pointer(obj.ref))
-               obj.ref = nil
-       }
        return nil // TODO catch errors
 }
 
-func (obj *implIndexer) AddDoc(doc interface{}) error {
-       stockDoc := C.LUCY_Indexer_Get_Stock_Doc(obj.ref)
+func (obj *IndexerIMP) AddDoc(doc interface{}) error {
+       self := ((*C.lucy_Indexer)(unsafe.Pointer(obj.TOPTR())))
+       stockDoc := C.LUCY_Indexer_Get_Stock_Doc(self)
        docFields := (*C.cfish_Hash)(C.LUCY_Doc_Get_Fields(stockDoc))
        C.CFISH_Hash_Clear(docFields)
 
@@ -148,19 +140,20 @@ func (obj *implIndexer) AddDoc(doc interface{}) error {
        // client to supply `boost`.
        boost := 1.0
        err := clownfish.TrapErr(func() {
-               C.LUCY_Indexer_Add_Doc(obj.ref, stockDoc, C.float(boost))
+               C.LUCY_Indexer_Add_Doc(self, stockDoc, C.float(boost))
        })
 
        return err
 }
 
-func (obj *implIndexer) findFieldC(name string) *C.cfish_String {
+func (obj *IndexerIMP) findFieldC(name string) *C.cfish_String {
+       self := ((*C.lucy_Indexer)(unsafe.Pointer(obj.TOPTR())))
        if obj.fieldNames == nil {
                obj.fieldNames = make(map[string]clownfish.String)
        }
        f, ok := obj.fieldNames[name]
        if !ok {
-               schema := C.LUCY_Indexer_Get_Schema(obj.ref)
+               schema := C.LUCY_Indexer_Get_Schema(self)
                fieldList := C.LUCY_Schema_All_Fields(schema)
                defer C.cfish_dec_refcount(unsafe.Pointer(fieldList))
                for i := 0; i < int(C.CFISH_Vec_Get_Size(fieldList)); i++ {
@@ -176,16 +169,9 @@ func (obj *implIndexer) findFieldC(name string) 
*C.cfish_String {
        return (*C.cfish_String)(unsafe.Pointer(f.TOPTR()))
 }
 
-func (obj *implIndexer) Commit() error {
+func (obj *IndexerIMP) Commit() error {
+       self := ((*C.lucy_Indexer)(unsafe.Pointer(obj.TOPTR())))
        return clownfish.TrapErr(func() {
-               C.LUCY_Indexer_Commit(obj.ref)
+               C.LUCY_Indexer_Commit(self)
        })
 }
-
-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/9330b149/go/lucy/plan.go
----------------------------------------------------------------------
diff --git a/go/lucy/plan.go b/go/lucy/plan.go
index a2f4146..aa644b1 100644
--- a/go/lucy/plan.go
+++ b/go/lucy/plan.go
@@ -21,7 +21,6 @@ package lucy
 #include "Lucy/Plan/FullTextType.h"
 */
 import "C"
-import "runtime"
 import "unsafe"
 
 import 
"git-wip-us.apache.org/repos/asf/lucy-clownfish.git/runtime/go/clownfish"
@@ -31,24 +30,24 @@ type Schema interface {
        SpecField(field string, fieldType FieldType)
 }
 
-type implSchema struct {
-       ref *C.lucy_Schema
+type SchemaIMP struct {
+       clownfish.ObjIMP
 }
 
 type FieldType interface {
        clownfish.Obj
 }
 
-type implFieldType struct {
-       ref *C.lucy_FieldType
+type FieldTypeIMP struct {
+       clownfish.ObjIMP
 }
 
 type FullTextType interface {
        FieldType
 }
 
-type implFullTextType struct {
-       ref *C.lucy_FullTextType
+type FullTextTypeIMP struct {
+       FieldTypeIMP
 }
 
 func NewSchema() Schema {
@@ -57,27 +56,19 @@ func NewSchema() Schema {
 }
 
 func WRAPSchema(ptr unsafe.Pointer) Schema {
-       obj := &implSchema{(*C.lucy_Schema)(ptr)}
-       runtime.SetFinalizer(obj, (*implSchema).finalize)
+       obj := &SchemaIMP{}
+       obj.INITOBJ(ptr);
        return obj
 }
 
-func (obj *implSchema) finalize() {
-       C.cfish_dec_refcount(unsafe.Pointer(obj.ref))
-       obj.ref = nil
-}
-
-func (obj *implSchema) SpecField(field string, fieldType FieldType) {
+func (obj *SchemaIMP) SpecField(field string, fieldType FieldType) {
+       self := ((*C.lucy_Schema)(unsafe.Pointer(obj.TOPTR())))
        fieldCF := clownfish.NewString(field)
-       C.LUCY_Schema_Spec_Field(obj.ref,
+       C.LUCY_Schema_Spec_Field(self,
                (*C.cfish_String)(unsafe.Pointer(fieldCF.TOPTR())),
                (*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())))
@@ -85,17 +76,7 @@ func NewFullTextType(analyzer Analyzer) FullTextType {
 }
 
 func WRAPFullTextType(ptr unsafe.Pointer) FullTextType {
-       obj := &implFullTextType{(*C.lucy_FullTextType)(ptr)}
-       runtime.SetFinalizer(obj, (*implFullTextType).finalize)
+       obj := &FullTextTypeIMP{}
+       obj.INITOBJ(ptr);
        return obj
 }
-
-func (obj *implFullTextType) finalize() {
-       C.cfish_dec_refcount(unsafe.Pointer(obj.ref))
-       obj.ref = nil
-}
-
-func (obj *implFullTextType) TOPTR() uintptr {
-       return uintptr(unsafe.Pointer(obj.ref))
-}
-

http://git-wip-us.apache.org/repos/asf/lucy/blob/9330b149/go/lucy/search.go
----------------------------------------------------------------------
diff --git a/go/lucy/search.go b/go/lucy/search.go
index cc80272..fc2a841 100644
--- a/go/lucy/search.go
+++ b/go/lucy/search.go
@@ -29,7 +29,6 @@ package lucy
 import "C"
 import "fmt"
 import "reflect"
-import "runtime"
 import "strings"
 import "unsafe"
 
@@ -39,8 +38,8 @@ type Query interface {
        clownfish.Obj
 }
 
-type implQuery struct {
-       ref *C.lucy_Query
+type QueryIMP struct {
+       clownfish.ObjIMP
 }
 
 type Searcher interface {
@@ -49,14 +48,18 @@ type Searcher interface {
        Close() error
 }
 
+type SearcherIMP struct {
+       clownfish.ObjIMP
+}
+
 type Hits interface {
        clownfish.Obj
        Next(hit interface{}) bool
        Error() error
 }
 
-type implHits struct {
-       ref *C.lucy_Hits
+type HitsIMP struct {
+       clownfish.ObjIMP
        err error
 }
 
@@ -64,16 +67,16 @@ type SortSpec interface {
        clownfish.Obj
 }
 
-type implSortSpec struct {
-       ref *C.lucy_SortSpec
+type SortSpecIMP struct {
+       clownfish.ObjIMP
 }
 
 type IndexSearcher interface {
        Searcher
 }
 
-type implIndexSearcher struct {
-       ref *C.lucy_IndexSearcher
+type IndexSearcherIMP struct {
+       SearcherIMP
 }
 
 func OpenIndexSearcher(index interface{}) (obj IndexSearcher, err error) {
@@ -93,28 +96,21 @@ func OpenIndexSearcher(index interface{}) (obj 
IndexSearcher, err error) {
 }
 
 func WRAPIndexSearcher(ptr unsafe.Pointer) IndexSearcher {
-       obj := &implIndexSearcher{(*C.lucy_IndexSearcher)(ptr)}
-       runtime.SetFinalizer(obj, (*implIndexSearcher).finalize)
+       obj := &IndexSearcherIMP{}
+       obj.INITOBJ(ptr);
        return obj
 }
 
-func (obj *implIndexSearcher) finalize() {
-       C.cfish_dec_refcount(unsafe.Pointer(obj.ref))
-       obj.ref = nil
-}
-
-func (obj *implIndexSearcher) Close() error {
+func (obj *IndexSearcherIMP) Close() error {
+       self := ((*C.lucy_IndexSearcher)(unsafe.Pointer(obj.TOPTR())))
        return clownfish.TrapErr(func() {
-               C.LUCY_IxSearcher_Close(obj.ref)
+               C.LUCY_IxSearcher_Close(self)
        })
 }
 
-func (obj *implIndexSearcher) TOPTR() uintptr {
-       return uintptr(unsafe.Pointer(obj.ref))
-}
-
-func (obj *implIndexSearcher) Hits(query interface{}, offset uint32, numWanted 
uint32,
+func (obj *IndexSearcherIMP) Hits(query interface{}, offset uint32, numWanted 
uint32,
        sortSpec SortSpec) (hits Hits, err error) {
+       self := ((*C.lucy_IndexSearcher)(unsafe.Pointer(obj.TOPTR())))
        var sortSpecC *C.lucy_SortSpec
        if sortSpec != nil {
                sortSpecC = (*C.lucy_SortSpec)(unsafe.Pointer(sortSpec.TOPTR()))
@@ -123,7 +119,7 @@ func (obj *implIndexSearcher) Hits(query interface{}, 
offset uint32, numWanted u
        case string:
                queryStringC := clownfish.NewString(query.(string))
                err = clownfish.TrapErr(func() {
-                       hitsC := C.LUCY_IxSearcher_Hits(obj.ref,
+                       hitsC := C.LUCY_IxSearcher_Hits(self,
                                
(*C.cfish_Obj)(unsafe.Pointer(queryStringC.TOPTR())),
                                C.uint32_t(offset), C.uint32_t(numWanted), 
sortSpecC)
                        hits = WRAPHits(unsafe.Pointer(hitsC))
@@ -135,16 +131,13 @@ func (obj *implIndexSearcher) Hits(query interface{}, 
offset uint32, numWanted u
 }
 
 func WRAPHits(ptr unsafe.Pointer) Hits {
-       obj := &implHits{(*C.lucy_Hits)(ptr), nil}
-       runtime.SetFinalizer(obj, (*implHits).finalize)
+       obj := &HitsIMP{}
+       obj.INITOBJ(ptr);
        return obj
 }
 
-func (obj *implHits) TOPTR() uintptr {
-       return uintptr(unsafe.Pointer(obj.ref))
-}
-
-func (obj *implHits) Next(hit interface{}) bool {
+func (obj *HitsIMP) Next(hit interface{}) bool {
+       self := ((*C.lucy_Hits)(unsafe.Pointer(obj.TOPTR())))
        // TODO: accept a HitDoc object and populate score.
 
        // Get reflection value and type for the supplied struct.
@@ -166,7 +159,7 @@ func (obj *implHits) Next(hit interface{}) bool {
 
        var docC *C.lucy_HitDoc
        errCallingNext := clownfish.TrapErr(func() {
-               docC = C.LUCY_Hits_Next(obj.ref)
+               docC = C.LUCY_Hits_Next(self)
        })
        if errCallingNext != nil {
                obj.err = errCallingNext
@@ -196,11 +189,6 @@ func (obj *implHits) Next(hit interface{}) bool {
        return true
 }
 
-func (obj *implHits) finalize() {
-       C.cfish_dec_refcount(unsafe.Pointer(obj.ref))
-       obj.ref = nil
-}
-
-func (obj *implHits) Error() error {
+func (obj *HitsIMP) Error() error {
        return obj.err
 }

Reply via email to