Exemplo n.º 1
0
LIBCOUCHSTORE_API
couchstore_error_t couchstore_all_docs(Db *db,
                                       const sized_buf* startKeyPtr,
                                       couchstore_docinfos_options options,
                                       couchstore_changes_callback_fn callback,
                                       void *ctx)
{
    sized_buf startKey = {NULL, 0};
    sized_buf *keylist = &startKey;
    lookup_context cbctx = {db, options, callback, ctx, 1, 0, NULL};
    couchfile_lookup_request rq;
    sized_buf cmptmp;
    couchstore_error_t errcode;

    if (db->header.by_id_root == NULL) {
        return COUCHSTORE_SUCCESS;
    }

    if (startKeyPtr) {
        startKey = *startKeyPtr;
    }

    rq.cmp.compare = ebin_cmp;
    rq.cmp.arg = &cmptmp;
    rq.file = &db->file;
    rq.num_keys = 1;
    rq.keys = &keylist;
    rq.callback_ctx = &cbctx;
    rq.fetch_callback = lookup_callback;
    rq.node_callback = NULL;
    rq.fold = 1;

    errcode = btree_lookup(&rq, db->header.by_id_root->pointer);
    return errcode;
}
Exemplo n.º 2
0
blob btree_dtable::lookup(const dtype & key, bool * found, ATX_DEF) const
{
	size_t index = btree_lookup(key, found);
	if(!*found)
		return blob();
	return base->index(index);
}
Exemplo n.º 3
0
bool btree_dtable::present(const dtype & key, bool * found, ATX_DEF) const
{
	size_t index = btree_lookup(key, found);
	if(!*found)
		return false;
	return base->contains_index(index);
}
Exemplo n.º 4
0
static couchstore_error_t couchstore_print_local_docs(Db *db, int *count)
{
    sized_buf key;
    sized_buf *keylist = &key;
    couchfile_lookup_request rq;
    couchstore_error_t errcode;

    if (db->header.local_docs_root == NULL) {
        if (oneKey) {
            return COUCHSTORE_ERROR_DOC_NOT_FOUND;
        } else {
            return COUCHSTORE_SUCCESS;
        }
    }

    key.buf = (char *)"\0";
    key.size = 0;

    rq.cmp.compare = ebin_cmp;
    rq.file = &db->file;
    rq.num_keys = 1;
    rq.keys = &keylist;
    rq.callback_ctx = count;
    rq.fetch_callback = local_doc_print;
    rq.node_callback = NULL;
    rq.fold = 1;

    if (oneKey) {
        rq.fold = 0;
        key = dumpKey;
    }

    errcode = btree_lookup(&rq, db->header.local_docs_root->pointer);
    return errcode;
}
Exemplo n.º 5
0
inline struct vnode *get_real_node(struct vnode *node)
{
    struct vfs *vfs_mounted = btree_lookup(node->v_vfs->vfs_mounts, (void*) node->v_inode);
    if (vfs_mounted)
        return vfs_mounted->v_root;

    return node;
}
Exemplo n.º 6
0
int test_btree(string **values, int n){
  int i;
  btree *tree = make_btree();
  uint64_t *hashes = xmalloc(n*sizeof(uint64_t));
  uint64_t *indices = xmalloc(n*sizeof(uint64_t));
  //nfor now I'm just using sequential ints for keys
  DEBUG_PRINTF("Testing btree insertion\n");
  for(i=0;i<n;i++){
    assert(check_tree(tree) >= 0);
    indices[i] = i;
    hashes[i] = fnv_hash(values[i]->mem, values[i]->len);
    btree_insert(tree, hashes[indices[i]], values[i]);
  }
  assert(check_tree(tree) >= 0);
  shuffle_array((void**)indices, n);
  DEBUG_PRINTF("Testing btree lookup\n");
  for(i=0;i<n;i++){
    string *str = btree_lookup(tree, hashes[indices[i]]);
    WARN_ON_ONCE(!string_ptr_eq(str, values[indices[i]]));
  }
  DEBUG_PRINTF("Testing btree deletion\n");  
  for(i=0;i<n;i++){
    uint64_t num_keys = count_num_keys(tree->root);
    if(num_keys != (n-i)){
      DEBUG_PRINTF("Tree has %lu keys, expected %lu\n",
                   num_keys, n-i);
      exit(1);
    }
    assert(check_tree(tree) >= 0);
    if(btree_lookup(tree, hashes[indices[i]])){
      if(!btree_delete(tree, hashes[indices[i]])){
        exit(1);
      }
    } else {
      DEBUG_PRINTF("Couldn't find key %lu\n",indices[i]);
    }
  }
  WARN_ON(check_tree(tree) < 0);
  WARN_ON(tree->root->n_keys != 0);
  return 0;
}
Exemplo n.º 7
0
// Common subroutine of couchstore_docinfos_by_{ids, sequence}
static couchstore_error_t iterate_docinfos(Db *db,
        const sized_buf keys[],
        unsigned numDocs,
        node_pointer *tree,
        int (*key_ptr_compare)(const void *, const void *),
        int (*key_compare)(const sized_buf *k1, const sized_buf *k2),
        couchstore_changes_callback_fn callback,
        int fold,
        void *ctx)
{
    // Nothing to do if the tree is empty
    if (tree == NULL) {
        return COUCHSTORE_SUCCESS;
    }

    // Create an array of *pointers to* sized_bufs, which is what btree_lookup wants:
    const sized_buf **keyptrs = malloc(numDocs * sizeof(sized_buf*));
    if (!keyptrs) {
        return COUCHSTORE_ERROR_ALLOC_FAIL;
    }
    unsigned i;
    for (i = 0; i< numDocs; ++i) {
        keyptrs[i] = &keys[i];
    }
    if (!fold) {
        // Sort the key pointers:
        qsort(keyptrs, numDocs, sizeof(keyptrs[0]), key_ptr_compare);
    }

    // Construct the lookup request:
    lookup_context cbctx = {db, 0, callback, ctx, (tree == db->header.by_id_root), 0, NULL};
    couchfile_lookup_request rq;
    sized_buf cmptmp;
    rq.cmp.compare = key_compare;
    rq.cmp.arg = &cmptmp;
    rq.file = &db->file;
    rq.num_keys = numDocs;
    rq.keys = (sized_buf**) keyptrs;
    rq.callback_ctx = &cbctx;
    rq.fetch_callback = lookup_callback;
    rq.node_callback = NULL;
    rq.fold = fold;

    // Go!
    couchstore_error_t errcode = btree_lookup(&rq, tree->pointer);

    free(keyptrs);
    return errcode;
}
Exemplo n.º 8
0
static
couchstore_error_t couchstore_walk_tree(Db *db,
                                        int by_id,
                                        const node_pointer* root,
                                        const sized_buf* startKeyPtr,
                                        couchstore_docinfos_options options,
                                        int (*compare)(const sized_buf *k1, const sized_buf *k2),
                                        couchstore_walk_tree_callback_fn callback,
                                        void *ctx)
{
    couchstore_error_t errcode;

    if (root == NULL) {
        return COUCHSTORE_SUCCESS;
    }

    // Invoke the callback on the root node:
    errcode = callback(db, 0, NULL,
                       root->subtreesize,
                       &root->reduce_value,
                       ctx);
    if (errcode < 0) {
        return errcode;
    }

    sized_buf startKey = {NULL, 0};
    if (startKeyPtr) {
        startKey = *startKeyPtr;
    }
    sized_buf *keylist = &startKey;

    lookup_context lookup_ctx = {db, options, NULL, ctx, by_id, 1, callback};
    sized_buf cmptmp;
    couchfile_lookup_request rq;

    rq.cmp.compare = compare;
    rq.cmp.arg = &cmptmp;
    rq.file = &db->file;
    rq.num_keys = 1;
    rq.keys = &keylist;
    rq.callback_ctx = &lookup_ctx;
    rq.fetch_callback = lookup_callback;
    rq.node_callback = walk_node_callback;
    rq.fold = 1;

    return btree_lookup(&rq, root->pointer);
}
Exemplo n.º 9
0
/* Btree iterator helper function */
static couchstore_error_t iter_btree(tree_file *file, node_pointer *root,
                                                    void *ctx,
                                                    fetch_callback_fn callback)
{
    couchfile_lookup_request rq;
    sized_buf k = {NULL, 0};
    sized_buf *keys = &k;

    rq.cmp.compare = int_cmp;
    rq.file = file;
    rq.num_keys = 1;
    rq.keys = &keys;
    rq.callback_ctx = ctx;
    rq.fetch_callback = callback;
    rq.node_callback = NULL;
    rq.fold = 1;

    return btree_lookup(&rq, root->pointer);
}
Exemplo n.º 10
0
LIBCOUCHSTORE_API
couchstore_error_t couchstore_open_local_document(Db *db,
        const void *id,
        size_t idlen,
        LocalDoc **pDoc)
{
    sized_buf key;
    sized_buf *keylist = &key;
    couchfile_lookup_request rq;
    sized_buf cmptmp;
    couchstore_error_t errcode;

    if (db->header.local_docs_root == NULL) {
        return COUCHSTORE_ERROR_DOC_NOT_FOUND;
    }

    key.buf = (char *) id;
    key.size = idlen;

    rq.cmp.compare = ebin_cmp;
    rq.cmp.arg = &cmptmp;
    rq.file = &db->file;
    rq.num_keys = 1;
    rq.keys = &keylist;
    rq.callback_ctx = pDoc;
    rq.fetch_callback = local_doc_fetch;
    rq.node_callback = NULL;
    rq.fold = 0;

    errcode = btree_lookup(&rq, db->header.local_docs_root->pointer);
    if (errcode == COUCHSTORE_SUCCESS) {
        if (*pDoc == NULL) {
            errcode = COUCHSTORE_ERROR_DOC_NOT_FOUND;
        }
    }
    return errcode;
}
Exemplo n.º 11
0
LIBCOUCHSTORE_API
couchstore_error_t couchstore_docinfo_by_sequence(Db *db,
        uint64_t sequence,
        DocInfo **pInfo)
{
    sized_buf key;
    sized_buf *keylist = &key;
    couchfile_lookup_request rq;
    sized_buf cmptmp;
    couchstore_error_t errcode;

    if (db->header.by_id_root == NULL) {
        return COUCHSTORE_ERROR_DOC_NOT_FOUND;
    }

    sequence = htonll(sequence);
    key.buf = (char *)&sequence + 2;
    key.size = 6;

    rq.cmp.compare = seq_cmp;
    rq.cmp.arg = &cmptmp;
    rq.file = &db->file;
    rq.num_keys = 1;
    rq.keys = &keylist;
    rq.callback_ctx = pInfo;
    rq.fetch_callback = docinfo_fetch_by_seq;
    rq.node_callback = NULL;
    rq.fold = 0;

    errcode = btree_lookup(&rq, db->header.by_seq_root->pointer);
    if (errcode == COUCHSTORE_SUCCESS) {
        if (*pInfo == NULL) {
            errcode = COUCHSTORE_ERROR_DOC_NOT_FOUND;
        }
    }
    return errcode;
}
Exemplo n.º 12
0
LIBCOUCHSTORE_API
couchstore_error_t couchstore_docinfo_by_id(Db *db,
                                            const void *id,
                                            size_t idlen,
                                            DocInfo **pInfo)
{
    sized_buf key;
    sized_buf *keylist = &key;
    couchfile_lookup_request rq;
    sized_buf cmptmp;
    couchstore_error_t errcode;

    if (db->header.by_id_root == NULL) {
        return COUCHSTORE_ERROR_DOC_NOT_FOUND;
    }

    key.buf = (char *) id;
    key.size = idlen;

    rq.cmp.compare = ebin_cmp;
    rq.cmp.arg = &cmptmp;
    rq.db = db;
    rq.num_keys = 1;
    rq.keys = &keylist;
    rq.callback_ctx = pInfo;
    rq.fetch_callback = docinfo_fetch_by_id;
    rq.node_callback = NULL;
    rq.fold = 0;

    errcode = btree_lookup(&rq, db->header.by_id_root->pointer);
    if (errcode == COUCHSTORE_SUCCESS) {
        if (*pInfo == NULL) {
            errcode = COUCHSTORE_ERROR_DOC_NOT_FOUND;
        }
    }
    return errcode;
}
Exemplo n.º 13
0
LIBCOUCHSTORE_API
couchstore_error_t couchstore_changes_since(Db *db,
        uint64_t since,
        couchstore_docinfos_options options,
        couchstore_changes_callback_fn callback,
        void *ctx)
{
    char since_termbuf[6];
    sized_buf since_term;
    sized_buf *keylist = &since_term;
    lookup_context cbctx = {db, options, callback, ctx, 0, 0, NULL};
    couchfile_lookup_request rq;
    sized_buf cmptmp;
    couchstore_error_t errcode;

    if (db->header.by_seq_root == NULL) {
        return COUCHSTORE_SUCCESS;
    }

    since_term.buf = since_termbuf;
    since_term.size = 6;
    *(raw_48*)since_term.buf = encode_raw48(since);

    rq.cmp.compare = seq_cmp;
    rq.cmp.arg = &cmptmp;
    rq.file = &db->file;
    rq.num_keys = 1;
    rq.keys = &keylist;
    rq.callback_ctx = &cbctx;
    rq.fetch_callback = lookup_callback;
    rq.node_callback = NULL;
    rq.fold = 1;

    errcode = btree_lookup(&rq, db->header.by_seq_root->pointer);
    return errcode;
}