FFmpegfs Fuse Multi Media Filesystem 2.16
cache.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2017-2024 Norbert Schlia (nschlia@oblivion-software.de)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
17 *
18 * On Debian systems, the complete text of the GNU General Public License
19 * Version 3 can be found in `/usr/share/common-licenses/GPL-3'.
20 */
21
32#ifndef CACHE_H
33#define CACHE_H
34
35#pragma once
36
37#include "buffer.h"
38
39#include <map>
40#include <memory>
41
42#define DB_BASE_VERSION_MAJOR 1
43#define DB_BASE_VERSION_MINOR 0
45#define DB_VERSION_MAJOR 1
46#define DB_VERSION_MINOR 97
48#define DB_MIN_VERSION_MAJOR 1
49#define DB_MIN_VERSION_MINOR 97
51typedef struct sqlite3 sqlite3;
52typedef struct sqlite3_stmt sqlite3_stmt;
57enum class RESULTCODE
58{
59 NONE,
63};
64typedef RESULTCODE const *LPCRESULTCODE;
70typedef struct CACHE_INFO
71{
72 std::string m_origfile;
73 std::string m_destfile;
74 std::array<char, 11> m_desttype;
81 int64_t m_duration;
85 uint32_t m_segment_count;
87 bool m_error;
88 int m_errno;
92 time_t m_file_time;
93 size_t m_file_size;
94 unsigned int m_access_count;
96typedef CACHE_INFO const *LPCCACHE_INFO;
99class Cache_Entry;
100
104class Cache
105{
106 typedef std::pair<std::string, std::string> cache_key_t;
107 typedef std::map<cache_key_t, Cache_Entry *> cache_t;
108public:
112 typedef struct
113 {
114 const char * name;
115 const char * primary_key;
116 } TABLE_DEF;
117 typedef TABLE_DEF const *LPCTABLE_DEF;
123 typedef struct
124 {
125 const char * name;
126 const char * type;
131 typedef std::vector<TABLE_COLUMNS> TABLECOLUMNS_VEC;
135 friend class Cache_Entry;
136
142 {
143 public:
150 explicit sqlite_t(const std::string & filename, int flags, const char *zVfs = nullptr);
154 virtual ~sqlite_t();
155
160 int ret() const { return m_ret; };
161
162#ifdef HAVE_SQLITE_CACHEFLUSH
167 bool flush_index();
168#endif // HAVE_SQLITE_CACHEFLUSH
169
175 operator sqlite3*() { return m_db_handle; };
176
181 const std::string & filename() const { return m_filename; };
182
183 protected:
184 int m_ret;
185 std::string m_filename;
187 public:
191 };
192
193public:
197 explicit Cache();
201 virtual ~Cache();
210 Cache_Entry * openio(LPVIRTUALFILE virtualfile);
220 bool closeio(Cache_Entry **cache_entry, int flags = CACHE_CLOSE_NOOPT);
225 bool load_index();
235 bool maintenance(size_t predicted_filesize = 0);
240 bool clear();
245 bool prune_expired();
250 bool prune_cache_size();
255 bool prune_disk_space(size_t predicted_filesize);
262 bool remove_cachefile(const std::string & filename, const std::string &fileext);
263
264protected:
270 bool read_info(LPCACHE_INFO cache_info);
276 bool write_info(LPCCACHE_INFO cache_info);
283 bool delete_info(const std::string & filename, const std::string & desttype);
290 Cache_Entry* create_entry(LPVIRTUALFILE virtualfile, const std::string & desttype);
297 bool delete_entry(Cache_Entry **cache_entry, int flags);
301 void close_index();
307 std::string expanded_sql(sqlite3_stmt *pStmt);
312 bool prepare_stmts();
318 bool table_exists(const char *table);
325 bool column_exists(const char *table, const char *column);
332 bool check_min_version(int *db_version_major, int *db_version_minor);
341 int cmp_version(int version_major_l, int version_minor_l, int version_major_r, int version_minor_r);
346 bool begin_transaction();
351 bool end_transaction();
361 bool create_table_cache_entry(LPCTABLE_DEF table, const TABLECOLUMNS_VEC & columns);
362 /*
363 */
370 bool upgrade_db(int *db_version_major, int *db_version_minor);
371
372private:
378 std::recursive_mutex m_mutex;
380 std::unique_ptr<sqlite_t> m_cacheidx_db;
383};
384
385#endif
Buffer class.
#define CACHE_CLOSE_NOOPT
Dummy, do nothing special.
Definition: buffer.h:45
struct sqlite3 sqlite3
Forward declaration of sqlite3 handle.
Definition: cache.h:51
RESULTCODE * LPRESULTCODE
Pointer to const version of RESULTCODE.
Definition: cache.h:65
RESULTCODE
RESULTCODE of transcoding operation.
Definition: cache.h:58
@ FINISHED_ERROR
Transcode finished with error.
@ FINISHED_INCOMPLETE
Transcode finished, but incomplete.
@ NONE
No result code available.
@ FINISHED_SUCCESS
Transcode finished successfully.
RESULTCODE const * LPCRESULTCODE
Pointer version of RESULTCODE.
Definition: cache.h:64
CACHE_INFO * LPCACHE_INFO
Pointer to const version of CACHE_INFO.
Definition: cache.h:97
CACHE_INFO const * LPCCACHE_INFO
Pointer version of CACHE_INFO.
Definition: cache.h:96
struct CACHE_INFO CACHE_INFO
Cache information block.
struct sqlite3_stmt sqlite3_stmt
Forward declaration of sqlite3 statement handle.
Definition: cache.h:52
The sqlite_t class Wrapper for sqlite3 struct to make use of std::shared_ptr.
Definition: cache.h:142
sqlite3_stmt * m_insert_stmt
Prepared insert statement.
Definition: cache.h:189
sqlite3 * m_db_handle
SQLite handle of cache index database.
Definition: cache.h:186
std::string m_filename
Name of SQLite cache index database.
Definition: cache.h:185
virtual ~sqlite_t()
Free sqlite_t object.
Definition: cache.cc:58
sqlite_t(const std::string &filename, int flags, const char *zVfs=nullptr)
Construct sqlite_t object.
Definition: cache.cc:48
const std::string & filename() const
Get current database file name.
Definition: cache.h:181
int ret() const
Return code of last Sqlite operation.
Definition: cache.h:160
sqlite3_stmt * m_delete_stmt
Prepared delete statement.
Definition: cache.h:190
int m_ret
Return code of last SQL operation.
Definition: cache.h:184
sqlite3_stmt * m_select_stmt
Prepared select statement.
Definition: cache.h:188
The Cache_Entry class.
Definition: cache_entry.h:49
The Cache class.
Definition: cache.h:105
Cache_Entry * openio(LPVIRTUALFILE virtualfile)
Open cache entry.
Definition: cache.cc:1075
static const TABLECOLUMNS_VEC m_columns_cache_entry
Columns of table "cache_entry".
Definition: cache.h:374
bool prune_disk_space(size_t predicted_filesize)
Prune cache entries to ensure disk space.
Definition: cache.cc:1257
bool delete_info(const std::string &filename, const std::string &desttype)
Delete cache file info.
Definition: cache.cc:978
bool delete_entry(Cache_Entry **cache_entry, int flags)
Delete cache entry object.
Definition: cache.cc:1051
TABLECOLUMNS_VEC * LPTABLECOLUMNS_VEC
Pointer to const version of TABLECOLUMNS_VEC.
Definition: cache.h:133
int cmp_version(int version_major_l, int version_minor_l, int version_major_r, int version_minor_r)
Compare two versions.
Definition: cache.cc:276
bool create_table_cache_entry(LPCTABLE_DEF table, const TABLECOLUMNS_VEC &columns)
Create cache_entry table.
Definition: cache.cc:351
bool clear()
Clear cache: deletes all entries.
Definition: cache.cc:1365
cache_t m_cache
Cache file (memory mapped file)
Definition: cache.h:382
bool prepare_stmts()
Prepare all SQL statements.
Definition: cache.cc:155
bool remove_cachefile(const std::string &filename, const std::string &fileext)
Remove a cache file from disk.
Definition: cache.cc:1418
bool maintenance(size_t predicted_filesize=0)
Run disk maintenance.
Definition: cache.cc:1349
Cache()
Construct Cache object.
Definition: cache.cc:138
TABLE_COLUMNS * LPTABLE_COLUMNS
Pointer to const version of TABLE_COLUMNS.
Definition: cache.h:129
std::map< cache_key_t, Cache_Entry * > cache_t
Map of cache entries.
Definition: cache.h:107
std::vector< TABLE_COLUMNS > TABLECOLUMNS_VEC
Table columns array.
Definition: cache.h:131
std::string expanded_sql(sqlite3_stmt *pStmt)
Get expanded SQL string for a statement.
Definition: cache.cc:1437
TABLE_DEF const * LPCTABLE_DEF
Pointer version of TABLE_DEF.
Definition: cache.h:117
bool load_index()
Load cache index from disk.
Definition: cache.cc:611
bool write_info(LPCCACHE_INFO cache_info)
Write cache file info.
Definition: cache.cc:912
std::unique_ptr< sqlite_t > m_cacheidx_db
SQLite handle of cache index database.
Definition: cache.h:380
std::pair< std::string, std::string > cache_key_t
Filenames and destination types.
Definition: cache.h:106
static const TABLECOLUMNS_VEC m_columns_version
Columns of table "version".
Definition: cache.h:376
static const TABLE_DEF m_table_version
Definition and indexes of table "version".
Definition: cache.h:375
bool rollback_transaction()
Rollback a database transaction.
Definition: cache.cc:335
void close_index()
Close cache index.
Definition: cache.cc:1031
bool begin_transaction()
Begin a database transactio.n.
Definition: cache.cc:303
bool read_info(LPCACHE_INFO cache_info)
Read cache file info.
Definition: cache.cc:785
bool table_exists(const char *table)
Check if SQL table exists in database.
Definition: cache.cc:189
bool end_transaction()
End a database transaction.
Definition: cache.cc:319
bool upgrade_db(int *db_version_major, int *db_version_minor)
Upgrade database from version below 1.95.
Definition: cache.cc:391
bool prune_cache_size()
Prune cache entries to keep cache size within limit.
Definition: cache.cc:1179
std::recursive_mutex m_mutex
Access mutex.
Definition: cache.h:378
bool closeio(Cache_Entry **cache_entry, int flags=CACHE_CLOSE_NOOPT)
Close a cache entry.
Definition: cache.cc:1093
TABLE_DEF * LPTABLE_DEF
Pointer to const version of TABLE_DEF.
Definition: cache.h:118
TABLECOLUMNS_VEC const * LPCTABLECOLUMNS_VEC
Pointer version of TABLECOLUMNS_VEC.
Definition: cache.h:132
TABLE_COLUMNS const * LPCTABLE_COLUMNS
Pointer version of TABLE_COLUMNS.
Definition: cache.h:128
Cache_Entry * create_entry(LPVIRTUALFILE virtualfile, const std::string &desttype)
Create cache entry object for a VIRTUALFILE.
Definition: cache.cc:1036
bool column_exists(const char *table, const char *column)
Check if column exists in SQL table.
Definition: cache.cc:218
bool prune_expired()
Prune expired cache entries.
Definition: cache.cc:1117
bool check_min_version(int *db_version_major, int *db_version_minor)
Check the db version if upgrade needed.
Definition: cache.cc:249
virtual ~Cache()
Destruct Cache object.
Definition: cache.cc:142
static const TABLE_DEF m_table_cache_entry
Definition and indexes of table "cache_entry".
Definition: cache.h:373
Cache information block.
Definition: cache.h:71
uint32_t m_video_frame_count
Number of frames in video or 0 if not a video.
Definition: cache.h:84
int64_t m_audiobitrate
Audio bitrate in bit/s.
Definition: cache.h:75
size_t m_file_size
Source file file size.
Definition: cache.h:93
time_t m_access_time
Source file last access time.
Definition: cache.h:91
RESULTCODE m_result
Result code:
Definition: cache.h:86
bool m_deinterlace
true if video was deinterlaced
Definition: cache.h:80
int64_t m_duration
File duration, in AV_TIME_BASE fractional seconds.
Definition: cache.h:81
uint32_t m_segment_count
Number of segments for HLS.
Definition: cache.h:85
size_t m_encoded_filesize
Actual file size after encode.
Definition: cache.h:83
int m_audiosamplerate
Audio sample rate in Hz.
Definition: cache.h:76
bool m_error
true if encode failed
Definition: cache.h:87
size_t m_predicted_filesize
Predicted file size.
Definition: cache.h:82
std::string m_destfile
Destination filename after transcode.
Definition: cache.h:73
int m_averror
FFmpeg error code if encode failed.
Definition: cache.h:89
std::string m_origfile
Original filename before transcode.
Definition: cache.h:72
time_t m_file_time
Source file file time.
Definition: cache.h:92
std::array< char, 11 > m_desttype
Destination type.
Definition: cache.h:74
int m_videowidth
Video width.
Definition: cache.h:78
int m_videoheight
Video height.
Definition: cache.h:79
int64_t m_videobitrate
Video bitrate in bit/s.
Definition: cache.h:77
unsigned int m_access_count
Read access counter.
Definition: cache.h:94
int m_errno
errno if encode failed
Definition: cache.h:88
time_t m_creation_time
Source file creation time.
Definition: cache.h:90
Column definition of sql table.
Definition: cache.h:124
const char * name
Column name.
Definition: cache.h:125
const char * type
Column type (INT, CHAR etc)
Definition: cache.h:126
Definition of sql table.
Definition: cache.h:113
const char * name
Table name.
Definition: cache.h:114
const char * primary_key
Primary key of table.
Definition: cache.h:115
Virtual file definition.
Definition: fileio.h:123