wait to be verified
diff --git a/src/db/README.txt b/src/db/README.txt
new file mode 100644
index 0000000..ea73d04
--- /dev/null
+++ b/src/db/README.txt
@@ -0,0 +1,11 @@
+#how to create the database
+pre-requirements:
+1. sqlite3 installed
+
+Steps to install and reset the database:
+1. run shell and go to the src/db directory as working directory 
+2. run command sqlite3 ndns-local.db on shell and thus, enter the sqlit3 shell program
+3. run .read ndns-database-tables.sql, which create the tables
+4. run .read ndns-database-data-demo.sql, which insert demo data into database
+
+
diff --git a/src/db/db-mgr.cpp b/src/db/db-mgr.cpp
new file mode 100644
index 0000000..5ab9db1
--- /dev/null
+++ b/src/db/db-mgr.cpp
@@ -0,0 +1,103 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * 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.
+ *
+ * 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;
+ * 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/>.
+ */
+
+#include <iostream>
+#include <fstream>
+
+#include "db-mgr.hpp"
+
+namespace ndn {
+namespace ndns {
+
+DBMgr::DBMgr()
+: m_dbfile("src/db/ndns-local.db")
+, m_conn(0)
+, m_reCode(-1)
+, m_status(DBClosed)
+, m_resultNum(0)
+{
+  std::fstream file;
+  file.open(m_dbfile,std::ios::in);
+  if (file)
+  {
+    std::cout<<"database file "<<m_dbfile<<" does exist"<<std::endl;
+  } else
+  {
+    std::cout<<"database file "<<m_dbfile<<" does not exist"<<std::endl;
+  }
+
+}
+
+
+DBMgr::~DBMgr()
+{
+}
+
+void DBMgr::open()
+{
+  if (m_status == DBConnected)
+    return;
+
+  m_reCode = sqlite3_open(this->m_dbfile.c_str(), &(this->m_conn));
+  if (m_reCode != SQLITE_OK)
+  {
+    m_err = "Cannot connect to the db: "+this->m_dbfile;
+    m_status = DBError;
+    //exit(1);
+  }else
+  {
+    std::cout<<"connect to the db: "<<m_dbfile<<std::endl;
+  }
+  m_status = DBConnected;
+}
+
+void DBMgr::close()
+{
+  if (m_status == DBClosed)
+    return;
+
+  m_reCode = sqlite3_close(this->m_conn);
+  if (m_reCode != SQLITE_OK)
+  {
+        m_err = "Cannot close the db: "+this->m_dbfile;
+        m_status = DBError;
+  }
+  m_status = DBClosed;
+}
+
+void DBMgr::execute(std::string sql, int (*callback)(void*,int,char**,char**), void * paras)
+{
+  if (m_status == DBClosed)
+    this->open();
+
+  clearResultNum();
+  char *err_msg;
+  m_reCode = sqlite3_exec(m_conn, sql.c_str(), callback, paras, &err_msg);
+  if (m_reCode != SQLITE_OK)
+  {
+    m_status = DBError;
+    this->m_err.append(err_msg);
+    std::cout<<this->m_err<<std::endl;
+  }
+  this->close();
+}
+
+}//namespace ndns
+}//namespace ndn
+
diff --git a/src/db/db-mgr.hpp b/src/db/db-mgr.hpp
new file mode 100644
index 0000000..c6445af
--- /dev/null
+++ b/src/db/db-mgr.hpp
@@ -0,0 +1,109 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * 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.
+ *
+ * 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;
+ * 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/>.
+ */
+
+#ifndef NDNS_DB_MGR_CPP
+#define NDNS_DB_MGR_CPP
+
+#include <sqlite3.h>
+#include <iostream>
+
+
+namespace ndn {
+namespace ndns {
+
+
+class DBMgr
+{
+public:
+  enum m_status
+  {
+    DBConnected,
+    DBClosed,
+    DBError
+  };
+
+public:
+  DBMgr();
+  virtual ~DBMgr();
+
+  void open();
+  void close();
+  void execute(std::string sql,
+        int (*callback)(void*,int,char**,char**), void * paras);
+
+  inline void addResultNum(){
+    m_resultNum += 1;
+  }
+  inline void clearResultNum() {
+    m_resultNum = 0;
+  }
+
+  const std::string& getDbfile() const {
+    return m_dbfile;
+  }
+
+  void setDbfile(const std::string& dbfile) {
+    this->m_dbfile = dbfile;
+  }
+
+  const std::string& getErr() const {
+    return m_err;
+  }
+
+  void setErr(const std::string& err) {
+    this->m_err = err;
+  }
+
+  int getReCode() const {
+    return m_reCode;
+  }
+
+  void setReCode(int reCode) {
+    this->m_reCode = reCode;
+  }
+
+  m_status getStatus() const {
+    return m_status;
+  }
+
+  void setStatus(m_status status) {
+    this->m_status = status;
+  }
+
+  int getResultNum() const {
+    return m_resultNum;
+  }
+
+  void setResultNum(int resultNum) {
+    m_resultNum = resultNum;
+  }
+
+
+private:
+  std::string m_dbfile;
+  sqlite3 *m_conn;
+  std::string m_err;
+  int m_reCode;
+  m_status m_status;
+  int m_resultNum;
+}; //class DBMgr
+}//namespace ndns
+}//namespace ndn
+
+#endif
diff --git a/src/db/ndns-db-data-demo.sql b/src/db/ndns-db-data-demo.sql
new file mode 100644
index 0000000..8564207
--- /dev/null
+++ b/src/db/ndns-db-data-demo.sql
@@ -0,0 +1,40 @@
+
+delete from rrs;
+delete from rrsets;
+delete from zones;
+
+insert into zones (id, name) values 
+                  (1, "/"),   
+                  (2, "/net"),
+                  (3, "/net/ndnsim"),
+                  (4, "/dns/google/com"),
+                  (5, "/net/ndnsim/git/doc")
+                  ;
+
+insert into rrsets (id, zone_id, label, class, type, ndndata) values
+                   (1,   3,     "/www", NULL, "TXT", NULL),
+                   (2,   3,     "/doc", NULL, "TXT", NULL),
+                   (3,   2,     "/ndnsim", NULL, "NS", NULL),
+                   (4,   1,     "/net",  NULL,   "NS", NULL),
+                   (5,   4,     "/net",  NULL,   "NS", NULL),
+                   (6,   3,     "/git/www", NULL, "TXT", NULL),
+                   (7,   3,     "/git/doc", NULL, "NS", NULL),
+                   (8,   5,     "/www", NULL, "TXT", NULL),
+                   (9,   3,     "/repo/www", NULL, "TXT", NULL),
+                   (10,  3,     "/norrdata", NULL, "TXT", NULL)
+                   ;
+                   
+                   
+insert into rrs (id, rrset_id, ttl, rrdata) values
+                (1, 1, 3600, "/ucla"),
+                (2, 1, 8000, "/att"),
+                (3, 2, 3600, "/ucla"),
+                (4, 3, 4000, "/net/ndnsim2"),
+                (5, 4, 5000, "/net"),
+                (6, 5, 6000, "/net"),
+                (7, 3, 4000, "/net/ndnsim"),
+                (8, 6, 3000, "/net/ndnsim/git/www"),
+                (9, 7, 3000, "/net/ndnsim/git/doc"),
+                (10,8, 3000, "/net/ndnsim/git/doc/www"),
+                (11,9, 3000, "/net/ndnsim/repo/www")
+                ;
diff --git a/src/db/ndns-db-tables.sql b/src/db/ndns-db-tables.sql
new file mode 100644
index 0000000..8ecc3b0
--- /dev/null
+++ b/src/db/ndns-db-tables.sql
@@ -0,0 +1,39 @@
+/*
+some difference with Alex's sql file:
+1. class field in table rrsets could be NULL
+2. type field in table rrsets is changed to text from integer(10)
+*/
+DROP TRIGGER IF EXISTS rrs_update;
+DROP TABLE IF EXISTS zones;
+DROP TABLE IF EXISTS rrsets;
+DROP TABLE IF EXISTS rrs;
+CREATE TABLE zones (
+  id    INTEGER NOT NULL PRIMARY KEY, 
+  name blob NOT NULL UNIQUE);
+CREATE TABLE rrsets (
+  id       INTEGER NOT NULL PRIMARY KEY, 
+  zone_id integer(10) NOT NULL, 
+  label   text NOT NULL, 
+  class   integer(10), 
+  type    text NOT NULL, 
+  ndndata blob, 
+  FOREIGN KEY(zone_id) REFERENCES zones(id) ON UPDATE Cascade ON DELETE Cascade);
+CREATE TABLE rrs (
+  id        INTEGER NOT NULL PRIMARY KEY, 
+  rrset_id integer(10) NOT NULL, 
+  ttl      integer(10) NOT NULL, 
+  rrdata   blob NOT NULL, 
+  FOREIGN KEY(rrset_id) REFERENCES rrsets(id) ON UPDATE Cascade ON DELETE Cascade);
+CREATE UNIQUE INDEX rrsets_zone_id_label_class_type 
+  ON rrsets (zone_id, label, class, type);
+CREATE INDEX rrs_rrset_id 
+  ON rrs (rrset_id);
+CREATE INDEX rrs_rrset_id_rrdata 
+  ON rrs (rrset_id, rrdata);
+CREATE TRIGGER rrs_update
+BEFORE INSERT ON rrs
+FOR EACH ROW
+BEGIN
+    DELETE FROM rrs WHERE rrset_id = NEW.rrset_id AND rrdata = NEW.rrdata;
+END;
+
diff --git a/src/db/ndns-local.db b/src/db/ndns-local.db
new file mode 100644
index 0000000..a9177ca
--- /dev/null
+++ b/src/db/ndns-local.db
Binary files differ
diff --git a/src/db/rr-mgr.cpp b/src/db/rr-mgr.cpp
new file mode 100644
index 0000000..51e0e3b
--- /dev/null
+++ b/src/db/rr-mgr.cpp
@@ -0,0 +1,112 @@
+/*
+ * rr-mgr.cpp
+ *
+ *  Created on: 23 Jul, 2014
+ *      Author: shock
+ */
+
+#include "rr-mgr.hpp"
+
+namespace ndn {
+namespace ndns{
+RRMgr::RRMgr(Zone& zone, Query& query, Response& response)
+  : m_count(0U)
+  , m_zone(zone)
+  , m_query(query)
+  , m_response(response)
+{
+
+}
+
+
+RRMgr::~RRMgr() {
+  // TODO Auto-generated destructor stub
+}
+
+
+
+int
+RRMgr::count()
+{
+  std::string sql;
+  sql =  "SELECT count(*) FROM rrs INNER JOIN rrsets ON rrs.rrset_id=rrsets.id";
+  sql += " WHERE rrsets.zone_id=";
+  sql += std::to_string(m_zone.getId());
+  sql += " AND ";
+  sql += "rrsets.type=\'";
+  sql += RR::toString(m_query.getRrType());
+  sql += "\' AND ";
+  sql += "rrsets.label LIKE\'";
+  sql += m_query.getRrLabel().toUri()+"/%\'";
+
+  std::cout<<"sql="<<sql<<std::endl;
+  this->execute(sql, static_callback_countRr, this);
+
+  if (this->getStatus() == DBMgr::DBError)
+  {
+    return -1;
+  }
+
+  return this->m_count;
+}
+int
+RRMgr::callback_countRr(int argc, char **argv, char **azColName)
+{
+  this->addResultNum();
+
+  std::cout<<this->getResultNum()<<"th result: "<<"count="<<argv[0]<<std::endl;
+  m_count = std::atoi(argv[0]);
+  return 0;
+}
+
+int RRMgr::lookup()
+{
+  std::string sql;
+
+  sql =  "SELECT rrs.ttl, rrs.rrdata, rrs.id FROM rrs INNER JOIN rrsets ON rrs.rrset_id=rrsets.id";
+  sql += " WHERE rrsets.zone_id=";
+  sql += std::to_string(m_zone.getId());
+  sql += " AND ";
+  sql += "rrsets.type=\'";
+  sql += RR::toString(m_query.getRrType());
+  sql += "\' AND ";
+  sql += "rrsets.label=\'";
+  sql += m_query.getRrLabel().toUri()+"\'";
+  sql += " ORDER BY rrs.id";
+
+  std::cout<<"sql="<<sql<<std::endl;
+  this->execute(sql, static_callback_getRr, this);
+
+  if (this->getStatus() == DBMgr::DBError)
+  {
+    return -1;
+  }
+
+  //m_response.setQueryName(m_query.getAuthorityZone());
+  return 0;
+}
+
+int RRMgr::callback_getRr(int argc, char **argv, char **azColName)
+{
+  this->addResultNum();
+  if (argc < 1)
+  {
+    this->setErr("No RRType="+RR::toString(m_query.getRrType())+
+        " and label="+m_query.getRrLabel().toUri()+
+        " and zone="+m_zone.getAuthorizedName().toUri());
+    return 0;
+  }
+
+  std::cout<<this->getResultNum()<<"th result: "<<"id="<<argv[2]
+           <<" ttl="<<argv[0]<<" rrdata="<<argv[1]<<std::endl;
+
+  m_response.setFreshness(time::milliseconds(std::atoi(argv[0])));
+
+  m_response.addRr(std::atoi(argv[2]), argv[1]);
+  //std::cout<<"after add a Rr: current size="<<m_response.getRrs().size()<<std::endl;
+  //m_response.setFreshness(time::milliseconds(std::atoi(argv[0])));
+  return 0;
+}
+
+}//namespace ndnsn
+} /* namespace ndn */
diff --git a/src/db/rr-mgr.hpp b/src/db/rr-mgr.hpp
new file mode 100644
index 0000000..e940d10
--- /dev/null
+++ b/src/db/rr-mgr.hpp
@@ -0,0 +1,94 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * 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.
+ *
+ * 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;
+ * 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/>.
+ */
+
+#ifndef RR_MGR_HPP
+#define RR_MGR_HPP
+
+#include "db-mgr.hpp"
+#include "query.hpp"
+#include "response.hpp"
+#include "zone.hpp"
+
+
+namespace ndn {
+namespace ndns{
+class RRMgr : public DBMgr {
+public:
+  RRMgr( Zone& zone, Query& query, Response& response);
+  virtual ~RRMgr();
+
+public:
+  int lookup();
+
+  int callback_getRr(int argc, char **argv, char **azColName);
+
+  static int
+  static_callback_getRr(void *param, int argc, char **argv, char **azColName)
+  {
+    RRMgr *mgr = reinterpret_cast<RRMgr*>(param);
+    return mgr->callback_getRr(argc, argv, azColName);
+   }
+
+  int count();
+  int callback_countRr(int argc, char **argv, char **azColName);
+
+  static int
+  static_callback_countRr(void *param, int argc, char **argv, char **azColName)
+  {
+    RRMgr *mgr = reinterpret_cast<RRMgr*>(param);
+    return mgr->callback_countRr(argc, argv, azColName);
+   }
+
+  const Query& getQuery() const {
+    return m_query;
+  }
+
+  void setQuery(const Query& query) {
+    m_query = query;
+  }
+
+  const Response& getResponse() const {
+    return m_response;
+  }
+
+  void setResponse(const Response& response) {
+    m_response = response;
+  }
+
+  const Zone& getZone() const {
+    return m_zone;
+  }
+
+  void setZone(const Zone& zone) {
+    m_zone = zone;
+  }
+
+private:
+  unsigned int m_count;
+  Zone& m_zone;
+  Query& m_query;
+  Response& m_response;
+};
+
+
+
+} //namespace ndns
+} /* namespace ndn */
+
+#endif /* RR_MGR_HPP_ */
diff --git a/src/db/zone-mgr.cpp b/src/db/zone-mgr.cpp
new file mode 100644
index 0000000..cc0b613
--- /dev/null
+++ b/src/db/zone-mgr.cpp
@@ -0,0 +1,77 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * 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.
+ *
+ * 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;
+ * 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/>.
+ */
+
+#include "zone-mgr.hpp"
+
+
+namespace ndn {
+namespace ndns {
+
+ZoneMgr::ZoneMgr( Zone& zone)
+: m_zone(zone)
+{
+  this->lookupId();
+}
+
+void
+ZoneMgr::lookupId(const Name& name)
+{
+  this->open();
+  std::string sql = "SELECT id FROM zones WHERE name=\'"+name.toUri()+"\'";
+  //sql = "SELECT * FROM ZONES";
+  std::cout<<"sql="<<sql<<std::endl;
+  //std::cout<<"*this="<<this<<" m_zone.id="<<m_zone.getId()<<" zoneId="<<&m_zone<<std::endl;
+  this->execute(sql, static_callback_setId, this);
+  //std::cout<<"*this="<<this<<" m_zone.id="<<m_zone.getId()<<" zoneId="<<&m_zone<<std::endl;
+  this->close();
+
+}
+int
+ZoneMgr::callback_setId(int argc, char **argv, char **azColName)
+{
+  //Zone zone = this->getZone();
+  this->addResultNum();
+
+  Zone zone = this->m_zone;
+
+  if (argc < 1)
+  {
+    this->setErr("No Zone with Name "+zone.getAuthorizedName().toUri());
+    return -1;
+  }
+  //std::cout<<"id="<<(uint32_t)std::atoi(argv[0])<<" "<<std::endl;
+  int t1 = std::atoi(argv[0]);
+
+
+  m_zone.setId(t1);
+
+  return 0;
+}
+void
+ZoneMgr::lookupId()
+{
+  lookupId(this->m_zone.getAuthorizedName());
+}
+
+
+}//namepsace ndns
+}//namespace ndn
+
+
+
diff --git a/src/db/zone-mgr.hpp b/src/db/zone-mgr.hpp
new file mode 100644
index 0000000..8ced971
--- /dev/null
+++ b/src/db/zone-mgr.hpp
@@ -0,0 +1,72 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * 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.
+ *
+ * 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;
+ * 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/>.
+ */
+
+#ifndef NDNS_ZONE_MGR_CPP
+#define NDNS_ZONE_MGR_CPP
+
+#include <sqlite3.h>
+
+#include "db-mgr.hpp"
+#include "zone.hpp"
+#include <ndn-cxx/name.hpp>
+
+namespace ndn {
+namespace ndns {
+
+class ZoneMgr : public DBMgr
+{
+
+public:
+  ZoneMgr( Zone& zone);
+
+public:
+  void
+  lookupId(const Name& name);
+
+  void
+  lookupId();
+
+   const Zone& getZone() const {
+    return m_zone;
+  }
+
+  void setZone(const Zone& zone) {
+    this->m_zone = zone;
+  }
+
+  int
+  callback_setId(int argc, char **argv, char **azColName);
+
+
+  static int
+  static_callback_setId(void *param, int argc, char **argv, char **azColName){
+
+     ZoneMgr *mgr = reinterpret_cast<ZoneMgr*>(param);
+     return mgr->callback_setId(argc, argv, azColName);
+   }
+
+private:
+  Zone& m_zone;
+};//class ZoneMgr
+
+
+
+}//namespace ndns
+}//namespace ndn
+#endif