2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
2019-02-12 14:47:13 +01:00
|
|
|
#include "td/telegram/files/FileData.h"
|
2019-01-06 21:39:10 +01:00
|
|
|
#include "td/telegram/files/FileDbId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-01-19 22:12:55 +01:00
|
|
|
#include "td/utils/buffer.h"
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
2022-06-27 12:30:18 +02:00
|
|
|
#include "td/utils/Promise.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
2019-01-19 22:12:55 +01:00
|
|
|
#include "td/utils/tl_storers.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace td {
|
2019-01-06 21:39:10 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class SqliteDb;
|
|
|
|
class SqliteConnectionSafe;
|
|
|
|
class SqliteKeyValue;
|
|
|
|
|
|
|
|
Status drop_file_db(SqliteDb &db, int32 version) TD_WARN_UNUSED_RESULT;
|
|
|
|
Status init_file_db(SqliteDb &db, int32 version) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
class FileDbInterface;
|
|
|
|
std::shared_ptr<FileDbInterface> create_file_db(std::shared_ptr<SqliteConnectionSafe> connection,
|
|
|
|
int32 scheduler_id = -1) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
class FileDbInterface {
|
|
|
|
public:
|
|
|
|
FileDbInterface() = default;
|
|
|
|
FileDbInterface(const FileDbInterface &) = delete;
|
|
|
|
FileDbInterface &operator=(const FileDbInterface &) = delete;
|
|
|
|
virtual ~FileDbInterface() = default;
|
|
|
|
|
2022-10-12 14:59:58 +02:00
|
|
|
// non-thread-safe
|
2019-01-06 21:39:10 +01:00
|
|
|
virtual FileDbId create_pmc_id() = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-10-12 14:59:58 +02:00
|
|
|
// thread-safe
|
2018-12-31 20:04:05 +01:00
|
|
|
virtual void close(Promise<> promise) = 0;
|
2019-01-19 22:12:55 +01:00
|
|
|
|
|
|
|
template <class LocationT>
|
|
|
|
static string as_key(const LocationT &object) {
|
|
|
|
TlStorerCalcLength calc_length;
|
|
|
|
calc_length.store_int(0);
|
|
|
|
object.as_key().store(calc_length);
|
|
|
|
|
|
|
|
BufferSlice key_buffer{calc_length.get_length()};
|
|
|
|
auto key = key_buffer.as_slice();
|
|
|
|
TlStorerUnsafe storer(key.ubegin());
|
|
|
|
storer.store_int(LocationT::KEY_MAGIC);
|
|
|
|
object.as_key().store(storer);
|
|
|
|
CHECK(storer.get_buf() == key.uend());
|
|
|
|
return key.str();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class LocationT>
|
|
|
|
void get_file_data(const LocationT &location, Promise<FileData> promise) {
|
2019-01-19 22:12:55 +01:00
|
|
|
get_file_data_impl(as_key(location), std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class LocationT>
|
|
|
|
Result<FileData> get_file_data_sync(const LocationT &location) {
|
|
|
|
auto res = get_file_data_sync_impl(as_key(location));
|
|
|
|
if (res.is_ok()) {
|
2022-10-06 12:48:52 +02:00
|
|
|
LOG(DEBUG) << "GET " << location << ": " << res.ok();
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2022-10-06 12:48:52 +02:00
|
|
|
LOG(DEBUG) << "GET " << location << ": " << res.error();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-01-06 21:39:10 +01:00
|
|
|
virtual void clear_file_data(FileDbId id, const FileData &file_data) = 0;
|
|
|
|
virtual void set_file_data(FileDbId id, const FileData &file_data, bool new_remote, bool new_local,
|
|
|
|
bool new_generate) = 0;
|
|
|
|
virtual void set_file_data_ref(FileDbId id, FileDbId new_id) = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// For FileStatsWorker. TODO: remove it
|
|
|
|
virtual SqliteKeyValue &pmc() = 0;
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual void get_file_data_impl(string key, Promise<FileData> promise) = 0;
|
|
|
|
virtual Result<FileData> get_file_data_sync_impl(string key) = 0;
|
|
|
|
};
|
2019-01-06 21:39:10 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|