File: rhythmdb-metadata-cache.c

package info (click to toggle)
rhythmbox 3.4.9-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 27,376 kB
  • sloc: ansic: 114,861; python: 4,941; xml: 730; javascript: 350; perl: 307; sh: 84; makefile: 43
file content (553 lines) | stat: -rw-r--r-- 14,484 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
 *  Copyright (C) 2015  Jonathan Matthew  <jonathan@d14n.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.
 *
 *  The Rhythmbox authors hereby grant permission for non-GPL compatible
 *  GStreamer plugins to be used and distributed together with GStreamer
 *  and Rhythmbox. This permission is above and beyond the permissions granted
 *  by the GPL license by which Rhythmbox is covered. If you modify this code
 *  you may extend this exception to your version of the code, but you are not
 *  obligated to do so. If you do not wish to do so, delete this exception
 *  statement from your 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 St, Fifth Floor, Boston, MA 02110-1301  USA.
 *
 */

#include "config.h"

#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>

#include <glib/gi18n.h>

#include <tdb.h>

#include "rhythmdb-metadata-cache.h"
#include "rb-file-helpers.h"
#include "rb-util.h"
#include "rb-debug.h"

/* is this enough? */
#define CACHE_HASH_SIZE		4096

static RhythmDBPropType cached_properties[] = {
	/* items that get accessed before being applied to an entry go first */
	RHYTHMDB_PROP_MTIME,
	RHYTHMDB_PROP_FILE_SIZE,
	RHYTHMDB_PROP_MEDIA_TYPE,

	RHYTHMDB_PROP_TITLE,
	RHYTHMDB_PROP_GENRE,
	RHYTHMDB_PROP_ARTIST,
	RHYTHMDB_PROP_ALBUM,
	RHYTHMDB_PROP_TRACK_NUMBER,
	RHYTHMDB_PROP_TRACK_TOTAL,
	RHYTHMDB_PROP_DISC_NUMBER,
	RHYTHMDB_PROP_DISC_TOTAL,
	RHYTHMDB_PROP_DURATION,
	RHYTHMDB_PROP_BITRATE,
	RHYTHMDB_PROP_DATE,
	RHYTHMDB_PROP_MUSICBRAINZ_TRACKID,
	RHYTHMDB_PROP_MUSICBRAINZ_ARTISTID,
	RHYTHMDB_PROP_MUSICBRAINZ_ALBUMID,
	RHYTHMDB_PROP_MUSICBRAINZ_ALBUMARTISTID,
	RHYTHMDB_PROP_ARTIST_SORTNAME,
	RHYTHMDB_PROP_ALBUM_SORTNAME,
	RHYTHMDB_PROP_ALBUM_ARTIST,
	RHYTHMDB_PROP_ALBUM_ARTIST_SORTNAME,
	RHYTHMDB_PROP_BPM,
	RHYTHMDB_PROP_COMPOSER,
	RHYTHMDB_PROP_COMPOSER_SORTNAME,
};

enum
{
	PROP_0,
	PROP_DB,
	PROP_NAME
};

static void	rhythmdb_metadata_cache_class_init (RhythmDBMetadataCacheClass *klass);
static void	rhythmdb_metadata_cache_init (RhythmDBMetadataCache *cache);

struct _RhythmDBMetadataCachePrivate
{
	RhythmDB *db;
	char *name;

	struct tdb_context *tdb_context;

	const char *purge_prefix;
	guint64 purge_age;
};

G_DEFINE_TYPE (RhythmDBMetadataCache, rhythmdb_metadata_cache, G_TYPE_OBJECT);

static GHashTable *instances = NULL;

/**
 * SECTION:rhythmdbmetadatacache
 * @short_description: file metadata cache
 *
 * Metadata caches store the #RhythmDBEntry fields that are directly derived
 * from the media file outside the #RhythmDB file used to store the local
 * library.
 */

/**
 * rhythmdb_metadata_cache_get:
 * @db: the #RhythmDB object
 * @name: metadata cache name
 *
 * Gets a metadata cache with the given name, creating it if necessary.
 * Can only be called from the main thread.
 *
 * Return value: new #RhythmDBMetadataCache object.
 */
RhythmDBMetadataCache *
rhythmdb_metadata_cache_get (RhythmDB *db, const char *name)
{
	GObject *obj;

	g_assert (rb_is_main_thread ());

	if (instances == NULL)
		instances = g_hash_table_new (g_str_hash, g_str_equal);

	obj = g_hash_table_lookup (instances, name);
	if (obj)
		return RHYTHMDB_METADATA_CACHE (g_object_ref (obj));

	obj = g_object_new (RHYTHMDB_TYPE_METADATA_CACHE,
			    "db", db,
			    "name", name,
			    NULL);
	g_hash_table_insert (instances, g_strdup (name), obj);
	return RHYTHMDB_METADATA_CACHE (obj);
}

static void
free_tdb_data(gpointer data)
{
	free (data);
}

static void
parse_value (TDB_DATA data, guint64 *missing_since, GVariant **metadata)
{
	GVariant *v;

	v = g_variant_new_from_data (G_VARIANT_TYPE ("(ta{sv})"), data.dptr, data.dsize, FALSE, free_tdb_data, data.dptr);

	g_variant_get_child (v, 0, "t", missing_since);
	*metadata = g_variant_get_child_value (v, 1);

	g_variant_unref (v);
}

static void
store_value (struct tdb_context *tdb, const char *key, guint64 missing_since, GVariant *metadata)
{
	GVariantBuilder b;
	GVariant *v;
	TDB_DATA tdbdata;
	TDB_DATA tdbkey;

	g_variant_builder_init (&b, G_VARIANT_TYPE ("(ta{sv})"));
	g_variant_builder_add (&b, "t", missing_since);
	g_variant_builder_add_value (&b, metadata);
	v = g_variant_builder_end (&b);

	tdbdata.dsize = g_variant_get_size (v);
	tdbdata.dptr = g_malloc0 (tdbdata.dsize);
	g_variant_store (v, tdbdata.dptr);
	g_variant_unref (v);

	tdbkey.dsize = strlen(key);
	tdbkey.dptr = (unsigned char *)key;

	tdb_store (tdb, tdbkey, tdbdata, 0);

	g_free (tdbdata.dptr);
}

/**
 * rhythmdb_metadata_cache_load:
 * @cache: a #RhythmDBMetadataCache
 * @key: cache key to load
 * @metadata: returns cached metadata items, if any
 *
 * Fetches metadata from the cache.
 *
 * Return value: %TRUE if metadata was added to the array
 */
gboolean
rhythmdb_metadata_cache_load (RhythmDBMetadataCache *cache,
			      const char *key,
			      GArray *metadata)
{
	TDB_DATA tdbkey;
	TDB_DATA tdbvalue;
	GVariant *cached_metadata;
	GVariantIter iter;
	RhythmDBPropType prop;
	RhythmDBEntryChange *fields;
	GVariant *value;
	guint64 missing_since;
	GType proptype;
	guint64 u64;
	char *pkey;
	int i;

	tdbkey.dptr = (unsigned char *)key;
	tdbkey.dsize = strlen(key);
	tdbvalue = tdb_fetch (cache->priv->tdb_context, tdbkey);
	if (tdbvalue.dptr == NULL)
		return FALSE;

	parse_value (tdbvalue, &missing_since, &cached_metadata);

	/* reset missing-since, if necessary */
	if (missing_since != 0) {
		store_value (cache->priv->tdb_context, key, 0, cached_metadata);
	}

	metadata->len = g_variant_n_children (cached_metadata);
	fields = g_new0 (RhythmDBEntryChange, metadata->len);
	metadata->data = (char *)fields;

	i = 0;
	g_variant_iter_init (&iter, cached_metadata);
	while (g_variant_iter_loop (&iter, "{sv}", &pkey, &value)) {
		prop = rhythmdb_propid_from_nice_elt_name (cache->priv->db, (xmlChar *)pkey);
		if (prop == -1) {
			rb_debug ("unknown property %s found in cache", pkey);
			continue;
		}

		fields[i].prop = prop;
		proptype = rhythmdb_get_property_type (cache->priv->db, prop);
		g_value_init (&fields[i].new, proptype);

		switch (proptype) {
		case G_TYPE_STRING:
			g_value_set_string (&fields[i].new, g_variant_get_string (value, NULL));
			break;
		case G_TYPE_BOOLEAN:
			g_value_set_boolean (&fields[i].new, g_variant_get_boolean (value));
			break;
		case G_TYPE_ULONG:
			/* we always store longs as uint64, so check for overflow */
			u64 = g_variant_get_uint64 (value);
			if (u64 > G_MAXULONG) {
				rb_debug ("value %" G_GUINT64_FORMAT " overflows", u64);
				u64 = G_MAXULONG;
			}
			g_value_set_ulong (&fields[i].new, u64);
			break;
		case G_TYPE_UINT64:
			g_value_set_uint64 (&fields[i].new, g_variant_get_uint64 (value));
			break;
		case G_TYPE_DOUBLE:
			g_value_set_double (&fields[i].new, g_variant_get_double (value));
			break;
		default:
			g_assert_not_reached ();
			break;
		}
		i++;
	}

	g_variant_unref (cached_metadata);
	return TRUE;
}

/**
 * rhythmdb_metadata_cache_store:
 * @cache: a #RhythmDBMetadataCache
 * @key: cache key to store
 * @entry: entry to store
 *
 * Stores metadata in the cache.
 */
void
rhythmdb_metadata_cache_store (RhythmDBMetadataCache *cache,
			       const char *key,
			       RhythmDBEntry *entry)
{
	GVariantBuilder vb;
	int i;

	g_variant_builder_init (&vb, G_VARIANT_TYPE ("a{sv}"));
	for (i = 0; i < G_N_ELEMENTS(cached_properties); i++) {
		GType proptype;
		const char *str;
		gulong ulong;
		guint64 u64;
		GVariant *v;

		v = NULL;
		proptype = rhythmdb_get_property_type (cache->priv->db, cached_properties[i]);
		switch (proptype) {
		case G_TYPE_STRING:
			str = rhythmdb_entry_get_string (entry, cached_properties[i]);
			if (str != NULL && str[0] != '\0' && g_str_equal (str, _("Unknown")) == FALSE) {
				v = g_variant_new_string (str);
			}
			break;

		case G_TYPE_ULONG:
			ulong = rhythmdb_entry_get_ulong (entry, cached_properties[i]);
			if (ulong != 0) {
				/* use uint64 for longs, even on ilp32 */
				v = g_variant_new_uint64 (ulong);
			}
			break;

		case G_TYPE_UINT64:
			u64 = rhythmdb_entry_get_uint64 (entry, cached_properties[i]);
			if (u64 != 0) {
				v = g_variant_new_uint64 (u64);
			}
			break;

		case G_TYPE_BOOLEAN:
			v = g_variant_new_boolean (rhythmdb_entry_get_boolean (entry, cached_properties[i]));
			break;

		case G_TYPE_DOUBLE:
			v = g_variant_new_double (rhythmdb_entry_get_double (entry, cached_properties[i]));
			break;

		default:
			g_assert_not_reached ();
		}

		if (v != NULL) {
			const char *tag;

			tag = (const char *)rhythmdb_nice_elt_name_from_propid (cache->priv->db, cached_properties[i]);
			g_variant_builder_add (&vb, "{sv}", tag, v);
		}
	}

	store_value (cache->priv->tdb_context, key, 0, g_variant_builder_end (&vb));
}

typedef struct {
	struct tdb_context *tdb;

	const char *prefix;
	guint64 time;
	guint64 before;

	RhythmDBMetadataCacheValidFunc valid_func;
	gpointer valid_func_data;
} RhythmDBMetadataCachePurge;

static int
purge_traverse_cb (struct tdb_context *tdb, TDB_DATA tdbkey, TDB_DATA tdbdata, RhythmDBMetadataCachePurge *purge)
{
	guint64 missing_since;
	GVariant *metadata;
	char *key;
	TDB_DATA aligndata;

	key = g_strndup ((const char *)tdbkey.dptr, tdbkey.dsize);
	if (g_str_has_prefix (key, purge->prefix) == FALSE) {
		g_free (key);
		return 0;
	}

	aligndata.dptr = g_memdup (tdbdata.dptr, tdbdata.dsize);
	aligndata.dsize = tdbdata.dsize;

	parse_value (aligndata, &missing_since, &metadata);
	if (missing_since == 0) {
		if (purge->valid_func (key, purge->valid_func_data) == FALSE) {
			store_value (purge->tdb, key, purge->time, metadata);
		}
	} else if (missing_since < purge->before) {
		rb_debug ("entry %s is too old, deleting", key);
		tdb_delete (tdb, tdbkey);
	}
	g_variant_unref (metadata); /* also frees aligndata.dptr */
	g_free (key);

	return 0;
}

/**
 * rhythmdb_metadata_cache_purge:
 * @cache: a #RhythmDBMetadataCache
 * @prefix: prefix to scan
 * @max_age: maximum age of entries to keep (in seconds)
 *
 * Purges cache entries that have not been used for some amount of time.
 */
void
rhythmdb_metadata_cache_purge (RhythmDBMetadataCache *cache,
			       const char *prefix,
			       gulong max_age,
			       RhythmDBMetadataCacheValidFunc cb,
			       gpointer cb_data,
			       GDestroyNotify cb_data_destroy)
{
	RhythmDBMetadataCachePurge purge;

	time_t now;
	time (&now);
	purge.time = now;
	purge.before = now - max_age;
	purge.prefix = prefix;
	purge.valid_func = cb;
	purge.valid_func_data = cb_data;
	purge.tdb = cache->priv->tdb_context;
	tdb_traverse (cache->priv->tdb_context, (tdb_traverse_func)purge_traverse_cb, &purge);

	if (cb_data_destroy && cb_data)
		cb_data_destroy (cb_data);
}



static void
rhythmdb_metadata_cache_init (RhythmDBMetadataCache *cache)
{
	cache->priv = G_TYPE_INSTANCE_GET_PRIVATE (cache,
						   RHYTHMDB_TYPE_METADATA_CACHE,
						   RhythmDBMetadataCachePrivate);
}

static void
impl_set_property (GObject *object,
		   guint prop_id,
		   const GValue *value,
		   GParamSpec *pspec)
{
	RhythmDBMetadataCache *cache = RHYTHMDB_METADATA_CACHE (object);

	switch (prop_id) {
	case PROP_DB:
		cache->priv->db = RHYTHMDB (g_value_dup_object (value));
		break;
	case PROP_NAME:
		cache->priv->name = g_value_dup_string (value);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
impl_get_property (GObject *object,
		   guint prop_id,
		   GValue *value,
		   GParamSpec *pspec)
{
	RhythmDBMetadataCache *cache = RHYTHMDB_METADATA_CACHE (object);

	switch (prop_id) {
	case PROP_DB:
		g_value_set_object (value, cache->priv->db);
		break;
	case PROP_NAME:
		g_value_set_string (value, cache->priv->name);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
impl_constructed (GObject *object)
{
	RhythmDBMetadataCache *cache;
	char *cachedir;
	char *tdbfile;
	char *tdbpath;

	RB_CHAIN_GOBJECT_METHOD (rhythmdb_metadata_cache_parent_class, constructed, object);

	cache = RHYTHMDB_METADATA_CACHE (object);
	cachedir = g_build_filename (rb_user_cache_dir (), "metadata", NULL);
	if (g_mkdir_with_parents (cachedir, 0700) != 0) {
		rb_debug ("unable to create metadata cache directory %s", cachedir);
	} else {
		tdbfile = g_strdup_printf ("%s.tdb", cache->priv->name);
		tdbpath = g_build_filename (cachedir, tdbfile, NULL);
		cache->priv->tdb_context = tdb_open (tdbpath, CACHE_HASH_SIZE, TDB_INCOMPATIBLE_HASH, O_RDWR | O_CREAT, 0600);
		if (cache->priv->tdb_context == NULL) {
			rb_debug ("unable to open metadata cache %s: %s", tdbpath, strerror(errno));
		}
		g_free (tdbfile);
		g_free (tdbpath);
	}
	g_free (cachedir);
}


static void
impl_dispose (GObject *object)
{
	RhythmDBMetadataCache *cache = RHYTHMDB_METADATA_CACHE (object);

	g_clear_object (&cache->priv->db);

	G_OBJECT_CLASS (rhythmdb_metadata_cache_parent_class)->dispose (object);
}

static void
impl_finalize (GObject *object)
{
	RhythmDBMetadataCache *cache = RHYTHMDB_METADATA_CACHE (object);

	g_free (cache->priv->name);

	G_OBJECT_CLASS (rhythmdb_metadata_cache_parent_class)->finalize (object);
}

static void
rhythmdb_metadata_cache_class_init (RhythmDBMetadataCacheClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->set_property = impl_set_property;
	object_class->get_property = impl_get_property;
	object_class->constructed = impl_constructed;
	object_class->dispose = impl_dispose;
	object_class->finalize = impl_finalize;

	g_object_class_install_property (object_class,
					 PROP_DB,
					 g_param_spec_object ("db",
							      "db",
							      "RhythmDB object",
							      RHYTHMDB_TYPE,
							      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
	g_object_class_install_property (object_class,
					 PROP_NAME,
					 g_param_spec_string ("name",
							      "name",
							      "cache file name",
							      NULL,
							      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

	g_type_class_add_private (klass, sizeof (RhythmDBMetadataCachePrivate));
}