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 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 struct vector; /* forward declaration */
28 extern DB_ENV *trackdb_env;
30 extern DB *trackdb_tracksdb;
31 extern DB *trackdb_prefsdb;
32 extern DB *trackdb_searchdb;
33 extern DB *trackdb_tagsdb;
34 extern DB *trackdb_noticeddb;
35 extern DB *trackdb_globaldb;
36 extern DB *trackdb_usersdb;
37 extern DB *trackdb_scheduledb;
39 DBC *trackdb_opencursor(DB *db, DB_TXN *tid);
40 /* open a transaction */
42 int trackdb_closecursor(DBC *c);
43 /* close transaction, returns 0 or DB_LOCK_DEADLOCK */
45 int trackdb_notice(const char *track,
47 int trackdb_notice_tid(const char *track,
50 /* notice a track; return DB_NOTFOUND if new, else 0. _tid can return
51 * DB_LOCK_DEADLOCK too. */
53 int trackdb_obsolete(const char *track, DB_TXN *tid);
54 /* obsolete a track */
56 DB_TXN *trackdb_begin_transaction(void);
57 void trackdb_abort_transaction(DB_TXN *tid);
58 void trackdb_commit_transaction(DB_TXN *tid);
59 /* begin, abort or commit a transaction */
61 /** @brief Evaluate @p expr in a transaction, looping on deadlock
63 * @c tid will be the transaction handle. @p e will be the error code.
65 #define WITH_TRANSACTION(expr) do { \
68 tid = trackdb_begin_transaction(); \
69 while((e = (expr)) == DB_LOCK_DEADLOCK) { \
70 trackdb_abort_transaction(tid); \
71 tid = trackdb_begin_transaction(); \
74 trackdb_abort_transaction(tid); \
76 trackdb_commit_transaction(tid); \
79 int trackdb_getdata(DB *db,
83 /* fetch and decode a database entry. Returns 0, DB_NOTFOUND or
84 * DB_LOCK_DEADLOCK. */
86 int trackdb_putdata(DB *db,
91 /* encode and store a database entry. Returns 0, DB_KEYEXIST or
92 * DB_LOCK_DEADLOCK. */
94 int trackdb_delkey(DB *db,
97 /* delete a database entry. Returns 0, DB_NOTFOUND or DB_LOCK_DEADLOCK. */
99 int trackdb_delkeydata(DB *db,
103 /* delete a (key,data) pair. Returns 0, DB_NOTFOUND or DB_LOCK_DEADLOCK. */
105 int trackdb_scan(const char *root,
106 int (*callback)(const char *track,
113 /* Call CALLBACK for each non-alias track below ROOT (or all tracks if ROOT is
114 * 0). Return 0 or DB_LOCK_DEADLOCK. CALLBACK should return 0 on success or
115 * EINTR to cancel the scan. */
117 int trackdb_listkeys(DB *db, struct vector *v, DB_TXN *tid);
119 /* fill KEY in with S, returns KEY */
120 static inline DBT *make_key(DBT *key, const char *s) {
121 memset(key, 0, sizeof *key);
122 key->data = (void *)s;
123 key->size = strlen(s);
127 /* set DATA up to receive data, returns DATA */
128 static inline DBT *prepare_data(DBT *data) {
129 memset(data, 0, sizeof *data);
130 data->flags = DB_DBT_MALLOC;
134 /* encode K and store in DATA, returns DATA */
135 static inline DBT *encode_data(DBT *data, const struct kvp *k) {
138 memset(data, 0, sizeof *data);
139 data->data = kvp_urlencode(k, &size);
144 int trackdb_set_global_tid(const char *name,
147 int trackdb_get_global_tid(const char *name,
151 char **parsetags(const char *s);
152 int tag_intersection(char **a, char **b);
154 #endif /* TRACKDB_INT_H */