Ccnx::BytesPtr ObjectDb::fetchSegment(const Ccnx::Name& deviceName, sqlite3_int64 segment) { sqlite3_stmt* stmt; sqlite3_prepare_v2(m_db, "SELECT content_object FROM File WHERE device_name=? AND segment=?", -1, &stmt, 0); CcnxCharbufPtr buf = deviceName.toCcnxCharbuf(); sqlite3_bind_blob(stmt, 1, buf->buf(), buf->length(), SQLITE_TRANSIENT); sqlite3_bind_int64(stmt, 2, segment); BytesPtr ret; int res = sqlite3_step(stmt); if (res == SQLITE_ROW) { const unsigned char* buf = reinterpret_cast<const unsigned char*>(sqlite3_column_blob(stmt, 0)); int bufBytes = sqlite3_column_bytes(stmt, 0); ret = make_shared<Bytes>(buf, buf + bufBytes); } sqlite3_finalize(stmt); // update last used time m_lastUsed = time(NULL); return ret; }
bool ObjectDb::DoesExist(const boost::filesystem::path& folder, const Ccnx::Name& deviceName, const std::string& hash) { fs::path actualFolder = folder / "objects" / hash.substr(0, 2); bool retval = false; sqlite3* db; int res = sqlite3_open((actualFolder / hash.substr(2, hash.size() - 2)).c_str(), &db); if (res == SQLITE_OK) { sqlite3_stmt* stmt; sqlite3_prepare_v2(db, "SELECT count(*), count(nullif(content_object,0)) FROM File WHERE device_name=?", -1, &stmt, 0); CcnxCharbufPtr buf = deviceName.toCcnxCharbuf(); sqlite3_bind_blob(stmt, 1, buf->buf(), buf->length(), SQLITE_TRANSIENT); int res = sqlite3_step(stmt); if (res == SQLITE_ROW) { int countAll = sqlite3_column_int(stmt, 0); int countNonNull = sqlite3_column_int(stmt, 1); _LOG_TRACE("Total segments: " << countAll << ", non-empty segments: " << countNonNull); if (countAll > 0 && countAll == countNonNull) { retval = true; } } sqlite3_finalize(stmt); } sqlite3_close(db); return retval; }
void ObjectDb::saveContentObject(const Ccnx::Name& deviceName, sqlite3_int64 segment, const Ccnx::Bytes& data) { sqlite3_stmt* stmt; sqlite3_prepare_v2(m_db, "INSERT INTO File " "(device_name, segment, content_object) " "VALUES (?, ?, ?)", -1, &stmt, 0); //_LOG_DEBUG ("Saving content object for [" << deviceName << ", seqno: " << segment << ", size: " << data.size () << "]"); CcnxCharbufPtr buf = deviceName.toCcnxCharbuf(); sqlite3_bind_blob(stmt, 1, buf->buf(), buf->length(), SQLITE_STATIC); sqlite3_bind_int64(stmt, 2, segment); sqlite3_bind_blob(stmt, 3, &data[0], data.size(), SQLITE_STATIC); sqlite3_step(stmt); //_LOG_DEBUG ("After saving object: " << sqlite3_errmsg (m_db)); sqlite3_finalize(stmt); // update last used time m_lastUsed = time(NULL); }