SimpleDatabasePlugin.hxx 3.55 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
	std::chrono::system_clock::time_point mtime;
54

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 76 77
	static Database *Create(EventLoop &main_event_loop,
				EventLoop &io_event_loop,
				DatabaseListener &listener,
78
				const ConfigBlock &block);
79

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

84
		return *root;
85 86
	}

87
	void Save();
88

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

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

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

	gcc_nonnull_all
	bool Unmount(const char *uri);

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

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

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

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

	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

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

extern const DatabasePlugin simple_db_plugin;

#endif