inline static void *__child_push(dbtree *tree, const char *key, void *value, unsigned int size) { key++; while (*key != (char) 0) { dbtree_create(tmp); tmp->c = *key; key++; tmp->value = NULL; tmp->child = NULL; tmp->previous = NULL; tmp->next = NULL; tmp->parent = tree; tree = tree->child = tmp; } tree->size = size; if (size > 0) { tree->value = malloc(size); memcpy(tree->value, value, size); } else { tree->value = value; } return tree->value; }
void *dbtree_store(dbtree *tree, const char *key, void *value, unsigned int size) { if (tree == NULL) return NULL; while (*key++ != (char) 0) { while (tree->c != *key && tree->next != NULL) tree = tree->next; if (tree->c == *key && *key > (char) 0) { if (*(key+1) == (char) 0) { if (tree->value != NULL) free(tree->value); return tree->value = value; } if (tree->child != NULL) tree = tree->child; else return __child_push(tree, key, value, size); } else { dbtree_create(tmp); tmp->c = *key; tmp->value = NULL; tmp->child = NULL; tmp->next = NULL; tmp->parent = NULL; tmp->previous = tree; tree = tree->next = tmp; return __child_push(tree, key, value, size); } } return NULL; }
static int ik_btr_open_create(bool create, char *args) { bool inplace = false; uint64_t feats = 0; int rc; if (!daos_handle_is_inval(ik_toh)) { D_ERROR("Tree has been opened\n"); return -1; } if (create && args != NULL) { if (args[0] == '+') { feats = BTR_FEAT_UINT_KEY; args += 1; } if (args[0] == 'i') { /* inplace create/open */ inplace = true; if (args[1] != IK_SEP) { D_ERROR("wrong parameter format %s\n", args); return -1; } args += 2; } if (args[0] != 'o' || args[1] != IK_SEP_VAL) { D_ERROR("incorrect format for tree order: %s\n", args); return -1; } ik_order = atoi(&args[2]); if (ik_order < BTR_ORDER_MIN || ik_order > BTR_ORDER_MAX) { D_ERROR("Invalid tree order %d\n", ik_order); return -1; } } else if (!create) { inplace = (ik_root->tr_class != 0); if (TMMID_IS_NULL(ik_root_mmid) && !inplace) { D_ERROR("Please create tree first\n"); return -1; } } if (create) { D_PRINT("Create btree with order %d%s feats "DF_X64"\n", ik_order, inplace ? " inplace" : "", feats); if (inplace) { rc = dbtree_create_inplace(IK_TREE_CLASS, feats, ik_order, ik_uma, ik_root, &ik_toh); } else { rc = dbtree_create(IK_TREE_CLASS, feats, ik_order, ik_uma, &ik_root_mmid, &ik_toh); } } else { D_PRINT("Open btree%s\n", inplace ? " inplace" : ""); if (inplace) { rc = dbtree_open_inplace(ik_root, ik_uma, &ik_toh); } else { rc = dbtree_open(ik_root_mmid, ik_uma, &ik_toh); } } if (rc != 0) { D_ERROR("Tree %s failed: %d\n", create ? "create" : "open", rc); return -1; } return 0; }