[Groonga-commit] groonga/grngo at 7038c41 [master] Implement GetValue() for fixed-size data types.

Back to archive index

susumu.yata null+****@clear*****
Fri Jul 24 14:29:23 JST 2015


susumu.yata	2015-07-24 14:29:23 +0900 (Fri, 24 Jul 2015)

  New Revision: 7038c41e147db0fd24759151d0952ad3f00eaf3f
  https://github.com/groonga/grngo/commit/7038c41e147db0fd24759151d0952ad3f00eaf3f

  Message:
    Implement GetValue() for fixed-size data types.

  Modified files:
    grngo.c
    grngo.go
    grngo.h
    grngo_test.go

  Modified: grngo.c (+36 -0)
===================================================================
--- grngo.c    2015-07-24 12:27:39 +0900 (b68be14)
+++ grngo.c    2015-07-24 14:29:23 +0900 (4025068)
@@ -1073,6 +1073,42 @@ grngo_set_geo_point_vector(grngo_column *column, grn_id id,
   return rc;
 }
 
+grn_rc
+grngo_get(grngo_column *column, grn_id id, void **value) {
+  if (!column || !value) {
+    return GRN_INVALID_ARGUMENT;
+  }
+  grn_ctx *ctx = column->db->ctx;
+  if (grn_table_at(ctx, column->table->obj, id) == GRN_ID_NIL) {
+    return GRN_INVALID_ARGUMENT;
+  }
+  const grn_id *ids = &id;
+  size_t n_ids = 1;
+  size_t i, j;
+  for (i = 0; i < (column->n_srcs - 1); i++) {
+    GRN_BULK_REWIND(column->src_bufs[i]);
+    // TODO: Vector.
+    for (j = 0; j < n_ids; j++) {
+      grn_obj_get_value(ctx, column->srcs[i], ids[j], column->src_bufs[i]);
+      if (ctx->rc != GRN_SUCCESS) {
+        return ctx->rc;
+      }
+    }
+    ids = (const grn_id *)GRN_BULK_HEAD(column->src_bufs[i]);
+    n_ids = grn_vector_size(ctx, column->src_bufs[i]);
+  }
+  GRN_BULK_REWIND(column->src_bufs[i]);
+  for (j = 0; j < n_ids; j++) {
+    // TODO: Vector and Text.
+    grn_obj_get_value(ctx, column->srcs[i], ids[j], column->src_bufs[i]);
+    if (ctx->rc != GRN_SUCCESS) {
+      return ctx->rc;
+    }
+  }
+  *value = GRN_BULK_HEAD(column->src_bufs[i]);
+  return GRN_SUCCESS;
+}
+
 // -- old... --
 
 grn_rc grngo_find_table(grn_ctx *ctx, const char *name, size_t name_len,

  Modified: grngo.go (+63 -16)
===================================================================
--- grngo.go    2015-07-24 12:27:39 +0900 (5e779be)
+++ grngo.go    2015-07-24 14:29:23 +0900 (8804997)
@@ -749,14 +749,14 @@ func (db *DB) SetValue(tableName, columnName string, id uint32, value interface{
 	return table.SetValue(columnName, id, value)
 }
 
-//// GetValue gets a value.
-//func (db *DB) GetValue(tableName, columnName string, id uint32) (interface{}, error) {
-//	table, err := db.FindTable(tableName)
-//	if err != nil {
-//		return nil, err
-//	}
-//	return table.GetValue(columnName, id)
-//}
+// GetValue gets a value.
+func (db *DB) GetValue(tableName, columnName string, id uint32) (interface{}, error) {
+	table, err := db.FindTable(tableName)
+	if err != nil {
+		return nil, err
+	}
+	return table.GetValue(columnName, id)
+}
 
 // -- Table --
 
@@ -827,14 +827,14 @@ func (table *Table) SetValue(columnName string, id uint32, value interface{}) er
 	return column.SetValue(id, value)
 }
 
-//// GetValue gets a value.
-//func (table *Table) GetValue(columnName string, id uint32) (interface{}, error) {
-//	column, err := table.FindColumn(columnName)
-//	if err != nil {
-//		return nil, err
-//	}
-//	return column.GetValue(id)
-//}
+// GetValue gets a value.
+func (table *Table) GetValue(columnName string, id uint32) (interface{}, error) {
+	column, err := table.FindColumn(columnName)
+	if err != nil {
+		return nil, err
+	}
+	return column.GetValue(id)
+}
 
 // createColumnOptionsMap creates an options map for column_create.
 //
@@ -1040,6 +1040,53 @@ func (column *Column) SetValue(id uint32, value interface{}) error {
 	return nil
 }
 
+// GetValue gets a value.
+func (column *Column) GetValue(id uint32) (interface{}, error) {
+	if column.c.dimension != 0 {
+		return nil, fmt.Errorf("Vector is not supported yet")
+	}
+	switch column.c.value_type {
+	case C.GRN_DB_SHORT_TEXT, C.GRN_DB_TEXT, C.GRN_DB_LONG_TEXT:
+		return nil, fmt.Errorf("Text is not supported yet")
+	}
+	var ptr unsafe.Pointer
+	rc := C.grngo_get(column.c, C.grn_id(id), &ptr)
+	if rc != C.GRN_SUCCESS {
+		return nil, newGrnError("grngo_get()", rc, column.table.db)
+	}
+	switch column.c.value_type {
+	case C.GRN_DB_BOOL:
+		cValue := *(*C.grn_bool)(ptr)
+		return cValue == C.GRN_TRUE, nil
+	case C.GRN_DB_INT8:
+		return int64(*(*C.int8_t)(ptr)), nil
+	case C.GRN_DB_INT16:
+		return int64(*(*C.int16_t)(ptr)), nil
+	case C.GRN_DB_INT32:
+		return int64(*(*C.int32_t)(ptr)), nil
+	case C.GRN_DB_INT64:
+		return int64(*(*C.int64_t)(ptr)), nil
+	case C.GRN_DB_UINT8:
+		return int64(*(*C.uint8_t)(ptr)), nil
+	case C.GRN_DB_UINT16:
+		return int64(*(*C.uint16_t)(ptr)), nil
+	case C.GRN_DB_UINT32:
+		return int64(*(*C.uint32_t)(ptr)), nil
+	case C.GRN_DB_UINT64:
+		return int64(*(*C.uint64_t)(ptr)), nil
+	case C.GRN_DB_FLOAT:
+		return float64(*(*C.double)(ptr)), nil
+	case C.GRN_DB_TIME:
+		return int64(*(*C.int64_t)(ptr)), nil
+	case C.GRN_DB_TOKYO_GEO_POINT, C.GRN_DB_WGS84_GEO_POINT:
+		cValue := *(*C.grn_geo_point)(ptr)
+		return GeoPoint{int32(cValue.latitude), int32(cValue.longitude)}, nil
+	default:
+		return nil, fmt.Errorf("unsupported value type")
+	}
+	return nil, fmt.Errorf("unknown error")
+}
+
 //// getBool gets a Bool value.
 //func (column *Column) getBool(id uint32) (interface{}, error) {
 //	var grnValue C.grn_bool

  Modified: grngo.h (+1 -1)
===================================================================
--- grngo.h    2015-07-24 12:27:39 +0900 (5ecbf47)
+++ grngo.h    2015-07-24 14:29:23 +0900 (69490b1)
@@ -100,7 +100,7 @@ grn_rc grngo_set_text_vector(grngo_column *column, grn_id id,
 grn_rc grngo_set_geo_point_vector(grngo_column *column, grn_id id,
                                   grngo_vector value);
 
-//grn_rc grngo_get(grngo_column *column, grn_id id, void **value);
+grn_rc grngo_get(grngo_column *column, grn_id id, void **value);
 
 // -- old... --
 

  Modified: grngo_test.go (+61 -62)
===================================================================
--- grngo_test.go    2015-07-24 12:27:39 +0900 (02b6732)
+++ grngo_test.go    2015-07-24 14:29:23 +0900 (3ccd6f5)
@@ -5,7 +5,7 @@ import (
 	"io/ioutil"
 	"math/rand"
 	"os"
-//	"reflect"
+	"reflect"
 	"strconv"
 	"strings"
 	"testing"
@@ -1043,72 +1043,71 @@ func TestColumnSetValueForWGS84GeoPointVector(t *testing.T) {
 	testColumnSetValue(t, "[]WGS84GeoPoint")
 }
 
-//func testColumnGetValue(t *testing.T, valueType string) {
-//	dirPath, _, db, table, column :=
-//		createTempColumn(t, "Table", nil, "Value", valueType, nil)
-//	defer removeTempDB(t, dirPath, db)
-
-//	for i := 0; i < 100; i++ {
-//		_, id, err := table.InsertRow(nil)
-//		if err != nil {
-//			t.Fatalf("Table.InsertRow() failed: %v", err)
-//		}
-//		value := generateRandomValue(valueType)
-//		if err := column.SetValue(id, value); err != nil {
-//			t.Fatalf("Column.SetValue() failed: %v", err)
-//		}
-//		if storedValue, err := column.GetValue(id); err != nil {
-//			t.Fatalf("Column.GetValue() failed: %v", err)
-//		} else if !reflect.DeepEqual(value, storedValue) {
-//			t.Fatalf("Column.GetValue() failed: value = %v, storedValue = %v",
-//				value, storedValue)
-//		}
-//	}
-//}
+func testColumnGetValue(t *testing.T, valueType string) {
+	dirPath, _, db, table, column :=
+		createTempColumn(t, "Table", nil, "Value", valueType, nil)
+	defer removeTempDB(t, dirPath, db)
+	for i := 0; i < 100; i++ {
+		_, id, err := table.InsertRow(nil)
+		if err != nil {
+			t.Fatalf("Table.InsertRow() failed: %v", err)
+		}
+		value := generateRandomValue(valueType)
+		if err := column.SetValue(id, value); err != nil {
+			t.Fatalf("Column.SetValue() failed: %v", err)
+		}
+		if storedValue, err := column.GetValue(id); err != nil {
+			t.Fatalf("Column.GetValue() failed: %v", err)
+		} else if !reflect.DeepEqual(value, storedValue) {
+			t.Fatalf("Column.GetValue() failed: value = %v, storedValue = %v",
+				value, storedValue)
+		}
+	}
+}
 
-//func TestColumnGetValueForBool(t *testing.T) {
-//	testColumnGetValue(t, "Bool")
-//}
+func TestColumnGetValueForBool(t *testing.T) {
+	testColumnGetValue(t, "Bool")
+}
 
-//func TestColumnGetValueForInt8(t *testing.T) {
-//	testColumnGetValue(t, "Int8")
-//}
+func TestColumnGetValueForInt8(t *testing.T) {
+	testColumnGetValue(t, "Int8")
+}
 
-//func TestColumnGetValueForInt16(t *testing.T) {
-//	testColumnGetValue(t, "Int16")
-//}
+func TestColumnGetValueForInt16(t *testing.T) {
+	testColumnGetValue(t, "Int16")
+}
 
-//func TestColumnGetValueForInt32(t *testing.T) {
-//	testColumnGetValue(t, "Int32")
-//}
+func TestColumnGetValueForInt32(t *testing.T) {
+	testColumnGetValue(t, "Int32")
+}
 
-//func TestColumnGetValueForInt64(t *testing.T) {
-//	testColumnGetValue(t, "Int64")
-//}
+func TestColumnGetValueForInt64(t *testing.T) {
+	testColumnGetValue(t, "Int64")
+}
 
-//func TestColumnGetValueForUInt8(t *testing.T) {
-//	testColumnGetValue(t, "UInt8")
-//}
+func TestColumnGetValueForUInt8(t *testing.T) {
+	testColumnGetValue(t, "UInt8")
+}
 
-//func TestColumnGetValueForUInt16(t *testing.T) {
-//	testColumnGetValue(t, "UInt16")
-//}
+func TestColumnGetValueForUInt16(t *testing.T) {
+	testColumnGetValue(t, "UInt16")
+}
 
-//func TestColumnGetValueForUInt32(t *testing.T) {
-//	testColumnGetValue(t, "UInt32")
-//}
+func TestColumnGetValueForUInt32(t *testing.T) {
+	testColumnGetValue(t, "UInt32")
+}
 
-//func TestColumnGetValueForUInt64(t *testing.T) {
-//	testColumnGetValue(t, "UInt64")
-//}
+func TestColumnGetValueForUInt64(t *testing.T) {
+	testColumnGetValue(t, "UInt64")
+}
 
-//func TestColumnGetValueForFloat(t *testing.T) {
-//	testColumnGetValue(t, "Float")
-//}
+func TestColumnGetValueForFloat(t *testing.T) {
+	testColumnGetValue(t, "Float")
+}
 
-//func TestColumnGetValueForTime(t *testing.T) {
-//	testColumnGetValue(t, "Time")
-//}
+func TestColumnGetValueForTime(t *testing.T) {
+	testColumnGetValue(t, "Time")
+}
 
 //func TestColumnGetValueForShortText(t *testing.T) {
 //	testColumnGetValue(t, "ShortText")
@@ -1122,13 +1121,13 @@ func TestColumnSetValueForWGS84GeoPointVector(t *testing.T) {
 //	testColumnGetValue(t, "LongText")
 //}
 
-//func TestColumnGetValueForTokyoGeoPoint(t *testing.T) {
-//	testColumnGetValue(t, "TokyoGeoPoint")
-//}
+func TestColumnGetValueForTokyoGeoPoint(t *testing.T) {
+	testColumnGetValue(t, "TokyoGeoPoint")
+}
 
-//func TestColumnGetValueForWGS84GeoPoint(t *testing.T) {
-//	testColumnGetValue(t, "WGS84GeoPoint")
-//}
+func TestColumnGetValueForWGS84GeoPoint(t *testing.T) {
+	testColumnGetValue(t, "WGS84GeoPoint")
+}
 
 //func TestColumnGetValueForBoolVector(t *testing.T) {
 //	testColumnGetValue(t, "[]Bool")
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index