SimpleDatabasePlugin.hxx 3.52 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 config_param;
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 43 44 45
#ifdef HAVE_ZLIB
	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 76 77 78
	static Database *Create(EventLoop &loop, DatabaseListener &listener,
				const config_param &param,
				Error &error);

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

83
		return *root;
84 85
	}

86
	bool Save(Error &error);
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 100 101 102 103 104 105 106 107 108
	/**
	 * @param db the #Database to be mounted; must be "open"; on
	 * success, this object gains ownership of the given #Database
	 */
	gcc_nonnull_all
	bool Mount(const char *uri, Database *db, Error &error);

	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 bool Open(Error &error) override;
111
	virtual void Close() override;
112

113 114 115
	virtual const LightSong *GetSong(const char *uri_utf8,
					 Error &error) const override;
	virtual void ReturnSong(const LightSong *song) const;
116

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

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

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

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

136
private:
137
	bool Configure(const config_param &param, Error &error);
138

139
	gcc_pure
140
	bool Check(Error &error) const;
141

142
	bool Load(Error &error);
Max Kellermann's avatar
Max Kellermann committed
143 144

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

extern const DatabasePlugin simple_db_plugin;

#endif