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 */
27 struct vector
; /* forward declaration */
29 extern DB_ENV
*trackdb_env
;
31 extern DB
*trackdb_tracksdb
;
32 extern DB
*trackdb_prefsdb
;
33 extern DB
*trackdb_searchdb
;
34 extern DB
*trackdb_tagsdb
;
35 extern DB
*trackdb_noticeddb
;
36 extern DB
*trackdb_globaldb
;
37 extern DB
*trackdb_usersdb
;
38 extern DB
*trackdb_scheduledb
;
40 DBC
*trackdb_opencursor(DB
*db
, DB_TXN
*tid
);
41 /* open a transaction */
43 int trackdb_closecursor(DBC
*c
);
44 /* close transaction, returns 0 or DB_LOCK_DEADLOCK */
46 int trackdb_notice(const char *track
,
48 int trackdb_notice_tid(const char *track
,
51 /* notice a track; return DB_NOTFOUND if new, else 0. _tid can return
52 * DB_LOCK_DEADLOCK too. */
54 int trackdb_obsolete(const char *track
, DB_TXN
*tid
);
55 /* obsolete a track */
57 DB_TXN
*trackdb_begin_transaction(void);
58 void trackdb_abort_transaction(DB_TXN
*tid
);
59 void trackdb_commit_transaction(DB_TXN
*tid
);
60 /* begin, abort or commit a transaction */
62 /** @brief Evaluate @p expr in a transaction, looping on deadlock
64 * @c tid will be the transaction handle. @p e will be the error code.
66 #define WITH_TRANSACTION(expr) do { \
69 tid = trackdb_begin_transaction(); \
70 while((e = (expr)) == DB_LOCK_DEADLOCK) { \
71 trackdb_abort_transaction(tid); \
72 tid = trackdb_begin_transaction(); \
75 trackdb_abort_transaction(tid); \
77 trackdb_commit_transaction(tid); \
80 int trackdb_getdata(DB
*db
,
84 /* fetch and decode a database entry. Returns 0, DB_NOTFOUND or
85 * DB_LOCK_DEADLOCK. */
87 int trackdb_putdata(DB
*db
,
92 /* encode and store a database entry. Returns 0, DB_KEYEXIST or
93 * DB_LOCK_DEADLOCK. */
95 int trackdb_delkey(DB
*db
,
98 /* delete a database entry. Returns 0, DB_NOTFOUND or DB_LOCK_DEADLOCK. */
100 int trackdb_delkeydata(DB
*db
,
104 /* delete a (key,data) pair. Returns 0, DB_NOTFOUND or DB_LOCK_DEADLOCK. */
106 int trackdb_scan(const char *root
,
107 int (*callback
)(const char *track
,
114 /* Call CALLBACK for each non-alias track below ROOT (or all tracks if ROOT is
115 * 0). Return 0 or DB_LOCK_DEADLOCK. CALLBACK should return 0 on success or
116 * EINTR to cancel the scan. */
118 int trackdb_listkeys(DB
*db
, struct vector
*v
, DB_TXN
*tid
);
120 /* fill KEY in with S, returns KEY */
121 static inline DBT
*make_key(DBT
*key
, const char *s
) {
122 memset(key
, 0, sizeof *key
);
123 key
->data
= (void *)s
;
124 key
->size
= strlen(s
);
128 /* set DATA up to receive data, returns DATA */
129 static inline DBT
*prepare_data(DBT
*data
) {
130 memset(data
, 0, sizeof *data
);
131 data
->flags
= DB_DBT_MALLOC
;
135 /* encode K and store in DATA, returns DATA */
136 static inline DBT
*encode_data(DBT
*data
, const struct kvp
*k
) {
139 memset(data
, 0, sizeof *data
);
140 data
->data
= kvp_urlencode(k
, &size
);
145 int trackdb_set_global_tid(const char *name
,
148 int trackdb_get_global_tid(const char *name
,
152 char **parsetags(const char *s
);
153 int tag_intersection(char **a
, char **b
);
155 #endif /* TRACKDB_INT_H */