2 * Copyright (C) 2007-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file osl.c Object storage layer functions. */
8 #include <dirent.h> /* readdir() */
18 * A wrapper for lseek(2).
20 * \param fd The file descriptor whose offset is to be to repositioned.
21 * \param offset A value-result parameter.
22 * \param whence Usual repositioning directive.
24 * Reposition the offset of the file descriptor \a fd to the argument \a offset
25 * according to the directive \a whence. Upon successful return, \a offset
26 * contains the resulting offset location as measured in bytes from the
27 * beginning of the file.
29 * \return Positive on success. Otherwise, the function returns \p -E_LSEEK.
33 int para_lseek(int fd, off_t *offset, int whence)
35 *offset = lseek(fd, *offset, whence);
43 * Wrapper for the write system call.
45 * \param fd The file descriptor to write to.
46 * \param buf The buffer to write.
47 * \param size The length of \a buf in bytes.
49 * This function writes out the given buffer and retries if an interrupt
50 * occurred during the write.
52 * \return On success, the number of bytes written is returned, otherwise, the
53 * function returns \p -E_WRITE.
57 ssize_t para_write(int fd, const void *buf, size_t size)
62 ret = write(fd, buf, size);
63 if ((ret < 0) && (errno == EAGAIN || errno == EINTR))
65 return ret >= 0? ret : -E_WRITE;
70 * Write the whole buffer to a file descriptor.
72 * \param fd The file descriptor to write to.
73 * \param buf The buffer to write.
74 * \param size The length of \a buf in bytes.
76 * This function writes the given buffer and continues on short writes and
77 * when interrupted by a signal.
79 * \return Positive on success, negative on errors. Possible errors: any
80 * errors returned by para_write().
84 ssize_t para_write_all(int fd, const void *buf, size_t size)
86 // PARA_DEBUG_LOG("writing %zu bytes\n", size);
89 ssize_t ret = para_write(fd, b, size);
90 // PARA_DEBUG_LOG("ret: %zd\n", ret);
99 * Open a file, write the given buffer and close the file.
101 * \param filename Full path to the file to open.
102 * \param buf The buffer to write to the file.
103 * \param size The size of \a buf.
105 * \return Positive on success, negative on errors. Possible errors include:
106 * any errors from para_open() or para_write().
108 * \sa para_open(), para_write().
110 int para_write_file(const char *filename, const void *buf, size_t size)
114 ret = para_open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
118 ret = para_write_all(fd, buf, size);
127 static int append_file(const char *filename, char *header, size_t header_size,
128 char *data, size_t data_size, uint32_t *new_pos)
132 // PARA_DEBUG_LOG("appending %zu + %zu bytes\n", header_size, data_size);
133 ret = para_open(filename, O_WRONLY | O_CREAT | O_APPEND, 0644);
137 if (header && header_size) {
138 ret = para_write_all(fd, header, header_size);
142 ret = para_write_all(fd, data, data_size);
147 ret = para_lseek(fd, &offset, SEEK_END);
150 // PARA_DEBUG_LOG("new file size: " FMT_OFF_T "\n", offset);
160 * Traverse the given directory recursively.
162 * \param dirname The directory to traverse.
163 * \param func The function to call for each entry.
164 * \param private_data Pointer to an arbitrary data structure.
166 * For each regular file under \a dirname, the supplied function \a func is
167 * called. The full path of the regular file and the \a private_data pointer
168 * are passed to \a func. Directories for which the calling process has no
169 * permissions to change to are silently ignored.
171 * \return On success, 1 is returned. Otherwise, this function returns a
172 * negative value which indicates the kind of the error.
174 int for_each_file_in_dir(const char *dirname,
175 int (*func)(const char *, const void *), const void *private_data)
178 struct dirent *entry;
179 int cwd_fd, ret2, ret = para_opendir(dirname, &dir, &cwd_fd);
182 return ret == -ERRNO_TO_PARA_ERROR(EACCES)? 1 : ret;
183 /* scan cwd recursively */
184 while ((entry = readdir(dir))) {
189 if (!strcmp(entry->d_name, "."))
191 if (!strcmp(entry->d_name, ".."))
193 if (lstat(entry->d_name, &s) == -1)
196 if (!S_ISREG(m) && !S_ISDIR(m))
198 tmp = make_message("%s/%s", dirname, entry->d_name);
200 ret = func(tmp, private_data);
207 ret = for_each_file_in_dir(tmp, func, private_data);
215 ret2 = para_fchdir(cwd_fd);
216 if (ret2 < 0 && ret >= 0)
222 static int verify_name(const char *name)
228 if (strchr(name, '/'))
230 if (!strcmp(name, ".."))
232 if (!strcmp(name, "."))
238 * Compare two osl objects pointing to unsigned integers of 32 bit size.
240 * \param obj1 Pointer to the first integer.
241 * \param obj2 Pointer to the second integer.
243 * \return The values required for an osl compare function.
245 * \sa osl_compare_func, osl_hash_compare().
247 int uint32_compare(const struct osl_object *obj1, const struct osl_object *obj2)
249 uint32_t d1 = read_u32((const char *)obj1->data);
250 uint32_t d2 = read_u32((const char *)obj2->data);
260 * Compare two osl objects pointing to hash values.
262 * \param obj1 Pointer to the first hash object.
263 * \param obj2 Pointer to the second hash object.
265 * \return The values required for an osl compare function.
267 * \sa osl_compare_func, uint32_compare().
269 int osl_hash_compare(const struct osl_object *obj1, const struct osl_object *obj2)
271 return hash_compare((HASH_TYPE *)obj1->data, (HASH_TYPE *)obj2->data);
274 static char *disk_storage_dirname(const struct osl_table *t, unsigned col_num,
277 char *dirname, *column_name = column_filename(t, col_num);
279 if (!(t->desc->flags & OSL_LARGE_TABLE))
281 dirname = make_message("%s/%.2s", column_name, ds_name);
286 static char *disk_storage_name_of_object(const struct osl_table *t,
287 const struct osl_object *obj)
289 HASH_TYPE hash[HASH_SIZE];
290 hash_object(obj, hash);
291 return disk_storage_name_of_hash(t, hash);
294 static int disk_storage_name_of_row(const struct osl_table *t,
295 const struct osl_row *row, char **name)
297 struct osl_object obj;
298 int ret = osl_get_object(t, row, t->disk_storage_name_column, &obj);
302 *name = disk_storage_name_of_object(t, &obj);
306 static void column_name_hash(const char *col_name, HASH_TYPE *hash)
308 return hash_function(col_name, strlen(col_name), hash);
311 static int init_column_descriptions(struct osl_table *t)
314 const struct osl_column_description *cd;
316 ret = -E_BAD_TABLE_DESC;
317 ret = verify_name(t->desc->name);
321 if (!t->desc->dir && (t->num_disk_storage_columns || t->num_mapped_columns))
323 /* the size of the index header without column descriptions */
324 t->index_header_size = IDX_COLUMN_DESCRIPTIONS;
325 FOR_EACH_COLUMN(i, t->desc, cd) {
326 struct osl_column *col = t->columns + i;
327 if (cd->storage_flags & OSL_RBTREE) {
328 if (!cd->compare_function)
329 return -E_NO_COMPARE_FUNC;
331 if (cd->storage_type == OSL_NO_STORAGE)
333 ret = -E_NO_COLUMN_NAME;
334 if (!cd->name || !cd->name[0])
336 ret = verify_name(cd->name);
339 t->index_header_size += index_column_description_size(cd->name);
340 column_name_hash(cd->name, col->name_hash);
341 ret = -E_DUPLICATE_COL_NAME;
342 for (j = i + 1; j < t->desc->num_columns; j++) {
343 const char *name2 = get_column_description(t->desc,
345 if (cd->name && name2 && !strcmp(cd->name, name2))
355 * Initialize a struct table from given table description.
357 * \param desc The description of the osl table.
358 * \param table_ptr Result is returned here.
360 * This function performs several sanity checks on \p desc and returns if any
361 * of these tests fail. On success, a struct \p osl_table is allocated and
362 * initialized with data derived from \p desc.
364 * \return Positive on success, negative on errors. Possible errors include: \p
365 * E_BAD_TABLE_DESC, \p E_NO_COLUMN_DESC, \p E_NO_COLUMNS, \p
366 * E_BAD_STORAGE_TYPE, \p E_BAD_STORAGE_FLAGS, \p E_BAD_STORAGE_SIZE, \p
367 * E_NO_UNIQUE_RBTREE_COLUMN, \p E_NO_RBTREE_COL.
369 * \sa struct osl_table.
371 int init_table_structure(const struct osl_table_description *desc,
372 struct osl_table **table_ptr)
374 const struct osl_column_description *cd;
375 struct osl_table *t = para_calloc(sizeof(*t));
376 int i, ret = -E_BAD_TABLE_DESC, have_disk_storage_name_column = 0;
380 PARA_DEBUG_LOG("creating table structure for '%s' from table "
381 "description\n", desc->name);
382 ret = -E_NO_COLUMN_DESC;
383 if (!desc->column_descriptions)
386 if (!desc->num_columns)
388 t->columns = para_calloc(desc->num_columns * sizeof(struct osl_column));
390 FOR_EACH_COLUMN(i, t->desc, cd) {
391 enum osl_storage_type st = cd->storage_type;
392 enum osl_storage_flags sf = cd->storage_flags;
393 struct osl_column *col = &t->columns[i];
395 ret = -E_BAD_STORAGE_TYPE;
396 if (st != OSL_MAPPED_STORAGE && st != OSL_DISK_STORAGE
397 && st != OSL_NO_STORAGE)
399 ret = -E_BAD_STORAGE_FLAGS;
400 if (st == OSL_DISK_STORAGE && sf & OSL_RBTREE)
402 ret = -E_BAD_STORAGE_SIZE;
403 if (sf & OSL_FIXED_SIZE && !cd->data_size)
406 case OSL_DISK_STORAGE:
407 t->num_disk_storage_columns++;
409 case OSL_MAPPED_STORAGE:
410 t->num_mapped_columns++;
411 col->index_offset = t->row_index_size;
412 t->row_index_size += 8;
415 col->volatile_num = t->num_volatile_columns;
416 t->num_volatile_columns++;
419 if (sf & OSL_RBTREE) {
420 col->rbtree_num = t->num_rbtrees;
422 if ((sf & OSL_UNIQUE) && (st == OSL_MAPPED_STORAGE)) {
423 if (!have_disk_storage_name_column)
424 t->disk_storage_name_column = i;
425 have_disk_storage_name_column = 1;
429 ret = -E_NO_UNIQUE_RBTREE_COLUMN;
430 if (t->num_disk_storage_columns && !have_disk_storage_name_column)
432 ret = -E_NO_RBTREE_COL;
436 PARA_DEBUG_LOG("OK. Index entry size: %u\n", t->row_index_size);
437 ret = init_column_descriptions(t);
449 * Read the table description from index header.
451 * \param map The memory mapping of the index file.
452 * \param desc The values found in the index header are returned here.
454 * Read the index header, check for the paraslash magic string and the table version number.
455 * Read all information stored in the index header into \a desc.
457 * \return Positive on success, negative on errors.
459 * \sa struct osl_table_description, osl_create_table.
461 int read_table_desc(struct osl_object *map, struct osl_table_description *desc)
463 char *buf = map->data;
465 uint16_t header_size;
468 struct osl_column_description *cd;
470 if (map->size < MIN_INDEX_HEADER_SIZE(1))
471 return -E_SHORT_TABLE;
472 if (strncmp(buf + IDX_PARA_MAGIC, PARA_MAGIC, strlen(PARA_MAGIC)))
474 version = read_u8(buf + IDX_VERSION);
475 if (version < MIN_TABLE_VERSION || version > MAX_TABLE_VERSION)
476 return -E_VERSION_MISMATCH;
477 desc->num_columns = read_u8(buf + IDX_TABLE_FLAGS);
478 desc->flags = read_u8(buf + IDX_TABLE_FLAGS);
479 desc->num_columns = read_u16(buf + IDX_NUM_COLUMNS);
480 PARA_DEBUG_LOG("%u columns\n", desc->num_columns);
481 if (!desc->num_columns)
482 return -E_NO_COLUMNS;
483 header_size = read_u16(buf + IDX_HEADER_SIZE);
484 if (map->size < header_size)
486 desc->column_descriptions = para_calloc(desc->num_columns
487 * sizeof(struct osl_column_description));
488 offset = IDX_COLUMN_DESCRIPTIONS;
489 FOR_EACH_COLUMN(i, desc, cd) {
492 ret = -E_SHORT_TABLE;
493 if (map->size < offset + MIN_IDX_COLUMN_DESCRIPTION_SIZE) {
494 PARA_ERROR_LOG("map size = %zu < %u = offset + min desc size\n",
495 map->size, offset + MIN_IDX_COLUMN_DESCRIPTION_SIZE);
498 cd->storage_type = read_u16(buf + offset + IDX_CD_STORAGE_TYPE);
499 cd->storage_flags = read_u16(buf + offset +
500 IDX_CD_STORAGE_FLAGS);
501 cd->data_size = read_u32(buf + offset + IDX_CD_DATA_SIZE);
502 null_byte = memchr(buf + offset + IDX_CD_NAME, '\0',
503 map->size - offset - IDX_CD_NAME);
504 ret = -E_INDEX_CORRUPTION;
507 cd->name = para_strdup(buf + offset + IDX_CD_NAME);
508 offset += index_column_description_size(cd->name);
510 if (offset != header_size) {
511 ret = -E_INDEX_CORRUPTION;
512 PARA_ERROR_LOG("real header size = %u != %u = stored header size\n",
513 offset, header_size);
518 FOR_EACH_COLUMN(i, desc, cd)
524 * check whether the table description given by \p t->desc matches the on-disk
525 * table structure stored in the index of \a t.
527 static int compare_table_descriptions(struct osl_table *t)
530 struct osl_table_description desc;
531 const struct osl_column_description *cd1, *cd2;
533 /* read the on-disk structure into desc */
534 ret = read_table_desc(&t->index_map, &desc);
537 ret = -E_BAD_TABLE_FLAGS;
538 if (desc.flags != t->desc->flags)
540 ret = -E_BAD_COLUMN_NUM;
541 if (desc.num_columns != t->desc->num_columns)
543 FOR_EACH_COLUMN(i, t->desc, cd1) {
544 cd2 = get_column_description(&desc, i);
545 ret = -E_BAD_STORAGE_TYPE;
546 if (cd1->storage_type != cd2->storage_type)
548 ret = -E_BAD_STORAGE_FLAGS;
549 if (cd1->storage_flags != cd2->storage_flags) {
550 PARA_ERROR_LOG("sf1 = %u != %u = sf2\n",
551 cd1->storage_flags, cd2->storage_flags);
554 ret = -E_BAD_DATA_SIZE;
555 if (cd1->storage_flags & OSL_FIXED_SIZE)
556 if (cd1->data_size != cd2->data_size)
558 ret = -E_BAD_COLUMN_NAME;
559 if (strcmp(cd1->name, cd2->name))
562 PARA_DEBUG_LOG("table description of '%s' matches on-disk data, good\n",
566 FOR_EACH_COLUMN(i, &desc, cd1)
568 free(desc.column_descriptions);
572 static int create_table_index(struct osl_table *t)
574 char *buf, *filename;
576 size_t size = t->index_header_size;
577 const struct osl_column_description *cd;
580 PARA_INFO_LOG("creating %zu byte index for table %s\n", size,
582 buf = para_calloc(size);
583 sprintf(buf + IDX_PARA_MAGIC, "%s", PARA_MAGIC);
584 write_u8(buf + IDX_TABLE_FLAGS, t->desc->flags);
585 write_u8(buf + IDX_DIRTY_FLAG, 0);
586 write_u8(buf + IDX_VERSION, CURRENT_TABLE_VERSION);
587 write_u16(buf + IDX_NUM_COLUMNS, t->desc->num_columns);
588 write_u16(buf + IDX_HEADER_SIZE, t->index_header_size);
589 offset = IDX_COLUMN_DESCRIPTIONS;
590 FOR_EACH_COLUMN(i, t->desc, cd) {
591 write_u16(buf + offset + IDX_CD_STORAGE_TYPE,
593 write_u16(buf + offset + IDX_CD_STORAGE_FLAGS,
595 if (cd->storage_flags & OSL_FIXED_SIZE)
596 write_u32(buf + offset + IDX_CD_DATA_SIZE,
598 strcpy(buf + offset + IDX_CD_NAME, cd->name);
599 offset += index_column_description_size(cd->name);
601 assert(offset = size);
602 filename = index_filename(t->desc);
603 ret = para_write_file(filename, buf, size);
610 * Create a new osl table.
612 * \param desc Pointer to the table description.
616 int osl_create_table(const struct osl_table_description *desc)
618 const struct osl_column_description *cd;
619 char *table_dir = NULL, *filename;
621 int i, ret = init_table_structure(desc, &t);
625 PARA_INFO_LOG("creating %s\n", desc->name);
626 FOR_EACH_COLUMN(i, t->desc, cd) {
627 if (cd->storage_type == OSL_NO_STORAGE)
630 ret = para_mkdir(desc->dir, 0777);
631 if (ret < 0 && !is_errno(-ret, EEXIST))
633 table_dir = make_message("%s/%s", desc->dir,
635 ret = para_mkdir(table_dir, 0777);
639 filename = column_filename(t, i);
640 PARA_INFO_LOG("filename: %s\n", filename);
641 if (cd->storage_type == OSL_MAPPED_STORAGE) {
642 ret = para_open(filename, O_RDWR | O_CREAT | O_EXCL,
651 ret = para_mkdir(filename, 0777);
656 if (t->num_mapped_columns) {
657 ret = create_table_index(t);
669 static int table_is_dirty(struct osl_table *t)
671 char *buf = (char *)t->index_map.data + IDX_DIRTY_FLAG;
672 uint8_t dirty = read_u8(buf) & 0x1;
676 static void mark_table_dirty(struct osl_table *t)
678 char *buf = (char *)t->index_map.data + IDX_DIRTY_FLAG;
679 write_u8(buf, read_u8(buf) | 1);
682 static void mark_table_clean(struct osl_table *t)
684 char *buf = (char *)t->index_map.data + IDX_DIRTY_FLAG;
685 write_u8(buf, read_u8(buf) & 0xfe);
688 static void unmap_column(struct osl_table *t, unsigned col_num)
690 struct osl_object map = t->columns[col_num].data_map;
694 ret = para_munmap(map.data, map.size);
700 * Unmap all mapped files of an osl table.
702 * \param t Pointer to a mapped table.
703 * \param flags Options for unmapping.
705 * \return Positive on success, negative on errors.
707 * \sa map_table(), enum osl_close_flags, para_munmap().
709 int unmap_table(struct osl_table *t, enum osl_close_flags flags)
712 const struct osl_column_description *cd;
715 if (!t->num_mapped_columns) /* can this ever happen? */
717 PARA_DEBUG_LOG("unmapping table '%s'\n", t->desc->name);
718 if (!t->index_map.data)
719 return -E_NOT_MAPPED;
720 if (flags & OSL_MARK_CLEAN)
722 ret = para_munmap(t->index_map.data, t->index_map.size);
725 t->index_map.data = NULL;
728 FOR_EACH_MAPPED_COLUMN(i, t, cd)
733 static int map_column(struct osl_table *t, unsigned col_num)
736 char *filename = column_filename(t, col_num);
738 if (stat(filename, &statbuf) < 0) {
742 if (!(S_IFREG & statbuf.st_mode)) {
746 ret = mmap_full_file(filename, O_RDWR,
747 &t->columns[col_num].data_map.data,
748 &t->columns[col_num].data_map.size,
755 * Map the index file and all columns of type \p OSL_MAPPED_STORAGE into memory.
757 * \param t Pointer to an initialized table structure.
758 * \param flags Mapping options.
760 * \return Negative return value on errors; on success the number of rows
761 * (including invalid rows) is returned.
763 * \sa unmap_table(), enum map_table_flags, osl_open_table(), mmap(2).
765 int map_table(struct osl_table *t, enum map_table_flags flags)
768 const struct osl_column_description *cd;
769 int i = 0, ret, num_rows = 0;
771 if (!t->num_mapped_columns)
773 if (t->index_map.data)
774 return -E_ALREADY_MAPPED;
775 filename = index_filename(t->desc);
776 PARA_DEBUG_LOG("mapping table '%s' (index: %s)\n", t->desc->name, filename);
777 ret = mmap_full_file(filename, flags & MAP_TBL_FL_MAP_RDONLY?
778 O_RDONLY : O_RDWR, &t->index_map.data, &t->index_map.size, NULL);
782 if (flags & MAP_TBL_FL_VERIFY_INDEX) {
783 ret = compare_table_descriptions(t);
788 if (!(flags & MAP_TBL_FL_IGNORE_DIRTY)) {
789 if (table_is_dirty(t)) {
790 PARA_ERROR_LOG("%s is dirty\n", t->desc->name);
795 num_rows = table_num_rows(t);
799 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
800 ret = map_column(t, i);
805 err: /* unmap what is already mapped */
806 for (i--; i >= 0; i--) {
807 struct osl_object map = t->columns[i].data_map;
808 para_munmap(map.data, map.size);
811 para_munmap(t->index_map.data, t->index_map.size);
812 t->index_map.data = NULL;
817 * Retrieve a mapped object by row and column number.
819 * \param t Pointer to an open osl table.
820 * \param col_num Number of the mapped column containing the object to retrieve.
821 * \param row_num Number of the row containing the object to retrieve.
822 * \param obj The result is returned here.
824 * It is considered an error if \a col_num does not refer to a column
825 * of storage type \p OSL_MAPPED_STORAGE.
827 * \return Positive on success, negative on errors. Possible errors include:
828 * \p E_BAD_ROW_NUM, \p E_INVALID_OBJECT.
830 * \sa osl_storage_type.
832 int get_mapped_object(const struct osl_table *t, unsigned col_num,
833 uint32_t row_num, struct osl_object *obj)
835 struct osl_column *col = &t->columns[col_num];
841 if (t->num_rows <= row_num)
842 return -E_BAD_ROW_NUM;
843 ret = get_cell_index(t, row_num, col_num, &cell_index);
846 offset = read_u32(cell_index);
847 obj->size = read_u32(cell_index + 4) - 1;
848 header = col->data_map.data + offset;
849 obj->data = header + 1;
850 if (read_u8(header) == 0xff) {
851 PARA_ERROR_LOG("col %u, size %zu, offset %u\n", col_num,
853 return -E_INVALID_OBJECT;
858 static int search_rbtree(const struct osl_object *obj,
859 const struct osl_table *t, unsigned col_num,
860 struct rb_node **result, struct rb_node ***rb_link)
862 struct osl_column *col = &t->columns[col_num];
863 struct rb_node **new = &col->rbtree.rb_node, *parent = NULL;
864 const struct osl_column_description *cd =
865 get_column_description(t->desc, col_num);
866 enum osl_storage_type st = cd->storage_type;
868 struct osl_row *this_row = get_row_pointer(*new,
871 struct osl_object this_obj;
873 if (st == OSL_MAPPED_STORAGE) {
874 ret = get_mapped_object(t, col_num, this_row->num,
879 this_obj = this_row->volatile_objects[col->volatile_num];
880 ret = cd->compare_function(obj, &this_obj);
883 *result = get_rb_node_pointer(this_row,
888 new = &((*new)->rb_left);
890 new = &((*new)->rb_right);
896 return -E_RB_KEY_NOT_FOUND;
899 static int insert_rbtree(struct osl_table *t, unsigned col_num,
900 const struct osl_row *row, const struct osl_object *obj)
902 struct rb_node *parent, **rb_link;
905 int ret = search_rbtree(obj, t, col_num, &parent, &rb_link);
908 return -E_RB_KEY_EXISTS;
909 rbtree_num = t->columns[col_num].rbtree_num;
910 n = get_rb_node_pointer(row, rbtree_num);
911 rb_link_node(n, parent, rb_link);
912 rb_insert_color(n, &t->columns[col_num].rbtree);
916 static void remove_rb_node(struct osl_table *t, unsigned col_num,
917 const struct osl_row *row)
919 struct osl_column *col = &t->columns[col_num];
920 const struct osl_column_description *cd =
921 get_column_description(t->desc, col_num);
922 enum osl_storage_flags sf = cd->storage_flags;
923 struct rb_node *victim, *splice_out_node, *tmp;
924 if (!(sf & OSL_RBTREE))
927 * Which node is removed/spliced out actually depends on how many
928 * children the victim node has: If it has no children, it gets
929 * deleted. If it has one child, it gets spliced out. If it has two
930 * children, its successor (which has at most a right child) gets
933 victim = get_rb_node_pointer(row, col->rbtree_num);
934 if (victim->rb_left && victim->rb_right)
935 splice_out_node = rb_next(victim);
937 splice_out_node = victim;
938 /* Go up to the root and decrement the size of each node in the path. */
939 for (tmp = splice_out_node; tmp; tmp = rb_parent(tmp))
941 rb_erase(victim, &col->rbtree);
944 static int add_row_to_rbtrees(struct osl_table *t, uint32_t row_num,
945 struct osl_object *volatile_objs, struct osl_row **row_ptr)
949 struct osl_row *row = allocate_row(t->num_rbtrees);
950 const struct osl_column_description *cd;
953 row->volatile_objects = volatile_objs;
954 FOR_EACH_RBTREE_COLUMN(i, t, cd) {
955 if (cd->storage_type == OSL_MAPPED_STORAGE) {
956 struct osl_object obj;
957 ret = get_mapped_object(t, i, row_num, &obj);
960 ret = insert_rbtree(t, i, row, &obj);
961 } else { /* volatile */
962 const struct osl_object *obj
963 = volatile_objs + t->columns[i].volatile_num;
964 ret = insert_rbtree(t, i, row, obj);
972 err: /* rollback changes, i.e. remove added entries from rbtrees */
974 remove_rb_node(t, i--, row);
979 static void free_volatile_objects(const struct osl_table *t,
980 enum osl_close_flags flags)
984 struct osl_column *rb_col;
985 const struct osl_column_description *cd;
987 if (!t->num_volatile_columns)
989 /* find the first rbtree column (any will do) */
990 FOR_EACH_RBTREE_COLUMN(i, t, cd)
992 rb_col = t->columns + i;
993 /* walk that rbtree and free all volatile objects */
994 for (n = rb_first(&rb_col->rbtree); n; n = rb_next(n)) {
995 struct osl_row *r = get_row_pointer(n, rb_col->rbtree_num);
996 if (flags & OSL_FREE_VOLATILE)
997 FOR_EACH_VOLATILE_COLUMN(j, t, cd) {
998 if (cd->storage_flags & OSL_DONT_FREE)
1000 free(r->volatile_objects[
1001 t->columns[j].volatile_num].data);
1003 // for (j = 0; j < t->num_volatile_columns; j++)
1004 // free(r->volatile_objects[j].data);
1005 free(r->volatile_objects);
1010 * Erase all rbtree nodes and free resources.
1012 * \param t Pointer to an open osl table.
1014 * This function is called by osl_close_table().
1016 void clear_rbtrees(struct osl_table *t)
1018 const struct osl_column_description *cd;
1019 unsigned i, rbtrees_cleared = 0;
1021 FOR_EACH_RBTREE_COLUMN(i, t, cd) {
1022 struct osl_column *col = &t->columns[i];
1025 for (n = rb_first(&col->rbtree); n;) {
1027 rb_erase(n, &col->rbtree);
1028 if (rbtrees_cleared == t->num_rbtrees) {
1029 r = get_row_pointer(n, col->rbtree_num);
1040 * Close an osl table.
1042 * \param t Pointer to the table to be closed.
1043 * \param flags Options for what should be cleaned up.
1045 * If osl_open_table() succeeds, the resulting table pointer must later be
1046 * passed to this function in order to flush all changes to the file system and
1047 * to free the resources that were allocated by osl_open_table().
1049 * \return Positive on success, negative on errors. Possible errors: \p E_BAD_TABLE,
1050 * errors returned by unmap_table().
1052 * \sa osl_open_table(), unmap_table().
1054 int osl_close_table(struct osl_table *t, enum osl_close_flags flags)
1059 return -E_BAD_TABLE;
1060 free_volatile_objects(t, flags);
1062 ret = unmap_table(t, flags);
1064 PARA_ERROR_LOG("unmap_table failed: %d\n", ret);
1071 * Find out whether the given row number corresponds to an invalid row.
1073 * \param t Pointer to the osl table.
1074 * \param row_num The number of the row in question.
1076 * By definition, a row is considered invalid if all its index entries
1079 * \return Positive if \a row_num corresponds to an invalid row,
1080 * zero if it corresponds to a valid row, negative on errors.
1082 int row_is_invalid(struct osl_table *t, uint32_t row_num)
1085 int i, ret = get_row_index(t, row_num, &row_index);
1089 for (i = 0; i < t->row_index_size; i++) {
1090 if ((unsigned char)row_index[i] != 0xff)
1093 PARA_INFO_LOG("row %d is invalid\n", row_num);
1098 * Invalidate a row of an osl table.
1100 * \param t Pointer to an open osl table.
1101 * \param row_num Number of the row to mark as invalid.
1103 * This function marks each mapped object in the index entry of \a row as
1106 * \return Positive on success, negative on errors.
1108 int mark_row_invalid(struct osl_table *t, uint32_t row_num)
1111 int ret = get_row_index(t, row_num, &row_index);
1115 PARA_INFO_LOG("marking row %d as invalid\n", row_num);
1116 memset(row_index, 0xff, t->row_index_size);
1121 * Initialize all rbtrees and compute number of invalid rows.
1123 * \param t The table containing the rbtrees to be initialized.
1125 * \return Positive on success, negative on errors.
1127 int init_rbtrees(struct osl_table *t)
1130 const struct osl_column_description *cd;
1132 /* create rbtrees */
1133 FOR_EACH_RBTREE_COLUMN(i, t, cd)
1134 t->columns[i].rbtree = RB_ROOT;
1135 /* add valid rows to rbtrees */
1136 t->num_invalid_rows = 0;
1137 for (i = 0; i < t->num_rows; i++) {
1138 ret = row_is_invalid(t, i);
1142 t->num_invalid_rows++;
1145 ret = add_row_to_rbtrees(t, i, NULL, NULL);
1153 * Open an osl table.
1155 * Each osl table must be opened before its data can be accessed.
1157 * \param table_desc Describes the table to be opened.
1158 * \param result Contains a pointer to the open table on success.
1160 * The table description given by \a desc should coincide with the
1161 * description used at creation time.
1165 int osl_open_table(const struct osl_table_description *table_desc,
1166 struct osl_table **result)
1169 struct osl_table *t;
1170 const struct osl_column_description *cd;
1172 PARA_INFO_LOG("opening table %s\n", table_desc->name);
1173 ret = init_table_structure(table_desc, &t);
1176 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
1177 /* check if directory exists */
1178 char *dirname = column_filename(t, i);
1179 struct stat statbuf;
1180 ret = stat(dirname, &statbuf);
1183 ret = -ERRNO_TO_PARA_ERROR(errno);
1186 ret = -ERRNO_TO_PARA_ERROR(ENOTDIR);
1187 if (!S_ISDIR(statbuf.st_mode))
1190 ret = map_table(t, MAP_TBL_FL_VERIFY_INDEX);
1194 PARA_DEBUG_LOG("num rows: %d\n", t->num_rows);
1195 ret = init_rbtrees(t);
1197 osl_close_table(t, OSL_MARK_CLEAN); /* ignore further errors */
1208 static int create_disk_storage_object_dir(const struct osl_table *t,
1209 unsigned col_num, const char *ds_name)
1214 if (!(t->desc->flags & OSL_LARGE_TABLE))
1216 dirname = disk_storage_dirname(t, col_num, ds_name);
1217 ret = para_mkdir(dirname, 0777);
1219 if (ret < 0 && !is_errno(-ret, EEXIST))
1224 static int write_disk_storage_file(const struct osl_table *t, unsigned col_num,
1225 const struct osl_object *obj, const char *ds_name)
1230 ret = create_disk_storage_object_dir(t, col_num, ds_name);
1233 filename = disk_storage_path(t, col_num, ds_name);
1234 ret = para_write_file(filename, obj->data, obj->size);
1239 static int append_map_file(const struct osl_table *t, unsigned col_num,
1240 const struct osl_object *obj, uint32_t *new_size)
1242 char *filename = column_filename(t, col_num);
1244 char header = 0; /* zero means valid object */
1246 // PARA_DEBUG_LOG("appending %zu + 1 byte\n", obj->size);
1247 ret = append_file(filename, &header, 1, obj->data, obj->size,
1253 static int append_row_index(const struct osl_table *t, char *row_index)
1258 if (!t->num_mapped_columns)
1260 filename = index_filename(t->desc);
1261 ret = append_file(filename, NULL, 0, row_index,
1262 t->row_index_size, NULL);
1268 * A wrapper for truncate(2)
1270 * \param path Name of the regular file to truncate
1271 * \param size Number of bytes to \b shave \b off
1273 * Truncate the regular file named by \a path by \a size bytes.
1275 * \return Positive on success, negative on errors. Possible errors include: \p
1276 * E_STAT, \p E_BAD_SIZE, \p E_TRUNC.
1280 int para_truncate(const char *path, off_t size)
1283 struct stat statbuf;
1286 if (stat(path, &statbuf) < 0)
1289 if (statbuf.st_size < size)
1292 if (truncate(path, statbuf.st_size - size) < 0)
1299 static int truncate_mapped_file(const struct osl_table *t, unsigned col_num,
1302 char *filename = column_filename(t, col_num);
1303 int ret = para_truncate(filename, size);
1308 static int delete_disk_storage_file(const struct osl_table *t, unsigned col_num,
1309 const char *ds_name)
1311 char *dirname, *filename = disk_storage_path(t, col_num, ds_name);
1312 int ret = unlink(filename), err = errno;
1316 return -ERRNO_TO_PARA_ERROR(err);
1317 if (!(t->desc->flags & OSL_LARGE_TABLE))
1319 dirname = disk_storage_dirname(t, col_num, ds_name);
1326 * Add a new row to an osl table and retrieve this row.
1328 * \param t Pointer to an open osl table.
1329 * \param objects Array of objects to be added.
1330 * \param row Result pointer.
1332 * The \a objects parameter must point to an array containing one object per
1333 * column. The order of the objects in the array is given by the table
1334 * description of \a table. Several sanity checks are performed during object
1335 * insertion and the function returns without modifying the table if any of
1336 * these tests fail. In fact, it is atomic in the sense that it either
1337 * succeeds or leaves the table unchanged (i.e. either all or none of the
1338 * objects are added to the table).
1340 * It is considered an error if an object is added to a column with associated
1341 * rbtree if this object is equal to an object already contained in that column
1342 * (i.e. the compare function for the column's rbtree returns zero).
1344 * Possible errors include: \p E_RB_KEY_EXISTS, \p E_BAD_DATA_SIZE.
1346 * \return Positive on success, negative on errors.
1348 * \sa struct osl_table_description, osl_compare_func, osl_add_row().
1350 int osl_add_and_get_row(struct osl_table *t, struct osl_object *objects,
1351 struct osl_row **row)
1354 char *ds_name = NULL;
1355 struct rb_node **rb_parents = NULL, ***rb_links = NULL;
1356 char *new_row_index = NULL;
1357 struct osl_object *volatile_objs = NULL;
1358 const struct osl_column_description *cd;
1361 return -E_BAD_TABLE;
1362 rb_parents = para_malloc(t->num_rbtrees * sizeof(struct rn_node*));
1363 rb_links = para_malloc(t->num_rbtrees * sizeof(struct rn_node**));
1364 if (t->num_mapped_columns)
1365 new_row_index = para_malloc(t->row_index_size);
1366 /* pass 1: sanity checks */
1367 // PARA_DEBUG_LOG("sanity tests: %p:%p\n", objects[0].data,
1368 // objects[1].data);
1369 FOR_EACH_COLUMN(i, t->desc, cd) {
1370 enum osl_storage_type st = cd->storage_type;
1371 enum osl_storage_flags sf = cd->storage_flags;
1373 // ret = -E_NULL_OBJECT;
1376 if (st == OSL_DISK_STORAGE)
1378 if (sf & OSL_RBTREE) {
1379 unsigned rbtree_num = t->columns[i].rbtree_num;
1380 ret = -E_RB_KEY_EXISTS;
1381 // PARA_DEBUG_LOG("checking whether %p exists\n",
1382 // objects[i].data);
1383 if (search_rbtree(objects + i, t, i,
1384 &rb_parents[rbtree_num],
1385 &rb_links[rbtree_num]) > 0)
1388 if (sf & OSL_FIXED_SIZE) {
1389 // PARA_DEBUG_LOG("fixed size. need: %zu, have: %d\n",
1390 // objects[i].size, cd->data_size);
1391 ret = -E_BAD_DATA_SIZE;
1392 if (objects[i].size != cd->data_size)
1396 if (t->num_disk_storage_columns)
1397 ds_name = disk_storage_name_of_object(t,
1398 &objects[t->disk_storage_name_column]);
1399 ret = unmap_table(t, OSL_MARK_CLEAN);
1402 // PARA_DEBUG_LOG("sanity tests passed%s\n", "");
1403 /* pass 2: create data files, append map data */
1404 FOR_EACH_COLUMN(i, t->desc, cd) {
1405 enum osl_storage_type st = cd->storage_type;
1406 if (st == OSL_NO_STORAGE)
1408 if (st == OSL_MAPPED_STORAGE) {
1410 struct osl_column *col = &t->columns[i];
1411 // PARA_DEBUG_LOG("appending object of size %zu\n",
1412 // objects[i].size);
1413 ret = append_map_file(t, i, objects + i, &new_size);
1416 update_cell_index(new_row_index, col, new_size,
1421 ret = write_disk_storage_file(t, i, objects + i, ds_name);
1425 ret = append_row_index(t, new_row_index);
1428 ret = map_table(t, MAP_TBL_FL_VERIFY_INDEX);
1429 if (ret < 0) { /* truncate index and rollback changes */
1430 char *filename = index_filename(t->desc);
1431 para_truncate(filename, t->row_index_size);
1435 /* pass 3: add entry to rbtrees */
1436 if (t->num_volatile_columns) {
1437 volatile_objs = para_calloc(t->num_volatile_columns
1438 * sizeof(struct osl_object));
1439 FOR_EACH_VOLATILE_COLUMN(i, t, cd)
1440 volatile_objs[t->columns[i].volatile_num] = objects[i];
1443 // PARA_DEBUG_LOG("adding new entry as row #%d\n", t->num_rows - 1);
1444 ret = add_row_to_rbtrees(t, t->num_rows - 1, volatile_objs, row);
1447 // PARA_DEBUG_LOG("added new entry as row #%d\n", t->num_rows - 1);
1450 rollback: /* rollback all changes made, ignore further errors */
1451 for (i--; i >= 0; i--) {
1452 cd = get_column_description(t->desc, i);
1453 enum osl_storage_type st = cd->storage_type;
1454 if (st == OSL_NO_STORAGE)
1457 if (st == OSL_MAPPED_STORAGE)
1458 truncate_mapped_file(t, i, objects[i].size);
1459 else /* disk storage */
1460 delete_disk_storage_file(t, i, ds_name);
1462 /* ignore error and return previous error value */
1463 map_table(t, MAP_TBL_FL_VERIFY_INDEX);
1465 free(new_row_index);
1473 * Add a new row to an osl table.
1475 * \param t Same meaning as osl_add_and_get_row().
1476 * \param objects Same meaning as osl_add_and_get_row().
1478 * \return The return value of the underlying call to osl_add_and_get_row().
1480 * This is equivalent to osl_add_and_get_row(t, objects, NULL).
1482 int osl_add_row(struct osl_table *t, struct osl_object *objects)
1484 return osl_add_and_get_row(t, objects, NULL);
1488 * Retrieve an object identified by row and column
1490 * \param t Pointer to an open osl table.
1491 * \param r Pointer to the row.
1492 * \param col_num The column number.
1493 * \param object The result pointer.
1495 * The column determined by \a col_num must be of type \p OSL_MAPPED_STORAGE
1496 * or \p OSL_NO_STORAGE, i.e. no disk storage objects may be retrieved by this
1499 * \return Positive if object was found, negative on errors. Possible errors
1500 * include: \p E_BAD_TABLE, \p E_BAD_STORAGE_TYPE.
1502 * \sa osl_storage_type, osl_open_disk_object().
1504 int osl_get_object(const struct osl_table *t, const struct osl_row *r,
1505 unsigned col_num, struct osl_object *object)
1507 const struct osl_column_description *cd;
1510 return -E_BAD_TABLE;
1511 cd = get_column_description(t->desc, col_num);
1512 /* col must not be disk storage */
1513 if (cd->storage_type == OSL_DISK_STORAGE)
1514 return -E_BAD_STORAGE_TYPE;
1515 if (cd->storage_type == OSL_MAPPED_STORAGE)
1516 return get_mapped_object(t, col_num, r->num, object);
1518 *object = r->volatile_objects[t->columns[col_num].volatile_num];
1522 static int mark_mapped_object_invalid(const struct osl_table *t,
1523 uint32_t row_num, unsigned col_num)
1525 struct osl_object obj;
1527 int ret = get_mapped_object(t, col_num, row_num, &obj);
1538 * Delete a row from an osl table.
1540 * \param t Pointer to an open osl table.
1541 * \param row Pointer to the row to delete.
1543 * This removes all disk storage objects, removes all rbtree nodes, and frees
1544 * all volatile objects belonging to the given row. For mapped columns, the
1545 * data is merely marked invalid and may be pruned from time to time by
1548 * \return Positive on success, negative on errors. Possible errors include:
1549 * \p E_BAD_TABLE, errors returned by osl_get_object().
1551 int osl_del_row(struct osl_table *t, struct osl_row *row)
1553 struct osl_row *r = row;
1555 const struct osl_column_description *cd;
1558 return -E_BAD_TABLE;
1559 PARA_INFO_LOG("deleting row %p\n", row);
1561 if (t->num_disk_storage_columns) {
1563 ret = disk_storage_name_of_row(t, r, &ds_name);
1566 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd)
1567 delete_disk_storage_file(t, i, ds_name);
1570 FOR_EACH_COLUMN(i, t->desc, cd) {
1571 struct osl_column *col = t->columns + i;
1572 enum osl_storage_type st = cd->storage_type;
1573 remove_rb_node(t, i, r);
1574 if (st == OSL_MAPPED_STORAGE) {
1575 mark_mapped_object_invalid(t, r->num, i);
1578 if (st == OSL_NO_STORAGE && !(cd->storage_flags & OSL_DONT_FREE))
1579 free(r->volatile_objects[col->volatile_num].data);
1581 if (t->num_mapped_columns) {
1582 ret = mark_row_invalid(t, r->num);
1585 t->num_invalid_rows++;
1590 free(r->volatile_objects);
1595 /* test if column has an rbtree */
1596 static int check_rbtree_col(const struct osl_table *t, unsigned col_num,
1597 struct osl_column **col)
1600 return -E_BAD_TABLE;
1601 if (!(get_column_description(t->desc, col_num)->storage_flags & OSL_RBTREE))
1602 return -E_BAD_STORAGE_FLAGS;
1603 *col = t->columns + col_num;
1608 * Get the row that contains the given object.
1610 * \param t Pointer to an open osl table.
1611 * \param col_num The number of the column to be searched.
1612 * \param obj The object to be looked up.
1613 * \param result Points to the row containing \a obj.
1615 * Lookup \a obj in \a t and return the row containing \a obj. The column
1616 * specified by \a col_num must have an associated rbtree.
1618 * \return Positive on success, negative on errors. If an error occurred, \a
1619 * result is set to \p NULL. Possible errors include: \p E_BAD_TABLE, \p
1620 * E_BAD_STORAGE_FLAGS, errors returned by get_mapped_object(), \p
1621 * E_RB_KEY_NOT_FOUND.
1623 * \sa osl_storage_flags
1625 int osl_get_row(const struct osl_table *t, unsigned col_num,
1626 const struct osl_object *obj, struct osl_row **result)
1629 struct rb_node *node;
1630 struct osl_row *row;
1631 struct osl_column *col;
1634 ret = check_rbtree_col(t, col_num, &col);
1637 ret = search_rbtree(obj, t, col_num, &node, NULL);
1640 row = get_row_pointer(node, t->columns[col_num].rbtree_num);
1645 static int rbtree_loop(struct osl_column *col, void *private_data,
1646 osl_rbtree_loop_func *func)
1648 struct rb_node *n, *tmp;
1650 /* this for-loop is safe against removal of an entry */
1651 for (n = rb_first(&col->rbtree), tmp = n? rb_next(n) : NULL;
1653 n = tmp, tmp = tmp? rb_next(tmp) : NULL) {
1654 struct osl_row *r = get_row_pointer(n, col->rbtree_num);
1655 int ret = func(r, private_data);
1662 static int rbtree_loop_reverse(struct osl_column *col, void *private_data,
1663 osl_rbtree_loop_func *func)
1665 struct rb_node *n, *tmp;
1667 /* safe against removal of an entry */
1668 for (n = rb_last(&col->rbtree), tmp = n? rb_prev(n) : NULL;
1670 n = tmp, tmp = tmp? rb_prev(tmp) : NULL) {
1671 struct osl_row *r = get_row_pointer(n, col->rbtree_num);
1672 int ret = func(r, private_data);
1680 * Loop over all nodes in an rbtree.
1682 * \param t Pointer to an open osl table.
1683 * \param col_num The column to use for iterating over the elements.
1684 * \param private_data Pointer that gets passed to \a func.
1685 * \param func The function to be called for each node in the rbtree.
1687 * This function does an in-order walk of the rbtree associated with \a
1688 * col_num. It is an error if the \p OSL_RBTREE flag is not set for this
1689 * column. For each node in the rbtree, the given function \a func is called
1690 * with two pointers as arguments: The first osl_row* argument points to the
1691 * row that contains the object corresponding to the rbtree node currently
1692 * traversed, and the \a private_data pointer is passed verbatim to \a func as the
1693 * second argument. The loop terminates either if \a func returns a negative
1694 * value, or if all nodes of the tree have been visited.
1697 * \return Positive on success, negative on errors. If the termination of the
1698 * loop was caused by \a func returning a negative value, this value is
1701 * \sa osl_storage_flags, osl_rbtree_loop_reverse(), osl_compare_func.
1703 int osl_rbtree_loop(const struct osl_table *t, unsigned col_num,
1704 void *private_data, osl_rbtree_loop_func *func)
1706 struct osl_column *col;
1708 int ret = check_rbtree_col(t, col_num, &col);
1711 return rbtree_loop(col, private_data, func);
1715 * Loop over all nodes in an rbtree in reverse order.
1717 * \param t Identical meaning as in \p osl_rbtree_loop().
1718 * \param col_num Identical meaning as in \p osl_rbtree_loop().
1719 * \param private_data Identical meaning as in \p osl_rbtree_loop().
1720 * \param func Identical meaning as in \p osl_rbtree_loop().
1722 * This function is identical to \p osl_rbtree_loop(), the only difference
1723 * is that the tree is walked in reverse order.
1725 * \return The same return value as \p osl_rbtree_loop().
1727 * \sa osl_rbtree_loop().
1729 int osl_rbtree_loop_reverse(const struct osl_table *t, unsigned col_num,
1730 void *private_data, osl_rbtree_loop_func *func)
1732 struct osl_column *col;
1734 int ret = check_rbtree_col(t, col_num, &col);
1737 return rbtree_loop_reverse(col, private_data, func);
1740 /* TODO: Rollback changes on errors */
1741 static int rename_disk_storage_objects(struct osl_table *t,
1742 struct osl_object *old_obj, struct osl_object *new_obj)
1745 const struct osl_column_description *cd;
1746 char *old_ds_name, *new_ds_name;
1748 if (!t->num_disk_storage_columns)
1749 return 1; /* nothing to do */
1750 if (old_obj->size == new_obj->size && !memcmp(new_obj->data,
1751 old_obj->data, new_obj->size))
1752 return 1; /* object did not change */
1753 old_ds_name = disk_storage_name_of_object(t, old_obj);
1754 new_ds_name = disk_storage_name_of_object(t, new_obj);
1755 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
1756 char *old_filename, *new_filename;
1757 ret = create_disk_storage_object_dir(t, i, new_ds_name);
1760 old_filename = disk_storage_path(t, i, old_ds_name);
1761 new_filename = disk_storage_path(t, i, new_ds_name);
1762 ret = para_rename(old_filename, new_filename);
1777 * Change an object in an osl table.
1779 * \param t Pointer to an open osl table.
1780 * \param r Pointer to the row containing the object to be updated.
1781 * \param col_num Number of the column containing the object to be updated.
1782 * \param obj Pointer to the replacement object.
1784 * This function gets rid of all references to the old object. This includes
1785 * removal of the rbtree node in case there is an rbtree associated with \a
1786 * col_num. It then inserts \a obj into the table and the rbtree if necessary.
1788 * If the \p OSL_RBTREE flag is set for \a col_num, you \b MUST call this
1789 * function in order to change the contents of an object, even for volatile or
1790 * mapped columns of constant size (which may be updated directly if \p
1791 * OSL_RBTREE is not set). Otherwise the rbtree might become corrupted.
1795 int osl_update_object(struct osl_table *t, const struct osl_row *r,
1796 unsigned col_num, struct osl_object *obj)
1798 struct osl_column *col;
1799 const struct osl_column_description *cd;
1803 return -E_BAD_TABLE;
1804 col = &t->columns[col_num];
1805 cd = get_column_description(t->desc, col_num);
1806 PARA_DEBUG_LOG("updating column %u of %s\n", col_num, t->desc->name);
1807 if (cd->storage_flags & OSL_RBTREE) {
1808 if (search_rbtree(obj, t, col_num, NULL, NULL) > 0)
1809 return -E_RB_KEY_EXISTS;
1811 if (cd->storage_flags & OSL_FIXED_SIZE) {
1812 if (obj->size != cd->data_size)
1813 return -E_BAD_DATA_SIZE;
1815 remove_rb_node(t, col_num, r);
1816 if (cd->storage_type == OSL_NO_STORAGE) { /* TODO: If fixed size, reuse object? */
1817 free(r->volatile_objects[col->volatile_num].data);
1818 r->volatile_objects[col->volatile_num] = *obj;
1819 } else if (cd->storage_type == OSL_DISK_STORAGE) {
1821 ret = disk_storage_name_of_row(t, r, &ds_name);
1824 ret = delete_disk_storage_file(t, col_num, ds_name);
1825 if (ret < 0 && !is_errno(-ret, ENOENT)) {
1829 ret = write_disk_storage_file(t, col_num, obj, ds_name);
1833 } else { /* mapped storage */
1834 struct osl_object old_obj;
1835 ret = get_mapped_object(t, col_num, r->num, &old_obj);
1839 * If the updated column is the disk storage name column, the
1840 * disk storage name changes, so we have to rename all disk
1841 * storage objects accordingly.
1843 if (col_num == t->disk_storage_name_column) {
1844 ret = rename_disk_storage_objects(t, &old_obj, obj);
1848 if (cd->storage_flags & OSL_FIXED_SIZE)
1849 memcpy(old_obj.data, obj->data, cd->data_size);
1850 else { /* TODO: if the size doesn't change, use old space */
1851 uint32_t new_data_map_size;
1853 ret = get_row_index(t, r->num, &row_index);
1856 ret = mark_mapped_object_invalid(t, r->num, col_num);
1859 unmap_column(t, col_num);
1860 ret = append_map_file(t, col_num, obj,
1861 &new_data_map_size);
1864 ret = map_column(t, col_num);
1867 update_cell_index(row_index, col, new_data_map_size,
1871 if (cd->storage_flags & OSL_RBTREE) {
1872 ret = insert_rbtree(t, col_num, r, obj);
1880 * Retrieve an object of type \p OSL_DISK_STORAGE by row and column.
1882 * \param t Pointer to an open osl table.
1883 * \param r Pointer to the row containing the object.
1884 * \param col_num The column number.
1885 * \param obj Points to the result upon successful return.
1887 * For columns of type \p OSL_DISK_STORAGE, this function must be used to
1888 * retrieve one of its containing objects. Afterwards, osl_close_disk_object()
1889 * must be called in order to deallocate the resources.
1891 * \return Positive on success, negative on errors. Possible errors include:
1892 * \p E_BAD_TABLE, \p E_BAD_STORAGE_TYPE, errors returned by osl_get_object().
1894 * \sa osl_get_object(), osl_storage_type, osl_close_disk_object().
1896 int osl_open_disk_object(const struct osl_table *t, const struct osl_row *r,
1897 unsigned col_num, struct osl_object *obj)
1899 const struct osl_column_description *cd;
1900 char *ds_name, *filename;
1904 return -E_BAD_TABLE;
1905 cd = get_column_description(t->desc, col_num);
1906 if (cd->storage_type != OSL_DISK_STORAGE)
1907 return -E_BAD_STORAGE_TYPE;
1909 ret = disk_storage_name_of_row(t, r, &ds_name);
1912 filename = disk_storage_path(t, col_num, ds_name);
1914 PARA_DEBUG_LOG("filename: %s\n", filename);
1915 ret = mmap_full_file(filename, O_RDONLY, &obj->data, &obj->size, NULL);
1921 * Free resources that were allocated during osl_open_disk_object().
1923 * \param obj Pointer to the object previously returned by open_disk_object().
1925 * \return The return value of the underlying call to para_munmap().
1927 * \sa para_munmap().
1929 int osl_close_disk_object(struct osl_object *obj)
1931 return para_munmap(obj->data, obj->size);
1935 * Get the number of rows of the given table.
1937 * \param t Pointer to an open osl table.
1938 * \param num_rows Result is returned here.
1940 * The number of rows returned via \a num_rows excluding any invalid rows.
1942 * \return Positive on success, \p -E_BAD_TABLE if \a t is \p NULL.
1944 int osl_get_num_rows(const struct osl_table *t, unsigned *num_rows)
1947 return -E_BAD_TABLE;
1948 assert(t->num_rows >= t->num_invalid_rows);
1949 *num_rows = t->num_rows - t->num_invalid_rows;
1954 * Get the rank of a row.
1956 * \param t An open osl table.
1957 * \param r The row to get the rank of.
1958 * \param col_num The number of an rbtree column.
1959 * \param rank Result pointer.
1961 * The rank is, by definition, the position of the row in the linear order
1962 * determined by an in-order tree walk of the rbtree associated with column
1963 * number \a col_num of \a table.
1965 * \return Positive on success, negative on errors.
1967 * \sa osl_get_nth_row().
1969 int osl_get_rank(const struct osl_table *t, struct osl_row *r,
1970 unsigned col_num, unsigned *rank)
1972 struct osl_object obj;
1973 struct osl_column *col;
1974 struct rb_node *node;
1975 int ret = check_rbtree_col(t, col_num, &col);
1979 ret = osl_get_object(t, r, col_num, &obj);
1982 ret = search_rbtree(&obj, t, col_num, &node, NULL);
1985 ret = rb_rank(node, rank);
1992 * Get the row with n-th greatest value.
1994 * \param t Pointer to an open osl table.
1995 * \param col_num The column number.
1996 * \param n The rank of the desired row.
1997 * \param result Row is returned here.
1999 * Retrieve the n-th order statistic with respect to the compare function
2000 * of the rbtree column \a col_num. In other words, get that row with
2001 * \a n th greatest value in column \a col_num. It's an error if
2002 * \a col_num is not a rbtree column, or if \a n is larger than the
2003 * number of rows in the table.
2005 * \return Positive on success, negative on errors. Possible errors:
2006 * \p E_BAD_TABLE, \p E_BAD_STORAGE_FLAGS, \p E_RB_KEY_NOT_FOUND.
2008 * \sa osl_storage_flags, osl_compare_func, osl_get_row(),
2009 * osl_rbtree_last_row(), osl_rbtree_first_row(), osl_get_rank().
2011 int osl_get_nth_row(const struct osl_table *t, unsigned col_num,
2012 unsigned n, struct osl_row **result)
2014 struct osl_column *col;
2015 struct rb_node *node;
2020 return -E_RB_KEY_NOT_FOUND;
2021 ret = osl_get_num_rows(t, &num_rows);
2025 return -E_RB_KEY_NOT_FOUND;
2026 ret = check_rbtree_col(t, col_num, &col);
2029 node = rb_nth(col->rbtree.rb_node, n);
2031 return -E_RB_KEY_NOT_FOUND;
2032 *result = get_row_pointer(node, col->rbtree_num);
2037 * Get the row corresponding to the smallest rbtree node of a column.
2039 * \param t An open rbtree table.
2040 * \param col_num The number of the rbtree column.
2041 * \param result A pointer to the first row is returned here.
2043 * The rbtree node of the smallest object (with respect to the corresponding
2044 * compare function) is selected and the row containing this object is
2045 * returned. It is an error if \a col_num refers to a column without an
2046 * associated rbtree.
2048 * \return Positive on success, negative on errors.
2050 * \sa osl_get_nth_row(), osl_rbtree_last_row().
2052 int osl_rbtree_first_row(const struct osl_table *t, unsigned col_num,
2053 struct osl_row **result)
2055 return osl_get_nth_row(t, col_num, 1, result);
2059 * Get the row corresponding to the greatest rbtree node of a column.
2061 * \param t The same meaning as in \p osl_rbtree_first_row().
2062 * \param col_num The same meaning as in \p osl_rbtree_first_row().
2063 * \param result The same meaning as in \p osl_rbtree_first_row().
2065 * This function works just like osl_rbtree_first_row(), the only difference
2066 * is that the row containing the greatest rather than the smallest object is
2069 * \return Positive on success, negative on errors.
2071 * \sa osl_get_nth_row(), osl_rbtree_first_row().
2073 int osl_rbtree_last_row(const struct osl_table *t, unsigned col_num,
2074 struct osl_row **result)
2077 int ret = osl_get_num_rows(t, &num_rows);
2081 return osl_get_nth_row(t, col_num, num_rows, result);