chiark / gitweb /
Merge Unicode 5.1.0 support.
[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 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
e7eb3a27
RK
9 *
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.
14 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
132a5a4a
RK
18/** @file lib/trackdb-int.h
19 * @brief Track database internals */
460b9539 20#ifndef TRACKDB_INT_H
21#define TRACKDB_INT_H
22
05b75f8d
RK
23#include <db.h>
24
fdca70ee
RK
25#include "kvp.h"
26
c3be4f19
RK
27struct vector; /* forward declaration */
28
460b9539 29extern DB_ENV *trackdb_env;
30
31extern DB *trackdb_tracksdb;
32extern DB *trackdb_prefsdb;
33extern DB *trackdb_searchdb;
3dc3d7db 34extern DB *trackdb_tagsdb;
2a10b70b 35extern DB *trackdb_noticeddb;
3dc3d7db 36extern DB *trackdb_globaldb;
a745dd43 37extern DB *trackdb_usersdb;
fdca70ee 38extern DB *trackdb_scheduledb;
460b9539 39
40DBC *trackdb_opencursor(DB *db, DB_TXN *tid);
41/* open a transaction */
42
43int trackdb_closecursor(DBC *c);
44/* close transaction, returns 0 or DB_LOCK_DEADLOCK */
45
46int trackdb_notice(const char *track,
47 const char *path);
48int trackdb_notice_tid(const char *track,
49 const char *path,
50 DB_TXN *tid);
51/* notice a track; return DB_NOTFOUND if new, else 0. _tid can return
52 * DB_LOCK_DEADLOCK too. */
53
54int trackdb_obsolete(const char *track, DB_TXN *tid);
55/* obsolete a track */
56
57DB_TXN *trackdb_begin_transaction(void);
58void trackdb_abort_transaction(DB_TXN *tid);
59void trackdb_commit_transaction(DB_TXN *tid);
60/* begin, abort or commit a transaction */
61
f0feb22e
RK
62/** @brief Evaluate @p expr in a transaction, looping on deadlock
63 *
64 * @c tid will be the transaction handle. @p e will be the error code.
65 */
66#define WITH_TRANSACTION(expr) do { \
67 DB_TXN *tid; \
68 \
69 tid = trackdb_begin_transaction(); \
70 while((e = (expr)) == DB_LOCK_DEADLOCK) { \
71 trackdb_abort_transaction(tid); \
72 tid = trackdb_begin_transaction(); \
73 } \
74 if(e) \
75 trackdb_abort_transaction(tid); \
76 else \
77 trackdb_commit_transaction(tid); \
78} while(0)
79
460b9539 80int trackdb_getdata(DB *db,
81 const char *track,
82 struct kvp **kp,
83 DB_TXN *tid);
84/* fetch and decode a database entry. Returns 0, DB_NOTFOUND or
85 * DB_LOCK_DEADLOCK. */
86
87int trackdb_putdata(DB *db,
88 const char *track,
89 const struct kvp *k,
90 DB_TXN *tid,
91 u_int32_t flags);
92/* encode and store a database entry. Returns 0, DB_KEYEXIST or
93 * DB_LOCK_DEADLOCK. */
94
95int trackdb_delkey(DB *db,
96 const char *track,
97 DB_TXN *tid);
98/* delete a database entry. Returns 0, DB_NOTFOUND or DB_LOCK_DEADLOCK. */
99
100int trackdb_delkeydata(DB *db,
101 const char *word,
102 const char *track,
103 DB_TXN *tid);
104/* delete a (key,data) pair. Returns 0, DB_NOTFOUND or DB_LOCK_DEADLOCK. */
105
106int trackdb_scan(const char *root,
107 int (*callback)(const char *track,
108 struct kvp *data,
bea6f6d5 109 struct kvp *prefs,
460b9539 110 void *u,
111 DB_TXN *tid),
112 void *u,
113 DB_TXN *tid);
d1694464 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. */
460b9539 117
c3be4f19 118int trackdb_listkeys(DB *db, struct vector *v, DB_TXN *tid);
2a10b70b 119
c3be4f19 120/* fill KEY in with S, returns KEY */
460b9539 121static 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);
125 return key;
126}
127
128/* set DATA up to receive data, returns DATA */
129static inline DBT *prepare_data(DBT *data) {
130 memset(data, 0, sizeof *data);
131 data->flags = DB_DBT_MALLOC;
132 return data;
133}
134
135/* encode K and store in DATA, returns DATA */
136static inline DBT *encode_data(DBT *data, const struct kvp *k) {
137 size_t size;
138
139 memset(data, 0, sizeof *data);
140 data->data = kvp_urlencode(k, &size);
141 data->size = size;
142 return data;
143}
144
f9635e06
RK
145int trackdb_set_global_tid(const char *name,
146 const char *value,
147 DB_TXN *tid);
148int trackdb_get_global_tid(const char *name,
149 DB_TXN *tid,
150 const char **rp);
151
91c9324a
RK
152char **parsetags(const char *s);
153int tag_intersection(char **a, char **b);
154
460b9539 155#endif /* TRACKDB_INT_H */
156
157/*
158Local Variables:
159c-basic-offset:2
160comment-column:40
161fill-column:79
162indent-tabs-mode:nil
163End:
164*/