Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder | |
78efa64e | 3 | * Copyright (C) 2005, 2007 Richard Kettlewell |
460b9539 | 4 | * |
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. | |
9 | * | |
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. | |
14 | * | |
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 | |
18 | * USA | |
19 | */ | |
20 | ||
21 | #ifndef TRACKDB_INT_H | |
22 | #define TRACKDB_INT_H | |
23 | ||
fdca70ee RK |
24 | #include "kvp.h" |
25 | ||
c3be4f19 RK |
26 | struct vector; /* forward declaration */ |
27 | ||
460b9539 | 28 | extern DB_ENV *trackdb_env; |
29 | ||
30 | extern DB *trackdb_tracksdb; | |
31 | extern DB *trackdb_prefsdb; | |
32 | extern DB *trackdb_searchdb; | |
3dc3d7db | 33 | extern DB *trackdb_tagsdb; |
2a10b70b | 34 | extern DB *trackdb_noticeddb; |
3dc3d7db | 35 | extern DB *trackdb_globaldb; |
a745dd43 | 36 | extern DB *trackdb_usersdb; |
fdca70ee | 37 | extern DB *trackdb_scheduledb; |
460b9539 | 38 | |
39 | DBC *trackdb_opencursor(DB *db, DB_TXN *tid); | |
40 | /* open a transaction */ | |
41 | ||
42 | int trackdb_closecursor(DBC *c); | |
43 | /* close transaction, returns 0 or DB_LOCK_DEADLOCK */ | |
44 | ||
45 | int trackdb_notice(const char *track, | |
46 | const char *path); | |
47 | int trackdb_notice_tid(const char *track, | |
48 | const char *path, | |
49 | DB_TXN *tid); | |
50 | /* notice a track; return DB_NOTFOUND if new, else 0. _tid can return | |
51 | * DB_LOCK_DEADLOCK too. */ | |
52 | ||
53 | int trackdb_obsolete(const char *track, DB_TXN *tid); | |
54 | /* obsolete a track */ | |
55 | ||
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 */ | |
60 | ||
f0feb22e RK |
61 | /** @brief Evaluate @p expr in a transaction, looping on deadlock |
62 | * | |
63 | * @c tid will be the transaction handle. @p e will be the error code. | |
64 | */ | |
65 | #define WITH_TRANSACTION(expr) do { \ | |
66 | DB_TXN *tid; \ | |
67 | \ | |
68 | tid = trackdb_begin_transaction(); \ | |
69 | while((e = (expr)) == DB_LOCK_DEADLOCK) { \ | |
70 | trackdb_abort_transaction(tid); \ | |
71 | tid = trackdb_begin_transaction(); \ | |
72 | } \ | |
73 | if(e) \ | |
74 | trackdb_abort_transaction(tid); \ | |
75 | else \ | |
76 | trackdb_commit_transaction(tid); \ | |
77 | } while(0) | |
78 | ||
460b9539 | 79 | int trackdb_getdata(DB *db, |
80 | const char *track, | |
81 | struct kvp **kp, | |
82 | DB_TXN *tid); | |
83 | /* fetch and decode a database entry. Returns 0, DB_NOTFOUND or | |
84 | * DB_LOCK_DEADLOCK. */ | |
85 | ||
86 | int trackdb_putdata(DB *db, | |
87 | const char *track, | |
88 | const struct kvp *k, | |
89 | DB_TXN *tid, | |
90 | u_int32_t flags); | |
91 | /* encode and store a database entry. Returns 0, DB_KEYEXIST or | |
92 | * DB_LOCK_DEADLOCK. */ | |
93 | ||
94 | int trackdb_delkey(DB *db, | |
95 | const char *track, | |
96 | DB_TXN *tid); | |
97 | /* delete a database entry. Returns 0, DB_NOTFOUND or DB_LOCK_DEADLOCK. */ | |
98 | ||
99 | int trackdb_delkeydata(DB *db, | |
100 | const char *word, | |
101 | const char *track, | |
102 | DB_TXN *tid); | |
103 | /* delete a (key,data) pair. Returns 0, DB_NOTFOUND or DB_LOCK_DEADLOCK. */ | |
104 | ||
105 | int trackdb_scan(const char *root, | |
106 | int (*callback)(const char *track, | |
107 | struct kvp *data, | |
bea6f6d5 | 108 | struct kvp *prefs, |
460b9539 | 109 | void *u, |
110 | DB_TXN *tid), | |
111 | void *u, | |
112 | DB_TXN *tid); | |
d1694464 | 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. */ | |
460b9539 | 116 | |
c3be4f19 | 117 | int trackdb_listkeys(DB *db, struct vector *v, DB_TXN *tid); |
2a10b70b | 118 | |
c3be4f19 | 119 | /* fill KEY in with S, returns KEY */ |
460b9539 | 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); | |
124 | return key; | |
125 | } | |
126 | ||
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; | |
131 | return data; | |
132 | } | |
133 | ||
134 | /* encode K and store in DATA, returns DATA */ | |
135 | static inline DBT *encode_data(DBT *data, const struct kvp *k) { | |
136 | size_t size; | |
137 | ||
138 | memset(data, 0, sizeof *data); | |
139 | data->data = kvp_urlencode(k, &size); | |
140 | data->size = size; | |
141 | return data; | |
142 | } | |
143 | ||
f9635e06 RK |
144 | int trackdb_set_global_tid(const char *name, |
145 | const char *value, | |
146 | DB_TXN *tid); | |
147 | int trackdb_get_global_tid(const char *name, | |
148 | DB_TXN *tid, | |
149 | const char **rp); | |
150 | ||
91c9324a RK |
151 | char **parsetags(const char *s); |
152 | int tag_intersection(char **a, char **b); | |
153 | ||
460b9539 | 154 | #endif /* TRACKDB_INT_H */ |
155 | ||
156 | /* | |
157 | Local Variables: | |
158 | c-basic-offset:2 | |
159 | comment-column:40 | |
160 | fill-column:79 | |
161 | indent-tabs-mode:nil | |
162 | End: | |
163 | */ |