[Groonga-commit] groonga/grnxx at 38ae669 [master] Add IntColumn.

Back to archive index

susumu.yata null+****@clear*****
Fri Jul 4 18:26:25 JST 2014


susumu.yata	2014-07-04 18:26:25 +0900 (Fri, 04 Jul 2014)

  New Revision: 38ae66941072a5a4ea64fdf2ab1ff7e585183012
  https://github.com/groonga/grnxx/commit/38ae66941072a5a4ea64fdf2ab1ff7e585183012

  Message:
    Add IntColumn.

  Modified files:
    lib/grnxx/column.cpp
    lib/grnxx/column_impl.hpp

  Modified: lib/grnxx/column.cpp (+67 -0)
===================================================================
--- lib/grnxx/column.cpp    2014-07-04 18:14:18 +0900 (882b12b)
+++ lib/grnxx/column.cpp    2014-07-04 18:26:25 +0900 (c88aa09)
@@ -143,6 +143,8 @@ bool Column::set_default_value(Error *error, Int row_id) {
 void Column::unset(Int row_id) {
 }
 
+// -- BoolColumn --
+
 bool BoolColumn::set(Error *error, Int row_id, const Datum &datum) {
   if (datum.type() != BOOL_DATA) {
     GRNXX_ERROR_SET(error, INVALID_ARGUMENT, "Wrong data type");
@@ -206,4 +208,69 @@ void BoolColumn::unset(Int row_id) {
 
 BoolColumn::BoolColumn() : Column(), values_() {}
 
+// -- IntColumn --
+
+bool IntColumn::set(Error *error, Int row_id, const Datum &datum) {
+  if (datum.type() != INT_DATA) {
+    GRNXX_ERROR_SET(error, INVALID_ARGUMENT, "Wrong data type");
+    return false;
+  }
+  if (!table_->test_row(error, row_id)) {
+    return false;
+  }
+  values_[row_id] = static_cast<Int>(datum);
+  return true;
+}
+
+bool IntColumn::get(Error *error, Int row_id, Datum *datum) const {
+  if (!table_->test_row(error, row_id)) {
+    return false;
+  }
+  *datum = values_[row_id];
+  return true;
+}
+
+unique_ptr<IntColumn> IntColumn::create(Error *error,
+                                        Table *table,
+                                        String name,
+                                        const ColumnOptions &options) {
+  unique_ptr<IntColumn> column(new (nothrow) IntColumn);
+  if (!column) {
+    GRNXX_ERROR_SET(error, NO_MEMORY, "Memory allocation failed");
+    return nullptr;
+  }
+  if (!column->initialize_base(error, table, name, INT_DATA, options)) {
+    return nullptr;
+  }
+  try {
+    column->values_.resize(table->max_row_id() + 1, 0);
+  } catch (...) {
+    GRNXX_ERROR_SET(error, NO_MEMORY, "Memory allocation failed");
+    return nullptr;
+  }
+  return column;
+}
+
+IntColumn::~IntColumn() {}
+
+bool IntColumn::set_default_value(Error *error, Int row_id) {
+  if (row_id >= values_.size()) {
+    try {
+      values_.resize(row_id + 1, 0);
+      return true;
+    } catch (...) {
+      GRNXX_ERROR_SET(error, NO_MEMORY, "Memory allocation failed");
+      return false;
+    }
+  }
+  values_[row_id] = false;
+  return true;
+}
+
+void IntColumn::unset(Int row_id) {
+  values_[row_id] = false;
+}
+
+IntColumn::IntColumn() : Column(), values_() {}
+
 }  // namespace grnxx

  Modified: lib/grnxx/column_impl.hpp (+38 -1)
===================================================================
--- lib/grnxx/column_impl.hpp    2014-07-04 18:14:18 +0900 (46cbbe2)
+++ lib/grnxx/column_impl.hpp    2014-07-04 18:26:25 +0900 (102dcba)
@@ -39,11 +39,48 @@ class BoolColumn : public Column {
   }
 
  protected:
-  std::vector<bool> values_;
+  std::vector<Bool> values_;
 
   BoolColumn();
 };
 
+class IntColumn : public Column {
+ public:
+  // -- Public API --
+
+  bool set(Error *error, Int row_id, const Datum &datum);
+  bool get(Error *error, Int row_id, Datum *datum) const;
+
+  // -- Internal API --
+
+  // Create a new column.
+  //
+  // Returns a pointer to the column on success.
+  // On failure, returns nullptr and stores error information into "*error" if
+  // "error" != nullptr.
+  static unique_ptr<IntColumn> create(Error *error,
+                                      Table *table,
+                                      String name,
+                                      const ColumnOptions &options);
+
+  ~IntColumn();
+
+  bool set_default_value(Error *error, Int row_id);
+  void unset(Int row_id);
+
+  // Return a value identified by "row_id".
+  //
+  // Assumes that "row_id" is valid. Otherwise, the result is undefined.
+  Int get(Int row_id) const {
+    return values_[row_id];
+  }
+
+ protected:
+  std::vector<Int> values_;
+
+  IntColumn();
+};
+
 }  // namespace grnxx
 
 #endif  // GRNXX_COLUMN_BASE_HPP
-------------- next part --------------
HTML����������������������������...
Download 



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