blob: 5391edf7a02010ce9a2e82480756f8c5653bfd11 [file] [log] [blame]
Yingdi Yu783ce4b2013-10-18 17:04:19 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * Yingdi Yu
5 *
6 * BSD license, See the LICENSE file for more information
7 *
8 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
9 */
10
11#include "dns-storage.h"
Yingdi Yu783ce4b2013-10-18 17:04:19 -070012
13#include <boost/filesystem.hpp>
Yingdi Yuec3d9a32013-10-18 18:35:09 -070014#include "logging.h"
Yingdi Yu783ce4b2013-10-18 17:04:19 -070015
16
17using namespace std;
18using namespace ndn;
Yingdi Yu76dd8002013-12-24 11:16:32 +080019using namespace ndn::ptr_lib;
Yingdi Yu783ce4b2013-10-18 17:04:19 -070020namespace fs = boost::filesystem;
21
Yingdi Yuec3d9a32013-10-18 18:35:09 -070022INIT_LOGGER("DnsStorage");
23
Yingdi Yufa4ce792014-02-06 18:09:22 -080024namespace chronos{
25
Yingdi Yu783ce4b2013-10-18 17:04:19 -070026const string INIT_DD_TABLE = "\
27CREATE TABLE IF NOT EXISTS \n \
28 DnsData( \n \
29 dns_identity BLOB NOT NULL, \n \
30 dns_name BLOB NOT NULL, \n \
31 dns_type BLOB NOT NULL, \n \
32 data_name BLOB NOT NULL, \n \
33 dns_value BLOB NOT NULL, \n \
34 \
35 PRIMARY KEY (dns_identity, dns_name, dns_type) \n \
36 ); \
37CREATE INDEX dd_index ON DnsData(dns_identity, dns_name, dns_type); \n \
38CREATE INDEX dd_index2 ON DnsData(data_name); \n \
39";
40
41DnsStorage::DnsStorage()
42{
43 fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos";
44 fs::create_directories (chronosDir);
45
46 int res = sqlite3_open((chronosDir / "dns.db").c_str (), &m_db);
47 if (res != SQLITE_OK)
Yingdi Yuf8f572d2014-01-13 11:19:47 -080048 throw Error("Chronos DNS DB cannot be open/created");
Yingdi Yu783ce4b2013-10-18 17:04:19 -070049
50 // Check if SelfProfile table exists
51 sqlite3_stmt *stmt;
52 sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='DnsData'", -1, &stmt, 0);
53 res = sqlite3_step (stmt);
54
55 bool ddTableExist = false;
56 if (res == SQLITE_ROW)
57 ddTableExist = true;
58 sqlite3_finalize (stmt);
59
60 if(!ddTableExist)
61 {
62 char *errmsg = 0;
63 res = sqlite3_exec (m_db, INIT_DD_TABLE.c_str (), NULL, NULL, &errmsg);
64 if (res != SQLITE_OK && errmsg != 0)
Yingdi Yuf8f572d2014-01-13 11:19:47 -080065 throw Error("Init \"error\" in DnsData");
Yingdi Yu783ce4b2013-10-18 17:04:19 -070066 }
67}
68
Yingdi Yu783ce4b2013-10-18 17:04:19 -070069void
Yingdi Yuf8f572d2014-01-13 11:19:47 -080070DnsStorage::updateDnsData(const ndn::Block& data, const std::string& identity, const std::string& name, const std::string& type, const string& dataName)
Yingdi Yu783ce4b2013-10-18 17:04:19 -070071{
72 sqlite3_stmt *stmt;
Yingdi Yufa4ce792014-02-06 18:09:22 -080073 sqlite3_prepare_v2 (m_db, "INSERT OR REPLACE INTO DnsData (dns_identity, dns_name, dns_type, dns_value, data_name) VALUES (?, ?, ?, ?, ?)", -1, &stmt, 0);
Yingdi Yu783ce4b2013-10-18 17:04:19 -070074 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size(), SQLITE_TRANSIENT);
75 sqlite3_bind_text(stmt, 2, name.c_str(), name.size(), SQLITE_TRANSIENT);
76 sqlite3_bind_text(stmt, 3, type.c_str(), type.size(), SQLITE_TRANSIENT);
Yingdi Yufa4ce792014-02-06 18:09:22 -080077 sqlite3_bind_text(stmt, 4, (const char*)data.wire(), data.size(), SQLITE_TRANSIENT);
78 sqlite3_bind_text(stmt, 5, dataName.c_str(), dataName.size(), SQLITE_TRANSIENT);
79 sqlite3_step(stmt);
80 sqlite3_finalize(stmt);
Yingdi Yu783ce4b2013-10-18 17:04:19 -070081}
82
Yingdi Yu76dd8002013-12-24 11:16:32 +080083shared_ptr<Data>
Yingdi Yu783ce4b2013-10-18 17:04:19 -070084DnsStorage::getData(const Name& dataName)
85{
86 sqlite3_stmt *stmt;
87 sqlite3_prepare_v2 (m_db, "SELECT dns_value FROM DnsData where data_name=?", -1, &stmt, 0);
88 sqlite3_bind_text(stmt, 1, dataName.toUri().c_str(), dataName.toUri().size(), SQLITE_TRANSIENT);
89
90 if(sqlite3_step (stmt) == SQLITE_ROW)
91 {
Yingdi Yu76dd8002013-12-24 11:16:32 +080092 shared_ptr<Data> data = make_shared<Data>();
Yingdi Yuf8f572d2014-01-13 11:19:47 -080093 data->wireDecode(Block(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
Yingdi Yue35bdb82013-11-07 11:32:40 -080094 sqlite3_finalize(stmt);
Yingdi Yu76dd8002013-12-24 11:16:32 +080095 return data;
Yingdi Yu783ce4b2013-10-18 17:04:19 -070096 }
Yingdi Yue35bdb82013-11-07 11:32:40 -080097 sqlite3_finalize(stmt);
98
Yingdi Yu93adb1a2014-01-16 10:30:26 -080099 return shared_ptr<Data>();
Yingdi Yu783ce4b2013-10-18 17:04:19 -0700100}
Yingdi Yufa4ce792014-02-06 18:09:22 -0800101
102}//chronos