MPD  0.20.15
SimpleDatabasePlugin.hxx
Go to the documentation of this file.
1 /*
2  * Copyright 2003-2017 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef MPD_SIMPLE_DATABASE_PLUGIN_HXX
21 #define MPD_SIMPLE_DATABASE_PLUGIN_HXX
22 
23 #include "check.h"
24 #include "db/Interface.hxx"
25 #include "fs/AllocatedPath.hxx"
26 #include "db/LightSong.hxx"
27 #include "Compiler.h"
28 
29 #include <cassert>
30 
31 struct ConfigBlock;
32 struct Directory;
33 struct DatabasePlugin;
34 class EventLoop;
35 class DatabaseListener;
36 class PrefixedLightSong;
37 
38 class SimpleDatabase : public Database {
39  AllocatedPath path;
40  std::string path_utf8;
41 
42 #ifdef ENABLE_ZLIB
43  bool compress;
44 #endif
45 
49  AllocatedPath cache_path;
50 
51  Directory *root;
52 
53  time_t mtime;
54 
59  mutable PrefixedLightSong *prefixed_light_song;
60 
64  mutable LightSong light_song;
65 
66 #ifndef NDEBUG
67  mutable unsigned borrowed_song_count;
68 #endif
69 
70  SimpleDatabase(const ConfigBlock &block);
71 
72  SimpleDatabase(AllocatedPath &&_path, bool _compress);
73 
74 public:
75  static Database *Create(EventLoop &loop, DatabaseListener &listener,
76  const ConfigBlock &block);
77 
78  gcc_pure
79  Directory &GetRoot() noexcept {
80  assert(root != NULL);
81 
82  return *root;
83  }
84 
85  void Save();
86 
90  bool FileExists() const {
91  return mtime > 0;
92  }
93 
99  void Mount(const char *uri, Database *db);
100 
105  void Mount(const char *local_uri, const char *storage_uri);
106 
108  bool Unmount(const char *uri);
109 
110  /* virtual methods from class Database */
111  void Open() override;
112  void Close() override;
113 
114  const LightSong *GetSong(const char *uri_utf8) const override;
115  void ReturnSong(const LightSong *song) const override;
116 
117  void Visit(const DatabaseSelection &selection,
118  VisitDirectory visit_directory,
119  VisitSong visit_song,
120  VisitPlaylist visit_playlist) const override;
121 
122  void VisitUniqueTags(const DatabaseSelection &selection,
123  TagType tag_type, tag_mask_t group_mask,
124  VisitTag visit_tag) const override;
125 
126  DatabaseStats GetStats(const DatabaseSelection &selection) const override;
127 
128  time_t GetUpdateStamp() const noexcept override {
129  return mtime;
130  }
131 
132 private:
133  void Configure(const ConfigBlock &block);
134 
135  void Check() const;
136 
140  void Load();
141 
142  Database *LockUmountSteal(const char *uri);
143 };
144 
145 extern const DatabasePlugin simple_db_plugin;
146 
147 #endif
void Visit(const DatabaseSelection &selection, VisitDirectory visit_directory, VisitSong visit_song, VisitPlaylist visit_playlist) const override
Visit the selected entities.
gcc_nonnull_all void Mount(const char *uri, Database *db)
#define gcc_nonnull_all
Definition: Compiler.h:122
An event loop that polls for events on file/socket descriptors.
Definition: Loop.hxx:51
A path name in the native file system character set.
bool FileExists() const
Returns true if there is a valid database file on the disk.
std::function< void(const LightDirectory &)> VisitDirectory
Definition: Visitor.hxx:28
DatabaseStats GetStats(const DatabaseSelection &selection) const override
An object that listens to events from the Database.
std::function< void(const PlaylistInfo &, const LightDirectory &)> VisitPlaylist
Definition: Visitor.hxx:33
time_t GetUpdateStamp() const noexcept override
Returns the time stamp of the last database update.
const DatabasePlugin simple_db_plugin
const LightSong * GetSong(const char *uri_utf8) const override
Look up a song (including tag data) in the database.
TagType
Codes for the type of a tag item.
Definition: TagType.h:30
gcc_nonnull_all bool Unmount(const char *uri)
uint_least32_t tag_mask_t
Definition: Mask.hxx:25
std::function< void(const LightSong &)> VisitSong
Definition: Visitor.hxx:31
std::function< void(const Tag &)> VisitTag
Definition: Visitor.hxx:35
void Open() override
Open the database.
gcc_pure Directory & GetRoot() noexcept
void Close() override
Close the database, free allocated memory.
static Database * Create(EventLoop &loop, DatabaseListener &listener, const ConfigBlock &block)
#define gcc_pure
Definition: Compiler.h:116
void VisitUniqueTags(const DatabaseSelection &selection, TagType tag_type, tag_mask_t group_mask, VisitTag visit_tag) const override
Visit all unique tag values.
void ReturnSong(const LightSong *song) const override
Mark the song object as "unused".
const Storage const char * uri
A reference to a song file.
Definition: LightSong.hxx:40