chiark / gitweb /
copyright date
[disorder] / lib / trackdb-int.h
CommitLineData
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
c3be4f19
RK
24struct vector; /* forward declaration */
25
460b9539 26extern DB_ENV *trackdb_env;
27
28extern DB *trackdb_tracksdb;
29extern DB *trackdb_prefsdb;
30extern DB *trackdb_searchdb;
3dc3d7db 31extern DB *trackdb_tagsdb;
2a10b70b 32extern DB *trackdb_noticeddb;
3dc3d7db 33extern DB *trackdb_globaldb;
a745dd43 34extern DB *trackdb_usersdb;
460b9539 35
36DBC *trackdb_opencursor(DB *db, DB_TXN *tid);
37/* open a transaction */
38
39int trackdb_closecursor(DBC *c);
40/* close transaction, returns 0 or DB_LOCK_DEADLOCK */
41
42int trackdb_notice(const char *track,
43 const char *path);
44int trackdb_notice_tid(const char *track,
45 const char *path,
46 DB_TXN *tid);
47/* notice a track; return DB_NOTFOUND if new, else 0. _tid can return
48 * DB_LOCK_DEADLOCK too. */
49
50int trackdb_obsolete(const char *track, DB_TXN *tid);
51/* obsolete a track */
52
53DB_TXN *trackdb_begin_transaction(void);
54void trackdb_abort_transaction(DB_TXN *tid);
55void trackdb_commit_transaction(DB_TXN *tid);
56/* begin, abort or commit a transaction */
57
f0feb22e
RK
58/** @brief Evaluate @p expr in a transaction, looping on deadlock
59 *
60 * @c tid will be the transaction handle. @p e will be the error code.
61 */
62#define WITH_TRANSACTION(expr) do { \
63 DB_TXN *tid; \
64 \
65 tid = trackdb_begin_transaction(); \
66 while((e = (expr)) == DB_LOCK_DEADLOCK) { \
67 trackdb_abort_transaction(tid); \
68 tid = trackdb_begin_transaction(); \
69 } \
70 if(e) \
71 trackdb_abort_transaction(tid); \
72 else \
73 trackdb_commit_transaction(tid); \
74} while(0)
75
460b9539 76int trackdb_getdata(DB *db,
77 const char *track,
78 struct kvp **kp,
79 DB_TXN *tid);
80/* fetch and decode a database entry. Returns 0, DB_NOTFOUND or
81 * DB_LOCK_DEADLOCK. */
82
83int trackdb_putdata(DB *db,
84 const char *track,
85 const struct kvp *k,
86 DB_TXN *tid,
87 u_int32_t flags);
88/* encode and store a database entry. Returns 0, DB_KEYEXIST or
89 * DB_LOCK_DEADLOCK. */
90
91int trackdb_delkey(DB *db,
92 const char *track,
93 DB_TXN *tid);
94/* delete a database entry. Returns 0, DB_NOTFOUND or DB_LOCK_DEADLOCK. */
95
96int trackdb_delkeydata(DB *db,
97 const char *word,
98 const char *track,
99 DB_TXN *tid);
100/* delete a (key,data) pair. Returns 0, DB_NOTFOUND or DB_LOCK_DEADLOCK. */
101
102int trackdb_scan(const char *root,
103 int (*callback)(const char *track,
104 struct kvp *data,
105 void *u,
106 DB_TXN *tid),
107 void *u,
108 DB_TXN *tid);
d1694464 109/* Call CALLBACK for each non-alias track below ROOT (or all tracks if ROOT is
110 * 0). Return 0 or DB_LOCK_DEADLOCK. CALLBACK should return 0 on success or
111 * EINTR to cancel the scan. */
460b9539 112
c3be4f19 113int trackdb_listkeys(DB *db, struct vector *v, DB_TXN *tid);
2a10b70b 114
c3be4f19 115/* fill KEY in with S, returns KEY */
460b9539 116static inline DBT *make_key(DBT *key, const char *s) {
117 memset(key, 0, sizeof *key);
118 key->data = (void *)s;
119 key->size = strlen(s);
120 return key;
121}
122
123/* set DATA up to receive data, returns DATA */
124static inline DBT *prepare_data(DBT *data) {
125 memset(data, 0, sizeof *data);
126 data->flags = DB_DBT_MALLOC;
127 return data;
128}
129
130/* encode K and store in DATA, returns DATA */
131static inline DBT *encode_data(DBT *data, const struct kvp *k) {
132 size_t size;
133
134 memset(data, 0, sizeof *data);
135 data->data = kvp_urlencode(k, &size);
136 data->size = size;
137 return data;
138}
139
f9635e06
RK
140int trackdb_set_global_tid(const char *name,
141 const char *value,
142 DB_TXN *tid);
143int trackdb_get_global_tid(const char *name,
144 DB_TXN *tid,
145 const char **rp);
146
460b9539 147#endif /* TRACKDB_INT_H */
148
149/*
150Local Variables:
151c-basic-offset:2
152comment-column:40
153fill-column:79
154indent-tabs-mode:nil
155End:
156*/