SimpleDatabasePlugin.hxx 3.67 KB
Newer Older
1
/*
2
 * Copyright 2003-2019 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 "db/Interface.hxx"
24
#include "db/Ptr.hxx"
25
#include "fs/AllocatedPath.hxx"
26
#include "song/LightSong.hxx"
27
#include "util/Manual.hxx"
28
#include "util/Compiler.h"
29
#include "config.h"
30 31 32

#include <cassert>

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

class SimpleDatabase : public Database {
41
	AllocatedPath path;
42
	std::string path_utf8;
43

44
#ifdef ENABLE_ZLIB
45 46 47
	bool compress;
#endif

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

53
	Directory *root;
54

55
	std::chrono::system_clock::time_point mtime;
56

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

63 64 65
	/**
	 * A buffer for GetSong().
	 */
66
	mutable Manual<LightSong> light_song;
67

68
#ifndef NDEBUG
69
	mutable unsigned borrowed_song_count;
70 71
#endif

72
public:
73
	SimpleDatabase(const ConfigBlock &block);
74
	SimpleDatabase(AllocatedPath &&_path, bool _compress) noexcept;
Max Kellermann's avatar
Max Kellermann committed
75

76 77 78 79
	static DatabasePtr Create(EventLoop &main_event_loop,
				  EventLoop &io_event_loop,
				  DatabaseListener &listener,
				  const ConfigBlock &block);
80

81
	gcc_pure
82
	Directory &GetRoot() noexcept {
83 84
		assert(root != NULL);

85
		return *root;
86 87
	}

88
	void Save();
89

90 91 92
	/**
	 * Returns true if there is a valid database file on the disk.
	 */
93
	bool FileExists() const {
94
		return mtime >= std::chrono::system_clock::time_point(std::chrono::system_clock::duration::zero());
95 96
	}

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

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

	gcc_nonnull_all
111
	bool Unmount(const char *uri) noexcept;
Max Kellermann's avatar
Max Kellermann committed
112

Max Kellermann's avatar
Max Kellermann committed
113
	/* virtual methods from class Database */
114
	void Open() override;
115
	void Close() noexcept override;
116

117
	const LightSong *GetSong(const char *uri_utf8) const override;
118
	void ReturnSong(const LightSong *song) const noexcept override;
119

120 121 122 123 124
	void Visit(const DatabaseSelection &selection,
		   VisitDirectory visit_directory,
		   VisitSong visit_song,
		   VisitPlaylist visit_playlist) const override;

125 126
	RecursiveMap<std::string> CollectUniqueTags(const DatabaseSelection &selection,
						    ConstBuffer<TagType> tag_types) const override;
127 128

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

Max Kellermann's avatar
Max Kellermann committed
130
	std::chrono::system_clock::time_point GetUpdateStamp() const noexcept override {
131 132 133
		return mtime;
	}

134
private:
135
	void Configure(const ConfigBlock &block);
136

137
	void Check() const;
138

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

144
	DatabasePtr LockUmountSteal(const char *uri) noexcept;
145 146 147 148 149
};

extern const DatabasePlugin simple_db_plugin;

#endif