Exemplo n.º 1
0
Arquivo: mdt.go Projeto: 00k/mdt
// 查询接口(按primary key查)
func SearchByPrimaryKey(table *Table, primary_key string) (error, []string) {
	var c_request C.mdt_search_request_t
	var c_response C.mdt_search_response_t

	// convert request
	c_request.primary_key.data = C.CString(primary_key)
	c_request.primary_key.size = C.size_t(len(primary_key))

	// invoke C api
	C.mdt_search(table.rep, &c_request, &c_response, nil, nil)

	// convert result & free result memory
	err := Ok
	var data_list []string
	c_result_list := (*[1 << 30]C.mdt_search_result_t)(unsafe.Pointer(c_response.result_list))
	if c_response.result_list_len == C.size_t(1) {
		c_result := &c_result_list[0]
		C.free(unsafe.Pointer(c_result.primary_key.data))

		c_data_list := (*[1 << 30]C.mdt_slice_t)(unsafe.Pointer(c_result.data_list))
		for j := C.size_t(0); j < c_result.data_list_len; j++ {
			data_list = append(data_list, C.GoStringN(c_data_list[j].data, C.int(c_data_list[j].size)))
			C.free(unsafe.Pointer(c_data_list[j].data))
		}
		C.free(unsafe.Pointer(c_result.data_list))
	} else {
		err = NotFound
	}
	C.free(unsafe.Pointer(c_response.result_list))

	// free request memory
	C.free(unsafe.Pointer(c_request.primary_key.data))

	return GetError(err), data_list
}
Exemplo n.º 2
0
Arquivo: mdt.go Projeto: bdtbd/mdt
// 查询接口(按index key查)
func searchByIndexKey(table *Table,
	index_condition_list []IndexCondition,
	start_timestamp int64,
	end_timestamp int64,
	limit int32,
	need_index int,
	need_data int) (error, []Result) {
	var c_request C.mdt_search_request_t
	var c_response C.mdt_search_response_t

	// convert request
	c_request.primary_key.size = 0
	c_request.index_condition_list_len = C.size_t(len(index_condition_list))
	c_index_condition_size := C.size_t(unsafe.Sizeof(C.mdt_index_condition_t{}))
	c_index_condition_list_size := c_index_condition_size * c_request.index_condition_list_len
	c_request.index_condition_list = (*C.mdt_index_condition_t)(C.malloc(c_index_condition_list_size))
	c_index_condition_list := (*[1 << 30]C.mdt_index_condition_t)(unsafe.Pointer(c_request.index_condition_list))
	for i := C.size_t(0); i < c_request.index_condition_list_len; i++ {
		c_index_condition := &c_index_condition_list[i]
		index_condition := &index_condition_list[i]
		c_index_condition.index_name.data = C.CString(index_condition.IndexName)
		c_index_condition.index_name.size = C.size_t(len(index_condition.IndexName))
		c_index_condition.comparator = index_condition.Comparator
		c_index_condition.compare_value.data = C.CString(index_condition.CompareValue)
		c_index_condition.compare_value.size = C.size_t(len(index_condition.CompareValue))
	}
	c_request.start_timestamp = C.int64_t(start_timestamp)
	c_request.end_timestamp = C.int64_t(end_timestamp)
	c_request.limit = C.int32_t(limit)
	c_request.need_index = C.int(need_index)
	c_request.need_data = C.int(need_data)

	// invoke C api
	C.mdt_search(table.rep, &c_request, &c_response, nil, nil)

	// convert result & free result memory
	err := Ok
	var result_list []Result
	c_result_list := (*[1 << 30]C.mdt_search_result_t)(unsafe.Pointer(c_response.result_list))
	for i := C.size_t(0); i < c_response.result_list_len; i++ {
		c_result := &c_result_list[i]
		result := &Result{}

		result.PrimaryKey = C.GoStringN(c_result.primary_key.data, C.int(c_result.primary_key.size))
		C.free(unsafe.Pointer(c_result.primary_key.data))

		c_index_list := (*[1 << 30]C.mdt_index_t)(unsafe.Pointer(c_result.index_list))
		for j := C.size_t(0); j < c_result.index_list_len; j++ {
			index := &Index{}
			index.IndexName = C.GoStringN(c_index_list[j].index_name.data, C.int(c_index_list[j].index_name.size))
			index.IndexKey = C.GoStringN(c_index_list[j].index_key.data, C.int(c_index_list[j].index_key.size))
			result.IndexList = append(result.IndexList, *index)
			C.free(unsafe.Pointer(c_index_list[j].index_name.data))
			C.free(unsafe.Pointer(c_index_list[j].index_key.data))
		}
		C.free(unsafe.Pointer(c_result.index_list))

		c_data_list := (*[1 << 30]C.mdt_slice_t)(unsafe.Pointer(c_result.data_list))
		for j := C.size_t(0); j < c_result.data_list_len; j++ {
			result.DataList = append(result.DataList, C.GoStringN(c_data_list[j].data, C.int(c_data_list[j].size)))
			C.free(unsafe.Pointer(c_data_list[j].data))
		}
		C.free(unsafe.Pointer(c_result.data_list))

		result_list = append(result_list, *result)
	}
	C.free(unsafe.Pointer(c_response.result_list))
	if result_list == nil {
		err = NotFound
	}

	// free request memory
	for i := C.size_t(0); i < c_request.index_condition_list_len; i++ {
		c_index_condition := &c_index_condition_list[i]
		C.free(unsafe.Pointer(c_index_condition.index_name.data))
		C.free(unsafe.Pointer(c_index_condition.compare_value.data))
	}
	C.free(unsafe.Pointer(c_request.index_condition_list))

	return GetError(err), result_list
}