2012-07-30 07:26:08 +02:00
|
|
|
/*
|
2018-10-31 17:54:59 +01:00
|
|
|
* Copyright 2003-2018 The Music Player Daemon Project
|
2012-07-30 07:26:08 +02:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MPD_SIMPLE_DATABASE_PLUGIN_HXX
|
|
|
|
#define MPD_SIMPLE_DATABASE_PLUGIN_HXX
|
|
|
|
|
2014-02-19 22:54:52 +01:00
|
|
|
#include "db/Interface.hxx"
|
2013-10-17 21:59:35 +02:00
|
|
|
#include "fs/AllocatedPath.hxx"
|
2018-08-02 13:45:43 +02:00
|
|
|
#include "song/LightSong.hxx"
|
2018-07-06 16:46:01 +02:00
|
|
|
#include "util/Manual.hxx"
|
2018-08-20 16:19:17 +02:00
|
|
|
#include "util/Compiler.h"
|
2018-11-19 12:49:45 +01:00
|
|
|
#include "config.h"
|
2012-07-30 07:26:08 +02:00
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
2015-01-21 22:13:44 +01:00
|
|
|
struct ConfigBlock;
|
2013-01-02 23:06:20 +01:00
|
|
|
struct Directory;
|
2014-02-19 22:54:52 +01:00
|
|
|
struct DatabasePlugin;
|
|
|
|
class EventLoop;
|
|
|
|
class DatabaseListener;
|
2014-02-26 08:39:44 +01:00
|
|
|
class PrefixedLightSong;
|
2012-07-30 07:26:08 +02:00
|
|
|
|
|
|
|
class SimpleDatabase : public Database {
|
2013-10-17 21:59:35 +02:00
|
|
|
AllocatedPath path;
|
2013-02-02 15:27:06 +01:00
|
|
|
std::string path_utf8;
|
2012-07-30 07:26:08 +02:00
|
|
|
|
2014-11-21 22:19:57 +01:00
|
|
|
#ifdef ENABLE_ZLIB
|
2014-08-11 21:30:49 +02:00
|
|
|
bool compress;
|
|
|
|
#endif
|
|
|
|
|
2014-02-26 08:39:44 +01:00
|
|
|
/**
|
|
|
|
* The path where cache files for Mount() are located.
|
|
|
|
*/
|
|
|
|
AllocatedPath cache_path;
|
|
|
|
|
2013-01-02 23:06:20 +01:00
|
|
|
Directory *root;
|
2012-07-30 07:26:08 +02:00
|
|
|
|
2017-02-11 22:18:28 +01:00
|
|
|
std::chrono::system_clock::time_point mtime;
|
2012-07-30 07:26:08 +02:00
|
|
|
|
2014-02-26 08:39:44 +01:00
|
|
|
/**
|
|
|
|
* A buffer for GetSong() when prefixing the #LightSong
|
|
|
|
* instance from a mounted #Database.
|
|
|
|
*/
|
|
|
|
mutable PrefixedLightSong *prefixed_light_song;
|
|
|
|
|
2014-01-19 10:51:34 +01:00
|
|
|
/**
|
|
|
|
* A buffer for GetSong().
|
|
|
|
*/
|
2018-07-06 16:46:01 +02:00
|
|
|
mutable Manual<LightSong> light_song;
|
2014-01-19 10:51:34 +01:00
|
|
|
|
2012-08-15 23:28:19 +02:00
|
|
|
#ifndef NDEBUG
|
2014-01-19 11:23:02 +01:00
|
|
|
mutable unsigned borrowed_song_count;
|
2012-08-15 23:28:19 +02:00
|
|
|
#endif
|
|
|
|
|
2016-10-28 23:05:13 +02:00
|
|
|
SimpleDatabase(const ConfigBlock &block);
|
2013-01-17 00:56:57 +01:00
|
|
|
|
2018-09-02 10:16:43 +02:00
|
|
|
SimpleDatabase(AllocatedPath &&_path, bool _compress) noexcept;
|
2014-02-26 08:39:44 +01:00
|
|
|
|
2012-07-30 07:26:08 +02:00
|
|
|
public:
|
2017-08-24 19:49:54 +02:00
|
|
|
static Database *Create(EventLoop &main_event_loop,
|
|
|
|
EventLoop &io_event_loop,
|
|
|
|
DatabaseListener &listener,
|
2016-10-28 23:03:07 +02:00
|
|
|
const ConfigBlock &block);
|
2014-02-25 18:17:39 +01:00
|
|
|
|
2012-08-02 18:15:49 +02:00
|
|
|
gcc_pure
|
2017-06-03 21:33:44 +02:00
|
|
|
Directory &GetRoot() noexcept {
|
2012-07-30 07:26:08 +02:00
|
|
|
assert(root != NULL);
|
|
|
|
|
2014-02-27 19:29:10 +01:00
|
|
|
return *root;
|
2012-07-30 07:26:08 +02:00
|
|
|
}
|
|
|
|
|
2015-12-16 10:24:43 +01:00
|
|
|
void Save();
|
2012-07-30 07:26:08 +02:00
|
|
|
|
2014-02-01 00:26:34 +01:00
|
|
|
/**
|
|
|
|
* Returns true if there is a valid database file on the disk.
|
|
|
|
*/
|
2014-02-04 08:37:05 +01:00
|
|
|
bool FileExists() const {
|
2017-02-11 22:18:28 +01:00
|
|
|
return mtime >= std::chrono::system_clock::time_point(std::chrono::system_clock::duration::zero());
|
2014-02-04 08:37:05 +01:00
|
|
|
}
|
|
|
|
|
2014-02-26 08:39:44 +01:00
|
|
|
/**
|
|
|
|
* @param db the #Database to be mounted; must be "open"; on
|
|
|
|
* success, this object gains ownership of the given #Database
|
|
|
|
*/
|
|
|
|
gcc_nonnull_all
|
2016-02-28 11:19:28 +01:00
|
|
|
void Mount(const char *uri, Database *db);
|
2014-02-26 08:39:44 +01:00
|
|
|
|
2016-10-29 10:56:31 +02:00
|
|
|
/**
|
|
|
|
* Throws #std::runtime_error on error.
|
|
|
|
*/
|
2014-02-26 08:39:44 +01:00
|
|
|
gcc_nonnull_all
|
2016-10-29 10:56:31 +02:00
|
|
|
void Mount(const char *local_uri, const char *storage_uri);
|
2014-02-26 08:39:44 +01:00
|
|
|
|
|
|
|
gcc_nonnull_all
|
2018-09-02 10:16:43 +02:00
|
|
|
bool Unmount(const char *uri) noexcept;
|
2014-02-26 08:39:44 +01:00
|
|
|
|
2014-02-25 18:17:54 +01:00
|
|
|
/* virtual methods from class Database */
|
2017-01-23 18:57:23 +01:00
|
|
|
void Open() override;
|
2018-09-02 10:16:43 +02:00
|
|
|
void Close() noexcept override;
|
2012-08-15 22:20:28 +02:00
|
|
|
|
2016-03-19 00:13:57 +01:00
|
|
|
const LightSong *GetSong(const char *uri_utf8) const override;
|
2018-09-02 10:16:43 +02:00
|
|
|
void ReturnSong(const LightSong *song) const noexcept override;
|
2012-08-15 23:28:19 +02:00
|
|
|
|
2016-10-29 10:21:57 +02:00
|
|
|
void Visit(const DatabaseSelection &selection,
|
|
|
|
VisitDirectory visit_directory,
|
|
|
|
VisitSong visit_song,
|
|
|
|
VisitPlaylist visit_playlist) const override;
|
|
|
|
|
2018-10-22 11:35:22 +02:00
|
|
|
std::map<std::string, std::set<std::string>> CollectUniqueTags(const DatabaseSelection &selection,
|
|
|
|
TagType tag_type,
|
|
|
|
TagType group) const override;
|
2016-10-29 10:21:57 +02:00
|
|
|
|
|
|
|
DatabaseStats GetStats(const DatabaseSelection &selection) const override;
|
2012-08-15 22:20:28 +02:00
|
|
|
|
2017-06-04 12:57:05 +02:00
|
|
|
std::chrono::system_clock::time_point GetUpdateStamp() const noexcept override {
|
2013-11-22 00:35:29 +01:00
|
|
|
return mtime;
|
|
|
|
}
|
|
|
|
|
2014-02-25 19:48:01 +01:00
|
|
|
private:
|
2016-10-28 23:08:42 +02:00
|
|
|
void Configure(const ConfigBlock &block);
|
2012-07-30 07:26:08 +02:00
|
|
|
|
2016-03-18 22:21:09 +01:00
|
|
|
void Check() const;
|
2012-07-30 07:26:08 +02:00
|
|
|
|
2016-10-29 09:45:34 +02:00
|
|
|
/**
|
|
|
|
* Throws #std::runtime_error on error.
|
|
|
|
*/
|
|
|
|
void Load();
|
2014-02-26 08:39:44 +01:00
|
|
|
|
2018-09-02 10:16:43 +02:00
|
|
|
Database *LockUmountSteal(const char *uri) noexcept;
|
2012-07-30 07:26:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
extern const DatabasePlugin simple_db_plugin;
|
|
|
|
|
|
|
|
#endif
|