blob: 3e6f9cd684586864e73403e2c32b5b1cd36ef5f2 [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 <boost/filesystem.hpp>
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080025#include <boost/shared_ptr.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080026#include <ccnx-common.h>
27#include <ccnx-name.h>
Zhenkai Zhu92bb6952013-02-06 16:43:30 -080028#include <ctime>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080029#include <sqlite3.h>
30#include <string>
Zhenkai Zhu92bb6952013-02-06 16:43:30 -080031#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>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080037 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 Afanasyeveda3b7a2016-12-25 11:26:40 -080041 saveContentObject(const Ccnx::Name& deviceName, sqlite3_int64 segment, const Ccnx::Bytes& data);
Zhenkai Zhu7717d202012-12-29 18:01:36 -080042
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080043 Ccnx::BytesPtr
44 fetchSegment(const Ccnx::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 Afanasyeveda3b7a2016-12-25 11:26:40 -080053 DoesExist(const boost::filesystem::path& folder, const Ccnx::Name& deviceName,
54 const std::string& hash);
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -080055
56private:
57 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080058 willStartSave();
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -080059
60 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080061 didStopSave();
Alexander Afanasyevb2e608d2013-01-23 20:00:53 -080062
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080063private:
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080064 sqlite3* m_db;
Zhenkai Zhu92bb6952013-02-06 16:43:30 -080065 time_t m_lastUsed;
Zhenkai Zhu7717d202012-12-29 18:01:36 -080066};
67
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080068typedef boost::shared_ptr<ObjectDb> ObjectDbPtr;
69
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080070#endif // OBJECT_DB_H