2 * This file is part of DisOrder
3 * Copyright (C) 2005, 2007 Richard Kettlewell
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 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file lib/trackdb-int.h
19 * @brief Track database internals */
28 struct vector
; /* forward declaration */
30 extern DB_ENV
*trackdb_env
;
32 extern DB
*trackdb_tracksdb
;
33 extern DB
*trackdb_prefsdb
;
34 extern DB
*trackdb_searchdb
;
35 extern DB
*trackdb_tagsdb
;
36 extern DB
*trackdb_noticeddb
;
37 extern DB
*trackdb_globaldb
;
38 extern DB
*trackdb_usersdb
;
39 extern DB
*trackdb_scheduledb
;
40 extern DB
*trackdb_playlistsdb
;
42 DBC
*trackdb_opencursor(DB
*db
, DB_TXN
*tid
);
43 /* open a transaction */
45 int trackdb_closecursor(DBC
*c
);
46 /* close transaction, returns 0 or DB_LOCK_DEADLOCK */
48 int trackdb_notice(const char *track
,
50 int trackdb_notice_tid(const char *track
,
53 /* notice a track; return DB_NOTFOUND if new, else 0. _tid can return
54 * DB_LOCK_DEADLOCK too. */
56 int trackdb_obsolete(const char *track
, DB_TXN
*tid
);
57 /* obsolete a track */
59 DB_TXN
*trackdb_begin_transaction(void);
60 void trackdb_abort_transaction(DB_TXN
*tid
);
61 void trackdb_commit_transaction(DB_TXN
*tid
);
62 /* begin, abort or commit a transaction */
64 /** @brief Evaluate @p expr in a transaction, looping on deadlock
66 * @c tid will be the transaction handle. @p e will be the error code.
68 #define WITH_TRANSACTION(expr) do { \
71 tid = trackdb_begin_transaction(); \
72 while((e = (expr)) == DB_LOCK_DEADLOCK) { \
73 trackdb_abort_transaction(tid); \
74 tid = trackdb_begin_transaction(); \
77 trackdb_abort_transaction(tid); \
79 trackdb_commit_transaction(tid); \
82 int trackdb_getdata(DB
*db
,
86 /* fetch and decode a database entry. Returns 0, DB_NOTFOUND or
87 * DB_LOCK_DEADLOCK. */
89 int trackdb_putdata(DB
*db
,
94 /* encode and store a database entry. Returns 0, DB_KEYEXIST or
95 * DB_LOCK_DEADLOCK. */
97 int trackdb_delkey(DB
*db
,
100 /* delete a database entry. Returns 0, DB_NOTFOUND or DB_LOCK_DEADLOCK. */
102 int trackdb_delkeydata(DB
*db
,
106 /* delete a (key,data) pair. Returns 0, DB_NOTFOUND or DB_LOCK_DEADLOCK. */
108 int trackdb_scan(const char *root
,
109 int (*callback
)(const char *track
,
116 /* Call CALLBACK for each non-alias track below ROOT (or all tracks if ROOT is
117 * 0). Return 0 or DB_LOCK_DEADLOCK. CALLBACK should return 0 on success or
118 * EINTR to cancel the scan. */
120 int trackdb_listkeys(DB
*db
, struct vector
*v
, DB_TXN
*tid
);
122 /* fill KEY in with S, returns KEY */
123 static inline DBT
*make_key(DBT
*key
, const char *s
) {
124 memset(key
, 0, sizeof *key
);
125 key
->data
= (void *)s
;
126 key
->size
= strlen(s
);
130 /* set DATA up to receive data, returns DATA */
131 static inline DBT
*prepare_data(DBT
*data
) {
132 memset(data
, 0, sizeof *data
);
133 data
->flags
= DB_DBT_MALLOC
;
137 /* encode K and store in DATA, returns DATA */
138 static inline DBT
*encode_data(DBT
*data
, const struct kvp
*k
) {
141 memset(data
, 0, sizeof *data
);
142 data
->data
= kvp_urlencode(k
, &size
);
147 int trackdb_set_global_tid(const char *name
,
150 int trackdb_get_global_tid(const char *name
,
154 char **parsetags(const char *s
);
155 int tag_intersection(char **a
, char **b
);
157 #endif /* TRACKDB_INT_H */