SimpleDatabasePlugin.hxx 3.43 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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();
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
				Error &error);

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

83
		return *root;
84 85
	}

86
	void Save();
87

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

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

	gcc_nonnull_all
	bool Mount(const char *local_uri, const char *storage_uri,
		   Error &error);

	gcc_nonnull_all
	bool Unmount(const char *uri);

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

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

116
	virtual bool Visit(const DatabaseSelection &selection,
117 118 119
			   VisitDirectory visit_directory,
			   VisitSong visit_song,
			   VisitPlaylist visit_playlist,
120
			   Error &error) const override;
121

122
	virtual bool VisitUniqueTags(const DatabaseSelection &selection,
123
				     TagType tag_type, tag_mask_t group_mask,
124
				     VisitTag visit_tag,
125
				     Error &error) const override;
126

127 128
	virtual bool GetStats(const DatabaseSelection &selection,
			      DatabaseStats &stats,
129
			      Error &error) const override;
130

131 132 133 134
	virtual time_t GetUpdateStamp() const override {
		return mtime;
	}

135
private:
136
	bool Configure(const ConfigBlock &block, Error &error);
137

138
	void Check() const;
139

140
	bool Load(Error &error);
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