SimpleDatabasePlugin.hxx 3.59 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"
26
#include "song/LightSong.hxx"
27
#include "util/Manual.hxx"
28
#include "util/Compiler.h"
29 30 31

#include <cassert>

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

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

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

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

52
	Directory *root;
53

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

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

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

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

71
	SimpleDatabase(const ConfigBlock &block);
72

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

75
public:
76 77 78
	static Database *Create(EventLoop &main_event_loop,
				EventLoop &io_event_loop,
				DatabaseListener &listener,
79
				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, Database *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 111 112

	gcc_nonnull_all
	bool Unmount(const char *uri);

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

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

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

	void VisitUniqueTags(const DatabaseSelection &selection,
126
			     TagType tag_type, TagMask group_mask,
127 128 129
			     VisitTag visit_tag) const override;

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

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

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

138
	void Check() const;
139

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

	Database *LockUmountSteal(const char *uri);
146 147 148 149 150
};

extern const DatabasePlugin simple_db_plugin;

#endif