Port Doc code to CGO.
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/8f634425 Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/8f634425 Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/8f634425 Branch: refs/heads/master Commit: 8f634425b5390423d7c4013b28710e4ddc92bf0b Parents: 44fc440 Author: Marvin Humphrey <[email protected]> Authored: Sun Jul 19 12:57:13 2015 -0700 Committer: Marvin Humphrey <[email protected]> Committed: Fri Jul 31 17:59:15 2015 -0700 ---------------------------------------------------------------------- go/cfext/lucy.c | 67 ++++++++++-------------- go/lucy/lucy.go | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+), 39 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy/blob/8f634425/go/cfext/lucy.c ---------------------------------------------------------------------- diff --git a/go/cfext/lucy.c b/go/cfext/lucy.c index 5773f16..e2719bc 100644 --- a/go/cfext/lucy.c +++ b/go/cfext/lucy.c @@ -86,61 +86,53 @@ RegexTokenizer_Tokenize_Utf8_IMP(RegexTokenizer *self, const char *string, /********************************** Doc ********************************/ Doc* -Doc_init(Doc *self, void *fields, int32_t doc_id) { - DocIVARS *const ivars = Doc_IVARS(self); - Hash *hash; - - if (fields) { - hash = (Hash *)INCREF(CERTIFY(fields, HASH)); - } - else { - hash = Hash_new(0); - } - ivars->fields = hash; - ivars->doc_id = doc_id; +(*GOLUCY_Doc_init_BRIDGE)(Doc *self, void *fields, int32_t doc_id); - return self; +Doc* +Doc_init(Doc *self, void *fields, int32_t doc_id) { + return GOLUCY_Doc_init_BRIDGE(self, fields, doc_id); } +Doc_Set_Fields_t GOLUCY_Doc_Set_Fields_BRIDGE; + void Doc_Set_Fields_IMP(Doc *self, void *fields) { - DocIVARS *const ivars = Doc_IVARS(self); - DECREF(ivars->fields); - ivars->fields = CERTIFY(fields, HASH); + GOLUCY_Doc_Set_Fields_BRIDGE(self, fields); } +Doc_Get_Size_t GOLUCY_Doc_Get_Size_BRIDGE; + uint32_t Doc_Get_Size_IMP(Doc *self) { - Hash *hash = (Hash*)Doc_IVARS(self)->fields; - return Hash_Get_Size(hash); + return GOLUCY_Doc_Get_Size_BRIDGE(self); } +Doc_Store_t GOLUCY_Doc_Store_BRIDGE; + void Doc_Store_IMP(Doc *self, String *field, Obj *value) { - Hash *hash = (Hash*)Doc_IVARS(self)->fields; - Hash_Store(hash, field, INCREF(value)); + GOLUCY_Doc_Store_BRIDGE(self, field, value); } +Doc_Serialize_t GOLUCY_Doc_Serialize_BRIDGE; + void Doc_Serialize_IMP(Doc *self, OutStream *outstream) { - DocIVARS *const ivars = Doc_IVARS(self); - Hash *hash = (Hash*)ivars->fields; - Freezer_serialize_hash(hash, outstream); - OutStream_Write_C32(outstream, ivars->doc_id); + GOLUCY_Doc_Serialize_BRIDGE(self, outstream); } +Doc_Deserialize_t GOLUCY_Doc_Deserialize_BRIDGE; + Doc* Doc_Deserialize_IMP(Doc *self, InStream *instream) { - DocIVARS *const ivars = Doc_IVARS(self); - ivars->fields = Freezer_read_hash(instream); - ivars->doc_id = InStream_Read_C32(instream); - return self; + return GOLUCY_Doc_Deserialize_BRIDGE(self, instream); } +Doc_Extract_t GOLUCY_Doc_Extract_BRIDGE; + Obj* Doc_Extract_IMP(Doc *self, String *field) { - Hash *hash = (Hash*)Doc_IVARS(self)->fields; - return INCREF(Hash_Fetch(hash, field)); + return GOLUCY_Doc_Extract_BRIDGE(self, field); } Hash* @@ -158,23 +150,20 @@ Doc_Load_IMP(Doc *self, Obj *dump) { UNREACHABLE_RETURN(Doc*); } +Doc_Equals_t GOLUCY_Doc_Equals_BRIDGE; + bool Doc_Equals_IMP(Doc *self, Obj *other) { - if ((Doc*)other == self) { return true; } - if (!Obj_is_a(other, DOC)) { return false; } - DocIVARS *const ivars = Doc_IVARS(self); - DocIVARS *const ovars = Doc_IVARS((Doc*)other); - return Hash_Equals((Hash*)ivars->fields, (Obj*)ovars->fields); + return GOLUCY_Doc_Equals_BRIDGE(self, other); } +Doc_Destroy_t GOLUCY_Doc_Destroy_BRIDGE; + void Doc_Destroy_IMP(Doc *self) { - DocIVARS *const ivars = Doc_IVARS(self); - DECREF(ivars->fields); - SUPER_DESTROY(self, DOC); + GOLUCY_Doc_Destroy_BRIDGE(self); } - /**************************** DocReader *****************************/ HitDoc* http://git-wip-us.apache.org/repos/asf/lucy/blob/8f634425/go/lucy/lucy.go ---------------------------------------------------------------------- diff --git a/go/lucy/lucy.go b/go/lucy/lucy.go index 13bdafa..7d55798 100644 --- a/go/lucy/lucy.go +++ b/go/lucy/lucy.go @@ -17,10 +17,17 @@ package lucy /* +#define C_LUCY_DOC #define C_LUCY_REGEXTOKENIZER #include "lucy_parcel.h" #include "Lucy/Analysis/RegexTokenizer.h" +#include "Lucy/Document/Doc.h" + +#include "Clownfish/Hash.h" +#include "Lucy/Store/InStream.h" +#include "Lucy/Store/OutStream.h" +#include "Lucy/Util/Freezer.h" extern lucy_RegexTokenizer* GOLUCY_RegexTokenizer_init(lucy_RegexTokenizer *self, cfish_String *pattern); @@ -38,6 +45,44 @@ extern void (*GOLUCY_RegexTokenizer_Tokenize_Utf8_BRIDGE)(lucy_RegexTokenizer *self, const char *str, size_t string_len, lucy_Inversion *inversion); +extern lucy_Doc* +GOLUCY_Doc_init(lucy_Doc *doc, void *fields, int32_t doc_id); +extern lucy_Doc* +(*GOLUCY_Doc_init_BRIDGE)(lucy_Doc *doc, void *fields, int32_t doc_id); +extern void +GOLUCY_Doc_Set_Fields(lucy_Doc *self, void *fields); +extern void +(*GOLUCY_Doc_Set_Fields_BRIDGE)(lucy_Doc *self, void *fields); +extern uint32_t +GOLUCY_Doc_Get_Size(lucy_Doc *self); +extern uint32_t +(*GOLUCY_Doc_Get_Size_BRIDGE)(lucy_Doc *self); +extern void +GOLUCY_Doc_Store(lucy_Doc *self, cfish_String *field, cfish_Obj *value); +extern void +(*GOLUCY_Doc_Store_BRIDGE)(lucy_Doc *self, cfish_String *field, cfish_Obj *value); +extern void +GOLUCY_Doc_Serialize(lucy_Doc *self, lucy_OutStream *outstream); +extern void +(*GOLUCY_Doc_Serialize_BRIDGE)(lucy_Doc *self, lucy_OutStream *outstream); +extern lucy_Doc* +GOLUCY_Doc_Deserialize(lucy_Doc *self, lucy_InStream *instream); +extern lucy_Doc* +(*GOLUCY_Doc_Deserialize_BRIDGE)(lucy_Doc *self, lucy_InStream *instream); +extern cfish_Obj* +GOLUCY_Doc_Extract(lucy_Doc *self, cfish_String *field); +extern cfish_Obj* +(*GOLUCY_Doc_Extract_BRIDGE)(lucy_Doc *self, cfish_String *field); +extern bool +GOLUCY_Doc_Equals(lucy_Doc *self, cfish_Obj *other); +extern bool +(*GOLUCY_Doc_Equals_BRIDGE)(lucy_Doc *self, cfish_Obj *other); +extern void +GOLUCY_Doc_Destroy(lucy_Doc *self); +extern void +(*GOLUCY_Doc_Destroy_BRIDGE)(lucy_Doc *self); + + // C symbols linked into a Go-built package archive are not visible to // external C code -- but internal code *can* see symbols from outside. @@ -49,10 +94,20 @@ GOLUCY_glue_exported_symbols() { GOLUCY_RegexTokenizer_Destroy_BRIDGE = GOLUCY_RegexTokenizer_Destroy; GOLUCY_RegexTokenizer_Tokenize_Utf8_BRIDGE = (LUCY_RegexTokenizer_Tokenize_Utf8_t)GOLUCY_RegexTokenizer_Tokenize_Utf8; + GOLUCY_Doc_init_BRIDGE = GOLUCY_Doc_init; + GOLUCY_Doc_Set_Fields_BRIDGE = GOLUCY_Doc_Set_Fields; + GOLUCY_Doc_Get_Size_BRIDGE = GOLUCY_Doc_Get_Size; + GOLUCY_Doc_Store_BRIDGE = GOLUCY_Doc_Store; + GOLUCY_Doc_Serialize_BRIDGE = GOLUCY_Doc_Serialize; + GOLUCY_Doc_Deserialize_BRIDGE = GOLUCY_Doc_Deserialize; + GOLUCY_Doc_Extract_BRIDGE = GOLUCY_Doc_Extract; + GOLUCY_Doc_Equals_BRIDGE = GOLUCY_Doc_Equals; + GOLUCY_Doc_Destroy_BRIDGE = GOLUCY_Doc_Destroy; } */ import "C" +import "unsafe" import _ "git-wip-us.apache.org/repos/asf/lucy-clownfish.git/runtime/go/clownfish" func init() { @@ -73,3 +128,89 @@ func GOLUCY_RegexTokenizer_Destroy(rt *C.lucy_RegexTokenizer) { func GOLUCY_RegexTokenizer_Tokenize_Utf8(rt *C.lucy_RegexTokenizer, str *C.char, stringLen C.size_t, inversion *C.lucy_Inversion) { } + +func NewDoc(docID int32) Doc { + retvalCF := C.lucy_Doc_new(nil, C.int32_t(docID)) + return WRAPDoc(unsafe.Pointer(retvalCF)) +} + +//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)) + } else { + ivars.fields = unsafe.Pointer(C.cfish_Hash_new(0)) + } + ivars.doc_id = docID + return d +} + +//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) +} + +//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)) + 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) + 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) + C.lucy_Freezer_serialize_hash(hash, outstream) + C.LUCY_OutStream_Write_C32(outstream, C.uint32_t(ivars.doc_id)) +} + +//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)) + 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) + val := C.CFISH_Hash_Fetch(hash, field) + return C.cfish_inc_refcount(unsafe.Pointer(val)) +} + +//export GOLUCY_Doc_Equals +func GOLUCY_Doc_Equals(d *C.lucy_Doc, other *C.cfish_Obj) C.bool { + twin := (*C.lucy_Doc)(unsafe.Pointer(other)) + if twin == d { + return true + } + 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) + 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)) + C.cfish_super_destroy(unsafe.Pointer(d), C.LUCY_DOC) +}
