web: Fix typo in README.
[osl.git] / osl.h.in
1 /*
2  * Copyright (C) 2007-2009 Andre Noll <maan@tuebingen.mpg.de>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file osl.h User interface for the object storage layer. */
8
9 #include <sys/mman.h>
10 #include <inttypes.h>
11
12 /** Export all declarations in this file. */
13 #pragma GCC visibility push(default)
14
15 /** Describes an object of the object storage layer (osl) */
16 struct osl_object {
17         /** Pointer to the data of the object. */
18         void *data;
19         /** The object's size. */
20         size_t size;
21 };
22
23 /** Flags that change the internal handling of osl tables. */
24 enum osl_table_flags {
25         /** This table will have many rows. */
26         OSL_LARGE_TABLE = 1
27 };
28
29 /** The three different types of storage for an osl column */
30 enum osl_storage_type {
31         /**
32          * All data for this column is stored in one file which gets mmapped by
33          * osl_open_table(). This is suitable for columns that do not hold much
34          * data.
35          */
36         OSL_MAPPED_STORAGE,
37         /**
38          * Each entry is stored on disk and is loaded on demand by
39          * open_disk_object(). This is the preferable storage type for large
40          * objects that need not be in memory all the time.
41          */
42          OSL_DISK_STORAGE,
43         /**
44          * Objects for columns of this type are volatile: They are only stored
45          * in memory and are discarded when the table is closed.
46          */
47         OSL_NO_STORAGE
48 };
49
50 /**
51  * Additional per-column flags
52  */
53 enum osl_storage_flags {
54         /**
55          * Build an rbtree for this column. This is only possible if the
56          * storage type of the column is either \a OSL_MAPPED_STORAGE or \a
57          * OSL_NO_STORAGE. In order to lookup objects in the table by using \a
58          * osl_get_row(), the lookup column must have an associated rbtree.
59          *
60          * \sa osl_storage_type, osl_get_row()
61          */
62         OSL_RBTREE = 1,
63         /** The data for this column will have constant size. */
64         OSL_FIXED_SIZE = 2,
65         /** All values are different. Must be set if \p OSL_RBTREE is set. */
66         OSL_UNIQUE = 4,
67         /** Do not free the data for this column (\p OSL_NO_STORAGE). */
68         OSL_DONT_FREE = 8
69 };
70
71 /** Opaque osl table structure. */
72 struct osl_table;
73 /** Opaque osl row structure. */
74 struct osl_row;
75
76 /**
77  * In order to build up an rbtree a compare function for the objects must be
78  * provided. This function takes pointers to the two objects to be compared. It
79  * must return -1, zero, or 1, if the first argument is considered to be
80  * respectively less than, equal to, or greater than the second. If two members
81  * compare as equal, their order in the rbtree is undefined.
82  */
83 typedef int osl_compare_func(const struct osl_object *obj1,
84         const struct osl_object *obj2);
85
86 /**
87  * The osl_rbreee_loop functions take a function pointer of this type. For each
88  * node in the rbtree, the given function is called.
89  *
90  * \sa osl_rbtree_loop(), osl_rbtree_loop_reverse().
91  */
92 typedef int osl_rbtree_loop_func(struct osl_row *row, void *data);
93
94 /**
95  * Describes one column of a osl table.
96  */
97 struct osl_column_description {
98         /** One of the three possible types of storage, \sa osl_storage_type. */
99         uint16_t storage_type;
100         /** Specifies further properties of the column, \sa osl_storage_flags. */
101         uint16_t storage_flags;
102         /**
103          * The column name determines the name of the directory where all data
104          * for this column will be stored. Its hash is stored in the table
105          * header. This field is ignored if the storage type is \a NO_STORAGE.
106          */
107         char *name;
108         /**
109          * For columns with an associated rbtree, this must point to a function
110          * that compares the values of two objects, either a built-in function
111          * or a function defined by the application may be supplied.  This
112          * field is ignored if the column does not have an associated rbtree.
113          *
114          * \sa osl_storage_flags, osl_compare_func
115          */
116         osl_compare_func *compare_function;
117         /**
118          * If the \a OSL_FIXED_SIZE flag is set for this column, this value
119          * describes the number of bytes of each object of this column. It is
120          * ignored, if \a OSL_FIXED_SIZE is not set.
121          */
122         uint32_t data_size;
123 };
124
125 /**
126  * Describes one osl table.
127  *
128  * A pointer to the table description is passed to \ref osl_create_table() and
129  * \ref osl_open_table(). The osl library calls which operate on an open table
130  * refer to the fields of the table description through this pointer. Hence the
131  * table description must not be modified or freed before the table is closed.
132  */
133 struct osl_table_description {
134         /**
135          * The directory which contains all files of this table. This may be
136          * either relative to the cwd or an absolute path.
137          */
138         const char *dir;
139         /**
140          * The table name. A subdirectory of \a dir called \a name is created
141          * at table creation time. It must be a valid name for a subdirectory.
142          * In particular, no slashes are allowed for \a name.
143          */
144         const char *name;
145         /** The number of columns of this table. */
146         uint16_t num_columns;
147         /** Further table-wide information, \sa osl_table_flags. */
148         uint8_t flags;
149         /** The array describing the individual columns of the table. */
150         struct osl_column_description *column_descriptions;
151 };
152
153 /** Flags to be passed to \a osl_close_table(). */
154 enum osl_close_flags {
155         /**
156          * The table header contains a "dirty" flag which indicates whether the
157          * table is currently held open by another process. The \a
158          * OSL_MARK_CLEAN flag instructs libosl to clear the dirty flag when
159          * the table is closed.
160          */
161         OSL_MARK_CLEAN = 1,
162         /**
163          * If the table contains columns of type \a OSL_NO_STORAGE and this
164          * flag is passed to osl_close_table(), free(3) is called for each
165          * object of each column of type \a OSL_NO_STORAGE.
166          */
167         OSL_FREE_VOLATILE = 2
168 };
169
170 /**
171  * Create a new osl table.
172  *
173  * \param desc Pointer to the table description.
174  *
175  * \return Standard.
176  */
177 int osl_create_table(const struct osl_table_description *desc);
178
179 /**
180  * Open an osl table.
181  *
182  * Each osl table must be opened before its data can be accessed.
183  *
184  * \param desc Describes the table to be opened.
185  * \param result Contains a pointer to the open table on success.
186  *
187  * The table description given by \a desc should coincide with the
188  * description used at creation time.
189  *
190  * \return Standard.
191  */
192 int osl_open_table(const struct osl_table_description *desc,
193         struct osl_table **result);
194
195 /**
196  * Close an osl table.
197  *
198  * \param t Pointer to the table to be closed.
199  * \param flags Options for what should be cleaned up.
200  *
201  * If osl_open_table() succeeds, the resulting table pointer must later be
202  * passed to this function in order to flush all changes to the file system and
203  * to free the resources that were allocated by osl_open_table().
204  *
205  * \return Standard.
206  *
207  * \sa osl_open_table(), unmap_table().
208  */
209 int osl_close_table(struct osl_table *t, enum osl_close_flags flags);
210
211 /**
212  * Get the row that contains the given object.
213  *
214  * \param t Pointer to an open osl table.
215  * \param col_num The number of the column to be searched.
216  * \param obj The object to be looked up.
217  * \param result Points to the row containing \a obj.
218  *
219  * Lookup \a obj in \a t and return the row containing \a obj. The column
220  * specified by \a col_num must have an associated rbtree.
221  *
222  * \return Standard. \a result is set to \p NULL if and only if the function
223  * returns negative.
224  *
225  * \sa osl_storage_flags
226  */
227 int osl_get_row(const struct osl_table *t, unsigned col_num,
228         const struct osl_object *obj, struct osl_row **result);
229
230 /**
231  * Retrieve an object identified by row and column.
232  *
233  * \param t Pointer to an open osl table.
234  * \param row Pointer to the row.
235  * \param col_num The column number.
236  * \param object The result pointer.
237  *
238  * The column determined by \a col_num must be of type \p OSL_MAPPED_STORAGE
239  * or \p OSL_NO_STORAGE, i.e. no disk storage objects may be retrieved by this
240  * function.
241  *
242  * \return Standard.
243  *
244  * \sa osl_storage_type, osl_open_disk_object().
245  */
246 int osl_get_object(const struct osl_table *t, const struct osl_row *row,
247         unsigned col_num, struct osl_object *object);
248
249 /**
250  * Retrieve an object of type \p OSL_DISK_STORAGE by row and column.
251  *
252  * \param t Pointer to an open osl table.
253  * \param r Pointer to the row containing the object.
254  * \param col_num The column number.
255  * \param obj Points to the result upon successful return.
256  *
257  * For columns of type \p OSL_DISK_STORAGE, this function must be used to
258  * retrieve one of its containing objects. Afterwards, osl_close_disk_object()
259  * must be called in order to deallocate the resources.
260  *
261  * \return Standard.
262  *
263  * \sa osl_get_object(), osl_storage_type, osl_close_disk_object().
264  */
265 int osl_open_disk_object(const struct osl_table *t,
266         const struct osl_row *r, unsigned col_num, struct osl_object *obj);
267
268 /**
269  * Free resources that were allocated during osl_open_disk_object().
270  *
271  * \param obj Pointer to the object previously returned by open_disk_object().
272  *
273  * \return The return value of the underlying call to munmap().
274  *
275  * \sa munmap(2).
276  */
277 int osl_close_disk_object(struct osl_object *obj);
278
279 /**
280  * Add a new row to an osl table and retrieve this row.
281  *
282  * \param t Pointer to an open osl table.
283  * \param objects Array of objects to be added.
284  * \param row Result pointer.
285  *
286  * The \a objects parameter must point to an array containing one object per
287  * column.  The order of the objects in the array is given by the table
288  * description of \a table. Several sanity checks are performed during object
289  * insertion and the function returns without modifying the table if any of
290  * these tests fail.  In fact, it is atomic in the sense that it either
291  * succeeds or leaves the table unchanged (i.e. either all or none of the
292  * objects are added to the table).
293  *
294  * It is considered an error if an object is added to a column with associated
295  * rbtree if this object is equal to an object already contained in that column
296  * (i.e. the compare function for the column's rbtree returns zero).
297  *
298  * \return Standard.
299  *
300  * \sa struct osl_table_description, osl_compare_func, osl_add_row().
301  */
302 int osl_add_and_get_row(struct osl_table *t, struct osl_object *objects,
303         struct osl_row **row);
304
305 /**
306  * Add a new row to an osl table.
307  *
308  * \param t Same meaning as osl_add_and_get_row().
309  * \param objects Same meaning as osl_add_and_get_row().
310  *
311  * \return The return value of the underlying call to osl_add_and_get_row().
312  *
313  * This is equivalent to osl_add_and_get_row(t, objects, NULL).
314  */
315 int osl_add_row(struct osl_table *t, struct osl_object *objects);
316
317 /**
318  * Delete a row from an osl table.
319  *
320  * \param t Pointer to an open osl table.
321  * \param row Pointer to the row to delete.
322  *
323  * This removes all disk storage objects, removes all rbtree nodes,  and frees
324  * all volatile objects belonging to the given row. For mapped columns, the
325  * data is merely marked invalid and may be pruned from time to time by
326  * oslfsck.
327  *
328  * \return Standard.
329  */
330 int osl_del_row(struct osl_table *t, struct osl_row *row);
331
332 /**
333  * Loop over all nodes in an rbtree.
334  *
335  * \param t Pointer to an open osl table.
336  * \param col_num The column to use for iterating over the elements.
337  * \param private_data Pointer that gets passed to \a func.
338  * \param func The function to be called for each node in the rbtree.
339  *
340  * This function does an in-order walk of the rbtree associated with \a
341  * col_num. It is an error if the \p OSL_RBTREE flag is not set for this
342  * column. For each node in the rbtree, the given function \a func is called
343  * with two pointers as arguments: The first osl_row* argument points to the
344  * row that contains the object corresponding to the rbtree node currently
345  * traversed, and the \a private_data pointer is passed verbatim to \a func as the
346  * second argument. The loop terminates either if \a func returns a negative
347  * value, or if all nodes of the tree have been visited.
348  *
349  * \return Standard. If the termination of the loop was caused by \a func
350  * returning a negative value, \p -E_OSL_LOOP is returned. This is the only
351  * possible error.
352  *
353  * \sa osl_storage_flags, osl_rbtree_loop_reverse(), osl_compare_func.
354  */
355 int osl_rbtree_loop(const struct osl_table *t, unsigned col_num,
356         void *private_data, osl_rbtree_loop_func *func);
357
358 /**
359  * Loop over all nodes in an rbtree in reverse order.
360  *
361  * \param t Identical meaning as in \p osl_rbtree_loop().
362  * \param col_num Identical meaning as in \p osl_rbtree_loop().
363  * \param private_data Identical meaning as in \p osl_rbtree_loop().
364  * \param func Identical meaning as in \p osl_rbtree_loop().
365  *
366  * This function is identical to \p osl_rbtree_loop(), the only difference
367  * is that the tree is walked in reverse order.
368  *
369  * \return The same return value as \p osl_rbtree_loop().
370  *
371  * \sa osl_rbtree_loop().
372  */
373 int osl_rbtree_loop_reverse(const struct osl_table *t, unsigned col_num,
374         void *private_data, osl_rbtree_loop_func *func);
375
376 /**
377  * Change an object in an osl table.
378  *
379  * \param t Pointer to an open osl table.
380  * \param r Pointer to the row containing the object to be updated.
381  * \param col_num Number of the column containing the object to be updated.
382  * \param obj Pointer to the replacement object.
383  *
384  * This function  gets rid of all references to the old object. This includes
385  * removal of the rbtree node in case there is an rbtree associated with \a
386  * col_num. It then inserts \a obj into the table and the rbtree if necessary.
387  *
388  * If the \p OSL_RBTREE flag is set for \a col_num, you \b MUST call this
389  * function in order to change the contents of an object, even for volatile or
390  * mapped columns of constant size (which may be updated directly if \p
391  * OSL_RBTREE is not set). Otherwise the rbtree might become corrupt.
392  *
393  * \return Standard
394  */
395 int osl_update_object(struct osl_table *t, const struct osl_row *r,
396         unsigned col_num, struct osl_object *obj);
397
398 /**
399  * Get the number of rows of the given table.
400  *
401  * \param t Pointer to an open osl table.
402  * \param num_rows Result is returned here.
403  *
404  * The number of rows returned via \a num_rows excluding any invalid rows.
405  *
406  * \return Positive on success, \p -E_OSL_BAD_TABLE if \a t is \p NULL.
407  */
408 int osl_get_num_rows(const struct osl_table *t, unsigned *num_rows);
409
410 /**
411  * Get the row corresponding to the smallest rbtree node of a column.
412  *
413  * \param t An open rbtree table.
414  * \param col_num The number of the rbtree column.
415  * \param result A pointer to the first row is returned here.
416  *
417  * The rbtree node of the smallest object (with respect to the corresponding
418  * compare function) is selected and the row containing this object is
419  * returned. It is an error if \a col_num refers to a column without an
420  * associated rbtree.
421  *
422  * \return Standard.
423  *
424  * \sa osl_get_nth_row(), osl_rbtree_last_row().
425  */
426 int osl_rbtree_first_row(const struct osl_table *t, unsigned col_num,
427         struct osl_row **result);
428
429 /**
430  * Get the row corresponding to the greatest rbtree node of a column.
431  *
432  * \param t The same meaning as in \p osl_rbtree_first_row().
433  * \param col_num The same meaning as in \p osl_rbtree_first_row().
434  * \param result The same meaning as in \p osl_rbtree_first_row().
435  *
436  * This function works just like osl_rbtree_first_row(), the only difference
437  * is that the row containing the greatest rather than the smallest object is
438  * returned.
439  *
440  * \return Standard.
441  *
442  * \sa osl_get_nth_row(), osl_rbtree_first_row().
443  */
444 int osl_rbtree_last_row(const struct osl_table *t, unsigned col_num,
445         struct osl_row **result);
446
447 /**
448  * Get the row with n-th greatest value.
449  *
450  * \param t Pointer to an open osl table.
451  * \param col_num The column number.
452  * \param n The rank of the desired row.
453  * \param result Row is returned here.
454  *
455  * Retrieve the n-th order statistic with respect to the compare function
456  * of the rbtree column \a col_num. In other words, get the row with
457  * \a n th greatest value in column \a col_num. It is an error if
458  * \a col_num is not a rbtree column, or if \a n is larger than the
459  * number of rows in the table.
460  *
461  * \return Standard.
462  *
463  * \sa osl_storage_flags, osl_compare_func, osl_get_row(),
464  * osl_rbtree_last_row(), osl_rbtree_first_row(), osl_get_rank().
465  */
466 int osl_get_nth_row(const struct osl_table *t, unsigned col_num,
467         unsigned n, struct osl_row **result);
468
469 /**
470  * Get the rank of a row.
471  *
472  * \param t An open osl table.
473  * \param r The row to get the rank of.
474  * \param col_num The number of an rbtree column.
475  * \param rank Result pointer.
476  *
477  * The rank is, by definition, the position of the row in the linear order
478  * determined by an in-order tree walk of the rbtree associated with column
479  * number \a col_num of \a table.
480  *
481  * \return Standard.
482  *
483  * \sa osl_get_nth_row().
484  */
485 int osl_get_rank(const struct osl_table *t, struct osl_row *r,
486         unsigned col_num, unsigned *rank);
487
488 /**
489  * Get a string describing the error code passed in the argument.
490  *
491  * \param num The error code.
492  *
493  * This works just like strerror(3). The given number must be an osl error
494  * code. The result must not be freed by the caller.
495  *
496  * \return The error text corresponding to an osl error code.
497  */
498 const char *osl_strerror(int num);
499
500 #pragma GCC visibility pop