コード例 #1
0
ファイル: clownfish.go プロジェクト: rectang/lucy-clownfish
func NewBoolean(val bool) Boolean {
	if val {
		return WRAPBoolean(unsafe.Pointer(C.cfish_inc_refcount(unsafe.Pointer(C.CFISH_TRUE))))
	} else {
		return WRAPBoolean(unsafe.Pointer(C.cfish_inc_refcount(unsafe.Pointer(C.CFISH_FALSE))))
	}
}
コード例 #2
0
ファイル: lucy.go プロジェクト: kidaa/lucy
//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))

	// Prepare for the new doc.
	C.LUCY_Inverter_Set_Doc(inverter, doc)

	// Extract and invert the doc's fields.
	iter := C.cfish_HashIter_new(fields)
	for C.CFISH_HashIter_Next(iter) {
		field := C.CFISH_HashIter_Get_Key(iter)
		obj := C.CFISH_HashIter_Get_Value(iter)
		if obj == nil {
			mess := "Invalid nil value for field" + clownfish.CFStringToGo(unsafe.Pointer(field))
			panic(clownfish.NewErr(mess))
		}

		inventry := fetchEntry(ivars, field)
		inventryIvars := C.lucy_InvEntry_IVARS(inventry)
		fieldType := inventryIvars._type

		// Get the field value.
		var expectedType *C.cfish_Class
		switch C.LUCY_FType_Primitive_ID(fieldType) & C.lucy_FType_PRIMITIVE_ID_MASK {
		case C.lucy_FType_TEXT:
			expectedType = C.CFISH_STRING
		case C.lucy_FType_BLOB:
			expectedType = C.CFISH_BLOB
		case C.lucy_FType_INT32:
			expectedType = C.CFISH_INTEGER
		case C.lucy_FType_INT64:
			expectedType = C.CFISH_INTEGER
		case C.lucy_FType_FLOAT32:
			expectedType = C.CFISH_FLOAT
		case C.lucy_FType_FLOAT64:
			expectedType = C.CFISH_FLOAT
		default:
			panic(clownfish.NewErr("Internal Lucy error: bad type id for field " +
				clownfish.CFStringToGo(unsafe.Pointer(field))))
		}
		if !C.cfish_Obj_is_a(obj, expectedType) {
			className := C.cfish_Obj_get_class_name((*C.cfish_Obj)(unsafe.Pointer(fieldType)))
			mess := fmt.Sprintf("Invalid type for field '%s': '%s'",
				clownfish.CFStringToGo(unsafe.Pointer(field)),
				clownfish.CFStringToGo(unsafe.Pointer(className)))
			panic(clownfish.NewErr(mess))
		}
		if inventryIvars.value != obj {
			C.cfish_decref(unsafe.Pointer(inventryIvars.value))
			inventryIvars.value = C.cfish_inc_refcount(unsafe.Pointer(obj))
		}

		C.LUCY_Inverter_Add_Field(inverter, inventry)
	}
	C.cfish_dec_refcount(unsafe.Pointer(iter))
}
コード例 #3
0
ファイル: lucy.go プロジェクト: kidaa/lucy
//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
}
コード例 #4
0
ファイル: index.go プロジェクト: kidaa/lucy
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)

	// TODO: Support map as doc in addition to struct as doc.

	// Get reflection value and type for the supplied struct.
	var docValue reflect.Value
	if reflect.ValueOf(doc).Kind() == reflect.Ptr {
		temp := reflect.ValueOf(doc).Elem()
		if temp.Kind() == reflect.Struct {
			docValue = temp
		}
	}
	if docValue == (reflect.Value{}) {
		mess := fmt.Sprintf("Doc not struct pointer: %v",
			reflect.TypeOf(doc))
		return clownfish.NewErr(mess)
	}
	docType := docValue.Type()

	for i := 0; i < docValue.NumField(); i++ {
		field := docType.Field(i).Name
		value := docValue.Field(i).String()
		fieldC := obj.findFieldC(field)
		valueC := clownfish.NewString(value)
		C.CFISH_Hash_Store(docFields,
			(*C.cfish_String)(unsafe.Pointer(fieldC)),
			C.cfish_inc_refcount(unsafe.Pointer(valueC.TOPTR())))
	}

	// TODO create an additional method AddDocWithBoost which allows the
	// client to supply `boost`.
	boost := 1.0
	err := clownfish.TrapErr(func() {
		C.LUCY_Indexer_Add_Doc(self, stockDoc, C.float(boost))
	})

	return err
}
コード例 #5
0
ファイル: index.go プロジェクト: kidaa/lucy
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(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++ {
			cfString := unsafe.Pointer(C.CFISH_Vec_Fetch(fieldList, C.size_t(i)))
			field := clownfish.CFStringToGo(cfString)
			if strings.EqualFold(name, field) {
				C.cfish_inc_refcount(cfString)
				f = clownfish.WRAPString(cfString)
				obj.fieldNames[name] = f
			}
		}
	}
	return (*C.cfish_String)(unsafe.Pointer(f.TOPTR()))
}
コード例 #6
0
ファイル: lucy.go プロジェクト: lazycrazyowl/lucy
//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 := fetchDocFields(doc)

	// Prepare for the new doc.
	C.LUCY_Inverter_Set_Doc(inverter, doc)

	// Extract and invert the doc's fields.
	for field, val := range fields {
		inventry := fetchEntry(ivars, field)
		inventryIvars := C.lucy_InvEntry_IVARS(inventry)
		fieldType := inventryIvars._type

		// Get the field value.
		var expectedType *C.cfish_Class
		switch C.LUCY_FType_Primitive_ID(fieldType) & C.lucy_FType_PRIMITIVE_ID_MASK {
		case C.lucy_FType_TEXT:
			expectedType = C.CFISH_STRING
		case C.lucy_FType_BLOB:
			expectedType = C.CFISH_BLOB
		case C.lucy_FType_INT32:
			expectedType = C.CFISH_INTEGER
		case C.lucy_FType_INT64:
			expectedType = C.CFISH_INTEGER
		case C.lucy_FType_FLOAT32:
			expectedType = C.CFISH_FLOAT
		case C.lucy_FType_FLOAT64:
			expectedType = C.CFISH_FLOAT
		default:
			panic(clownfish.NewErr("Internal Lucy error: bad type id for field " + field))
		}
		temp := inventryIvars.value
		valCF := clownfish.GoToClownfish(val, unsafe.Pointer(expectedType), false)
		inventryIvars.value = C.cfish_inc_refcount(valCF)
		C.cfish_decref(unsafe.Pointer(temp))

		C.LUCY_Inverter_Add_Field(inverter, inventry)
	}
}
コード例 #7
0
ファイル: lucy.go プロジェクト: kidaa/lucy
//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))
}
コード例 #8
0
ファイル: lucy.go プロジェクト: kidaa/lucy
//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)))
}
コード例 #9
0
ファイル: lucy.go プロジェクト: kidaa/lucy
//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)
}