blob: 02598a90406827c11da716eff9e264c208e3d1c2 [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2014, Regents of the University of California.
*
* This file is part of NDN repo-ng (Next generation of NDN repository).
* See AUTHORS.md for complete list of repo-ng authors and contributors.
*
* repo-ng 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.
*
* repo-ng 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
* repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.hpp"
#include "sqlite-handle.hpp"
#include <boost/filesystem.hpp>
namespace repo {
SqliteHandle::SqliteHandle(const string& dbPath)
: StorageHandle(STORAGE_METHOD_SQLITE)
{
if (dbPath.empty()) {
std::cerr << "Create db file in local location [" << dbPath << "]. " << std::endl
<< "You can assign the path using -d option" << std::endl;
m_dbPath = string("ndn_repo.db");
}
else {
boost::filesystem::path fsPath(dbPath);
boost::filesystem::file_status fsPathStatus = boost::filesystem::status(fsPath);
if (!boost::filesystem::is_directory(fsPathStatus)) {
if (!boost::filesystem::create_directory(boost::filesystem::path(fsPath))) {
throw Error("Folder '" + dbPath + "' does not exists and cannot be created");
}
}
m_dbPath = dbPath + "/ndn_repo.db";
}
initializeRepo();
}
void
SqliteHandle::initializeRepo()
{
char* errMsg = 0;
int rc = sqlite3_open_v2(m_dbPath.c_str(), &m_db,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
#ifdef DISABLE_SQLITE3_FS_LOCKING
"unix-dotfile"
#else
0
#endif
);
if (rc == SQLITE_OK) {
sqlite3_exec(m_db, "CREATE TABLE NDN_REPO ("
"name BLOB PRIMARY KEY, "
"data BLOB, "
"parentName BLOB, "
"nChildren INTEGER);\n"
"CREATE INDEX NdnRepoParentName ON NDN_REPO (parentName);\n"
"CREATE INDEX NdnRepoData ON NDN_REPO (data);\n"
, 0, 0, &errMsg);
// Ignore errors (when database already exists, errors are expected)
}
else {
std::cerr << "Database file open failure rc:" << rc << std::endl;
throw Error("Database file open failure");
}
Name rootName;
string sql = string("SELECT * FROM NDN_REPO WHERE name = ?;");
sqlite3_stmt* queryStmt = 0;
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &queryStmt, 0);
if (rc == SQLITE_OK) {
if (sqlite3_bind_blob(queryStmt, 1, rootName.wireEncode().wire(),
rootName.wireEncode().size(), 0) == SQLITE_OK) {
rc = sqlite3_step(queryStmt);
if (rc == SQLITE_ROW) {
std::cerr << "root has been created" << std::endl;
}
else if (rc == SQLITE_DONE) {
sqlite3_stmt* p2Stmt = 0;
sql = string("INSERT INTO NDN_REPO (name, data, parentName, nChildren) "
" VALUES (?, ?, ?, ?);");
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &p2Stmt, 0);
if (rc == SQLITE_OK) {
if (sqlite3_bind_blob(p2Stmt, 1, rootName.wireEncode().wire(),
rootName.wireEncode().size(), 0) == SQLITE_OK &&
sqlite3_bind_null(p2Stmt, 2) == SQLITE_OK &&
sqlite3_bind_null(p2Stmt, 3) == SQLITE_OK &&
sqlite3_bind_int(p2Stmt, 4, 0) == SQLITE_OK) {
rc = sqlite3_step(p2Stmt);;
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
std::cerr << "Root name insert failure rc:" << rc << std::endl;
sqlite3_finalize(p2Stmt);
throw Error("Root name insert failure");
}
}
else {
std::cerr << "bind blob failure rc:" << rc << std::endl;
sqlite3_finalize(p2Stmt);
throw Error("bind blob failure");
}
}
else {
std::cerr << "p2Stmt prepared rc:" << rc << std::endl;
sqlite3_finalize(p2Stmt);
throw Error("p2Stmt prepared");
}
sqlite3_finalize(p2Stmt);
}
else {
std::cerr << "Database query failure rc:" << rc << std::endl;
sqlite3_finalize(queryStmt);
throw Error("Database query failure");
}
}
sqlite3_finalize(queryStmt);
}
sqlite3_exec(m_db, "PRAGMA synchronous = OFF", 0, 0, &errMsg);
sqlite3_exec(m_db, "PRAGMA journal_mode = WAL", 0, 0, &errMsg);
}
SqliteHandle::~SqliteHandle()
{
sqlite3_close(m_db);
}
//Temporarily assigned the datatype of every component. needs to be further discussed
bool
SqliteHandle::insertData(const Data& data)
{
Name name = data.getName();
if (name.empty()) {
std::cerr << "name is empty" << std::endl;
return false;
}
int rc = 0;
string updateSql2 = string("UPDATE NDN_REPO SET data = ? WHERE name = ?;");
//std::cerr << "update" << std::endl;
sqlite3_stmt* update2Stmt = 0;
if (sqlite3_prepare_v2(m_db, updateSql2.c_str(), -1, &update2Stmt, 0) != SQLITE_OK) {
sqlite3_finalize(update2Stmt);
std::cerr << "update sql2 not prepared" << std::endl;
throw Error("update sql2 not prepared");
}
if (sqlite3_bind_blob(update2Stmt, 1,
data.wireEncode().wire(),
data.wireEncode().size(), 0) == SQLITE_OK &&
sqlite3_bind_blob(update2Stmt, 2,
name.wireEncode().wire(),
name.wireEncode().size(), 0) == SQLITE_OK) {
rc = sqlite3_step(update2Stmt);
sqlite3_finalize(update2Stmt);
if (rc != SQLITE_DONE) {
return false;
}
//what error??
//std::cerr << "update rc:" << rc << std::endl;
/// \todo Do something with rc
}
int changeCount = sqlite3_changes(m_db);
//std::cerr << "changeCount: " << changeCount << std::endl;
if (changeCount > 0) {
return true;
}
sqlite3_stmt* insertStmt = 0;
sqlite3_stmt* updateStmt = 0;
string insertSql = string("INSERT INTO NDN_REPO (name, data, parentName, nChildren) "
"VALUES (?, ?, ?, ?)");
string updateSql = string("UPDATE NDN_REPO SET nChildren = nChildren + 1 WHERE name = ?");
Name rootName;
if (sqlite3_prepare_v2(m_db, insertSql.c_str(), -1, &insertStmt, 0) != SQLITE_OK) {
sqlite3_finalize(insertStmt);
std::cerr << "insert sql not prepared" << std::endl;
}
if (sqlite3_prepare_v2(m_db, updateSql.c_str(), -1, &updateStmt, 0) != SQLITE_OK) {
sqlite3_finalize(updateStmt);
std::cerr << "update sql not prepared" << std::endl;
throw Error("update sql not prepared");
}
//Insert and read the prefix
Name parentName = name;
Name grandName;
do {
parentName = parentName.getPrefix(-1);
if (!hasName(parentName)) {
grandName = parentName.getPrefix(-1);
if (sqlite3_bind_blob(insertStmt, 1,
parentName.wireEncode().wire(),
parentName.wireEncode().size(), 0) == SQLITE_OK &&
sqlite3_bind_null(insertStmt, 2) == SQLITE_OK &&
sqlite3_bind_blob(insertStmt, 3,
grandName.wireEncode().wire(),
grandName.wireEncode().size(), 0) == SQLITE_OK &&
sqlite3_bind_int(insertStmt, 4, 1) == SQLITE_OK) {
rc = sqlite3_step(insertStmt);
if (rc == SQLITE_CONSTRAINT) {
std::cerr << "Insert parent prefix failed" << std::endl;
sqlite3_finalize(insertStmt);
throw Error("Insert parent prefix failed");
}
sqlite3_reset(insertStmt);
}
}
else {
break;
}
} while (!parentName.empty());
//The existed parent nChildren + 1
if (sqlite3_bind_blob(updateStmt, 1, parentName.wireEncode().wire(),
parentName.wireEncode().size(), 0) == SQLITE_OK) {
rc = sqlite3_step(updateStmt);
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
std::cerr << "update error rc:" << rc << std::endl;
sqlite3_finalize(updateStmt);
sqlite3_finalize(insertStmt);
throw Error("update error");
}
sqlite3_reset(updateStmt);
}
//Insert the name and the data, if this data name exists update, else insert data
parentName = name.getPrefix(-1);
sqlite3_reset(insertStmt);
if (sqlite3_bind_blob(insertStmt, 1,
name.wireEncode().wire(),
name.wireEncode().size(), 0) == SQLITE_OK &&
sqlite3_bind_blob(insertStmt, 2,
data.wireEncode().wire(),
data.wireEncode().size(), 0) == SQLITE_OK &&
sqlite3_bind_blob(insertStmt, 3,
parentName.wireEncode().wire(),
parentName.wireEncode().size(), 0) == SQLITE_OK &&
sqlite3_bind_int(insertStmt, 4, 0) == SQLITE_OK) {
rc = sqlite3_step(insertStmt);
//std::cerr << "insert rc:" << rc << std::endl;
//std::cerr << "insert the data: " << data.wireEncode().wire() << std::endl;
if (rc == SQLITE_CONSTRAINT) {
std::cerr << "The name of the data has existed!" << std::endl;
sqlite3_finalize(insertStmt);
return false;
}
}
sqlite3_finalize(updateStmt);
sqlite3_finalize(insertStmt);
return true;
}
bool
SqliteHandle::deleteData(const Name& name)
{
sqlite3_stmt* queryStmt = 0;
sqlite3_stmt* deleteStmt = 0;
sqlite3_stmt* updateStmt = 0;
sqlite3_stmt* update2Stmt = 0;
string querySql = string("SELECT * from NDN_REPO where name = ?;");
string deleteSql = string("DELETE from NDN_REPO where name = ?;");
string updateSql = string("UPDATE NDN_REPO SET nChildren = nChildren - 1 WHERE name = ?;");
string updateSql2 = string("UPDATE NDN_REPO SET data = NULL WHERE name = ?;");
int rc = sqlite3_prepare_v2(m_db, querySql.c_str(), -1, &queryStmt, 0);
Name tmpName = name;
int nChildren = -1;
if (sqlite3_prepare_v2(m_db, deleteSql.c_str(), -1, &deleteStmt, 0) != SQLITE_OK) {
sqlite3_finalize(deleteStmt);
std::cerr << "delete statement prepared failed" << std::endl;
throw Error("delete statement prepared failed");
}
if (sqlite3_prepare_v2(m_db, updateSql.c_str(), -1, &updateStmt, 0) != SQLITE_OK) {
sqlite3_finalize(updateStmt);
std::cerr << "delete update prepared failed" << std::endl;
throw Error("delete update prepared failed");
}
if (rc == SQLITE_OK) {
if (sqlite3_bind_blob(queryStmt, 1,
tmpName.wireEncode().wire(),
tmpName.wireEncode().size(), 0) == SQLITE_OK) {
rc = sqlite3_step(queryStmt);
if (rc == SQLITE_ROW) {
nChildren = sqlite3_column_int(queryStmt, 3);
}
else {
std::cerr << "Database query no such name or failure rc:" << rc << std::endl;
sqlite3_finalize(queryStmt);
return false;
}
}
if (nChildren > 0) {
//update internal node, so just update and return
if (sqlite3_prepare_v2(m_db, updateSql2.c_str(), -1, &update2Stmt, 0) != SQLITE_OK) {
sqlite3_finalize(update2Stmt);
std::cerr << "delete update prepared failed" << std::endl;
throw Error("delete update prepared failed");
}
if (sqlite3_bind_blob(update2Stmt, 1,
tmpName.wireEncode().wire(),
tmpName.wireEncode().size(), 0) == SQLITE_OK) {
rc = sqlite3_step(update2Stmt);
std::cerr << "deleteData update" << std::endl;
}
else {
std::cerr << "delete bind error" << std::endl;
sqlite3_finalize(update2Stmt);
throw Error("delete bind error");
}
return true;
}
else {
//Delete the leaf node
if (sqlite3_bind_blob(deleteStmt, 1,
tmpName.wireEncode().wire(),
tmpName.wireEncode().size(), 0) == SQLITE_OK) {
rc = sqlite3_step(deleteStmt);
if (rc != SQLITE_DONE && rc !=SQLITE_ROW) {
std::cerr << "leaf node delete error rc:" << rc << std::endl;
sqlite3_finalize(deleteStmt);
throw Error("leaf node delete error");
}
}
else {
std::cerr << "delete bind error" << std::endl;
sqlite3_finalize(deleteStmt);
throw Error("delete bind error");
}
sqlite3_reset(deleteStmt);
}
queryStmt = 0;
rc = sqlite3_prepare_v2(m_db, querySql.c_str(), -1, &queryStmt, 0);
if (rc != SQLITE_OK) {
std::cerr << "prepare error" << std::endl;
sqlite3_finalize(queryStmt);
throw Error("prepare error");
}
//read prefix if nChildren is 0 and data is 0
int dataSize = 0;
do {
tmpName = tmpName.getPrefix(-1);
if (sqlite3_bind_blob(queryStmt, 1,
tmpName.wireEncode().wire(),
tmpName.wireEncode().size(), 0) == SQLITE_OK) {
rc = sqlite3_step(queryStmt);
if (rc == SQLITE_ROW) {
nChildren = sqlite3_column_int(queryStmt, 3);
dataSize = sqlite3_column_bytes(queryStmt, 1);
}
else {
std::cerr << "Database query no such name or failure rc:" << rc << std::endl;
sqlite3_finalize(queryStmt);
return false;
}
if (nChildren == 1 && !tmpName.empty() && dataSize == 0) {
//Delete this internal node
if (sqlite3_bind_blob(deleteStmt, 1,
tmpName.wireEncode().wire(),
tmpName.wireEncode().size(), 0) == SQLITE_OK) {
rc = sqlite3_step(deleteStmt);
if (rc != SQLITE_DONE && rc !=SQLITE_ROW) {
std::cerr << "internal node delete error rc:" << rc << std::endl;
sqlite3_finalize(deleteStmt);
throw Error("internal node delete error");
}
}
else {
std::cerr << "delete bind error" << std::endl;
sqlite3_finalize(deleteStmt);
throw Error("delete bind error");
}
sqlite3_reset(deleteStmt);
}
else {
//nChildren - 1
if (sqlite3_bind_blob(updateStmt, 1,
tmpName.wireEncode().wire(),
tmpName.wireEncode().size(), 0) == SQLITE_OK) {
rc = sqlite3_step(updateStmt);
if (rc != SQLITE_DONE && rc !=SQLITE_ROW) {
std::cerr << "internal node nChildren update error rc:" << rc << std::endl;
sqlite3_finalize(updateStmt);
throw Error("internal node nChildren update error");
}
}
else {
std::cerr << "update bind error" << std::endl;
sqlite3_finalize(updateStmt);
throw Error("update bind error");
}
sqlite3_reset(updateStmt);
break;
}
}
else {
std::cerr << "query bind error" << std::endl;
sqlite3_finalize(queryStmt);
throw Error("query bind error");
}
sqlite3_reset(queryStmt);
} while (!tmpName.empty());
}
else {
std::cerr << "query prepared failure rc:" << rc << std::endl;
sqlite3_finalize(queryStmt);
throw Error("query prepared failure");
}
return true;
}
bool
SqliteHandle::readData(const Interest& interest, Data& data)
{
vector<Name> names;
Name resultName;
if (!interest.hasSelectors()) {
return readDataPlain(interest.getName(), data);
}
else {
if (readNameSelector(interest, names)) {
if (!filterNameChild(interest.getName(), interest.getChildSelector(), names, resultName)) {
return false;
}
}
return readData(resultName, data);
}
}
// This function is the first version of data read following longest prefix match.
// It will return the leftmost data
bool
SqliteHandle::readDataPlain(const Name& name, Data& data)
{
vector<Name> names;
Name resultName;
readDataName(name, names);
filterNameChild(name, 0, names, resultName);
if (!resultName.empty()) {
return readData(resultName, data);
}
else
{
return false;
}
}
// retrieve all the leaf nodes of a subtree
bool
SqliteHandle::readDataName(const Name& name, vector<Name>& names) const
{
if (name.empty()) {
std::cerr << "The name is empty" << std::endl;
return false;
}
Name tmpName = name;
//This queue is for internal node;
queue<Name> internalNames;
// Step 1. Check if the requested name corresponds to a leaf (data is not NULL)
string sql = string("SELECT * FROM NDN_REPO WHERE name = ? AND data IS NOT NULL;");
sqlite3_stmt* queryStmt = 0;
int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &queryStmt, 0);
if (rc != SQLITE_OK)
throw Error("prepare error");
if (sqlite3_bind_blob(queryStmt, 1,
tmpName.wireEncode().wire(),
tmpName.wireEncode().size(), 0) == SQLITE_OK) {
rc = sqlite3_step(queryStmt);
if (rc == SQLITE_ROW) {
int nChildren = sqlite3_column_int(queryStmt, 3);
Name elementName;
elementName.wireDecode(Block(sqlite3_column_blob(queryStmt, 0),
sqlite3_column_bytes(queryStmt, 0)));
names.push_back(elementName);
if (nChildren == 0) {
sqlite3_finalize(queryStmt);
return true;
}
}
else if (rc == SQLITE_DONE) {
// ignore
}
else {
std::cerr << "read error rc:" << rc << std::endl;
sqlite3_finalize(queryStmt);
throw Error("read error");
}
}
sqlite3_finalize(queryStmt);
// Step 2. Recursively find all data packets with the specified prefix
string psql = string("SELECT * FROM NDN_REPO WHERE parentName = ?;");
sqlite3_stmt* queryParentStmt = 0;
rc = sqlite3_prepare_v2(m_db, psql.c_str(), -1, &queryParentStmt, 0);
if (rc != SQLITE_OK)
throw Error("prepare error");
internalNames.push(tmpName);
while (!internalNames.empty()) {
tmpName = internalNames.front();
internalNames.pop();
if (sqlite3_bind_blob(queryParentStmt, 1,
tmpName.wireEncode().wire(),
tmpName.wireEncode().size(), 0) == SQLITE_OK) {
while (true) {
rc = sqlite3_step(queryParentStmt);
if (rc == SQLITE_ROW) {
Name elementName;
elementName.wireDecode(Block(sqlite3_column_blob(queryParentStmt, 0),
sqlite3_column_bytes(queryParentStmt, 0)));
int nChildren = sqlite3_column_int(queryParentStmt, 3);
if (nChildren > 0) {
internalNames.push(elementName);
}
if (sqlite3_column_type(queryParentStmt, 1) != SQLITE_NULL) {
names.push_back(elementName);
}
}
else if (rc == SQLITE_DONE) {
break;
}
else {
std::cerr << "read error rc:" << rc << std::endl;
sqlite3_finalize(queryParentStmt);
throw Error("read error");
}
}
sqlite3_reset(queryParentStmt);
}
else {
std::cerr << "bind error" << std::endl;
sqlite3_finalize(queryParentStmt);
throw Error("bind error");
}
}
sqlite3_finalize(queryParentStmt);
return true;
}
bool
SqliteHandle::readNameSelector(const Interest& interest, vector<Name>& names) const
{
if (interest.getName().empty()) {
std::cerr << "The name of interest is empty" << std::endl;
return false;
}
Name tmpName = interest.getName();
//This queue is for internal node;
queue<Name> internalNames;
// Step 1. Check if the requested Data corresponds to a leaf (data is not NULL)
sqlite3_stmt* queryStmt = 0;
string sql = string("SELECT * FROM NDN_REPO WHERE name = ? AND data IS NOT NULL;");
int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &queryStmt, 0);
if (rc != SQLITE_OK)
throw Error("prepare error");
if (sqlite3_bind_blob(queryStmt, 1,
tmpName.wireEncode().wire(),
tmpName.wireEncode().size(), 0) == SQLITE_OK) {
rc = sqlite3_step(queryStmt);
if (rc == SQLITE_ROW) {
Data elementData;
elementData.wireDecode(Block(sqlite3_column_blob(queryStmt, 1),
sqlite3_column_bytes(queryStmt, 1)));
if (interest.matchesData(elementData)) {
names.push_back(elementData.getName());
}
int nChildren = sqlite3_column_int(queryStmt, 3);
if (nChildren == 0) {
sqlite3_finalize(queryStmt);
return true;
}
}
else if (rc == SQLITE_DONE) {
// ignore
}
else {
std::cerr << "read error rc:" << rc << std::endl;
sqlite3_finalize(queryStmt);
throw Error("read error");
}
}
sqlite3_finalize(queryStmt);
// Step 2. Recursively find all data packets that match the Interest
internalNames.push(tmpName);
sqlite3_stmt* queryParentStmt = 0;
string psql = string("SELECT * FROM NDN_REPO WHERE parentName = ?;");
rc = sqlite3_prepare_v2(m_db, psql.c_str(), -1, &queryParentStmt, 0);
if (rc != SQLITE_OK)
throw Error("prepare error");
while (!internalNames.empty()) {
tmpName = internalNames.front();
internalNames.pop();
if (sqlite3_bind_blob(queryParentStmt, 1,
tmpName.wireEncode().wire(),
tmpName.wireEncode().size(), 0) == SQLITE_OK) {
while (true) {
rc = sqlite3_step(queryParentStmt);
if (rc == SQLITE_ROW) {
if (sqlite3_column_type(queryParentStmt, 1) != SQLITE_NULL) {
Data elementData;
elementData.wireDecode(Block(sqlite3_column_blob(queryParentStmt, 1),
sqlite3_column_bytes(queryParentStmt, 1)));
if (interest.matchesData(elementData)) {
names.push_back(elementData.getName());
}
}
Name elementName;
elementName.wireDecode(Block(sqlite3_column_blob(queryParentStmt, 0),
sqlite3_column_bytes(queryParentStmt, 0)));
int nChildren = sqlite3_column_int(queryParentStmt, 3);
if (nChildren > 0) {
internalNames.push(elementName);
}
}
else if (rc == SQLITE_DONE) {
break;
}
else {
std::cerr << "read error rc:" << rc << std::endl;
sqlite3_finalize(queryParentStmt);
throw Error("read error");
}
}
sqlite3_reset(queryParentStmt);
}
else {
std::cerr << "bind error" << std::endl;
sqlite3_finalize(queryParentStmt);
throw Error("bind error");
}
}
sqlite3_finalize(queryParentStmt);
return true;
}
bool
SqliteHandle::filterNameChild(const Name& name, int childSelector,
const vector<Name>& names, Name& resultName)
{
if (childSelector == 0) {
if (!names.empty()) {
resultName = *std::min_element(names.begin(), names.end());
}
else {
return false;
}
}
else if (childSelector == 1) {
if (!names.empty()) {
resultName = *std::max_element(names.begin(), names.end());
}
else {
return false;
}
}
else {
return false;
}
return true;
}
bool
SqliteHandle::readNameAny(const Name& name, const Selectors& selectors, vector<Name>& names)
{
if (selectors.empty()) {
if (hasName(name)) {
names.push_back(name);
}
return true;
}
else {
Interest interest(name);
interest.setSelectors(selectors);
readNameSelector(interest, names);
if (selectors.getChildSelector() >= 0) {
Name resultName;
if (!filterNameChild(name, selectors.getChildSelector(), names, resultName))
return false;
names.clear();
names.push_back(resultName);
return true;
}
else {
return true;
}
}
}
bool
SqliteHandle::readData(const Name& name, Data& data)
{
sqlite3_stmt* queryStmt = 0;
string sql = string("SELECT * FROM NDN_REPO WHERE name = ? AND data IS NOT NULL;");
int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &queryStmt, 0);
if (rc == SQLITE_OK) {
if (sqlite3_bind_blob(queryStmt, 1,
name.wireEncode().wire(),
name.wireEncode().size(), 0) == SQLITE_OK) {
rc = sqlite3_step(queryStmt);
if (rc == SQLITE_ROW) {
data.wireDecode(Block(sqlite3_column_blob(queryStmt, 1),
sqlite3_column_bytes(queryStmt, 1)));
sqlite3_finalize(queryStmt);
return true;
}
else if (rc == SQLITE_DONE) {
return false;
}
else {
std::cerr << "Database query failure rc:" << rc << std::endl;
sqlite3_finalize(queryStmt);
throw Error("Database query failure");
}
}
sqlite3_finalize(queryStmt);
}
return true;
}
//This is the exact name query in database.
bool
SqliteHandle::hasName(const Name& name)
{
sqlite3_stmt* queryStmt = 0;
string sql = string("select * from NDN_REPO where name = ?;");
int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &queryStmt, 0);
if (rc == SQLITE_OK) {
if (sqlite3_bind_blob(queryStmt, 1,
name.wireEncode().wire(),
name.wireEncode().size(), 0) == SQLITE_OK) {
rc = sqlite3_step(queryStmt);
if (rc == SQLITE_ROW) {
sqlite3_finalize(queryStmt);
return true;
}
else if (rc == SQLITE_DONE) {
sqlite3_finalize(queryStmt);
return false;
}
else {
std::cerr << "Database query failure rc:" << rc << std::endl;
sqlite3_finalize(queryStmt);
return false;
}
}
sqlite3_finalize(queryStmt);
}
return true;
}
//This is the exact parent name query in database.
bool
SqliteHandle::hasParentName(const Name& parentName) const
{
sqlite3_stmt* queryStmt = 0;
string sql = string("SELECT * FROM NDN_REPO WHERE parentName = ?;");
int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &queryStmt, 0);
if (rc == SQLITE_OK) {
if (sqlite3_bind_blob(queryStmt, 1,
parentName.wireEncode().wire(),
parentName.wireEncode().size(), 0) == SQLITE_OK) {
rc = sqlite3_step(queryStmt);
if (rc == SQLITE_ROW) {
sqlite3_finalize(queryStmt);
return true;
}
else if (rc == SQLITE_DONE) {
sqlite3_finalize(queryStmt);
return false;
}
else {
std::cerr << "Database query failure rc:" << rc << std::endl;
sqlite3_finalize(queryStmt);
return false;
}
}
sqlite3_finalize(queryStmt);
}
return true;
}
size_t
SqliteHandle::size()
{
sqlite3_stmt* queryStmt = 0;
string sql("SELECT count(*) FROM NDN_REPO WHERE data IS NOT NULL");
int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &queryStmt, 0);
if (rc != SQLITE_OK)
{
std::cerr << "Database query failure rc:" << rc << std::endl;
sqlite3_finalize(queryStmt);
throw Error("Database query failure");
}
rc = sqlite3_step(queryStmt);
if (rc != SQLITE_ROW)
{
std::cerr << "Database query failure rc:" << rc << std::endl;
sqlite3_finalize(queryStmt);
throw Error("Database query failure");
}
size_t nDatas = static_cast<size_t>(sqlite3_column_int64(queryStmt, 0));
return nDatas;
}
} //namespace repo