Esempio n. 1
0
File: lucy.go Progetto: 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))
}
Esempio n. 2
0
//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
	}
	fields := fetchDocFields(d)
	otherFields := fetchDocFields(twin)
	result := reflect.DeepEqual(fields, otherFields)
	return C.bool(result)
}
Esempio n. 3
0
File: lucy.go Progetto: kidaa/lucy
//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)
}
Esempio n. 4
0
func certifyCF(value interface{}, class *C.cfish_Class, nullable bool) {
	if nullable && value == nil {
		return
	}
	if cfObj, ok := value.(Obj); ok {
		o := (*C.cfish_Obj)(unsafe.Pointer(cfObj.TOPTR()))
		if o == nil {
			if nullable {
				return
			}
		} else if C.cfish_Obj_is_a(o, class) {
			return
		}
	}
	className := StringToGo(unsafe.Pointer(C.CFISH_Class_Get_Name(class)))
	panic(NewErr(fmt.Sprintf("Can't convert a %T to %s", value, className)))
}
Esempio n. 5
0
File: lucy.go Progetto: apache/lucy
func readDocPolyDR(pdr *C.lucy_PolyDocReader, docID int32, doc interface{}) error {
	ivars := C.lucy_PolyDocReader_IVARS(pdr)
	segTick := C.lucy_PolyReader_sub_tick(ivars.offsets, C.int32_t(docID))
	offset := C.LUCY_I32Arr_Get(ivars.offsets, C.size_t(segTick))
	defDocReader := (*C.lucy_DefaultDocReader)(C.CFISH_Vec_Fetch(ivars.readers, C.size_t(segTick)))
	if defDocReader == nil {
		return clownfish.NewErr(fmt.Sprintf("Invalid docID: %d", docID))
	}
	if !C.cfish_Obj_is_a((*C.cfish_Obj)(unsafe.Pointer(defDocReader)), C.LUCY_DEFAULTDOCREADER) {
		panic(clownfish.NewErr("Unexpected type")) // sanity check
	}
	adjustedDocID := docID - int32(offset)
	err := doReadDocData(defDocReader, adjustedDocID, doc)
	if docDoc, ok := doc.(Doc); ok {
		docDoc.SetDocID(docID)
	}
	return err
}