pianod2
multisource multiuser scriptable networked music player
musiccache.h
Go to the documentation of this file.
1 
9 #pragma once
10 
11 #include <retainer.h>
12 
15 struct ThingieCache {
17  mutable time_t expiration = 0;
18  void extend (int seconds = 600) const;
19 };
20 
22  using size_type = unsigned long;
23  time_t initial_duration{ 3600 };
24  time_t reprieve_duration{ 900 };
27  time_t purge_interval{ 307 };
28 };
29 
32 class ThingiePool : protected std::unordered_map<std::string, ThingieCache> {
33 private:
34  using map_type = std::unordered_map<std::string, ThingieCache>;
36  time_t next_purge{ time (nullptr) + 4000 };
37  time_t oldest_entry{ time (nullptr) };
38 
39  void purge (void);
40 
41 public:
42  ThingiePool();
43  ThingiePool (const ThingiePoolParameters &params);
44  void setParameters (const ThingiePoolParameters &params);
45  void add (MusicThingie *thing, bool update = false);
46  void add (const SongList &songs);
47  void add (const ThingieList &things);
48  inline void update (MusicThingie *thing) {
49  add (thing, true);
50  }
51  void update (const SongList &songs);
52  void update (const ThingieList &things);
53  void erase (MusicThingie *thing);
54  MusicThingie *get (const std::string &id);
55  ThingieList get (const Filter &filter);
57  inline void periodic (void) {
58  if (next_purge <= time (nullptr))
59  purge();
60  }
61 };
62 
67 class PersistentPool : public ThingiePool {
68 protected:
69  Media::Source * const source{ nullptr };
70  mutable time_t write_time{ 0 };
71 
72 public:
73  PersistentPool (Media::Source * const src);
74  PersistentPool (Media::Source * const src, const ThingiePoolParameters &params);
75 
77  inline void markDirty() {
78  time_t now = time (nullptr);
79  if (write_time == 0) {
80  write_time = now + 3600 * 6;
81  } else if (write_time > now) {
82  write_time -= 5;
83  }
84  }
85 
87  inline bool isDirty() const {
88  return (write_time != 0);
89  }
90 
92  inline void clearDirty() const {
93  write_time = 0;
94  }
95 
97  inline bool writeIsDue () const {
98  return (write_time > time (nullptr));
99  }
100 
101  Parsnip::Data persist() const;
102  void restore (const Parsnip::Data &data);
103 
109  virtual MusicThingie *reconstruct (MusicThingie::Type type, const Parsnip::Data &data) = 0;
110 
111  inline void add (MusicThingie *thing) {
112  ThingiePool::add (thing);
113  markDirty();
114  }
115  inline void add (const SongList &songs) {
116  ThingiePool::add (songs);
117  markDirty();
118  }
119  inline void add (const ThingieList &things) {
120  ThingiePool::add (things);
121  markDirty();
122  }
123 };
Track data filter.
Definition: filter.h:38
Base class that wraps any audio source, such as Pandora, Spotify, or local music.
Definition: mediaunit.h:68
Base class for songs, albums, artists, playlists, genres, etc.
Definition: musictypes.h:77
Type
Definition: musictypes.h:86
Generic data type.
Definition: parsnip.h:81
A ThingiePool that can persist & restore its data.
Definition: musiccache.h:67
void add(const ThingieList &things)
Definition: musiccache.h:119
PersistentPool(Media::Source *const src)
Initialize the cache/pool.
Definition: musiccache.cpp:207
bool isDirty() const
Check if the pool's data has been modified.
Definition: musiccache.h:87
void add(MusicThingie *thing)
Definition: musiccache.h:111
void add(const SongList &songs)
Definition: musiccache.h:115
void markDirty()
Mark data for persistence.
Definition: musiccache.h:77
Media::Source *const source
The source to which records will belong.
Definition: musiccache.h:69
virtual MusicThingie * reconstruct(MusicThingie::Type type, const Parsnip::Data &data)=0
Reconstructs a cached record.
time_t write_time
The time at which cached data should be saved.
Definition: musiccache.h:70
void restore(const Parsnip::Data &data)
Restore cache contents from serialized data.
Definition: musiccache.cpp:267
void clearDirty() const
Reset the pool-modified flag.
Definition: musiccache.h:92
bool writeIsDue() const
Check if the pool is due to be persisted.
Definition: musiccache.h:97
Parsnip::Data persist() const
Assemble the contents of the pool for serialization.
Definition: musiccache.cpp:220
A container for holding songs.
Definition: retainedlist.h:328
Base class for storing lists of thingies, which need to be reference counted accurately.
Definition: retainedlist.h:20
A cache/pool that retains all music thingies, mixed, and allows them to be retrieved from a single pl...
Definition: musiccache.h:32
void update(MusicThingie *thing)
Definition: musiccache.h:48
time_t next_purge
Definition: musiccache.h:36
time_t oldest_entry
Definition: musiccache.h:37
MusicThingie * get(const std::string &id)
Get an item from the cache.
Definition: musiccache.cpp:126
ThingiePoolParameters settings
Definition: musiccache.h:35
void erase(MusicThingie *thing)
Remove a thing from the cache.
Definition: musiccache.cpp:153
void purge(void)
Pare down the cache.
Definition: musiccache.cpp:161
std::unordered_map< std::string, ThingieCache > map_type
Definition: musiccache.h:34
ThingiePool()
Initialize a thingie storage cache/pool.
Definition: musiccache.cpp:34
void add(MusicThingie *thing, bool update=false)
Add (or update) an item to the cache.
Definition: musiccache.cpp:59
void periodic(void)
Periodically pare down the cache.
Definition: musiccache.h:57
void setParameters(const ThingiePoolParameters &params)
Update the cache's retention parameters.
Definition: musiccache.cpp:46
Smart pointers for music thingie types.
Structure for tracking pool objects and their status for ThingiePool.
Definition: musiccache.h:15
time_t expiration
Definition: musiccache.h:17
Retainer< MusicThingie * > item
Definition: musiccache.h:16
void extend(int seconds=600) const
Extend the life of a cache object.
Definition: musiccache.cpp:27
Definition: musiccache.h:21
unsigned long size_type
Definition: musiccache.h:22
time_t purge_interval
Schedule periodic purges at this interval.
Definition: musiccache.h:27
size_type minimum_retained_items
Don't purge if we have less than this many.
Definition: musiccache.h:25
time_t reprieve_duration
Retain in-use objects at least this much longer.
Definition: musiccache.h:24
time_t initial_duration
Retain new or reinserted items for at least this amount of times.
Definition: musiccache.h:23
size_type maximum_retained_items
If we hit this many items, try purging before adding.
Definition: musiccache.h:26