FFmpegfs Fuse Multi Media Filesystem 2.16
Classes | Public Types | Public Member Functions | Protected Member Functions | Private Types | Private Attributes | Static Private Attributes | Friends | List of all members
Cache Class Reference

The Cache class. More...

#include <cache.h>

Collaboration diagram for Cache:
Collaboration graph
[legend]

Classes

class  sqlite_t
 The sqlite_t class Wrapper for sqlite3 struct to make use of std::shared_ptr. More...
 
struct  TABLE_COLUMNS
 Column definition of sql table. More...
 
struct  TABLE_DEF
 Definition of sql table. More...
 

Public Types

typedef TABLE_DEF const * LPCTABLE_DEF
 Pointer version of TABLE_DEF. More...
 
typedef TABLE_DEFLPTABLE_DEF
 Pointer to const version of TABLE_DEF. More...
 
typedef TABLE_COLUMNS const * LPCTABLE_COLUMNS
 Pointer version of TABLE_COLUMNS. More...
 
typedef TABLE_COLUMNSLPTABLE_COLUMNS
 Pointer to const version of TABLE_COLUMNS. More...
 
typedef std::vector< TABLE_COLUMNSTABLECOLUMNS_VEC
 Table columns array. More...
 
typedef TABLECOLUMNS_VEC const * LPCTABLECOLUMNS_VEC
 Pointer version of TABLECOLUMNS_VEC. More...
 
typedef TABLECOLUMNS_VECLPTABLECOLUMNS_VEC
 Pointer to const version of TABLECOLUMNS_VEC. More...
 

Public Member Functions

 Cache ()
 Construct Cache object. More...
 
virtual ~Cache ()
 Destruct Cache object. More...
 
Cache_Entryopenio (LPVIRTUALFILE virtualfile)
 Open cache entry. More...
 
bool closeio (Cache_Entry **cache_entry, int flags=CACHE_CLOSE_NOOPT)
 Close a cache entry. More...
 
bool load_index ()
 Load cache index from disk. More...
 
bool maintenance (size_t predicted_filesize=0)
 Run disk maintenance. More...
 
bool clear ()
 Clear cache: deletes all entries. More...
 
bool prune_expired ()
 Prune expired cache entries. More...
 
bool prune_cache_size ()
 Prune cache entries to keep cache size within limit. More...
 
bool prune_disk_space (size_t predicted_filesize)
 Prune cache entries to ensure disk space. More...
 
bool remove_cachefile (const std::string &filename, const std::string &fileext)
 Remove a cache file from disk. More...
 

Protected Member Functions

bool read_info (LPCACHE_INFO cache_info)
 Read cache file info. More...
 
bool write_info (LPCCACHE_INFO cache_info)
 Write cache file info. More...
 
bool delete_info (const std::string &filename, const std::string &desttype)
 Delete cache file info. More...
 
Cache_Entrycreate_entry (LPVIRTUALFILE virtualfile, const std::string &desttype)
 Create cache entry object for a VIRTUALFILE. More...
 
bool delete_entry (Cache_Entry **cache_entry, int flags)
 Delete cache entry object. More...
 
void close_index ()
 Close cache index. More...
 
std::string expanded_sql (sqlite3_stmt *pStmt)
 Get expanded SQL string for a statement. More...
 
bool prepare_stmts ()
 Prepare all SQL statements. More...
 
bool table_exists (const char *table)
 Check if SQL table exists in database. More...
 
bool column_exists (const char *table, const char *column)
 Check if column exists in SQL table. More...
 
bool check_min_version (int *db_version_major, int *db_version_minor)
 Check the db version if upgrade needed. More...
 
int cmp_version (int version_major_l, int version_minor_l, int version_major_r, int version_minor_r)
 Compare two versions. More...
 
bool begin_transaction ()
 Begin a database transactio.n. More...
 
bool end_transaction ()
 End a database transaction. More...
 
bool rollback_transaction ()
 Rollback a database transaction. More...
 
bool create_table_cache_entry (LPCTABLE_DEF table, const TABLECOLUMNS_VEC &columns)
 Create cache_entry table. More...
 
bool upgrade_db (int *db_version_major, int *db_version_minor)
 Upgrade database from version below 1.95. More...
 

Private Types

typedef std::pair< std::string, std::string > cache_key_t
 Filenames and destination types. More...
 
typedef std::map< cache_key_t, Cache_Entry * > cache_t
 Map of cache entries. More...
 

Private Attributes

std::recursive_mutex m_mutex
 Access mutex. More...
 
std::unique_ptr< sqlite_tm_cacheidx_db
 SQLite handle of cache index database. More...
 
cache_t m_cache
 Cache file (memory mapped file) More...
 

Static Private Attributes

static const TABLE_DEF m_table_cache_entry
 Definition and indexes of table "cache_entry". More...
 
static const TABLECOLUMNS_VEC m_columns_cache_entry
 Columns of table "cache_entry". More...
 
static const TABLE_DEF m_table_version
 Definition and indexes of table "version". More...
 
static const TABLECOLUMNS_VEC m_columns_version
 Columns of table "version". More...
 

Friends

class Cache_Entry
 

Detailed Description

The Cache class.

Definition at line 104 of file cache.h.

Member Typedef Documentation

◆ cache_key_t

typedef std::pair<std::string, std::string> Cache::cache_key_t
private

Filenames and destination types.

Definition at line 106 of file cache.h.

◆ cache_t

typedef std::map<cache_key_t, Cache_Entry *> Cache::cache_t
private

Map of cache entries.

Definition at line 107 of file cache.h.

◆ LPCTABLE_COLUMNS

Pointer version of TABLE_COLUMNS.

Definition at line 128 of file cache.h.

◆ LPCTABLE_DEF

Pointer version of TABLE_DEF.

Definition at line 117 of file cache.h.

◆ LPCTABLECOLUMNS_VEC

Pointer version of TABLECOLUMNS_VEC.

Definition at line 132 of file cache.h.

◆ LPTABLE_COLUMNS

Pointer to const version of TABLE_COLUMNS.

Definition at line 129 of file cache.h.

◆ LPTABLE_DEF

Pointer to const version of TABLE_DEF.

Definition at line 118 of file cache.h.

◆ LPTABLECOLUMNS_VEC

Pointer to const version of TABLECOLUMNS_VEC.

Definition at line 133 of file cache.h.

◆ TABLECOLUMNS_VEC

typedef std::vector<TABLE_COLUMNS> Cache::TABLECOLUMNS_VEC

Table columns array.

Definition at line 131 of file cache.h.

Constructor & Destructor Documentation

◆ Cache()

Cache::Cache ( )
explicit

Construct Cache object.

Definition at line 138 of file cache.cc.

◆ ~Cache()

Cache::~Cache ( )
virtual

Destruct Cache object.

Definition at line 142 of file cache.cc.

References close_index(), and m_cache.

Member Function Documentation

◆ begin_transaction()

bool Cache::begin_transaction ( )
protected

Begin a database transactio.n.

Returns
Returns true on success; false on error.

Definition at line 303 of file cache.cc.

References Logging::error(), and m_cacheidx_db.

Referenced by load_index().

◆ check_min_version()

bool Cache::check_min_version ( int *  db_version_major,
int *  db_version_minor 
)
protected

Check the db version if upgrade needed.

Parameters
[out]db_version_major- Upon return, contains the major database version.
[out]db_version_minor- Upon return, contains the minor database version.
Returns
Returns true of version is OK, false if upgrade is needed.

Definition at line 249 of file cache.cc.

References cmp_version(), DB_MIN_VERSION_MAJOR, DB_MIN_VERSION_MINOR, Logging::error(), and m_cacheidx_db.

Referenced by load_index().

◆ clear()

bool Cache::clear ( )

Clear cache: deletes all entries.

Returns
Returns true on success; false on error.

Definition at line 1365 of file cache.cc.

◆ close_index()

void Cache::close_index ( )
protected

Close cache index.

Definition at line 1031 of file cache.cc.

References Logging::error(), and m_cacheidx_db.

Referenced by ~Cache().

◆ closeio()

bool Cache::closeio ( Cache_Entry **  cache_entry,
int  flags = CACHE_CLOSE_NOOPT 
)

Close a cache entry.

If the cache entry is in use will not be deleted.

Parameters
[in,out]cache_entry- Cache entry object to be closed.
[in]flags- One of the CACHE_CLOSE_* flags.
Returns
Returns true if the object was deleted; false if not.

Definition at line 1093 of file cache.cc.

References Logging::trace().

◆ cmp_version()

int Cache::cmp_version ( int  version_major_l,
int  version_minor_l,
int  version_major_r,
int  version_minor_r 
)
protected

Compare two versions.

Parameters
[in]version_major_l- Left major version
[in]version_minor_l- Left minor version
[in]version_major_r- Right major version
[in]version_minor_r- Right minor version
Returns
Returns +1 if left version is larger then right, 0 if versions are the same, -1 if right version is larger than left.

Definition at line 276 of file cache.cc.

Referenced by check_min_version().

◆ column_exists()

bool Cache::column_exists ( const char *  table,
const char *  column 
)
protected

Check if column exists in SQL table.

Parameters
[in]table- name of table
[in]column- name of column
Returns
Returns true if table exists, false if not.

Definition at line 218 of file cache.cc.

References Logging::error(), and m_cacheidx_db.

Referenced by upgrade_db().

◆ create_entry()

Cache_Entry * Cache::create_entry ( LPVIRTUALFILE  virtualfile,
const std::string &  desttype 
)
protected

Create cache entry object for a VIRTUALFILE.

Parameters
[in]virtualfile- VIRTUALFILE struct of a file.
[in]desttype- Destination type (MP4, WEBM etc.).
Returns
On success, returns pointer to a Cache_Entry. On error, returns nullptr.

Definition at line 1036 of file cache.cc.

◆ create_table_cache_entry()

bool Cache::create_table_cache_entry ( LPCTABLE_DEF  table,
const TABLECOLUMNS_VEC columns 
)
protected

Create cache_entry table.

Returns
Returns true on success; false on error.

Definition at line 351 of file cache.cc.

References Logging::error(), and m_cacheidx_db.

Referenced by load_index(), and upgrade_db().

◆ delete_entry()

bool Cache::delete_entry ( Cache_Entry **  cache_entry,
int  flags 
)
protected

Delete cache entry object.

Parameters
[in,out]cache_entry- Cache entry object to be closed.
[in]flags- One of the CACHE_CLOSE_* flags.
Returns
Returns true if the object was deleted; false if not.

Definition at line 1051 of file cache.cc.

References CACHE_CHECK_BIT, CACHE_CLOSE_FREE, and m_cache.

◆ delete_info()

bool Cache::delete_info ( const std::string &  filename,
const std::string &  desttype 
)
protected

Delete cache file info.

Parameters
[in]filename- Source file name.
[in]desttype- Destination type (MP4, WEBM etc.).
Returns
Returns true on success; false on error.

Definition at line 978 of file cache.cc.

Referenced by Cache_Entry::delete_info().

◆ end_transaction()

bool Cache::end_transaction ( )
protected

End a database transaction.

Returns
Returns true on success; false on error.

Definition at line 319 of file cache.cc.

References Logging::error(), and m_cacheidx_db.

Referenced by load_index().

◆ expanded_sql()

std::string Cache::expanded_sql ( sqlite3_stmt pStmt)
protected

Get expanded SQL string for a statement.

Parameters
[in]pStmt- SQLite statement handle.
Returns
Returns the SQL string bound to the statement handle.

Definition at line 1437 of file cache.cc.

◆ load_index()

bool Cache::load_index ( )

◆ maintenance()

bool Cache::maintenance ( size_t  predicted_filesize = 0)

Run disk maintenance.

Can be done before a new file is added. Set predicted_filesize to make sure disk space or cache size will be kept within limits.

Parameters
[in]predicted_filesize- Size of new file
Returns
Returns true on success; false on error.

Definition at line 1349 of file cache.cc.

◆ openio()

Cache_Entry * Cache::openio ( LPVIRTUALFILE  virtualfile)

Open cache entry.

Opens a cache entry and opens the cache file.

Parameters
[in]virtualfile- VIRTUALFILE struct of a file.
Returns
On success, returns pointer to a Cache_Entry. On error, returns nullptr.

Definition at line 1075 of file cache.cc.

References VIRTUALFILE::m_destfile, and Logging::trace().

◆ prepare_stmts()

bool Cache::prepare_stmts ( )
protected

Prepare all SQL statements.

Returns
Returns true on success, false on error.

Definition at line 155 of file cache.cc.

References Logging::error(), and m_cacheidx_db.

Referenced by load_index().

◆ prune_cache_size()

bool Cache::prune_cache_size ( )

Prune cache entries to keep cache size within limit.

Returns
Returns true on success; false on error.

Definition at line 1179 of file cache.cc.

◆ prune_disk_space()

bool Cache::prune_disk_space ( size_t  predicted_filesize)

Prune cache entries to ensure disk space.

Returns
Returns true on success; false on error.

Definition at line 1257 of file cache.cc.

◆ prune_expired()

bool Cache::prune_expired ( )

Prune expired cache entries.

Returns
Returns true on success; false on error.

Definition at line 1117 of file cache.cc.

◆ read_info()

bool Cache::read_info ( LPCACHE_INFO  cache_info)
protected

◆ remove_cachefile()

bool Cache::remove_cachefile ( const std::string &  filename,
const std::string &  fileext 
)

Remove a cache file from disk.

Parameters
[in]filename- Source file name.
[in]fileext- File extension of target file.
Returns
Returns true on success; false on error.

Definition at line 1418 of file cache.cc.

◆ rollback_transaction()

bool Cache::rollback_transaction ( )
protected

Rollback a database transaction.

Returns
Returns true on success; false on error.

Definition at line 335 of file cache.cc.

References Logging::error(), and m_cacheidx_db.

◆ table_exists()

bool Cache::table_exists ( const char *  table)
protected

Check if SQL table exists in database.

Parameters
[in]table- name of table
Returns
Returns true if table exists, false if not.

Definition at line 189 of file cache.cc.

References Logging::error(), and m_cacheidx_db.

Referenced by load_index().

◆ upgrade_db()

bool Cache::upgrade_db ( int *  db_version_major,
int *  db_version_minor 
)
protected

Upgrade database from version below 1.95.

Parameters
[out]db_version_major- Upon return, contains the new major database version.
[out]db_version_minor- Upon return, contains the new minor database version.
Returns
Returns true on success; false on error.

Definition at line 391 of file cache.cc.

References column_exists(), create_table_cache_entry(), DB_VERSION_MAJOR, DB_VERSION_MINOR, Logging::debug(), Logging::error(), Logging::info(), m_cacheidx_db, m_columns_cache_entry, m_table_cache_entry, Cache::TABLE_DEF::name, and TOSTRING.

Referenced by load_index().

◆ write_info()

bool Cache::write_info ( LPCCACHE_INFO  cache_info)
protected

Write cache file info.

Parameters
[in]cache_info- Structure with cache info data.
Returns
Returns true on success; false on error.

Definition at line 912 of file cache.cc.

Referenced by Cache_Entry::update_access(), and Cache_Entry::write_info().

Friends And Related Function Documentation

◆ Cache_Entry

friend class Cache_Entry
friend

Definition at line 135 of file cache.h.

Member Data Documentation

◆ m_cache

cache_t Cache::m_cache
private

Cache file (memory mapped file)

Definition at line 382 of file cache.h.

Referenced by delete_entry(), and ~Cache().

◆ m_cacheidx_db

std::unique_ptr<sqlite_t> Cache::m_cacheidx_db
private

◆ m_columns_cache_entry

const Cache::TABLECOLUMNS_VEC Cache::m_columns_cache_entry
staticprivate

Columns of table "cache_entry".

Definition at line 374 of file cache.h.

Referenced by load_index(), and upgrade_db().

◆ m_columns_version

const Cache::TABLECOLUMNS_VEC Cache::m_columns_version
staticprivate
Initial value:
=
{
{ "db_version_major", "INTEGER NOT NULL" },
{ "db_version_minor", "INTEGER NOT NULL" }
}

Columns of table "version".

Definition at line 376 of file cache.h.

Referenced by load_index().

◆ m_mutex

std::recursive_mutex Cache::m_mutex
private

Access mutex.

Definition at line 378 of file cache.h.

Referenced by read_info().

◆ m_table_cache_entry

const Cache::TABLE_DEF Cache::m_table_cache_entry
staticprivate
Initial value:
=
{
"cache_entry",
"PRIMARY KEY(`filename`,`desttype`)"
}

Definition and indexes of table "cache_entry".

Definition at line 373 of file cache.h.

Referenced by load_index(), and upgrade_db().

◆ m_table_version

const Cache::TABLE_DEF Cache::m_table_version
staticprivate
Initial value:
=
{
"version",
nullptr
}

Definition and indexes of table "version".

Definition at line 375 of file cache.h.

Referenced by load_index().


The documentation for this class was generated from the following files: