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() */
19 * Allocate a sufficiently large string and print into it.
21 * \param fmt A usual format string.
23 * Produce output according to \p fmt. No artificial bound on the length of the
24 * resulting string is imposed.
26 * \return This function either returns a pointer to a string that must be
27 * freed by the caller or aborts without returning.
31 static __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
43 * \param fml Usual format string.
45 * All XXX_LOG() macros use this function.
47 __printf_2_3 void __log(int ll, const char* fmt,...)
60 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
61 fprintf(outfd, "%s ", str);
63 vfprintf(outfd, fmt, argp);
68 * A wrapper for lseek(2).
70 * \param fd The file descriptor whose offset is to be to repositioned.
71 * \param offset A value-result parameter.
72 * \param whence Usual repositioning directive.
74 * Reposition the offset of the file descriptor \a fd to the argument \a offset
75 * according to the directive \a whence. Upon successful return, \a offset
76 * contains the resulting offset location as measured in bytes from the
77 * beginning of the file.
79 * \return Positive on success. Otherwise, the function returns \p -E_LSEEK.
83 int para_lseek(int fd, off_t *offset, int whence)
85 *offset = lseek(fd, *offset, whence);
93 * Wrapper for the write system call.
95 * \param fd The file descriptor to write to.
96 * \param buf The buffer to write.
97 * \param size The length of \a buf in bytes.
99 * This function writes out the given buffer and retries if an interrupt
100 * occurred during the write.
102 * \return On success, the number of bytes written is returned, otherwise, the
103 * function returns \p -E_WRITE.
107 ssize_t para_write(int fd, const void *buf, size_t size)
112 ret = write(fd, buf, size);
113 if ((ret < 0) && (errno == EAGAIN || errno == EINTR))
115 return ret >= 0? ret : -E_WRITE;
120 * Write the whole buffer to a file descriptor.
122 * \param fd The file descriptor to write to.
123 * \param buf The buffer to write.
124 * \param size The length of \a buf in bytes.
126 * This function writes the given buffer and continues on short writes and
127 * when interrupted by a signal.
129 * \return Positive on success, negative on errors. Possible errors: any
130 * errors returned by para_write().
134 ssize_t para_write_all(int fd, const void *buf, size_t size)
136 // DEBUG_LOG("writing %zu bytes\n", size);
139 ssize_t ret = para_write(fd, b, size);
140 // DEBUG_LOG("ret: %zd\n", ret);
149 * Open a file, write the given buffer and close the file.
151 * \param filename Full path to the file to open.
152 * \param buf The buffer to write to the file.
153 * \param size The size of \a buf.
155 * \return Positive on success, negative on errors. Possible errors include:
156 * any errors from para_open() or para_write().
158 * \sa para_open(), para_write().
160 int para_write_file(const char *filename, const void *buf, size_t size)
164 ret = para_open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
168 ret = para_write_all(fd, buf, size);
177 static int append_file(const char *filename, char *header, size_t header_size,
178 char *data, size_t data_size, uint32_t *new_pos)
182 // DEBUG_LOG("appending %zu + %zu bytes\n", header_size, data_size);
183 ret = para_open(filename, O_WRONLY | O_CREAT | O_APPEND, 0644);
187 if (header && header_size) {
188 ret = para_write_all(fd, header, header_size);
192 ret = para_write_all(fd, data, data_size);
197 ret = para_lseek(fd, &offset, SEEK_END);
200 // DEBUG_LOG("new file size: " FMT_OFF_T "\n", offset);
210 * Traverse the given directory recursively.
212 * \param dirname The directory to traverse.
213 * \param func The function to call for each entry.
214 * \param private_data Pointer to an arbitrary data structure.
216 * For each regular file under \a dirname, the supplied function \a func is
217 * called. The full path of the regular file and the \a private_data pointer
218 * are passed to \a func. Directories for which the calling process has no
219 * permissions to change to are silently ignored.
223 int for_each_file_in_dir(const char *dirname,
224 int (*func)(const char *, void *), void *private_data)
227 struct dirent *entry;
228 int cwd_fd, ret2, ret = para_opendir(dirname, &dir, &cwd_fd);
231 return ret == -ERRNO_TO_ERROR(EACCES)? 1 : ret;
232 /* scan cwd recursively */
233 while ((entry = readdir(dir))) {
238 if (!strcmp(entry->d_name, "."))
240 if (!strcmp(entry->d_name, ".."))
242 if (lstat(entry->d_name, &s) == -1)
245 if (!S_ISREG(m) && !S_ISDIR(m))
247 tmp = make_message("%s/%s", dirname, entry->d_name);
249 ret = func(tmp, private_data);
256 ret = for_each_file_in_dir(tmp, func, private_data);
264 ret2 = para_fchdir(cwd_fd);
265 if (ret2 < 0 && ret >= 0)
271 static int verify_name(const char *name)
277 if (strchr(name, '/'))
279 if (!strcmp(name, ".."))
281 if (!strcmp(name, "."))
287 * Compare two osl objects pointing to unsigned integers of 32 bit size.
289 * \param obj1 Pointer to the first integer.
290 * \param obj2 Pointer to the second integer.
292 * \return The values required for an osl compare function.
294 * \sa osl_compare_func, osl_hash_compare().
296 int uint32_compare(const struct osl_object *obj1, const struct osl_object *obj2)
298 uint32_t d1 = read_u32((const char *)obj1->data);
299 uint32_t d2 = read_u32((const char *)obj2->data);
309 * Compare two osl objects pointing to hash values.
311 * \param obj1 Pointer to the first hash object.
312 * \param obj2 Pointer to the second hash object.
314 * \return The values required for an osl compare function.
316 * \sa osl_compare_func, uint32_compare().
318 int osl_hash_compare(const struct osl_object *obj1, const struct osl_object *obj2)
320 return hash_compare((HASH_TYPE *)obj1->data, (HASH_TYPE *)obj2->data);
323 static char *disk_storage_dirname(const struct osl_table *t, unsigned col_num,
326 char *dirname, *column_name = column_filename(t, col_num);
328 if (!(t->desc->flags & OSL_LARGE_TABLE))
330 dirname = make_message("%s/%.2s", column_name, ds_name);
335 static char *disk_storage_name_of_object(const struct osl_table *t,
336 const struct osl_object *obj)
338 HASH_TYPE hash[HASH_SIZE];
339 hash_object(obj, hash);
340 return disk_storage_name_of_hash(t, hash);
343 static int disk_storage_name_of_row(const struct osl_table *t,
344 const struct osl_row *row, char **name)
346 struct osl_object obj;
347 int ret = osl_get_object(t, row, t->disk_storage_name_column, &obj);
351 *name = disk_storage_name_of_object(t, &obj);
355 static void column_name_hash(const char *col_name, HASH_TYPE *hash)
357 hash_function(col_name, strlen(col_name), hash);
360 static int init_column_descriptions(struct osl_table *t)
363 const struct osl_column_description *cd;
365 ret = -E_BAD_TABLE_DESC;
366 ret = verify_name(t->desc->name);
370 if (!t->desc->dir && (t->num_disk_storage_columns || t->num_mapped_columns))
372 /* the size of the index header without column descriptions */
373 t->index_header_size = IDX_COLUMN_DESCRIPTIONS;
374 FOR_EACH_COLUMN(i, t->desc, cd) {
375 struct osl_column *col = t->columns + i;
376 if (cd->storage_flags & OSL_RBTREE) {
377 if (!cd->compare_function)
378 return -E_NO_COMPARE_FUNC;
380 if (cd->storage_type == OSL_NO_STORAGE)
382 ret = -E_NO_COLUMN_NAME;
383 if (!cd->name || !cd->name[0])
385 ret = verify_name(cd->name);
388 t->index_header_size += index_column_description_size(cd->name);
389 column_name_hash(cd->name, col->name_hash);
390 ret = -E_DUPLICATE_COL_NAME;
391 for (j = i + 1; j < t->desc->num_columns; j++) {
392 const char *name2 = get_column_description(t->desc,
394 if (cd->name && name2 && !strcmp(cd->name, name2))
404 * Initialize a struct table from given table description.
406 * \param desc The description of the osl table.
407 * \param table_ptr Result is returned here.
409 * This function performs several sanity checks on \p desc and returns if any
410 * of these tests fail. On success, a struct \p osl_table is allocated and
411 * initialized with data derived from \p desc.
413 * \return Positive on success, negative on errors. Possible errors include: \p
414 * E_BAD_TABLE_DESC, \p E_NO_COLUMN_DESC, \p E_NO_COLUMNS, \p
415 * E_BAD_STORAGE_TYPE, \p E_BAD_STORAGE_FLAGS, \p E_BAD_STORAGE_SIZE, \p
416 * E_NO_UNIQUE_RBTREE_COLUMN, \p E_NO_RBTREE_COL.
418 * \sa struct osl_table.
420 int init_table_structure(const struct osl_table_description *desc,
421 struct osl_table **table_ptr)
423 const struct osl_column_description *cd;
424 struct osl_table *t = para_calloc(sizeof(*t));
425 int i, ret = -E_BAD_TABLE_DESC, have_disk_storage_name_column = 0;
429 DEBUG_LOG("creating table structure for '%s' from table "
430 "description\n", desc->name);
431 ret = -E_NO_COLUMN_DESC;
432 if (!desc->column_descriptions)
435 if (!desc->num_columns)
437 t->columns = para_calloc(desc->num_columns * sizeof(struct osl_column));
439 FOR_EACH_COLUMN(i, t->desc, cd) {
440 enum osl_storage_type st = cd->storage_type;
441 enum osl_storage_flags sf = cd->storage_flags;
442 struct osl_column *col = &t->columns[i];
444 ret = -E_BAD_STORAGE_TYPE;
445 if (st != OSL_MAPPED_STORAGE && st != OSL_DISK_STORAGE
446 && st != OSL_NO_STORAGE)
448 ret = -E_BAD_STORAGE_FLAGS;
449 if (st == OSL_DISK_STORAGE && sf & OSL_RBTREE)
451 ret = -E_BAD_STORAGE_SIZE;
452 if (sf & OSL_FIXED_SIZE && !cd->data_size)
455 case OSL_DISK_STORAGE:
456 t->num_disk_storage_columns++;
458 case OSL_MAPPED_STORAGE:
459 t->num_mapped_columns++;
460 col->index_offset = t->row_index_size;
461 t->row_index_size += 8;
464 col->volatile_num = t->num_volatile_columns;
465 t->num_volatile_columns++;
468 if (sf & OSL_RBTREE) {
469 col->rbtree_num = t->num_rbtrees;
471 if ((sf & OSL_UNIQUE) && (st == OSL_MAPPED_STORAGE)) {
472 if (!have_disk_storage_name_column)
473 t->disk_storage_name_column = i;
474 have_disk_storage_name_column = 1;
478 ret = -E_NO_UNIQUE_RBTREE_COLUMN;
479 if (t->num_disk_storage_columns && !have_disk_storage_name_column)
481 ret = -E_NO_RBTREE_COL;
485 DEBUG_LOG("OK. Index entry size: %u\n", t->row_index_size);
486 ret = init_column_descriptions(t);
498 * Read the table description from index header.
500 * \param map The memory mapping of the index file.
501 * \param desc The values found in the index header are returned here.
503 * Read the index header, check for the paraslash magic string and the table version number.
504 * Read all information stored in the index header into \a desc.
506 * \return Positive on success, negative on errors.
508 * \sa struct osl_table_description, osl_create_table.
510 int read_table_desc(struct osl_object *map, struct osl_table_description *desc)
512 char *buf = map->data;
514 uint16_t header_size;
517 struct osl_column_description *cd;
519 if (map->size < MIN_INDEX_HEADER_SIZE(1))
520 return -E_SHORT_TABLE;
521 if (strncmp(buf + IDX_PARA_MAGIC, PARA_MAGIC, strlen(PARA_MAGIC)))
523 version = read_u8(buf + IDX_VERSION);
524 if (version < MIN_TABLE_VERSION || version > MAX_TABLE_VERSION)
525 return -E_VERSION_MISMATCH;
526 desc->flags = read_u8(buf + IDX_TABLE_FLAGS);
527 desc->num_columns = read_u16(buf + IDX_NUM_COLUMNS);
528 DEBUG_LOG("%u columns\n", desc->num_columns);
529 if (!desc->num_columns)
530 return -E_NO_COLUMNS;
531 header_size = read_u16(buf + IDX_HEADER_SIZE);
532 if (map->size < header_size)
534 desc->column_descriptions = para_calloc(desc->num_columns
535 * sizeof(struct osl_column_description));
536 offset = IDX_COLUMN_DESCRIPTIONS;
537 FOR_EACH_COLUMN(i, desc, cd) {
540 ret = -E_SHORT_TABLE;
541 if (map->size < offset + MIN_IDX_COLUMN_DESCRIPTION_SIZE) {
542 ERROR_LOG("map size = %zu < %u = offset + min desc size\n",
543 map->size, offset + MIN_IDX_COLUMN_DESCRIPTION_SIZE);
546 cd->storage_type = read_u16(buf + offset + IDX_CD_STORAGE_TYPE);
547 cd->storage_flags = read_u16(buf + offset +
548 IDX_CD_STORAGE_FLAGS);
549 cd->data_size = read_u32(buf + offset + IDX_CD_DATA_SIZE);
550 null_byte = memchr(buf + offset + IDX_CD_NAME, '\0',
551 map->size - offset - IDX_CD_NAME);
552 ret = -E_INDEX_CORRUPTION;
555 cd->name = para_strdup(buf + offset + IDX_CD_NAME);
556 offset += index_column_description_size(cd->name);
558 if (offset != header_size) {
559 ret = -E_INDEX_CORRUPTION;
560 ERROR_LOG("real header size = %u != %u = stored header size\n",
561 offset, header_size);
566 FOR_EACH_COLUMN(i, desc, cd)
572 * check whether the table description given by \p t->desc matches the on-disk
573 * table structure stored in the index of \a t.
575 static int compare_table_descriptions(struct osl_table *t)
578 struct osl_table_description desc;
579 const struct osl_column_description *cd1, *cd2;
581 /* read the on-disk structure into desc */
582 ret = read_table_desc(&t->index_map, &desc);
585 ret = -E_BAD_TABLE_FLAGS;
586 if (desc.flags != t->desc->flags)
588 ret = -E_BAD_COLUMN_NUM;
589 if (desc.num_columns > t->desc->num_columns)
591 if (desc.num_columns < t->desc->num_columns) {
592 struct osl_column_description *cd;
593 unsigned diff = t->desc->num_columns - desc.num_columns;
594 INFO_LOG("extending table by %u volatile columns\n", diff);
595 ret = -ERRNO_TO_ERROR(ENOMEM);
596 desc.column_descriptions = realloc(desc.column_descriptions,
597 t->desc->num_columns * sizeof(struct osl_column_description));
598 if (!desc.column_descriptions)
600 for (i = desc.num_columns; i < t->desc->num_columns; i++) {
601 cd = get_column_description(&desc, i);
602 cd->storage_type = OSL_NO_STORAGE;
605 desc.num_columns += diff;
607 FOR_EACH_COLUMN(i, t->desc, cd1) {
608 cd2 = get_column_description(&desc, i);
609 ret = -E_BAD_STORAGE_TYPE;
610 if (cd1->storage_type != cd2->storage_type)
612 if (cd1->storage_type == OSL_NO_STORAGE)
614 ret = -E_BAD_STORAGE_FLAGS;
615 if (cd1->storage_flags != cd2->storage_flags) {
616 ERROR_LOG("sf1 = %u != %u = sf2\n",
617 cd1->storage_flags, cd2->storage_flags);
620 ret = -E_BAD_DATA_SIZE;
621 if (cd1->storage_flags & OSL_FIXED_SIZE)
622 if (cd1->data_size != cd2->data_size)
624 ret = -E_BAD_COLUMN_NAME;
625 if (strcmp(cd1->name, cd2->name))
628 DEBUG_LOG("table description of '%s' matches on-disk data, good\n",
632 FOR_EACH_COLUMN(i, &desc, cd1)
634 free(desc.column_descriptions);
638 static int create_table_index(struct osl_table *t)
640 char *buf, *filename;
642 size_t size = t->index_header_size;
643 const struct osl_column_description *cd;
646 INFO_LOG("creating %zu byte index for table %s\n", size,
648 buf = para_calloc(size);
649 sprintf(buf + IDX_PARA_MAGIC, "%s", PARA_MAGIC);
650 write_u8(buf + IDX_TABLE_FLAGS, t->desc->flags);
651 write_u8(buf + IDX_DIRTY_FLAG, 0);
652 write_u8(buf + IDX_VERSION, CURRENT_TABLE_VERSION);
653 write_u16(buf + IDX_NUM_COLUMNS, t->num_mapped_columns + t->num_disk_storage_columns);
654 write_u16(buf + IDX_HEADER_SIZE, t->index_header_size);
655 offset = IDX_COLUMN_DESCRIPTIONS;
656 FOR_EACH_COLUMN(i, t->desc, cd) {
657 /* no need to store info about volatile storage */
658 if (cd->storage_type == OSL_NO_STORAGE)
660 write_u16(buf + offset + IDX_CD_STORAGE_TYPE,
662 write_u16(buf + offset + IDX_CD_STORAGE_FLAGS,
664 if (cd->storage_flags & OSL_FIXED_SIZE)
665 write_u32(buf + offset + IDX_CD_DATA_SIZE,
667 strcpy(buf + offset + IDX_CD_NAME, cd->name);
668 offset += index_column_description_size(cd->name);
670 assert(offset = size);
671 filename = index_filename(t->desc);
672 ret = para_write_file(filename, buf, size);
679 * Create a new osl table.
681 * \param desc Pointer to the table description.
685 int osl_create_table(const struct osl_table_description *desc)
687 const struct osl_column_description *cd;
688 char *table_dir = NULL, *filename;
690 int i, ret = init_table_structure(desc, &t);
694 INFO_LOG("creating %s\n", desc->name);
695 FOR_EACH_COLUMN(i, t->desc, cd) {
696 if (cd->storage_type == OSL_NO_STORAGE)
699 ret = para_mkdir(desc->dir, 0777);
700 if (ret < 0 && !is_errno(-ret, EEXIST))
702 table_dir = make_message("%s/%s", desc->dir,
704 ret = para_mkdir(table_dir, 0777);
708 filename = column_filename(t, i);
709 INFO_LOG("filename: %s\n", filename);
710 if (cd->storage_type == OSL_MAPPED_STORAGE) {
711 ret = para_open(filename, O_RDWR | O_CREAT | O_EXCL,
720 ret = para_mkdir(filename, 0777);
725 if (t->num_mapped_columns) {
726 ret = create_table_index(t);
738 static int table_is_dirty(struct osl_table *t)
740 char *buf = (char *)t->index_map.data + IDX_DIRTY_FLAG;
741 uint8_t dirty = read_u8(buf) & 0x1;
745 static void mark_table_dirty(struct osl_table *t)
747 char *buf = (char *)t->index_map.data + IDX_DIRTY_FLAG;
748 write_u8(buf, read_u8(buf) | 1);
751 static void mark_table_clean(struct osl_table *t)
753 char *buf = (char *)t->index_map.data + IDX_DIRTY_FLAG;
754 write_u8(buf, read_u8(buf) & 0xfe);
757 static void unmap_column(struct osl_table *t, unsigned col_num)
759 struct osl_object map = t->columns[col_num].data_map;
763 ret = para_munmap(map.data, map.size);
769 * Unmap all mapped files of an osl table.
771 * \param t Pointer to a mapped table.
772 * \param flags Options for unmapping.
774 * \return Positive on success, negative on errors.
776 * \sa map_table(), enum osl_close_flags, para_munmap().
778 int unmap_table(struct osl_table *t, enum osl_close_flags flags)
781 const struct osl_column_description *cd;
784 if (!t->num_mapped_columns) /* can this ever happen? */
786 DEBUG_LOG("unmapping table '%s'\n", t->desc->name);
787 if (!t->index_map.data)
788 return -E_NOT_MAPPED;
789 if (flags & OSL_MARK_CLEAN)
791 ret = para_munmap(t->index_map.data, t->index_map.size);
794 t->index_map.data = NULL;
797 FOR_EACH_MAPPED_COLUMN(i, t, cd)
802 static int map_column(struct osl_table *t, unsigned col_num)
805 char *filename = column_filename(t, col_num);
807 if (stat(filename, &statbuf) < 0) {
811 if (!(S_IFREG & statbuf.st_mode)) {
815 ret = mmap_full_file(filename, O_RDWR,
816 &t->columns[col_num].data_map.data,
817 &t->columns[col_num].data_map.size,
824 * Map the index file and all columns of type \p OSL_MAPPED_STORAGE into memory.
826 * \param t Pointer to an initialized table structure.
827 * \param flags Mapping options.
829 * \return Negative return value on errors; on success the number of rows
830 * (including invalid rows) is returned.
832 * \sa unmap_table(), enum map_table_flags, osl_open_table(), mmap(2).
834 int map_table(struct osl_table *t, enum map_table_flags flags)
837 const struct osl_column_description *cd;
838 int i = 0, ret, num_rows = 0;
840 if (!t->num_mapped_columns)
842 if (t->index_map.data)
843 return -E_ALREADY_MAPPED;
844 filename = index_filename(t->desc);
845 DEBUG_LOG("mapping table '%s' (index: %s)\n", t->desc->name, filename);
846 ret = mmap_full_file(filename, flags & MAP_TBL_FL_MAP_RDONLY?
847 O_RDONLY : O_RDWR, &t->index_map.data, &t->index_map.size, NULL);
851 if (flags & MAP_TBL_FL_VERIFY_INDEX) {
852 ret = compare_table_descriptions(t);
857 if (!(flags & MAP_TBL_FL_IGNORE_DIRTY)) {
858 if (table_is_dirty(t)) {
859 ERROR_LOG("%s is dirty\n", t->desc->name);
864 num_rows = table_num_rows(t);
868 FOR_EACH_MAPPED_COLUMN(i, t, cd) {
869 ret = map_column(t, i);
874 err: /* unmap what is already mapped */
875 for (i--; i >= 0; i--) {
876 struct osl_object map = t->columns[i].data_map;
877 para_munmap(map.data, map.size);
880 para_munmap(t->index_map.data, t->index_map.size);
881 t->index_map.data = NULL;
886 * Retrieve a mapped object by row and column number.
888 * \param t Pointer to an open osl table.
889 * \param col_num Number of the mapped column containing the object to retrieve.
890 * \param row_num Number of the row containing the object to retrieve.
891 * \param obj The result is returned here.
893 * It is considered an error if \a col_num does not refer to a column
894 * of storage type \p OSL_MAPPED_STORAGE.
896 * \return Positive on success, negative on errors. Possible errors include:
897 * \p E_BAD_ROW_NUM, \p E_INVALID_OBJECT.
899 * \sa osl_storage_type.
901 int get_mapped_object(const struct osl_table *t, unsigned col_num,
902 uint32_t row_num, struct osl_object *obj)
904 struct osl_column *col = &t->columns[col_num];
910 if (t->num_rows <= row_num)
911 return -E_BAD_ROW_NUM;
912 ret = get_cell_index(t, row_num, col_num, &cell_index);
915 offset = read_u32(cell_index);
916 obj->size = read_u32(cell_index + 4) - 1;
917 header = col->data_map.data + offset;
918 obj->data = header + 1;
919 if (read_u8(header) == 0xff) {
920 ERROR_LOG("col %u, size %zu, offset %u\n", col_num,
922 return -E_INVALID_OBJECT;
927 static int search_rbtree(const struct osl_object *obj,
928 const struct osl_table *t, unsigned col_num,
929 struct rb_node **result, struct rb_node ***rb_link)
931 struct osl_column *col = &t->columns[col_num];
932 struct rb_node **new = &col->rbtree.rb_node, *parent = NULL;
933 const struct osl_column_description *cd =
934 get_column_description(t->desc, col_num);
935 enum osl_storage_type st = cd->storage_type;
937 struct osl_row *this_row = get_row_pointer(*new,
940 struct osl_object this_obj;
942 if (st == OSL_MAPPED_STORAGE) {
943 ret = get_mapped_object(t, col_num, this_row->num,
948 this_obj = this_row->volatile_objects[col->volatile_num];
949 ret = cd->compare_function(obj, &this_obj);
952 *result = get_rb_node_pointer(this_row,
957 new = &((*new)->rb_left);
959 new = &((*new)->rb_right);
965 return -E_RB_KEY_NOT_FOUND;
968 static int insert_rbtree(struct osl_table *t, unsigned col_num,
969 const struct osl_row *row, const struct osl_object *obj)
971 struct rb_node *parent, **rb_link;
974 int ret = search_rbtree(obj, t, col_num, &parent, &rb_link);
977 return -E_RB_KEY_EXISTS;
978 rbtree_num = t->columns[col_num].rbtree_num;
979 n = get_rb_node_pointer(row, rbtree_num);
980 rb_link_node(n, parent, rb_link);
981 rb_insert_color(n, &t->columns[col_num].rbtree);
985 static void remove_rb_node(struct osl_table *t, unsigned col_num,
986 const struct osl_row *row)
988 struct osl_column *col = &t->columns[col_num];
989 const struct osl_column_description *cd =
990 get_column_description(t->desc, col_num);
991 enum osl_storage_flags sf = cd->storage_flags;
992 struct rb_node *victim, *splice_out_node, *tmp;
993 if (!(sf & OSL_RBTREE))
996 * Which node is removed/spliced out actually depends on how many
997 * children the victim node has: If it has no children, it gets
998 * deleted. If it has one child, it gets spliced out. If it has two
999 * children, its successor (which has at most a right child) gets
1002 victim = get_rb_node_pointer(row, col->rbtree_num);
1003 if (victim->rb_left && victim->rb_right)
1004 splice_out_node = rb_next(victim);
1006 splice_out_node = victim;
1007 /* Go up to the root and decrement the size of each node in the path. */
1008 for (tmp = splice_out_node; tmp; tmp = rb_parent(tmp))
1010 rb_erase(victim, &col->rbtree);
1013 static int add_row_to_rbtrees(struct osl_table *t, uint32_t row_num,
1014 struct osl_object *volatile_objs, struct osl_row **row_ptr)
1018 struct osl_row *row = allocate_row(t->num_rbtrees);
1019 const struct osl_column_description *cd;
1022 row->volatile_objects = volatile_objs;
1023 FOR_EACH_RBTREE_COLUMN(i, t, cd) {
1024 if (cd->storage_type == OSL_MAPPED_STORAGE) {
1025 struct osl_object obj;
1026 ret = get_mapped_object(t, i, row_num, &obj);
1029 ret = insert_rbtree(t, i, row, &obj);
1030 } else { /* volatile */
1031 const struct osl_object *obj
1032 = volatile_objs + t->columns[i].volatile_num;
1033 ret = insert_rbtree(t, i, row, obj);
1041 err: /* rollback changes, i.e. remove added entries from rbtrees */
1043 remove_rb_node(t, i--, row);
1048 static void free_volatile_objects(const struct osl_table *t,
1049 enum osl_close_flags flags)
1053 struct osl_column *rb_col;
1054 const struct osl_column_description *cd;
1056 if (!t->num_volatile_columns)
1058 /* find the first rbtree column (any will do) */
1059 FOR_EACH_RBTREE_COLUMN(i, t, cd)
1061 rb_col = t->columns + i;
1062 /* walk that rbtree and free all volatile objects */
1063 for (n = rb_first(&rb_col->rbtree); n; n = rb_next(n)) {
1064 struct osl_row *r = get_row_pointer(n, rb_col->rbtree_num);
1065 if (flags & OSL_FREE_VOLATILE)
1066 FOR_EACH_VOLATILE_COLUMN(j, t, cd) {
1067 if (cd->storage_flags & OSL_DONT_FREE)
1069 free(r->volatile_objects[
1070 t->columns[j].volatile_num].data);
1072 // for (j = 0; j < t->num_volatile_columns; j++)
1073 // free(r->volatile_objects[j].data);
1074 free(r->volatile_objects);
1079 * Erase all rbtree nodes and free resources.
1081 * \param t Pointer to an open osl table.
1083 * This function is called by osl_close_table().
1085 void clear_rbtrees(struct osl_table *t)
1087 const struct osl_column_description *cd;
1088 unsigned i, rbtrees_cleared = 0;
1090 FOR_EACH_RBTREE_COLUMN(i, t, cd) {
1091 struct osl_column *col = &t->columns[i];
1094 for (n = rb_first(&col->rbtree); n;) {
1096 rb_erase(n, &col->rbtree);
1097 if (rbtrees_cleared == t->num_rbtrees) {
1098 r = get_row_pointer(n, col->rbtree_num);
1109 * Close an osl table.
1111 * \param t Pointer to the table to be closed.
1112 * \param flags Options for what should be cleaned up.
1114 * If osl_open_table() succeeds, the resulting table pointer must later be
1115 * passed to this function in order to flush all changes to the file system and
1116 * to free the resources that were allocated by osl_open_table().
1118 * \return Positive on success, negative on errors. Possible errors: \p E_BAD_TABLE,
1119 * errors returned by unmap_table().
1121 * \sa osl_open_table(), unmap_table().
1123 int osl_close_table(struct osl_table *t, enum osl_close_flags flags)
1128 return -E_BAD_TABLE;
1129 free_volatile_objects(t, flags);
1131 ret = unmap_table(t, flags);
1133 ERROR_LOG("unmap_table failed: %d\n", ret);
1140 * Find out whether the given row number corresponds to an invalid row.
1142 * \param t Pointer to the osl table.
1143 * \param row_num The number of the row in question.
1145 * By definition, a row is considered invalid if all its index entries
1148 * \return Positive if \a row_num corresponds to an invalid row,
1149 * zero if it corresponds to a valid row, negative on errors.
1151 int row_is_invalid(struct osl_table *t, uint32_t row_num)
1154 int i, ret = get_row_index(t, row_num, &row_index);
1158 for (i = 0; i < t->row_index_size; i++) {
1159 if ((unsigned char)row_index[i] != 0xff)
1162 INFO_LOG("row %d is invalid\n", row_num);
1167 * Invalidate a row of an osl table.
1169 * \param t Pointer to an open osl table.
1170 * \param row_num Number of the row to mark as invalid.
1172 * This function marks each mapped object in the index entry of \a row as
1175 * \return Positive on success, negative on errors.
1177 int mark_row_invalid(struct osl_table *t, uint32_t row_num)
1180 int ret = get_row_index(t, row_num, &row_index);
1184 INFO_LOG("marking row %d as invalid\n", row_num);
1185 memset(row_index, 0xff, t->row_index_size);
1190 * Initialize all rbtrees and compute number of invalid rows.
1192 * \param t The table containing the rbtrees to be initialized.
1194 * \return Positive on success, negative on errors.
1196 int init_rbtrees(struct osl_table *t)
1199 const struct osl_column_description *cd;
1201 /* create rbtrees */
1202 FOR_EACH_RBTREE_COLUMN(i, t, cd)
1203 t->columns[i].rbtree = RB_ROOT;
1204 /* add valid rows to rbtrees */
1205 t->num_invalid_rows = 0;
1206 for (i = 0; i < t->num_rows; i++) {
1207 ret = row_is_invalid(t, i);
1211 t->num_invalid_rows++;
1214 ret = add_row_to_rbtrees(t, i, NULL, NULL);
1222 * Open an osl table.
1224 * Each osl table must be opened before its data can be accessed.
1226 * \param table_desc Describes the table to be opened.
1227 * \param result Contains a pointer to the open table on success.
1229 * The table description given by \a desc should coincide with the
1230 * description used at creation time.
1234 int osl_open_table(const struct osl_table_description *table_desc,
1235 struct osl_table **result)
1238 struct osl_table *t;
1239 const struct osl_column_description *cd;
1241 INFO_LOG("opening table %s\n", table_desc->name);
1242 ret = init_table_structure(table_desc, &t);
1245 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
1246 /* check if directory exists */
1247 char *dirname = column_filename(t, i);
1248 struct stat statbuf;
1249 ret = stat(dirname, &statbuf);
1252 ret = -ERRNO_TO_ERROR(errno);
1255 ret = -ERRNO_TO_ERROR(ENOTDIR);
1256 if (!S_ISDIR(statbuf.st_mode))
1259 ret = map_table(t, MAP_TBL_FL_VERIFY_INDEX);
1263 DEBUG_LOG("num rows: %d\n", t->num_rows);
1264 ret = init_rbtrees(t);
1266 osl_close_table(t, OSL_MARK_CLEAN); /* ignore further errors */
1277 static int create_disk_storage_object_dir(const struct osl_table *t,
1278 unsigned col_num, const char *ds_name)
1283 if (!(t->desc->flags & OSL_LARGE_TABLE))
1285 dirname = disk_storage_dirname(t, col_num, ds_name);
1286 ret = para_mkdir(dirname, 0777);
1288 if (ret < 0 && !is_errno(-ret, EEXIST))
1293 static int write_disk_storage_file(const struct osl_table *t, unsigned col_num,
1294 const struct osl_object *obj, const char *ds_name)
1299 ret = create_disk_storage_object_dir(t, col_num, ds_name);
1302 filename = disk_storage_path(t, col_num, ds_name);
1303 ret = para_write_file(filename, obj->data, obj->size);
1308 static int append_map_file(const struct osl_table *t, unsigned col_num,
1309 const struct osl_object *obj, uint32_t *new_size)
1311 char *filename = column_filename(t, col_num);
1313 char header = 0; /* zero means valid object */
1315 // DEBUG_LOG("appending %zu + 1 byte\n", obj->size);
1316 ret = append_file(filename, &header, 1, obj->data, obj->size,
1322 static int append_row_index(const struct osl_table *t, char *row_index)
1327 if (!t->num_mapped_columns)
1329 filename = index_filename(t->desc);
1330 ret = append_file(filename, NULL, 0, row_index,
1331 t->row_index_size, NULL);
1337 * A wrapper for truncate(2)
1339 * \param path Name of the regular file to truncate
1340 * \param size Number of bytes to \b shave \b off
1342 * Truncate the regular file named by \a path by \a size bytes.
1344 * \return Positive on success, negative on errors. Possible errors include: \p
1345 * E_STAT, \p E_BAD_SIZE, \p E_TRUNC.
1349 int para_truncate(const char *path, off_t size)
1352 struct stat statbuf;
1355 if (stat(path, &statbuf) < 0)
1358 if (statbuf.st_size < size)
1361 if (truncate(path, statbuf.st_size - size) < 0)
1368 static int truncate_mapped_file(const struct osl_table *t, unsigned col_num,
1371 char *filename = column_filename(t, col_num);
1372 int ret = para_truncate(filename, size);
1377 static int delete_disk_storage_file(const struct osl_table *t, unsigned col_num,
1378 const char *ds_name)
1380 char *dirname, *filename = disk_storage_path(t, col_num, ds_name);
1381 int ret = unlink(filename), err = errno;
1385 return -ERRNO_TO_ERROR(err);
1386 if (!(t->desc->flags & OSL_LARGE_TABLE))
1388 dirname = disk_storage_dirname(t, col_num, ds_name);
1395 * Add a new row to an osl table and retrieve this row.
1397 * \param t Pointer to an open osl table.
1398 * \param objects Array of objects to be added.
1399 * \param row Result pointer.
1401 * The \a objects parameter must point to an array containing one object per
1402 * column. The order of the objects in the array is given by the table
1403 * description of \a table. Several sanity checks are performed during object
1404 * insertion and the function returns without modifying the table if any of
1405 * these tests fail. In fact, it is atomic in the sense that it either
1406 * succeeds or leaves the table unchanged (i.e. either all or none of the
1407 * objects are added to the table).
1409 * It is considered an error if an object is added to a column with associated
1410 * rbtree if this object is equal to an object already contained in that column
1411 * (i.e. the compare function for the column's rbtree returns zero).
1413 * Possible errors include: \p E_RB_KEY_EXISTS, \p E_BAD_DATA_SIZE.
1415 * \return Positive on success, negative on errors.
1417 * \sa struct osl_table_description, osl_compare_func, osl_add_row().
1419 int osl_add_and_get_row(struct osl_table *t, struct osl_object *objects,
1420 struct osl_row **row)
1423 char *ds_name = NULL;
1424 struct rb_node **rb_parents = NULL, ***rb_links = NULL;
1425 char *new_row_index = NULL;
1426 struct osl_object *volatile_objs = NULL;
1427 const struct osl_column_description *cd;
1430 return -E_BAD_TABLE;
1431 rb_parents = para_malloc(t->num_rbtrees * sizeof(struct rn_node*));
1432 rb_links = para_malloc(t->num_rbtrees * sizeof(struct rn_node**));
1433 if (t->num_mapped_columns)
1434 new_row_index = para_malloc(t->row_index_size);
1435 /* pass 1: sanity checks */
1436 // DEBUG_LOG("sanity tests: %p:%p\n", objects[0].data,
1437 // objects[1].data);
1438 FOR_EACH_COLUMN(i, t->desc, cd) {
1439 enum osl_storage_type st = cd->storage_type;
1440 enum osl_storage_flags sf = cd->storage_flags;
1442 // ret = -E_NULL_OBJECT;
1445 if (st == OSL_DISK_STORAGE)
1447 if (sf & OSL_RBTREE) {
1448 unsigned rbtree_num = t->columns[i].rbtree_num;
1449 ret = -E_RB_KEY_EXISTS;
1450 // DEBUG_LOG("checking whether %p exists\n",
1451 // objects[i].data);
1452 if (search_rbtree(objects + i, t, i,
1453 &rb_parents[rbtree_num],
1454 &rb_links[rbtree_num]) > 0)
1457 if (sf & OSL_FIXED_SIZE) {
1458 // DEBUG_LOG("fixed size. need: %zu, have: %d\n",
1459 // objects[i].size, cd->data_size);
1460 ret = -E_BAD_DATA_SIZE;
1461 if (objects[i].size != cd->data_size)
1465 if (t->num_disk_storage_columns)
1466 ds_name = disk_storage_name_of_object(t,
1467 &objects[t->disk_storage_name_column]);
1468 ret = unmap_table(t, OSL_MARK_CLEAN);
1471 // DEBUG_LOG("sanity tests passed%s\n", "");
1472 /* pass 2: create data files, append map data */
1473 FOR_EACH_COLUMN(i, t->desc, cd) {
1474 enum osl_storage_type st = cd->storage_type;
1475 if (st == OSL_NO_STORAGE)
1477 if (st == OSL_MAPPED_STORAGE) {
1479 struct osl_column *col = &t->columns[i];
1480 // DEBUG_LOG("appending object of size %zu\n",
1481 // objects[i].size);
1482 ret = append_map_file(t, i, objects + i, &new_size);
1485 update_cell_index(new_row_index, col, new_size,
1490 ret = write_disk_storage_file(t, i, objects + i, ds_name);
1494 ret = append_row_index(t, new_row_index);
1497 ret = map_table(t, MAP_TBL_FL_VERIFY_INDEX);
1498 if (ret < 0) { /* truncate index and rollback changes */
1499 char *filename = index_filename(t->desc);
1500 para_truncate(filename, t->row_index_size);
1504 /* pass 3: add entry to rbtrees */
1505 if (t->num_volatile_columns) {
1506 volatile_objs = para_calloc(t->num_volatile_columns
1507 * sizeof(struct osl_object));
1508 FOR_EACH_VOLATILE_COLUMN(i, t, cd)
1509 volatile_objs[t->columns[i].volatile_num] = objects[i];
1512 // DEBUG_LOG("adding new entry as row #%d\n", t->num_rows - 1);
1513 ret = add_row_to_rbtrees(t, t->num_rows - 1, volatile_objs, row);
1516 // DEBUG_LOG("added new entry as row #%d\n", t->num_rows - 1);
1519 rollback: /* rollback all changes made, ignore further errors */
1520 for (i--; i >= 0; i--) {
1521 cd = get_column_description(t->desc, i);
1522 enum osl_storage_type st = cd->storage_type;
1523 if (st == OSL_NO_STORAGE)
1526 if (st == OSL_MAPPED_STORAGE)
1527 truncate_mapped_file(t, i, objects[i].size);
1528 else /* disk storage */
1529 delete_disk_storage_file(t, i, ds_name);
1531 /* ignore error and return previous error value */
1532 map_table(t, MAP_TBL_FL_VERIFY_INDEX);
1534 free(new_row_index);
1542 * Add a new row to an osl table.
1544 * \param t Same meaning as osl_add_and_get_row().
1545 * \param objects Same meaning as osl_add_and_get_row().
1547 * \return The return value of the underlying call to osl_add_and_get_row().
1549 * This is equivalent to osl_add_and_get_row(t, objects, NULL).
1551 int osl_add_row(struct osl_table *t, struct osl_object *objects)
1553 return osl_add_and_get_row(t, objects, NULL);
1557 * Retrieve an object identified by row and column
1559 * \param t Pointer to an open osl table.
1560 * \param r Pointer to the row.
1561 * \param col_num The column number.
1562 * \param object The result pointer.
1564 * The column determined by \a col_num must be of type \p OSL_MAPPED_STORAGE
1565 * or \p OSL_NO_STORAGE, i.e. no disk storage objects may be retrieved by this
1568 * \return Positive if object was found, negative on errors. Possible errors
1569 * include: \p E_BAD_TABLE, \p E_BAD_STORAGE_TYPE.
1571 * \sa osl_storage_type, osl_open_disk_object().
1573 int osl_get_object(const struct osl_table *t, const struct osl_row *r,
1574 unsigned col_num, struct osl_object *object)
1576 const struct osl_column_description *cd;
1579 return -E_BAD_TABLE;
1580 cd = get_column_description(t->desc, col_num);
1581 /* col must not be disk storage */
1582 if (cd->storage_type == OSL_DISK_STORAGE)
1583 return -E_BAD_STORAGE_TYPE;
1584 if (cd->storage_type == OSL_MAPPED_STORAGE)
1585 return get_mapped_object(t, col_num, r->num, object);
1587 *object = r->volatile_objects[t->columns[col_num].volatile_num];
1591 static int mark_mapped_object_invalid(const struct osl_table *t,
1592 uint32_t row_num, unsigned col_num)
1594 struct osl_object obj;
1596 int ret = get_mapped_object(t, col_num, row_num, &obj);
1607 * Delete a row from an osl table.
1609 * \param t Pointer to an open osl table.
1610 * \param row Pointer to the row to delete.
1612 * This removes all disk storage objects, removes all rbtree nodes, and frees
1613 * all volatile objects belonging to the given row. For mapped columns, the
1614 * data is merely marked invalid and may be pruned from time to time by
1617 * \return Positive on success, negative on errors. Possible errors include:
1618 * \p E_BAD_TABLE, errors returned by osl_get_object().
1620 int osl_del_row(struct osl_table *t, struct osl_row *row)
1622 struct osl_row *r = row;
1624 const struct osl_column_description *cd;
1627 return -E_BAD_TABLE;
1628 INFO_LOG("deleting row %p\n", row);
1630 if (t->num_disk_storage_columns) {
1632 ret = disk_storage_name_of_row(t, r, &ds_name);
1635 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd)
1636 delete_disk_storage_file(t, i, ds_name);
1639 FOR_EACH_COLUMN(i, t->desc, cd) {
1640 struct osl_column *col = t->columns + i;
1641 enum osl_storage_type st = cd->storage_type;
1642 remove_rb_node(t, i, r);
1643 if (st == OSL_MAPPED_STORAGE) {
1644 mark_mapped_object_invalid(t, r->num, i);
1647 if (st == OSL_NO_STORAGE && !(cd->storage_flags & OSL_DONT_FREE))
1648 free(r->volatile_objects[col->volatile_num].data);
1650 if (t->num_mapped_columns) {
1651 ret = mark_row_invalid(t, r->num);
1654 t->num_invalid_rows++;
1659 free(r->volatile_objects);
1664 /* test if column has an rbtree */
1665 static int check_rbtree_col(const struct osl_table *t, unsigned col_num,
1666 struct osl_column **col)
1669 return -E_BAD_TABLE;
1670 if (!(get_column_description(t->desc, col_num)->storage_flags & OSL_RBTREE))
1671 return -E_BAD_STORAGE_FLAGS;
1672 *col = t->columns + col_num;
1677 * Get the row that contains the given object.
1679 * \param t Pointer to an open osl table.
1680 * \param col_num The number of the column to be searched.
1681 * \param obj The object to be looked up.
1682 * \param result Points to the row containing \a obj.
1684 * Lookup \a obj in \a t and return the row containing \a obj. The column
1685 * specified by \a col_num must have an associated rbtree.
1687 * \return Positive on success, negative on errors. If an error occurred, \a
1688 * result is set to \p NULL. Possible errors include: \p E_BAD_TABLE, \p
1689 * E_BAD_STORAGE_FLAGS, errors returned by get_mapped_object(), \p
1690 * E_RB_KEY_NOT_FOUND.
1692 * \sa osl_storage_flags
1694 int osl_get_row(const struct osl_table *t, unsigned col_num,
1695 const struct osl_object *obj, struct osl_row **result)
1698 struct rb_node *node;
1699 struct osl_row *row;
1700 struct osl_column *col;
1703 ret = check_rbtree_col(t, col_num, &col);
1706 ret = search_rbtree(obj, t, col_num, &node, NULL);
1709 row = get_row_pointer(node, t->columns[col_num].rbtree_num);
1714 static int rbtree_loop(struct osl_column *col, void *private_data,
1715 osl_rbtree_loop_func *func)
1717 struct rb_node *n, *tmp;
1719 /* this for-loop is safe against removal of an entry */
1720 for (n = rb_first(&col->rbtree), tmp = n? rb_next(n) : NULL;
1722 n = tmp, tmp = tmp? rb_next(tmp) : NULL) {
1723 struct osl_row *r = get_row_pointer(n, col->rbtree_num);
1724 int ret = func(r, private_data);
1731 static int rbtree_loop_reverse(struct osl_column *col, void *private_data,
1732 osl_rbtree_loop_func *func)
1734 struct rb_node *n, *tmp;
1736 /* safe against removal of an entry */
1737 for (n = rb_last(&col->rbtree), tmp = n? rb_prev(n) : NULL;
1739 n = tmp, tmp = tmp? rb_prev(tmp) : NULL) {
1740 struct osl_row *r = get_row_pointer(n, col->rbtree_num);
1741 int ret = func(r, private_data);
1749 * Loop over all nodes in an rbtree.
1751 * \param t Pointer to an open osl table.
1752 * \param col_num The column to use for iterating over the elements.
1753 * \param private_data Pointer that gets passed to \a func.
1754 * \param func The function to be called for each node in the rbtree.
1756 * This function does an in-order walk of the rbtree associated with \a
1757 * col_num. It is an error if the \p OSL_RBTREE flag is not set for this
1758 * column. For each node in the rbtree, the given function \a func is called
1759 * with two pointers as arguments: The first osl_row* argument points to the
1760 * row that contains the object corresponding to the rbtree node currently
1761 * traversed, and the \a private_data pointer is passed verbatim to \a func as the
1762 * second argument. The loop terminates either if \a func returns a negative
1763 * value, or if all nodes of the tree have been visited.
1766 * \return Positive on success, negative on errors. If the termination of the
1767 * loop was caused by \a func returning a negative value, this value is
1770 * \sa osl_storage_flags, osl_rbtree_loop_reverse(), osl_compare_func.
1772 int osl_rbtree_loop(const struct osl_table *t, unsigned col_num,
1773 void *private_data, osl_rbtree_loop_func *func)
1775 struct osl_column *col;
1777 int ret = check_rbtree_col(t, col_num, &col);
1780 return rbtree_loop(col, private_data, func);
1784 * Loop over all nodes in an rbtree in reverse order.
1786 * \param t Identical meaning as in \p osl_rbtree_loop().
1787 * \param col_num Identical meaning as in \p osl_rbtree_loop().
1788 * \param private_data Identical meaning as in \p osl_rbtree_loop().
1789 * \param func Identical meaning as in \p osl_rbtree_loop().
1791 * This function is identical to \p osl_rbtree_loop(), the only difference
1792 * is that the tree is walked in reverse order.
1794 * \return The same return value as \p osl_rbtree_loop().
1796 * \sa osl_rbtree_loop().
1798 int osl_rbtree_loop_reverse(const struct osl_table *t, unsigned col_num,
1799 void *private_data, osl_rbtree_loop_func *func)
1801 struct osl_column *col;
1803 int ret = check_rbtree_col(t, col_num, &col);
1806 return rbtree_loop_reverse(col, private_data, func);
1809 /* TODO: Rollback changes on errors */
1810 static int rename_disk_storage_objects(struct osl_table *t,
1811 struct osl_object *old_obj, struct osl_object *new_obj)
1814 const struct osl_column_description *cd;
1815 char *old_ds_name, *new_ds_name;
1817 if (!t->num_disk_storage_columns)
1818 return 1; /* nothing to do */
1819 if (old_obj->size == new_obj->size && !memcmp(new_obj->data,
1820 old_obj->data, new_obj->size))
1821 return 1; /* object did not change */
1822 old_ds_name = disk_storage_name_of_object(t, old_obj);
1823 new_ds_name = disk_storage_name_of_object(t, new_obj);
1824 FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
1825 char *old_filename, *new_filename;
1826 ret = create_disk_storage_object_dir(t, i, new_ds_name);
1829 old_filename = disk_storage_path(t, i, old_ds_name);
1830 new_filename = disk_storage_path(t, i, new_ds_name);
1831 ret = para_rename(old_filename, new_filename);
1846 * Change an object in an osl table.
1848 * \param t Pointer to an open osl table.
1849 * \param r Pointer to the row containing the object to be updated.
1850 * \param col_num Number of the column containing the object to be updated.
1851 * \param obj Pointer to the replacement object.
1853 * This function gets rid of all references to the old object. This includes
1854 * removal of the rbtree node in case there is an rbtree associated with \a
1855 * col_num. It then inserts \a obj into the table and the rbtree if necessary.
1857 * If the \p OSL_RBTREE flag is set for \a col_num, you \b MUST call this
1858 * function in order to change the contents of an object, even for volatile or
1859 * mapped columns of constant size (which may be updated directly if \p
1860 * OSL_RBTREE is not set). Otherwise the rbtree might become corrupted.
1864 int osl_update_object(struct osl_table *t, const struct osl_row *r,
1865 unsigned col_num, struct osl_object *obj)
1867 struct osl_column *col;
1868 const struct osl_column_description *cd;
1872 return -E_BAD_TABLE;
1873 col = &t->columns[col_num];
1874 cd = get_column_description(t->desc, col_num);
1875 DEBUG_LOG("updating column %u of %s\n", col_num, t->desc->name);
1876 if (cd->storage_flags & OSL_RBTREE) {
1877 if (search_rbtree(obj, t, col_num, NULL, NULL) > 0)
1878 return -E_RB_KEY_EXISTS;
1880 if (cd->storage_flags & OSL_FIXED_SIZE) {
1881 if (obj->size != cd->data_size)
1882 return -E_BAD_DATA_SIZE;
1884 remove_rb_node(t, col_num, r);
1885 if (cd->storage_type == OSL_NO_STORAGE) { /* TODO: If fixed size, reuse object? */
1886 free(r->volatile_objects[col->volatile_num].data);
1887 r->volatile_objects[col->volatile_num] = *obj;
1888 } else if (cd->storage_type == OSL_DISK_STORAGE) {
1890 ret = disk_storage_name_of_row(t, r, &ds_name);
1893 ret = delete_disk_storage_file(t, col_num, ds_name);
1894 if (ret < 0 && !is_errno(-ret, ENOENT)) {
1898 ret = write_disk_storage_file(t, col_num, obj, ds_name);
1902 } else { /* mapped storage */
1903 struct osl_object old_obj;
1904 ret = get_mapped_object(t, col_num, r->num, &old_obj);
1908 * If the updated column is the disk storage name column, the
1909 * disk storage name changes, so we have to rename all disk
1910 * storage objects accordingly.
1912 if (col_num == t->disk_storage_name_column) {
1913 ret = rename_disk_storage_objects(t, &old_obj, obj);
1917 if (cd->storage_flags & OSL_FIXED_SIZE)
1918 memcpy(old_obj.data, obj->data, cd->data_size);
1919 else { /* TODO: if the size doesn't change, use old space */
1920 uint32_t new_data_map_size;
1922 ret = get_row_index(t, r->num, &row_index);
1925 ret = mark_mapped_object_invalid(t, r->num, col_num);
1928 unmap_column(t, col_num);
1929 ret = append_map_file(t, col_num, obj,
1930 &new_data_map_size);
1933 ret = map_column(t, col_num);
1936 update_cell_index(row_index, col, new_data_map_size,
1940 if (cd->storage_flags & OSL_RBTREE) {
1941 ret = insert_rbtree(t, col_num, r, obj);
1949 * Retrieve an object of type \p OSL_DISK_STORAGE by row and column.
1951 * \param t Pointer to an open osl table.
1952 * \param r Pointer to the row containing the object.
1953 * \param col_num The column number.
1954 * \param obj Points to the result upon successful return.
1956 * For columns of type \p OSL_DISK_STORAGE, this function must be used to
1957 * retrieve one of its containing objects. Afterwards, osl_close_disk_object()
1958 * must be called in order to deallocate the resources.
1960 * \return Positive on success, negative on errors. Possible errors include:
1961 * \p E_BAD_TABLE, \p E_BAD_STORAGE_TYPE, errors returned by osl_get_object().
1963 * \sa osl_get_object(), osl_storage_type, osl_close_disk_object().
1965 int osl_open_disk_object(const struct osl_table *t, const struct osl_row *r,
1966 unsigned col_num, struct osl_object *obj)
1968 const struct osl_column_description *cd;
1969 char *ds_name, *filename;
1973 return -E_BAD_TABLE;
1974 cd = get_column_description(t->desc, col_num);
1975 if (cd->storage_type != OSL_DISK_STORAGE)
1976 return -E_BAD_STORAGE_TYPE;
1978 ret = disk_storage_name_of_row(t, r, &ds_name);
1981 filename = disk_storage_path(t, col_num, ds_name);
1983 DEBUG_LOG("filename: %s\n", filename);
1984 ret = mmap_full_file(filename, O_RDONLY, &obj->data, &obj->size, NULL);
1990 * Free resources that were allocated during osl_open_disk_object().
1992 * \param obj Pointer to the object previously returned by open_disk_object().
1994 * \return The return value of the underlying call to para_munmap().
1996 * \sa para_munmap().
1998 int osl_close_disk_object(struct osl_object *obj)
2000 return para_munmap(obj->data, obj->size);
2004 * Get the number of rows of the given table.
2006 * \param t Pointer to an open osl table.
2007 * \param num_rows Result is returned here.
2009 * The number of rows returned via \a num_rows excluding any invalid rows.
2011 * \return Positive on success, \p -E_BAD_TABLE if \a t is \p NULL.
2013 int osl_get_num_rows(const struct osl_table *t, unsigned *num_rows)
2016 return -E_BAD_TABLE;
2017 assert(t->num_rows >= t->num_invalid_rows);
2018 *num_rows = t->num_rows - t->num_invalid_rows;
2023 * Get the rank of a row.
2025 * \param t An open osl table.
2026 * \param r The row to get the rank of.
2027 * \param col_num The number of an rbtree column.
2028 * \param rank Result pointer.
2030 * The rank is, by definition, the position of the row in the linear order
2031 * determined by an in-order tree walk of the rbtree associated with column
2032 * number \a col_num of \a table.
2034 * \return Positive on success, negative on errors.
2036 * \sa osl_get_nth_row().
2038 int osl_get_rank(const struct osl_table *t, struct osl_row *r,
2039 unsigned col_num, unsigned *rank)
2041 struct osl_object obj;
2042 struct osl_column *col;
2043 struct rb_node *node;
2044 int ret = check_rbtree_col(t, col_num, &col);
2048 ret = osl_get_object(t, r, col_num, &obj);
2051 ret = search_rbtree(&obj, t, col_num, &node, NULL);
2054 ret = rb_rank(node, rank);
2061 * Get the row with n-th greatest value.
2063 * \param t Pointer to an open osl table.
2064 * \param col_num The column number.
2065 * \param n The rank of the desired row.
2066 * \param result Row is returned here.
2068 * Retrieve the n-th order statistic with respect to the compare function
2069 * of the rbtree column \a col_num. In other words, get that row with
2070 * \a n th greatest value in column \a col_num. It's an error if
2071 * \a col_num is not a rbtree column, or if \a n is larger than the
2072 * number of rows in the table.
2074 * \return Positive on success, negative on errors. Possible errors:
2075 * \p E_BAD_TABLE, \p E_BAD_STORAGE_FLAGS, \p E_RB_KEY_NOT_FOUND.
2077 * \sa osl_storage_flags, osl_compare_func, osl_get_row(),
2078 * osl_rbtree_last_row(), osl_rbtree_first_row(), osl_get_rank().
2080 int osl_get_nth_row(const struct osl_table *t, unsigned col_num,
2081 unsigned n, struct osl_row **result)
2083 struct osl_column *col;
2084 struct rb_node *node;
2089 return -E_RB_KEY_NOT_FOUND;
2090 ret = osl_get_num_rows(t, &num_rows);
2094 return -E_RB_KEY_NOT_FOUND;
2095 ret = check_rbtree_col(t, col_num, &col);
2098 node = rb_nth(col->rbtree.rb_node, n);
2100 return -E_RB_KEY_NOT_FOUND;
2101 *result = get_row_pointer(node, col->rbtree_num);
2106 * Get the row corresponding to the smallest rbtree node of a column.
2108 * \param t An open rbtree table.
2109 * \param col_num The number of the rbtree column.
2110 * \param result A pointer to the first row is returned here.
2112 * The rbtree node of the smallest object (with respect to the corresponding
2113 * compare function) is selected and the row containing this object is
2114 * returned. It is an error if \a col_num refers to a column without an
2115 * associated rbtree.
2117 * \return Positive on success, negative on errors.
2119 * \sa osl_get_nth_row(), osl_rbtree_last_row().
2121 int osl_rbtree_first_row(const struct osl_table *t, unsigned col_num,
2122 struct osl_row **result)
2124 return osl_get_nth_row(t, col_num, 1, result);
2128 * Get the row corresponding to the greatest rbtree node of a column.
2130 * \param t The same meaning as in \p osl_rbtree_first_row().
2131 * \param col_num The same meaning as in \p osl_rbtree_first_row().
2132 * \param result The same meaning as in \p osl_rbtree_first_row().
2134 * This function works just like osl_rbtree_first_row(), the only difference
2135 * is that the row containing the greatest rather than the smallest object is
2138 * \return Positive on success, negative on errors.
2140 * \sa osl_get_nth_row(), osl_rbtree_first_row().
2142 int osl_rbtree_last_row(const struct osl_table *t, unsigned col_num,
2143 struct osl_row **result)
2146 int ret = osl_get_num_rows(t, &num_rows);
2150 return osl_get_nth_row(t, col_num, num_rows, result);