2 * Copyright (C) 2007-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file osl.h User interface for the object storage layer. */
12 /** Export all declarations in this file. */
13 #pragma GCC visibility push(default)
15 /** Describes an object of the object storage layer (osl) */
17 /** Pointer to the data of the object. */
19 /** The object's size. */
23 /** Flags that change the internal handling of osl tables. */
24 enum osl_table_flags {
25 /** This table will have many rows. */
29 /** The three different types of storage for an osl column */
30 enum osl_storage_type {
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
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.
44 * Objects for columns of this type are volatile: They are only stored
45 * in memory and are discarded once the table gets closed.
51 * Additional per-column flags
53 enum osl_storage_flags {
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.
60 * \sa osl_storage_type, osl_get_row()
63 /** The data for this column will have constant size. */
65 /** All values of this column will be different. */
67 /** Do not free the data for this column (\p OSL_NO_STORAGE). */
71 /** Opaque osl table structure. */
73 /** Opaque osl row structure. */
77 * In order to build up an rbtree a compare function for the objects must be
78 * specified. Such a function always takes pointers to the two objects to be
79 * compared. It must return -1, zero, or 1, if the first argument is considered
80 * to be respectively less than, equal to, or greater than the second. If two
81 * members compare as equal, their order in the rbtree is undefined.
83 typedef int osl_compare_func(const struct osl_object *obj1,
84 const struct osl_object *obj2);
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.
90 * \sa osl_rbtree_loop(), osl_rbtree_loop_reverse().
92 typedef int osl_rbtree_loop_func(struct osl_row *row, void *data);
95 * Describes one column of a osl table.
97 struct osl_column_description {
98 /** One of the tree 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;
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
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.
114 * \sa osl_storage_flags, osl_compare_func
116 osl_compare_func *compare_function;
118 * If the \a OSL_FIXED_SIZE flag is set for this column, this value
119 * determines the fixed size of all objects of this column. It is
120 * ignored, if \a OSL_FIXED_SIZE is not set.
126 * Describes one osl table.
128 struct osl_table_description {
129 /** The directory which contains all files of this table. */
132 * The table name. A subdirectory of \a dir called \a name is created
133 * at table creation time. It must be a valid name for a subdirectory.
134 * In particular, no slashes are allowed for \a name.
137 /** The number of columns of this table. */
138 uint16_t num_columns;
139 /** Further table-wide information, \sa osl_table_flags. */
141 /** The array describing the individual columns of the table. */
142 struct osl_column_description *column_descriptions;
145 /** Flags to be passed to \a osl_close_table(). */
146 enum osl_close_flags {
148 * The table header contains a "dirty" flag which specifies whether
149 * the table is currently open by another process. This flag specifies
150 * that the dirty flag should be cleared before closing the table.
154 * If the table contains columns of type \a OSL_NO_STORAGE and this
155 * flag is passed to osl_close_table(), free(3) is called for each
156 * object of each column of type \a OSL_NO_STORAGE.
158 OSL_FREE_VOLATILE = 2
162 * Create a new osl table.
164 * \param desc Pointer to the table description.
168 int osl_create_table(const struct osl_table_description *desc);
173 * Each osl table must be opened before its data can be accessed.
175 * \param desc Describes the table to be opened.
176 * \param result Contains a pointer to the open table on success.
178 * The table description given by \a desc should coincide with the
179 * description used at creation time.
183 int osl_open_table(const struct osl_table_description *desc,
184 struct osl_table **result);
187 * Close an osl table.
189 * \param t Pointer to the table to be closed.
190 * \param flags Options for what should be cleaned up.
192 * If osl_open_table() succeeds, the resulting table pointer must later be
193 * passed to this function in order to flush all changes to the file system and
194 * to free the resources that were allocated by osl_open_table().
198 * \sa osl_open_table(), unmap_table().
200 int osl_close_table(struct osl_table *t, enum osl_close_flags flags);
203 * Get the row that contains the given object.
205 * \param t Pointer to an open osl table.
206 * \param col_num The number of the column to be searched.
207 * \param obj The object to be looked up.
208 * \param result Points to the row containing \a obj.
210 * Lookup \a obj in \a t and return the row containing \a obj. The column
211 * specified by \a col_num must have an associated rbtree.
215 * \sa osl_storage_flags
217 int osl_get_row(const struct osl_table *t, unsigned col_num,
218 const struct osl_object *obj, struct osl_row **result);
221 * Retrieve an object identified by row and column
223 * \param t Pointer to an open osl table.
224 * \param row Pointer to the row.
225 * \param col_num The column number.
226 * \param object The result pointer.
228 * The column determined by \a col_num must be of type \p OSL_MAPPED_STORAGE
229 * or \p OSL_NO_STORAGE, i.e. no disk storage objects may be retrieved by this
234 * \sa osl_storage_type, osl_open_disk_object().
236 int osl_get_object(const struct osl_table *t, const struct osl_row *row,
237 unsigned col_num, struct osl_object *object);
240 * Retrieve an object of type \p OSL_DISK_STORAGE by row and column.
242 * \param t Pointer to an open osl table.
243 * \param r Pointer to the row containing the object.
244 * \param col_num The column number.
245 * \param obj Points to the result upon successful return.
247 * For columns of type \p OSL_DISK_STORAGE, this function must be used to
248 * retrieve one of its containing objects. Afterwards, osl_close_disk_object()
249 * must be called in order to deallocate the resources.
253 * \sa osl_get_object(), osl_storage_type, osl_close_disk_object().
255 int osl_open_disk_object(const struct osl_table *t,
256 const struct osl_row *r, unsigned col_num, struct osl_object *obj);
259 * Free resources that were allocated during osl_open_disk_object().
261 * \param obj Pointer to the object previously returned by open_disk_object().
263 * \return The return value of the underlying call to munmap().
267 int osl_close_disk_object(struct osl_object *obj);
270 * Add a new row to an osl table and retrieve this row.
272 * \param t Pointer to an open osl table.
273 * \param objects Array of objects to be added.
274 * \param row Result pointer.
276 * The \a objects parameter must point to an array containing one object per
277 * column. The order of the objects in the array is given by the table
278 * description of \a table. Several sanity checks are performed during object
279 * insertion and the function returns without modifying the table if any of
280 * these tests fail. In fact, it is atomic in the sense that it either
281 * succeeds or leaves the table unchanged (i.e. either all or none of the
282 * objects are added to the table).
284 * It is considered an error if an object is added to a column with associated
285 * rbtree if this object is equal to an object already contained in that column
286 * (i.e. the compare function for the column's rbtree returns zero).
290 * \sa struct osl_table_description, osl_compare_func, osl_add_row().
292 int osl_add_and_get_row(struct osl_table *t, struct osl_object *objects,
293 struct osl_row **row);
297 * Add a new row to an osl table.
299 * \param t Same meaning as osl_add_and_get_row().
300 * \param objects Same meaning as osl_add_and_get_row().
302 * \return The return value of the underlying call to osl_add_and_get_row().
304 * This is equivalent to osl_add_and_get_row(t, objects, NULL).
306 int osl_add_row(struct osl_table *t, struct osl_object *objects);
309 * Delete a row from an osl table.
311 * \param t Pointer to an open osl table.
312 * \param row Pointer to the row to delete.
314 * This removes all disk storage objects, removes all rbtree nodes, and frees
315 * all volatile objects belonging to the given row. For mapped columns, the
316 * data is merely marked invalid and may be pruned from time to time by
321 int osl_del_row(struct osl_table *t, struct osl_row *row);
324 * Loop over all nodes in an rbtree.
326 * \param t Pointer to an open osl table.
327 * \param col_num The column to use for iterating over the elements.
328 * \param private_data Pointer that gets passed to \a func.
329 * \param func The function to be called for each node in the rbtree.
331 * This function does an in-order walk of the rbtree associated with \a
332 * col_num. It is an error if the \p OSL_RBTREE flag is not set for this
333 * column. For each node in the rbtree, the given function \a func is called
334 * with two pointers as arguments: The first osl_row* argument points to the
335 * row that contains the object corresponding to the rbtree node currently
336 * traversed, and the \a private_data pointer is passed verbatim to \a func as the
337 * second argument. The loop terminates either if \a func returns a negative
338 * value, or if all nodes of the tree have been visited.
341 * \return Standard. If the termination of the loop was caused by \a func
342 * returning a negative value, \p -E_OSL_LOOP is returned.
344 * \sa osl_storage_flags, osl_rbtree_loop_reverse(), osl_compare_func.
346 int osl_rbtree_loop(const struct osl_table *t, unsigned col_num,
347 void *private_data, osl_rbtree_loop_func *func);
350 * Loop over all nodes in an rbtree in reverse order.
352 * \param t Identical meaning as in \p osl_rbtree_loop().
353 * \param col_num Identical meaning as in \p osl_rbtree_loop().
354 * \param private_data Identical meaning as in \p osl_rbtree_loop().
355 * \param func Identical meaning as in \p osl_rbtree_loop().
357 * This function is identical to \p osl_rbtree_loop(), the only difference
358 * is that the tree is walked in reverse order.
360 * \return The same return value as \p osl_rbtree_loop().
362 * \sa osl_rbtree_loop().
364 int osl_rbtree_loop_reverse(const struct osl_table *t, unsigned col_num,
365 void *private_data, osl_rbtree_loop_func *func);
368 * Change an object in an osl table.
370 * \param t Pointer to an open osl table.
371 * \param r Pointer to the row containing the object to be updated.
372 * \param col_num Number of the column containing the object to be updated.
373 * \param obj Pointer to the replacement object.
375 * This function gets rid of all references to the old object. This includes
376 * removal of the rbtree node in case there is an rbtree associated with \a
377 * col_num. It then inserts \a obj into the table and the rbtree if necessary.
379 * If the \p OSL_RBTREE flag is set for \a col_num, you \b MUST call this
380 * function in order to change the contents of an object, even for volatile or
381 * mapped columns of constant size (which may be updated directly if \p
382 * OSL_RBTREE is not set). Otherwise the rbtree might become corrupted.
386 int osl_update_object(struct osl_table *t, const struct osl_row *r,
387 unsigned col_num, struct osl_object *obj);
390 * Get the number of rows of the given table.
392 * \param t Pointer to an open osl table.
393 * \param num_rows Result is returned here.
395 * The number of rows returned via \a num_rows excluding any invalid rows.
397 * \return Positive on success, \p -E_OSL_BAD_TABLE if \a t is \p NULL.
399 int osl_get_num_rows(const struct osl_table *t, unsigned *num_rows);
403 * Get the row corresponding to the smallest rbtree node of a column.
405 * \param t An open rbtree table.
406 * \param col_num The number of the rbtree column.
407 * \param result A pointer to the first row is returned here.
409 * The rbtree node of the smallest object (with respect to the corresponding
410 * compare function) is selected and the row containing this object is
411 * returned. It is an error if \a col_num refers to a column without an
416 * \sa osl_get_nth_row(), osl_rbtree_last_row().
418 int osl_rbtree_first_row(const struct osl_table *t, unsigned col_num,
419 struct osl_row **result);
422 * Get the row corresponding to the greatest rbtree node of a column.
424 * \param t The same meaning as in \p osl_rbtree_first_row().
425 * \param col_num The same meaning as in \p osl_rbtree_first_row().
426 * \param result The same meaning as in \p osl_rbtree_first_row().
428 * This function works just like osl_rbtree_first_row(), the only difference
429 * is that the row containing the greatest rather than the smallest object is
434 * \sa osl_get_nth_row(), osl_rbtree_first_row().
436 int osl_rbtree_last_row(const struct osl_table *t, unsigned col_num,
437 struct osl_row **result);
440 * Get the row with n-th greatest value.
442 * \param t Pointer to an open osl table.
443 * \param col_num The column number.
444 * \param n The rank of the desired row.
445 * \param result Row is returned here.
447 * Retrieve the n-th order statistic with respect to the compare function
448 * of the rbtree column \a col_num. In other words, get the row with
449 * \a n th greatest value in column \a col_num. It is an error if
450 * \a col_num is not a rbtree column, or if \a n is larger than the
451 * number of rows in the table.
455 * \sa osl_storage_flags, osl_compare_func, osl_get_row(),
456 * osl_rbtree_last_row(), osl_rbtree_first_row(), osl_get_rank().
458 int osl_get_nth_row(const struct osl_table *t, unsigned col_num,
459 unsigned n, struct osl_row **result);
462 * Get the rank of a row.
464 * \param t An open osl table.
465 * \param r The row to get the rank of.
466 * \param col_num The number of an rbtree column.
467 * \param rank Result pointer.
469 * The rank is, by definition, the position of the row in the linear order
470 * determined by an in-order tree walk of the rbtree associated with column
471 * number \a col_num of \a table.
475 * \sa osl_get_nth_row().
477 int osl_get_rank(const struct osl_table *t, struct osl_row *r,
478 unsigned col_num, unsigned *rank);
481 * Get a string describing the error code passed in the argument.
483 * \param num The error code.
485 * This works just like strerror(3). The given number must be an osl error
486 * code. The result must not be freed by the caller.
488 * \return The error text corresponding to an osl error code.
490 const char *osl_strerror(int num);
492 #pragma GCC visibility pop