[Groonga-commit] groonga/grnxx at 6304e20 [master] Enable Column<Bool>.

Back to archive index

susumu.yata null+****@clear*****
Tue Dec 16 10:40:57 JST 2014


susumu.yata	2014-11-06 11:24:04 +0900 (Thu, 06 Nov 2014)

  New Revision: 6304e207504dd9bde55228c82e3d4460304cefd1
  https://github.com/groonga/grnxx/commit/6304e207504dd9bde55228c82e3d4460304cefd1

  Message:
    Enable Column<Bool>.

  Modified files:
    include/grnxx/column.hpp
    lib/grnxx/impl/column.hpp
    lib/grnxx/impl/column/base.cpp
    lib/grnxx/impl/column/base.hpp
    lib/grnxx/impl/column/scalar.hpp
    lib/grnxx/impl/column/scalar/Makefile.am
    lib/grnxx/impl/column/scalar/bool.cpp
    lib/grnxx/impl/column/scalar/bool.hpp
    lib/grnxx/impl/column/vector.hpp
    lib/grnxx/impl/table.cpp

  Modified: include/grnxx/column.hpp (+1 -1)
===================================================================
--- include/grnxx/column.hpp    2014-11-06 11:22:52 +0900 (c32ea51)
+++ include/grnxx/column.hpp    2014-11-06 11:24:04 +0900 (fedbfb6)
@@ -16,7 +16,7 @@ struct ColumnOptions {
   // The referenced (parent) table.
   String reference_table_name;
 
-  ColumnOptions();
+  ColumnOptions() : reference_table_name() {}
 };
 
 class Column {

  Modified: lib/grnxx/impl/column.hpp (+2 -10)
===================================================================
--- lib/grnxx/impl/column.hpp    2014-11-06 11:22:52 +0900 (98e4360)
+++ lib/grnxx/impl/column.hpp    2014-11-06 11:24:04 +0900 (fe1f977)
@@ -2,15 +2,7 @@
 #define GRNXX_IMPL_COLUMN_HPP
 
 #include "grnxx/impl/column/base.hpp"
-//#include "grnxx/impl/column/scalar/bool.hpp"
-//#include "grnxx/impl/column/scalar/int.hpp"
-//#include "grnxx/impl/column/scalar/float.hpp"
-//#include "grnxx/impl/column/scalar/geo_point.hpp"
-//#include "grnxx/impl/column/scalar/text.hpp"
-//#include "grnxx/impl/column/vector/bool.hpp"
-//#include "grnxx/impl/column/vector/int.hpp"
-//#include "grnxx/impl/column/vector/float.hpp"
-//#include "grnxx/impl/column/vector/geo_point.hpp"
-//#include "grnxx/impl/column/vector/text.hpp"
+#include "grnxx/impl/column/scalar.hpp"
+#include "grnxx/impl/column/vector.hpp"
 
 #endif  // GRNXX_IMPL_COLUMN_HPP

  Modified: lib/grnxx/impl/column/base.cpp (+45 -41)
===================================================================
--- lib/grnxx/impl/column/base.cpp    2014-11-06 11:22:52 +0900 (48cca51)
+++ lib/grnxx/impl/column/base.cpp    2014-11-06 11:24:04 +0900 (7fdfba3)
@@ -1,8 +1,7 @@
 #include "grnxx/impl/column/base.hpp"
 
-//#include "grnxx/impl/column/scalar.hpp"
-//#include "grnxx/impl/column/vector.hpp"
-#include "grnxx/impl/db.hpp"
+#include "grnxx/impl/column/scalar.hpp"
+#include "grnxx/impl/column/vector.hpp"
 #include "grnxx/impl/table.hpp"
 
 namespace grnxx {
@@ -17,7 +16,8 @@ ColumnBase::ColumnBase(Table *table,
       name_(name),
       data_type_(data_type),
       reference_table_(reference_table),
-      is_key_(false) {}
+      is_key_(false),
+      indexes_() {}
 
 ColumnBase::~ColumnBase() {}
 
@@ -52,14 +52,6 @@ Index *ColumnBase::find_index(const String &name) const {
   throw "Not supported yet";  // TODO
 }
 
-void ColumnBase::set(Int row_id, const Datum &datum) {
-  throw "Not supported yet";  // TODO
-}
-
-void ColumnBase::get(Int row_id, Datum *datum) const {
-  throw "Not supported yet";  // TODO
-}
-
 bool ColumnBase::contains(const Datum &datum) const {
   throw "Not supported yet";  // TODO
 }
@@ -181,48 +173,60 @@ Int ColumnBase::find_one(const Datum &datum) const {
 //  return Int::na();
 //}
 
-std::unique_ptr<ColumnBase> ColumnBase::create(Table *table,
-                                               const String &name,
-                                               DataType data_type,
-                                               const ColumnOptions &options) {
-  throw "Not supported yet";  // TODO
-//  switch (data_type) {
-//    case BOOL_DATA: {
-//      return impl::Column<Bool>::create(error, table, name, options);
-//    }
+std::unique_ptr<ColumnBase> ColumnBase::create(
+    Table *table,
+    const String &name,
+    DataType data_type,
+    const ColumnOptions &options) try {
+  std::unique_ptr<ColumnBase> column;
+  switch (data_type) {
+    case BOOL_DATA: {
+      column.reset(new impl::Column<Bool>(table, name, options));
+      break;
+    }
 //    case INT_DATA: {
-//      return impl::Column<Int>::create(error, table, name, options);
+//      column.reset(new impl::Column<Int>(table, name, options));
+//      break;
 //    }
 //    case FLOAT_DATA: {
-//      return impl::Column<Float>::create(error, table, name, options);
+//      column.reset(new impl::Column<Float>(table, name, options));
+//      break;
 //    }
 //    case GEO_POINT_DATA: {
-//      return impl::Column<GeoPoint>::create(error, table, name, options);
+//      column.reset(new impl::Column<GeoPoint>(table, name, options));
+//      break;
 //    }
 //    case TEXT_DATA: {
-//      return impl::Column<Text>::create(error, table, name, options);
+//      column.reset(new impl::Column<Text>(table, name, options));
+//      break;
 //    }
 //    case BOOL_VECTOR_DATA: {
-//      return impl::Column<Vector<Bool>>::create(error, table, name, options);
+//      column.reset(new impl::Column<Vector<Bool>>(table, name, options));
+//      break;
 //    }
 //    case INT_VECTOR_DATA: {
-//      return impl::Column<Vector<Int>>::create(error, table, name, options);
+//      column.reset(new impl::Column<Vector<Int>>(table, name, options));
+//      break;
 //    }
 //    case FLOAT_VECTOR_DATA: {
-//      return impl::Column<Vector<Float>>::create(error, table, name, options);
+//      column.reset(new impl::Column<Vector<Float>>(table, name, options));
+//      break;
 //    }
 //    case GEO_POINT_VECTOR_DATA: {
-//      return impl::Column<Vector<GeoPoint>>::create(error, table, name, options);
+//      column.reset(new impl::Column<Vector<GeoPoint>>(table, name, options));
+//      break;
 //    }
 //    case TEXT_VECTOR_DATA: {
-//      return impl::Column<Vector<Text>>::create(error, table, name, options);
-//    }
-//    default: {
-//      // TODO: Other data types are not supported yet.
-//      GRNXX_ERROR_SET(error, NOT_SUPPORTED_YET, "Not suported yet");
-//      return nullptr;
+//      column.reset(new impl::Column<Vector<Text>>(table, name, options));
+//      break;
 //    }
-//  }
+    default: {
+      throw "Not supported";  // TODO
+    }
+  }
+  return column;
+} catch (const std::bad_alloc &) {
+  throw "Memory allocation failed";  // TODO
 }
 
 void ColumnBase::rename(const String &new_name) {
@@ -235,19 +239,19 @@ bool ColumnBase::is_removable() const {
 }
 
 void ColumnBase::set_key_attribute() {
-  throw "Not supported yet";  // TODO
+  throw "Not supported";  // TODO
 }
 
 void ColumnBase::unset_key_attribute() {
-  throw "Not supported yet";  // TODO
+  throw "Not supported";  // TODO
 }
 
-void ColumnBase::set_initial_key(Int, const Datum &) {
-  throw "Not supported yet";  // TODO
+void ColumnBase::set_key(Int, const Datum &) {
+  throw "Not supported";  // TODO
 }
 
 void ColumnBase::clear_references(Int) {
-  throw "Not supported yet";  // TODO
+  throw "Not supported";  // TODO
 }
 
 //bool ColumnBase::initialize_base(Error *error,

  Modified: lib/grnxx/impl/column/base.hpp (+5 -7)
===================================================================
--- lib/grnxx/impl/column/base.hpp    2014-11-06 11:22:52 +0900 (5a5adce)
+++ lib/grnxx/impl/column/base.hpp    2014-11-06 11:24:04 +0900 (13b0325)
@@ -15,8 +15,6 @@ using ColumnInterface = grnxx::Column;
 
 class Table;
 
-class Index;  // TODO
-
 class ColumnBase : public ColumnInterface {
  public:
   // -- Public API (grnxx/column.hpp) --
@@ -55,11 +53,11 @@ class ColumnBase : public ColumnInterface {
   }
   Index *find_index(const String &name) const;
 
-  void set(Int row_id, const Datum &datum);
-  void get(Int row_id, Datum *datum) const;
+  virtual void set(Int row_id, const Datum &datum) = 0;
+  virtual void get(Int row_id, Datum *datum) const = 0;
 
-  bool contains(const Datum &datum) const;
-  Int find_one(const Datum &datum) const;
+  virtual bool contains(const Datum &datum) const;
+  virtual Int find_one(const Datum &datum) const;
 
   // -- Internal API --
 
@@ -103,7 +101,7 @@ class ColumnBase : public ColumnInterface {
   // Set the initial key.
   //
   // On failure, throws an exception.
-  virtual void set_initial_key(Int row_id, const Datum &key);
+  virtual void set_key(Int row_id, const Datum &key);
 
   // Unset the value.
   virtual void unset(Int row_id) = 0;

  Modified: lib/grnxx/impl/column/scalar.hpp (+4 -4)
===================================================================
--- lib/grnxx/impl/column/scalar.hpp    2014-11-06 11:22:52 +0900 (f443aff)
+++ lib/grnxx/impl/column/scalar.hpp    2014-11-06 11:24:04 +0900 (1b33481)
@@ -2,9 +2,9 @@
 #define GRNXX_IMPL_COLUMN_SCALAR_HPP
 
 #include "grnxx/impl/column/scalar/bool.hpp"
-#include "grnxx/impl/column/scalar/float.hpp"
-#include "grnxx/impl/column/scalar/geo_point.hpp"
-#include "grnxx/impl/column/scalar/int.hpp"
-#include "grnxx/impl/column/scalar/text.hpp"
+//#include "grnxx/impl/column/scalar/float.hpp"
+//#include "grnxx/impl/column/scalar/geo_point.hpp"
+//#include "grnxx/impl/column/scalar/int.hpp"
+//#include "grnxx/impl/column/scalar/text.hpp"
 
 #endif  // GRNXX_IMPL_COLUMN_SCALAR_HPP

  Modified: lib/grnxx/impl/column/scalar/Makefile.am (+2 -2)
===================================================================
--- lib/grnxx/impl/column/scalar/Makefile.am    2014-11-06 11:22:52 +0900 (f706517)
+++ lib/grnxx/impl/column/scalar/Makefile.am    2014-11-06 11:24:04 +0900 (690cb71)
@@ -8,9 +8,9 @@ lib_LTLIBRARIES = libgrnxx_impl_column_scalar.la
 
 libgrnxx_impl_column_scalar_la_LDFLAGS = @AM_LTLDFLAGS@
 
-libgrnxx_impl_column_scalar_la_SOURCES =
+libgrnxx_impl_column_scalar_la_SOURCES =	\
+	bool.cpp
 
-#	bool.cpp				\
 #	float.cpp				\
 #	geo_point.cpp				\
 #	int.cpp					\

  Modified: lib/grnxx/impl/column/scalar/bool.cpp (+86 -0)
===================================================================
--- lib/grnxx/impl/column/scalar/bool.cpp    2014-11-06 11:22:52 +0900 (e69de29)
+++ lib/grnxx/impl/column/scalar/bool.cpp    2014-11-06 11:24:04 +0900 (ca15746)
@@ -0,0 +1,86 @@
+#include "grnxx/impl/column/scalar/bool.hpp"
+
+namespace grnxx {
+namespace impl {
+
+Column<Bool>::Column(Table *table,
+                     const String &name,
+                     const ColumnOptions &)
+    : ColumnBase(table, name, BOOL_DATA),
+      values_() {}
+
+Column<Bool>::~Column() {}
+
+void Column<Bool>::set(Int row_id, const Datum &datum) {
+  Bool value = parse_datum(datum);
+  size_t value_id = row_id.value();
+  if (value_id >= values_.size()) {
+    values_.resize(value_id + 1, Bool::na());
+  }
+  values_[value_id] = value;
+  // TODO: Update indexes if exist.
+}
+
+void Column<Bool>::get(Int row_id, Datum *datum) const {
+  size_t value_id = row_id.value();
+  if (value_id >= values_.size()) {
+    *datum = Bool::na();
+  } else {
+    *datum = values_[value_id];
+  }
+}
+
+bool Column<Bool>::contains(const Datum &datum) const {
+  // TODO: Use an index if exists.
+  Bool value = parse_datum(datum);
+  for (size_t i = 0; i < values_.size(); ++i) {
+    if (values_[i].value() == value.value()) {
+      return true;
+    }
+  }
+  return false;
+}
+
+Int Column<Bool>::find_one(const Datum &datum) const {
+  // TODO: Use an index if exists.
+  Bool value = parse_datum(datum);
+  for (size_t i = 0; i < values_.size(); ++i) {
+    if (values_[i] == value) {
+      return Int(i);
+    }
+  }
+  return Int::na();
+}
+
+void Column<Bool>::unset(Int row_id) {
+  size_t value_id = row_id.value();
+  values_[value_id] = Bool::na();
+  // TODO: Update indexes if exist.
+}
+
+void Column<Bool>::read(ArrayCRef<Record> records,
+                        ArrayRef<Bool> values) const {
+  if (records.size() != values.size()) {
+    throw "Data size conflict";  // TODO
+  }
+  for (size_t i = 0; i < records.size(); ++i) {
+    values.set(i, get(records[i].row_id));
+  }
+}
+
+Bool Column<Bool>::parse_datum(const Datum &datum) {
+  switch (datum.type()) {
+    case NA_DATA: {
+      return Bool::na();
+    }
+    case BOOL_DATA: {
+      return datum.as_bool();
+    }
+    default: {
+      throw "Wrong data type";  // TODO
+    }
+  }
+}
+
+}  // namespace impl
+}  // namespace grnxx

  Modified: lib/grnxx/impl/column/scalar/bool.hpp (+55 -2)
===================================================================
--- lib/grnxx/impl/column/scalar/bool.hpp    2014-11-06 11:22:52 +0900 (f95e8da)
+++ lib/grnxx/impl/column/scalar/bool.hpp    2014-11-06 11:24:04 +0900 (4a4999a)
@@ -1,12 +1,65 @@
 #ifndef GRNXX_IMPL_COLUMN_SCALAR_BOOL_HPP
 #define GRNXX_IMPL_COLUMN_SCALAR_BOOL_HPP
 
-#include "grnxx/impl/column/column.hpp"
+#include "grnxx/impl/column/base.hpp"
 
 namespace grnxx {
 namespace impl {
 
-// TODO
+template <typename T> class Column;
+
+template <>
+class Column<Bool> : public ColumnBase {
+ public:
+  // -- Public API (grnxx/column.hpp) --
+
+  Column(Table *table, const String &name, const ColumnOptions &options);
+  ~Column();
+
+  void set(Int row_id, const Datum &datum);
+  void get(Int row_id, Datum *datum) const;
+
+  bool contains(const Datum &datum) const;
+  Int find_one(const Datum &datum) const;
+
+  // -- Internal API (grnxx/impl/column/base.hpp) --
+
+  // Create a new column.
+  //
+  // On success, returns the column.
+  // On failure, throws an exception.
+  static std::unique_ptr<ColumnBase> create(
+      Table *table,
+      const String &name,
+      DataType data_type,
+      const ColumnOptions &options);
+
+  // Unset the value.
+  void unset(Int row_id);
+
+  // -- Internal API --
+
+  // Return a value.
+  //
+  // If "row_id" is valid, returns the stored value.
+  // If "row_id" is invalid, returns N/A.
+  Bool get(Int row_id) const {
+    size_t value_id = row_id.value();
+    if (value_id >= values_.size()) {
+      return Bool::na();
+    }
+    return values_[value_id];
+  }
+  // Read values.
+  //
+  // On failure, throws an exception.
+  void read(ArrayCRef<Record> records, ArrayRef<Bool> values) const;
+
+ protected:
+  Array<Bool> values_;
+
+  static Bool parse_datum(const Datum &datum);
+};
 
 }  // namespace impl
 }  // namespace grnxx

  Modified: lib/grnxx/impl/column/vector.hpp (+5 -5)
===================================================================
--- lib/grnxx/impl/column/vector.hpp    2014-11-06 11:22:52 +0900 (aed3214)
+++ lib/grnxx/impl/column/vector.hpp    2014-11-06 11:24:04 +0900 (1e2876c)
@@ -1,10 +1,10 @@
 #ifndef GRNXX_IMPL_COLUMN_VECTOR_HPP
 #define GRNXX_IMPL_COLUMN_VECTOR_HPP
 
-#include "grnxx/impl/column/vector/bool.hpp"
-#include "grnxx/impl/column/vector/float.hpp"
-#include "grnxx/impl/column/vector/geo_point.hpp"
-#include "grnxx/impl/column/vector/int.hpp"
-#include "grnxx/impl/column/vector/text.hpp"
+//#include "grnxx/impl/column/vector/bool.hpp"
+//#include "grnxx/impl/column/vector/float.hpp"
+//#include "grnxx/impl/column/vector/geo_point.hpp"
+//#include "grnxx/impl/column/vector/int.hpp"
+//#include "grnxx/impl/column/vector/text.hpp"
 
 #endif  // GRNXX_IMPL_COLUMN_VECTOR_HPP

  Modified: lib/grnxx/impl/table.cpp (+3 -3)
===================================================================
--- lib/grnxx/impl/table.cpp    2014-11-06 11:22:52 +0900 (4241a0b)
+++ lib/grnxx/impl/table.cpp    2014-11-06 11:24:04 +0900 (5a26a42)
@@ -336,7 +336,7 @@ Int Table::insert_row(const Datum &key) {
   Int row_id = find_next_row_id();
   reserve_row(row_id);
   if (key_column_) {
-    key_column_->set_initial_key(row_id, key);
+    key_column_->set_key(row_id, key);
   }
   validate_row(row_id);
   return row_id;
@@ -357,7 +357,7 @@ Int Table::find_or_insert_row(const Datum &key, bool *inserted) {
   Int row_id = find_next_row_id();
   reserve_row(row_id);
   if (key_column_) {
-    key_column_->set_initial_key(row_id, key);
+    key_column_->set_key(row_id, key);
   }
   validate_row(row_id);
   *inserted = true;
@@ -378,7 +378,7 @@ void Table::insert_row_at(Int row_id, const Datum &key) {
   }
   reserve_row(row_id);
   if (key_column_) {
-    key_column_->set_initial_key(row_id, key);
+    key_column_->set_key(row_id, key);
   }
   validate_row(row_id);
 }
-------------- next part --------------
HTML����������������������������...
Descargar 



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