File: cyrusdb2.rst.txt

package info (click to toggle)
cyrus-imapd 3.10.0~beta1-3
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 86,332 kB
  • sloc: ansic: 284,810; perl: 135,824; javascript: 9,562; sh: 5,728; yacc: 2,565; cpp: 2,147; makefile: 2,133; lex: 662; xml: 621; awk: 303; python: 279; asm: 262
file content (317 lines) | stat: -rw-r--r-- 12,353 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
.. _imap-developer-api-cyrusdb2:

..  Note: This document was converted from the original by Nic Bernstein
    (Onlight).  Any formatting mistakes are my fault and not the
    original author's.  Converted via the pandoc tool from HTML.

cyrusdb API
===========

Intro
-----

The ``cyrusdb`` API is a common interface to a key-value store, used
throughout the Cyrus code. It allows a choice of different backends for
different access patterns, while ensuring a consistent interface.

This document will describe the interface, and how to use the cyrusdb
interface from within parts of Cyrus code, as well as how to implement
your own backend

If you pass incorrect values to these APIs, you will get an assertion
failure in most cases. That's generally considered safer than silently
breaking things. Exceptions are noted below.

Code Layout
-----------

The implementation of each interface is in ``lib/cyrusdb_NAME.c``, for
example lib/cyrusdb\_flat.c. General functions are in ``lib/cyrusdb.c``
and the interface in ``lib/cyrusdb.h``.

Configuration
-------------

The name of the backend for each of the main internal databases can be
configured in imapd.conf, for example: ``annotation_db: skiplist``. This
is then read in imap/global.h and imap/global.c during startup, so that
the global variable ``config_annotation_db`` gets set to the configured
backend name:

::

        config_annotation_db = config_getstring(IMAPOPT_ANNOTATION_DB);

(Beware the misleading naming here: ``config_annotation_db`` is a string
describing the *backend* used by the database, not, say, its location on
disk.)

Internally, the main module for each database sets up struct of pointers
to the cyrusdb functions it implements, which is registered in
``lib/cyrusdb.c``

``lib/cyrusdb.c`` provides backend-agnostic wrapper functions for
interacting with cyrusdb databases.

A full example
--------------

::

      struct db *db = NULL;
      struct txn *tid = NULL;
      const char *filename = NULL;
      int flags = CYRUSDB_CREATE;

      cyrus_init(alt_config, "toolname", 0);

      filename = config_getstring(IMAPOPT_ANNOTATION_DB_PATH);

      r = cyrusdb_open(config_annotation_db, filename, flags, &db);

      r = cyrusdb_fetch(db, key, keylen, &data, &datalen, &tid);

      r = cyrusdb_commit(db, tid);

      r = cyrusdb_close(db);

      cyrus_done();

Note that you always open a database first, and close it at the end. You
must always call cyrus\_init() and cyrus\_done() to properly initialize
and clean up the ``cyrusdb`` environments.

This example also uses a transaction, meaning that the database is
locked in exclusive mode between the 'fetch' (the first use of the
transaction) and the commit.

About Transactions
------------------

The cyrusdb interface works in two modes - transactional and
non-transactional. The value of the 'tid' parameter decides which mode
is used. There are three possible values:

-  NULL - non-transactional. Will create a temporary lock for the
   duration of the current action - either a write lock for "store" or a
   read lock for "fetch". If you call "foreach", the lock will be
   dropped between each record fetched
-  Pointer to NULL - transactional, transaction not yet started. Will
   always take a write lock on the database, and update the pointer to
   point to the new transaction.
-  Pointer to a valid transaction. Will keep using this transaction

If you are currently in a transaction, you MUST pass the same
transaction to every database call. It is not possible to mix or nest
transactions. There is one exception in the skiplist backend: *If you
pass NULL to a fetch or foreach while the database is in a transaction,
it will silently do the read in the current transaction rather than
returning an error*

API Reference
-------------

All functions follow the normal C API of returning '0' on success, and
an error code on failure

cyrusdb\_init(void)
~~~~~~~~~~~~~~~~~~~

Is called once per process. Don't call this yourself, use
``cyrus_init()``. No other calls will be made until this is called.

cyrusdb\_done(void)
~~~~~~~~~~~~~~~~~~~

The opposite of ``cyrusdb_init()`` - called once per process to do any
cleaning up after all database usage is finished. Don't call this
yourself, use ``cyrus_done()``.

cyrusdb\_sync(const char \*backend)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Perform a checkpoint of the database environment. Used by berkeley
backend. Is called by ``ctl_cyrusdb -c`` on a regular basis

cyrusdb\_open(const char \*backend, const char \*fname, int flags, struct db \*\*retdb)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Opens the database with the specified 'file name' (or other descriptor,
for example the sql backend is not a filename), and if successful
returns an opaque database structure

Flags:

-  CYRUSDB\_CREATE - create the database if it doesn't exist
-  CYRUSDB\_MBOXSORT - sort '.' first, so folder listing is correct

Errors:

-  CYRUSDB\_IOERROR - if there is any error reading the file, or any
   corruption detected while loading the file

cyrusdb\_close(struct db \*db)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Close the named database. Will release any locks if they are still held,
but it's bad practice to close without committing or aborting, so the
backend should log an error

Errors:

-  CYRUSDB\_IOERROR - if there are any errors during close

cyrusdb\_fetch(struct db \*db, const char \*key, size\_t keylen, const char \*\*data, size\_t \*datalen, struct txn \*\*tidptr)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

cyrusdb\_fetchlock(struct db \*db, const char \*key, size\_t keylen, const char \*\*data, size\_t \*datalen, struct txn \*\*tidptr)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fetch the value for the exact key given by key and keylen. If data is
not NULL, set datalen and return a valid pointer to the start of the
value.

Fetchlock is identical to fetch, but gives a hint to the database that
the record is likely to be modified soon.

NOTE: it is possible to store a key with a zero length data record, in
which case \*datalen will be set to zero, and \*data will be set to a
non-NULL value

It is an error to call fetch with a NULL key or a zero keylen

It is an error to call fetch with a NULL datalen and a non-NULL data,
however it is acceptable to call with a NULL data and a non-NULL datalen
if you are only interested in the length

Errors:

-  CYRUSDB\_IOERROR - if any error occurs reading from the database
-  CYRUSDB\_LOCKED - if tidptr is incorrect
-  CYRUSDB\_NOTFOUND - if there is no record that matches the key

cyrusdb\_foreach(struct db \*db, const char \*prefix, size\_t prefixlen, foreach\_p \*goodp, foreach\_p \*procp, void \*rock, struct txn \*\*tidptr)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

cyrusdb\_forone(struct db \*db, const char \*key, size\_t keylen, foreach\_p \*goodp, foreach\_p \*procp, void \*rock, struct txn \*\*tidptr)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

``cyrusdb_foreach()`` iterates over all records matching the given
prefix, in database order (which may be MBOXLIST sort, depending on the
parameters given to open

It is legal to give a NULL pointer as prefix if prefixlen is zero, in
which case it will return all records in the database. It is an error to
give a non-zero prefixlen with a NULL prefix.

``cyrusdb_forone()`` "iterates" over the single record matched by the
given key. If you've already built callbacks for processing each record
from a foreach, this lets you use the same interface to process a single
record.

``goodp`` - this function is only used for deciding if the record needs
to be further processed. It can be used for basic filtering, and returns
true (non-zero) to process, or zero to skip and move straight to the
next record. Because goodp can't make any database changes, it doesn't
break the lock, so it's faster to use goodp to filter records if you
don't need to process all of them. NULL is a legal value for goodp, and
means that all records will be processed.

``procp`` - procp is the main callback function. If you use foreach in
non-transactional mode, the database is unlocked before calling procp,
and locked again afterwards. You are allowed to add, delete or modify
values in the same database from within procp. If procp returns
non-zero, the foreach loop breaks at this point, and the return value of
the foreach becomes the return value of procp. If procp returns zero,
the foreach loop will continue at the NEXT record by sort order,
regardless of whether the current record has changed or been removed.
procp MUST NOT be NULL.

Errors:

-  procp\_result - whatever your callback returns
-  CYRUSDB\_IOERROR - if any error occurs while reading
-  CYRUSDB\_LOCKED - if tidptr is incorrect

cyrusdb\_create(struct db \*db, const char \*key, size\_t keylen, const char \*data, size\_t datalen, struct txn \*\*tidptr)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

cyrusdb\_store(struct db \*db, const char \*key, size\_t keylen, const char \*data, size\_t datalen, struct txn \*\*tidptr)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Create a new record or replace an existing one. The only difference
between these two is that ``cyrusdb_create`` will return an error if the
record already exists, while ``cyrusdb_store`` will replace it

If tidptr is NULL, create/store will take a write lock for the duration
of the action.

Any failure during create/store will abort the current transaction as
well as returning an error

It is legal to pass NULL for the data field ONLY if datalen is zero. It
is not legal to pass NULL for key or zero for keylen

Errors:

-  CYRUSDB\_IOERROR - any error to write to the database
-  CYRUSDB\_LOCKED - if tidptr is incorrect
-  CYRUSDB\_EXISTS - if ``cyrusdb_create`` is called on an existing key
-  CYRUSDB\_AGAIN - if a deadlock is created. The current transaction
   has been aborted, but a retry may succeed

cyrusdb\_delete(struct db \*db, const char \*key, size\_t keylen, struct txn \*\*tidptr, int force)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Delete the given record from the database. If force is true, then
succeed even if the record doesn't currently exist.

It is not legal to pass NULL for key or zero for keylen

Errors:

-  CYRUSDB\_IOERROR - any error to write to the database
-  CYRUSDB\_LOCKED - if tidptr is incorrect
-  CYRUSDB\_NOTFOUND - if force is not set and the key doesn't exist
-  CYRUSDB\_AGAIN - if a deadlock is created. The current transaction
   has been aborted, but a retry may succeed

cyrusdb\_commit(struct db \*db, struct txn \*tid)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Commit the current transaction. tid will not be valid after this call,
regardless of success

If the commit fails, it will attempt to abort the transaction

Errors:

-  CYRUSDB\_IOERROR - any error to write to the database
-  CYRUSDB\_LOCKED - if tidptr is incorrect
-  CYRUSDB\_AGAIN - if a deadlock is created. The current transaction
   has been aborted, but a retry may succeed

cyrusdb\_abort(struct db \*db, struct txn \*tid)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Abort the current transaction. tid will not be valid after this call,
regardless of success

Attempt to roll back all changes made in the current transaction.

Errors:

-  CYRUSDB\_IOERROR - any error to write to the database
-  CYRUSDB\_LOCKED - if tidptr is incorrect

cyrusdb\_dump(struct db \*db, int detail)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Optional function to dump the internal structure of the database to
stdout for debugging purposes. Don't use.

cyrusdb\_consistent(struct db \*db)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Check if the DB is internally consistent. Looks pretty bogus, and isn't
used anywhere. Don't use.