Пример #1
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))
}
Пример #2
0
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
}