db-mgr: add Rrset find/insert/modify/remove

Change-Id: I777d8d32c951f703a768e91344234cd5620b102f
diff --git a/src/db-mgr.cpp b/src/db-mgr.cpp
index 1cd0d3a..691299e 100644
--- a/src/db-mgr.cpp
+++ b/src/db-mgr.cpp
@@ -109,13 +109,42 @@
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
+// Zone
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+void
+DbMgr::insert(Zone& zone)
+{
+  if (zone.getId() > 0)
+    return;
+
+  sqlite3_stmt* stmt;
+  const char* sql = "INSERT INTO zones (name, ttl) VALUES (?, ?)";
+  int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, 0);
+  if (rc != SQLITE_OK) {
+    throw PrepareError(sql);
+  }
+
+  const Block& zoneName = zone.getName().wireEncode();
+  sqlite3_bind_blob(stmt, 1, zoneName.wire(), zoneName.size(), SQLITE_STATIC);
+  sqlite3_bind_int(stmt,  2, zone.getTtl().count());
+
+  rc = sqlite3_step(stmt);
+  if (rc != SQLITE_DONE) {
+    sqlite3_finalize(stmt);
+    throw ExecuteError(sql);
+  }
+
+  zone.setId(sqlite3_last_insert_rowid(m_conn));
+  sqlite3_finalize(stmt);
+}
 
 bool
-DbMgr::lookup(Zone& zone)
+DbMgr::find(Zone& zone)
 {
   sqlite3_stmt* stmt;
   const char* sql = "SELECT id, ttl FROM zones WHERE name=?";
-  int rc = sqlite3_prepare_v2(m_conn, sql, strlen(sql), &stmt, 0);
+  int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, 0);
   if (rc != SQLITE_OK) {
     throw PrepareError(sql);
   }
@@ -136,33 +165,6 @@
 }
 
 void
-DbMgr::insert(Zone& zone)
-{
-  if (zone.getId() > 0)
-    return;
-
-  sqlite3_stmt* stmt;
-  const char* sql = "INSERT INTO zones (name, ttl) VALUES (?, ?)";
-  int rc = sqlite3_prepare_v2(m_conn, sql, strlen(sql), &stmt, 0);
-  if (rc != SQLITE_OK) {
-    throw PrepareError(sql);
-  }
-
-  const Block& zoneName = zone.getName().wireEncode();
-  sqlite3_bind_blob(stmt, 1, zoneName.wire(), zoneName.size(), SQLITE_STATIC);
-  sqlite3_bind_int(stmt,  2, zone.getTtl().count());
-
-  rc = sqlite3_step(stmt);
-  if (rc != SQLITE_DONE) {
-    sqlite3_finalize(stmt);
-    throw ExecuteError(sql);
-  }
-
-  zone.setId(sqlite3_last_insert_rowid(m_conn));
-  sqlite3_finalize(stmt);
-}
-
-void
 DbMgr::remove(Zone& zone)
 {
   if (zone.getId() == 0)
@@ -170,7 +172,7 @@
 
   sqlite3_stmt* stmt;
   const char* sql = "DELETE FROM zones where id=?";
-  int rc = sqlite3_prepare_v2(m_conn, sql, strlen(sql), &stmt, 0);
+  int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, 0);
   if (rc != SQLITE_OK) {
     throw PrepareError(sql);
   }
@@ -185,10 +187,155 @@
 
   sqlite3_finalize(stmt);
 
-  zone.setId(0);
-  zone.setTtl(time::seconds(3600));
+  zone = Zone();
 }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// Rrset
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+void
+DbMgr::insert(Rrset& rrset)
+{
+  if (rrset.getId() != 0)
+    return;
+
+  if (rrset.getZone() == 0) {
+    throw RrsetError("Rrset has not been assigned to a zone");
+  }
+
+  if (rrset.getZone()->getId() == 0) {
+    insert(*rrset.getZone());
+  }
+
+  const char* sql =
+    "INSERT INTO rrsets (zone_id, label, type, version, ttl, data)"
+    "    VALUES (?, ?, ?, ?, ?, ?)";
+
+  sqlite3_stmt* stmt;
+  int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, 0);
+  if (rc != SQLITE_OK) {
+    throw PrepareError(sql);
+  }
+
+  sqlite3_bind_int64(stmt, 1, rrset.getZone()->getId());
+
+  const Block& label = rrset.getLabel().wireEncode();
+  sqlite3_bind_blob(stmt,  2, label.wire(),              label.size(),              SQLITE_STATIC);
+  sqlite3_bind_blob(stmt,  3, rrset.getType().wire(),    rrset.getType().size(),    SQLITE_STATIC);
+  sqlite3_bind_blob(stmt,  4, rrset.getVersion().wire(), rrset.getVersion().size(), SQLITE_STATIC);
+  sqlite3_bind_int64(stmt, 5, rrset.getTtl().count());
+  sqlite3_bind_blob(stmt,  6, rrset.getData().wire(),    rrset.getData().size(),    SQLITE_STATIC);
+
+  rc = sqlite3_step(stmt);
+  if (rc != SQLITE_DONE) {
+    sqlite3_finalize(stmt);
+    throw ExecuteError(sql);
+  }
+
+  rrset.setId(sqlite3_last_insert_rowid(m_conn));
+  sqlite3_finalize(stmt);
+}
+
+bool
+DbMgr::find(Rrset& rrset)
+{
+  if (rrset.getZone() == 0) {
+    throw RrsetError("Rrset has not been assigned to a zone");
+  }
+
+  if (rrset.getZone()->getId() == 0) {
+    bool isFound = find(*rrset.getZone());
+    if (!isFound) {
+      return false;
+    }
+  }
+
+  sqlite3_stmt* stmt;
+  const char* sql =
+    "SELECT id, ttl, version, data FROM rrsets"
+    "    WHERE zone_id=? and label=? and type=?";
+  int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, 0);
+
+  if (rc != SQLITE_OK) {
+    throw PrepareError(sql);
+  }
+
+  sqlite3_bind_int64(stmt, 1, rrset.getZone()->getId());
+
+  const Block& label = rrset.getLabel().wireEncode();
+  sqlite3_bind_blob(stmt, 2, label.wire(), label.size(), SQLITE_STATIC);
+  sqlite3_bind_blob(stmt, 3, rrset.getType().wire(), rrset.getType().size(), SQLITE_STATIC);
+
+  if (sqlite3_step(stmt) == SQLITE_ROW) {
+    rrset.setId(sqlite3_column_int64(stmt, 0));
+    rrset.setTtl(time::seconds(sqlite3_column_int64(stmt, 1)));
+    rrset.setVersion(Block(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, 2)),
+                           sqlite3_column_bytes(stmt, 2)));
+    rrset.setData(Block(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, 3)),
+                        sqlite3_column_bytes(stmt, 3)));
+  } else {
+    rrset.setId(0);
+  }
+  sqlite3_finalize(stmt);
+
+  return rrset.getId() != 0;
+}
+
+void
+DbMgr::remove(Rrset& rrset)
+{
+  if (rrset.getId() == 0)
+    throw RrsetError("Attempting to remove Rrset that has no assigned id");
+
+  sqlite3_stmt* stmt;
+  const char* sql = "DELETE FROM rrsets WHERE id=?";
+  int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, 0);
+
+  if (rc != SQLITE_OK) {
+    throw PrepareError(sql);
+  }
+
+  sqlite3_bind_int64(stmt, 1, rrset.getId());
+
+  rc = sqlite3_step(stmt);
+  if (rc != SQLITE_DONE) {
+    sqlite3_finalize(stmt);
+    throw ExecuteError(sql);
+  }
+
+  sqlite3_finalize(stmt);
+
+  rrset = Rrset(rrset.getZone());
+}
+
+void
+DbMgr::modify(Rrset& rrset)
+{
+  if (rrset.getId() == 0) {
+    throw RrsetError("Attempting to replace Rrset that has no assigned id");
+  }
+
+  if (rrset.getZone() == 0) {
+    throw RrsetError("Rrset has not been assigned to a zone");
+  }
+
+  sqlite3_stmt* stmt;
+  const char* sql = "UPDATE rrsets SET ttl=?, version=?, data=? WHERE id=?";
+  int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, 0);
+
+  if (rc != SQLITE_OK) {
+    throw PrepareError(sql);
+  }
+
+  sqlite3_bind_int64(stmt, 1, rrset.getTtl().count());
+  sqlite3_bind_blob(stmt,  2, rrset.getVersion().wire(), rrset.getVersion().size(), SQLITE_STATIC);
+  sqlite3_bind_blob(stmt,  3, rrset.getData().wire(),    rrset.getData().size(),    SQLITE_STATIC);
+  sqlite3_bind_int64(stmt, 4, rrset.getId());
+
+  sqlite3_step(stmt);
+  sqlite3_finalize(stmt);
+}
 
 } // namespace ndns
 } // namespace ndn
diff --git a/src/db-mgr.hpp b/src/db-mgr.hpp
index 26041c6..29738be 100644
--- a/src/db-mgr.hpp
+++ b/src/db-mgr.hpp
@@ -22,6 +22,7 @@
 
 #include "config.hpp"
 #include "zone.hpp"
+#include "rrset.hpp"
 
 #include <ndn-cxx/common.hpp>
 #include <sqlite3.h>
@@ -42,6 +43,8 @@
 
 /**
  * @brief Database Manager, which provides some common DB functionalities
+ *
+ * @note Function naming here follows MongoDB
  */
 class DbMgr : noncopyable
 {
@@ -64,7 +67,7 @@
    * @brief constructor
    */
   explicit
-  DbMgr(const std::string& dbFile = DEFAULT_CONFIG_PATH "/" "ndns.conf");
+  DbMgr(const std::string& dbFile = DEFAULT_CONFIG_PATH "/" "ndns.db");
 
   /**
    * @brief destructor
@@ -108,12 +111,22 @@
   DEFINE_ERROR(ZoneError, Error);
 
   /**
+   * @brief insert the m_zone to the database, and set the zone's id.
+   * If the zone is already in the db, handle the exception without leaving it to upper level,
+   * meanwhile, set the zone's id too.
+   * @pre m_zone.getId() == 0
+   * @post m_zone.getId() > 0
+   */
+  void
+  insert(Zone& zone);
+
+  /**
    * @brief lookup the zone by name, fill the m_id and m_ttl
    * @post whatever the previous id is
    * @return true if the record exist
    */
   bool
-  lookup(Zone& zone);
+  find(Zone& zone);
 
   /**
    * @brief remove the zone
@@ -123,15 +136,44 @@
   void
   remove(Zone& zone);
 
+public: // Rrset manipulation
+  DEFINE_ERROR(RrsetError, Error);
+
   /**
-   * @brief insert the m_zone to the database, and set the zone's id.
-   * If the zone is already in the db, handle the exception without leaving it to upper level,
-   * meanwhile, set the zone's id too.
-   * @pre m_zone.getId() == 0
-   * @post m_zone.getId() > 0
+   * @brief add the rrset
+   * @pre m_rrset.getId() == 0
+   * @post m_rrset.getId() > 0
    */
   void
-  insert(Zone& zone);
+  insert(Rrset& rrset);
+
+  /**
+   * @brief get the data from db according to `m_zone`, `m_label`, `m_type`.
+   *
+   * If record exists, `m_ttl`, `m_version` and `m_data` is set
+   *
+   * @pre m_rrset.getZone().getId() > 0
+   * @post whatever the previous id is,
+   *       m_rrset.getId() > 0 if record exists, otherwise m_rrset.getId() == 0
+   * @return true if the record exist
+   */
+  bool
+  find(Rrset& rrset);
+
+  /**
+   * @brief remove the rrset
+   * @pre m_rrset.getId() > 0
+   * @post m_rrset.getId() == 0
+   */
+  void
+  remove(Rrset& rrset);
+
+  /**
+   * @brief replace ttl, version, and Data with new values
+   * @pre m_rrset.getId() > 0
+   */
+  void
+  modify(Rrset& rrset);
 
 private:
   /**
diff --git a/src/ndns-tlv.hpp b/src/ndns-tlv.hpp
index c81c1a6..7c48920 100644
--- a/src/ndns-tlv.hpp
+++ b/src/ndns-tlv.hpp
@@ -3,18 +3,18 @@
  * Copyright (c) 2014, Regents of the University of California.
  *
  * This file is part of NDNS (Named Data Networking Domain Name Service).
- * See AUTHORS.md for complete list of NdnS authors and contributors.
+ * See AUTHORS.md for complete list of NDNS authors and contributors.
  *
- * NdnS is free software: you can redistribute it and/or modify it under the terms
+ * NDNS is free software: you can redistribute it and/or modify it under the terms
  * of the GNU General Public License as published by the Free Software Foundation,
  * either version 3 of the License, or (at your option) any later version.
  *
- * NdnS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  * PURPOSE.  See the GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License along with
- * NdnS, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ * NDNS, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 #ifndef NDNS_NDNS_TLV_HPP