Change Doc to use registry internally.

Instead of storing a pointer to a raw Clownfish Hash in a doc object,
wrap the Hash in a Go struct, store it in the registry and store the
ID returned by the registry in the Doc.

This commit is a transition from using a Clownfish Hash as Doc's fields
to using a Go `map[string]interface{}`.


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

Branch: refs/heads/master
Commit: a41f86cbcc63b711c8087a4861e70ec803a69ebf
Parents: 900fc89
Author: Marvin Humphrey <[email protected]>
Authored: Tue Sep 15 18:54:11 2015 -0700
Committer: Marvin Humphrey <[email protected]>
Committed: Mon Sep 28 12:54:38 2015 -0700

----------------------------------------------------------------------
 go/lucy/document.go | 16 ++++++++++++++++
 go/lucy/index.go    |  2 +-
 go/lucy/lucy.go     | 45 ++++++++++++++++++++-------------------------
 go/lucy/search.go   |  2 +-
 4 files changed, 38 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/a41f86cb/go/lucy/document.go
----------------------------------------------------------------------
diff --git a/go/lucy/document.go b/go/lucy/document.go
index 02a8f82..3196986 100644
--- a/go/lucy/document.go
+++ b/go/lucy/document.go
@@ -17,11 +17,17 @@
 package lucy
 
 /*
+#define C_LUCY_DOC
+
 #include "Lucy/Document/Doc.h"
 #include "Lucy/Document/HitDoc.h"
 */
 import "C"
 import "unsafe"
+import "fmt"
+
+import 
"git-wip-us.apache.org/repos/asf/lucy-clownfish.git/runtime/go/clownfish"
+
 
 func NewDoc(docID int32) Doc {
        retvalCF := C.lucy_Doc_new(nil, C.int32_t(docID))
@@ -32,3 +38,13 @@ func NewHitDoc(docID int32, score float32) HitDoc {
        retvalCF := C.lucy_HitDoc_new(nil, C.int32_t(docID), C.float(score))
        return WRAPHitDoc(unsafe.Pointer(retvalCF))
 }
+
+func fetchDocFields(d *C.lucy_Doc) *C.cfish_Hash {
+       ivars := C.lucy_Doc_IVARS(d)
+       fieldsID := uintptr(ivars.fields)
+       fieldsGo, ok := registry.fetch(fieldsID).(clownfish.Hash)
+       if !ok {
+               panic(clownfish.NewErr(fmt.Sprintf("Failed to fetch doc %d from 
registry ", fieldsID)))
+       }
+       return (*C.cfish_Hash)(clownfish.Unwrap(fieldsGo, "fieldsGo"))
+}

http://git-wip-us.apache.org/repos/asf/lucy/blob/a41f86cb/go/lucy/index.go
----------------------------------------------------------------------
diff --git a/go/lucy/index.go b/go/lucy/index.go
index 4aab0a2..6543c99 100644
--- a/go/lucy/index.go
+++ b/go/lucy/index.go
@@ -73,7 +73,7 @@ func (obj *IndexerIMP) Close() error {
 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))
+       docFields := fetchDocFields(stockDoc)
        C.CFISH_Hash_Clear(docFields)
 
        // TODO: Support map as doc in addition to struct as doc.

http://git-wip-us.apache.org/repos/asf/lucy/blob/a41f86cb/go/lucy/lucy.go
----------------------------------------------------------------------
diff --git a/go/lucy/lucy.go b/go/lucy/lucy.go
index e67ead2..50ac2c9 100644
--- a/go/lucy/lucy.go
+++ b/go/lucy/lucy.go
@@ -252,10 +252,11 @@ func GOLUCY_RegexTokenizer_Tokenize_Utf8(rt 
*C.lucy_RegexTokenizer, str *C.char,
 //export GOLUCY_Doc_init
 func GOLUCY_Doc_init(d *C.lucy_Doc, fields unsafe.Pointer, docID C.int32_t) 
*C.lucy_Doc {
        ivars := C.lucy_Doc_IVARS(d)
-       if fields != nil {
-               ivars.fields = unsafe.Pointer(C.cfish_inc_refcount(fields))
+       if fields == nil {
+               fieldsID := registry.store(clownfish.NewHash(0))
+               ivars.fields = unsafe.Pointer(fieldsID)
        } else {
-               ivars.fields = unsafe.Pointer(C.cfish_Hash_new(0))
+               ivars.fields = fields
        }
        ivars.doc_id = docID
        return d
@@ -263,30 +264,25 @@ func GOLUCY_Doc_init(d *C.lucy_Doc, fields 
unsafe.Pointer, docID C.int32_t) *C.l
 
 //export GOLUCY_Doc_Set_Fields
 func GOLUCY_Doc_Set_Fields(d *C.lucy_Doc, fields unsafe.Pointer) {
-       ivars := C.lucy_Doc_IVARS(d)
-       temp := ivars.fields
-       ivars.fields = unsafe.Pointer(C.cfish_inc_refcount(fields))
-       C.cfish_decref(temp)
+       panic(clownfish.NewErr("Set_Fields unsupported in Go bindings"))
 }
 
 //export GOLUCY_Doc_Get_Size
 func GOLUCY_Doc_Get_Size(d *C.lucy_Doc) C.uint32_t {
-       ivars := C.lucy_Doc_IVARS(d)
-       hash := ((*C.cfish_Hash)(ivars.fields))
+       hash := fetchDocFields(d)
        return C.uint32_t(C.CFISH_Hash_Get_Size(hash))
 }
 
 //export GOLUCY_Doc_Store
 func GOLUCY_Doc_Store(d *C.lucy_Doc, field *C.cfish_String, value 
*C.cfish_Obj) {
-       ivars := C.lucy_Doc_IVARS(d)
-       hash := (*C.cfish_Hash)(ivars.fields)
+       hash := fetchDocFields(d)
        C.CFISH_Hash_Store(hash, field, 
C.cfish_inc_refcount(unsafe.Pointer(value)))
 }
 
 //export GOLUCY_Doc_Serialize
 func GOLUCY_Doc_Serialize(d *C.lucy_Doc, outstream *C.lucy_OutStream) {
        ivars := C.lucy_Doc_IVARS(d)
-       hash := (*C.cfish_Hash)(ivars.fields)
+       hash := fetchDocFields(d)
        C.lucy_Freezer_serialize_hash(hash, outstream)
        C.LUCY_OutStream_Write_C32(outstream, C.uint32_t(ivars.doc_id))
 }
@@ -294,23 +290,23 @@ func GOLUCY_Doc_Serialize(d *C.lucy_Doc, outstream 
*C.lucy_OutStream) {
 //export GOLUCY_Doc_Deserialize
 func GOLUCY_Doc_Deserialize(d *C.lucy_Doc, instream *C.lucy_InStream) 
*C.lucy_Doc {
        ivars := C.lucy_Doc_IVARS(d)
-       ivars.fields = unsafe.Pointer(C.lucy_Freezer_read_hash(instream))
+       hash := unsafe.Pointer(C.lucy_Freezer_read_hash(instream))
+       fieldsID := registry.store(clownfish.WRAPAny(hash))
+       ivars.fields = unsafe.Pointer(fieldsID)
        ivars.doc_id = C.int32_t(C.LUCY_InStream_Read_C32(instream))
        return d
 }
 
 //export GOLUCY_Doc_Extract
 func GOLUCY_Doc_Extract(d *C.lucy_Doc, field *C.cfish_String) *C.cfish_Obj {
-       ivars := C.lucy_Doc_IVARS(d)
-       hash := (*C.cfish_Hash)(ivars.fields)
+       hash := fetchDocFields(d)
        val := C.CFISH_Hash_Fetch(hash, field)
        return C.cfish_inc_refcount(unsafe.Pointer(val))
 }
 
 //export GOLUCY_Doc_Field_Names
 func GOLUCY_Doc_Field_Names(d *C.lucy_Doc) *C.cfish_Vector {
-       ivars := C.lucy_Doc_IVARS(d)
-       hash := (*C.cfish_Hash)(ivars.fields)
+       hash := fetchDocFields(d)
        return C.CFISH_Hash_Keys(hash)
 }
 
@@ -323,17 +319,16 @@ func GOLUCY_Doc_Equals(d *C.lucy_Doc, other *C.cfish_Obj) 
C.bool {
        if !C.cfish_Obj_is_a(other, C.LUCY_DOC) {
                return false
        }
-       ivars := C.lucy_Doc_IVARS(d)
-       ovars := C.lucy_Doc_IVARS(twin)
-       hash := (*C.cfish_Hash)(ivars.fields)
-       otherHash := (*C.cfish_Obj)(ovars.fields)
+       hash := fetchDocFields(d)
+       otherHash := (*C.cfish_Obj)(unsafe.Pointer(fetchDocFields(twin)))
        return C.CFISH_Hash_Equals(hash, otherHash)
 }
 
 //export GOLUCY_Doc_Destroy
 func GOLUCY_Doc_Destroy(d *C.lucy_Doc) {
        ivars := C.lucy_Doc_IVARS(d)
-       C.cfish_decref(unsafe.Pointer(ivars.fields))
+       fieldsID := uintptr(ivars.fields)
+       registry.delete(fieldsID)
        C.cfish_super_destroy(unsafe.Pointer(d), C.LUCY_DOC)
 }
 
@@ -432,15 +427,15 @@ func GOLUCY_DefDocReader_Fetch_Doc(ddr 
*C.lucy_DefaultDocReader,
        }
        C.free(unsafe.Pointer(fieldName))
 
-       retval := C.lucy_HitDoc_new(unsafe.Pointer(fields), docID, 0.0)
-       C.cfish_dec_refcount(unsafe.Pointer(fields))
+       fieldsID := registry.store(clownfish.WRAPAny(unsafe.Pointer(fields)))
+       retval := C.lucy_HitDoc_new(unsafe.Pointer(fieldsID), docID, 0.0)
        return retval
 }
 
 //export GOLUCY_Inverter_Invert_Doc
 func GOLUCY_Inverter_Invert_Doc(inverter *C.lucy_Inverter, doc *C.lucy_Doc) {
        ivars := C.lucy_Inverter_IVARS(inverter)
-       fields := (*C.cfish_Hash)(C.LUCY_Doc_Get_Fields(doc))
+       fields := fetchDocFields(doc)
 
        // Prepare for the new doc.
        C.LUCY_Inverter_Set_Doc(inverter, doc)

http://git-wip-us.apache.org/repos/asf/lucy/blob/a41f86cb/go/lucy/search.go
----------------------------------------------------------------------
diff --git a/go/lucy/search.go b/go/lucy/search.go
index e18eee7..7f6ce76 100644
--- a/go/lucy/search.go
+++ b/go/lucy/search.go
@@ -149,7 +149,7 @@ func (obj *HitsIMP) Next(hit interface{}) bool {
        }
        defer C.cfish_dec_refcount(unsafe.Pointer(docC))
 
-       fields := 
(*C.cfish_Hash)(unsafe.Pointer(C.LUCY_HitDoc_Get_Fields(docC)))
+       fields := fetchDocFields((*C.lucy_Doc)(unsafe.Pointer(docC)))
        iterator := C.cfish_HashIter_new(fields)
        defer C.cfish_dec_refcount(unsafe.Pointer(iterator))
        for C.CFISH_HashIter_Next(iterator) {

Reply via email to