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