blob: c2c2282c7a18f1ecb5df96f4052b517357482abc [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, Regents of the University of California.
Alexander Afanasyev68f2a952013-01-08 14:34:16 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Alexander Afanasyev68f2a952013-01-08 14:34:16 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * ChronoShare is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * You should have received copies of the GNU General Public License along with
16 * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ChronoShare authors and contributors.
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080019 */
20
Zhenkai Zhu7717d202012-12-29 18:01:36 -080021#ifndef OBJECT_DB_H
22#define OBJECT_DB_H
23
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080024#include <string>
25#include <sqlite3.h>
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070026#include <ndnx-common.h>
27#include <ndnx-name.h>
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080028#include <boost/filesystem.hpp>
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080029#include <boost/shared_ptr.hpp>
Zhenkai Zhu92bb6952013-02-06 16:43:30 -080030#include <ctime>
31#include <vector>
Zhenkai Zhu7717d202012-12-29 18:01:36 -080032
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080033class ObjectDb
Zhenkai Zhu7717d202012-12-29 18:01:36 -080034{
35public:
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080036 // database will be create in <folder>/<first-pair-of-hash-bytes>/<rest-of-hash>
37 ObjectDb (const boost::filesystem::path &folder, const std::string &hash);
38 ~ObjectDb ();
Zhenkai Zhu7717d202012-12-29 18:01:36 -080039
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080040 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070041 saveContentObject (const Ndnx::Name &deviceName, sqlite3_int64 segment, const Ndnx::Bytes &data);
Zhenkai Zhu7717d202012-12-29 18:01:36 -080042
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070043 Ndnx::BytesPtr
44 fetchSegment (const Ndnx::Name &deviceName, sqlite3_int64 segment);
Zhenkai Zhu7717d202012-12-29 18:01:36 -080045
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080046 // sqlite3_int64
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070047 // getNumberOfSegments (const Ndnx::Name &deviceName);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080048
Zhenkai Zhu92bb6952013-02-06 16:43:30 -080049 time_t
50 secondsSinceLastUse();
51
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080052 static bool
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070053 DoesExist (const boost::filesystem::path &folder, const Ndnx::Name &deviceName, const std::string &hash);
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -080054
55private:
56 void
57 willStartSave ();
58
59 void
60 didStopSave ();
61
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080062private:
63 sqlite3 *m_db;
Zhenkai Zhu92bb6952013-02-06 16:43:30 -080064 time_t m_lastUsed;
Zhenkai Zhu7717d202012-12-29 18:01:36 -080065};
66
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080067typedef boost::shared_ptr<ObjectDb> ObjectDbPtr;
68
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080069#endif // OBJECT_DB_H