SimpleDatabasePlugin.hxx 3.4 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * 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

23
#include "check.h"
24
#include "db/Interface.hxx"
25
#include "fs/AllocatedPath.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "db/LightSong.hxx"
27
#include "Compiler.h"
28 29 30

#include <cassert>

31
struct ConfigBlock;
32
struct Directory;
33 34 35
struct DatabasePlugin;
class EventLoop;
class DatabaseListener;
Max Kellermann's avatar
Max Kellermann committed
36
class PrefixedLightSong;
37 38

class SimpleDatabase : public Database {
39
	AllocatedPath path;
40
	std::string path_utf8;
41

42
#ifdef ENABLE_ZLIB
43 44 45
	bool compress;
#endif

Max Kellermann's avatar
Max Kellermann committed
46 47 48 49 50
	/**
	 * The path where cache files for Mount() are located.
	 */
	AllocatedPath cache_path;

51
	Directory *root;
52 53 54

	time_t mtime;

Max Kellermann's avatar
Max Kellermann committed
55 56 57 58 59 60
	/**
	 * A buffer for GetSong() when prefixing the #LightSong
	 * instance from a mounted #Database.
	 */
	mutable PrefixedLightSong *prefixed_light_song;

61 62 63 64 65
	/**
	 * A buffer for GetSong().
	 */
	mutable LightSong light_song;

66
#ifndef NDEBUG
67
	mutable unsigned borrowed_song_count;
68 69
#endif

70
	SimpleDatabase(const ConfigBlock &block);
71

72
	SimpleDatabase(AllocatedPath &&_path, bool _compress);
Max Kellermann's avatar
Max Kellermann committed
73

74
public:
75
	static Database *Create(EventLoop &loop, DatabaseListener &listener,
76
				const ConfigBlock &block);
77

78
	gcc_pure
79
	Directory &GetRoot() noexcept {
80 81
		assert(root != NULL);

82
		return *root;
83 84
	}

85
	void Save();
86

87 88 89
	/**
	 * Returns true if there is a valid database file on the disk.
	 */
90 91 92 93
	bool FileExists() const {
		return mtime > 0;
	}

Max Kellermann's avatar
Max Kellermann committed
94 95 96 97 98
	/**
	 * @param db the #Database to be mounted; must be "open"; on
	 * success, this object gains ownership of the given #Database
	 */
	gcc_nonnull_all
99
	void Mount(const char *uri, Database *db);
Max Kellermann's avatar
Max Kellermann committed
100

101 102 103
	/**
	 * Throws #std::runtime_error on error.
	 */
Max Kellermann's avatar
Max Kellermann committed
104
	gcc_nonnull_all
105
	void Mount(const char *local_uri, const char *storage_uri);
Max Kellermann's avatar
Max Kellermann committed
106 107 108 109

	gcc_nonnull_all
	bool Unmount(const char *uri);

Max Kellermann's avatar
Max Kellermann committed
110
	/* virtual methods from class Database */
111 112
	void Open() override;
	void Close() override;
113

114
	const LightSong *GetSong(const char *uri_utf8) const override;
115
	void ReturnSong(const LightSong *song) const override;
116

117 118 119 120 121
	void Visit(const DatabaseSelection &selection,
		   VisitDirectory visit_directory,
		   VisitSong visit_song,
		   VisitPlaylist visit_playlist) const override;

122 123 124
	std::map<std::string, std::set<std::string>> CollectUniqueTags(const DatabaseSelection &selection,
								       TagType tag_type,
								       TagType group) const override;
125 126

	DatabaseStats GetStats(const DatabaseSelection &selection) const override;
127

128
	time_t GetUpdateStamp() const noexcept override {
129 130 131
		return mtime;
	}

132
private:
133
	void Configure(const ConfigBlock &block);
134

135
	void Check() const;
136

137 138 139 140
	/**
	 * Throws #std::runtime_error on error.
	 */
	void Load();
Max Kellermann's avatar
Max Kellermann committed
141 142

	Database *LockUmountSteal(const char *uri);
143 144 145 146 147
};

extern const DatabasePlugin simple_db_plugin;

#endif