Exemplo n.º 1
0
static int btreeSeqCreate(
    sqlite3_context *context, Btree *p, SEQ_COOKIE *cookie)
{
	BtShared *pBt;
	DBT key;
	DB_SEQUENCE *seq;
	char seq_key[BT_MAX_SEQ_NAME];
	int ret, seq_len;
	u_int32_t flags;

	pBt = p->pBt;

	if (cookie->cache != 0) {
		if ((ret = db_sequence_create(&seq, pBt->metadb, 0)) != 0)
			return ret;

		if (cookie->cache > 0)
			seq->set_cachesize(seq, cookie->cache);
		if (cookie->decrementing != 0)
			seq->set_flags(seq, DB_SEQ_DEC);

		/* Always set min_val and max_val */
		if ((ret =
		    seq->set_range(seq, cookie->min_val, cookie->max_val)) != 0)
			return ret;
		if ((ret = seq->initial_value(seq, cookie->start_val)) != 0)
			return ret;

		flags = DB_CREATE;
#ifndef BDBSQL_SINGLE_THREAD
		flags |= DB_THREAD;
#endif
		sqlite3_snprintf(
		    BT_MAX_SEQ_NAME, seq_key, "%s_db", cookie->name);
		seq_len = (int)strlen(seq_key);
		memset(&key, 0, sizeof(key));
		key.data = seq_key;
		key.size = key.ulen = seq_len;
		key.flags = DB_DBT_USERMEM;

		if ((ret = seq->open(seq, p->savepoint_txn, &key, flags)) != 0)
			return ret;

		cookie->handle = seq;
	} else {
		cookie->handle = NULL;
		cookie->val = cookie->start_val;
		if ((cookie->decrementing &&
		    cookie->start_val < cookie->min_val) ||
		    (!cookie->decrementing &&
		    cookie->start_val > cookie->max_val) ||
		    cookie->min_val == cookie->max_val)
			return EINVAL;
	}

	if ((ret = btreeSeqPutCookie(context, p, cookie, DB_NOOVERWRITE)) != 0)
		return (ret);

	return (0);
}
Exemplo n.º 2
0
DbSequence::DbSequence(Db *db, u_int32_t flags)
:	imp_(0)
{
	DB_SEQUENCE *seq;
	int ret;

	if ((ret = db_sequence_create(&seq, unwrap(db), flags)) != 0)
		DB_ERROR(db->get_env(), "DbSequence::DbSequence", ret,
		    ON_ERROR_UNKNOWN);
	else {
		imp_ = seq;
		seq->api_internal = this;
	}
}
Exemplo n.º 3
0
static int btreeSeqOpen(
    sqlite3_context *context, Btree *p, SEQ_COOKIE *cookie)
{
	BtShared *pBt;
	DBT key;
	DB_SEQUENCE *seq;
	char seq_key[BT_MAX_SEQ_NAME];
	int ret, seq_len;
	u_int32_t flags;

	pBt = p->pBt;

	if ((ret = btreeSeqGetCookie(context, p, cookie, 0)) != 0)
		return (ret);

	if (cookie->cache != 0) {
		if ((ret = db_sequence_create(&seq, pBt->metadb, 0)) != 0)
			return ret;

		seq->set_cachesize(seq, cookie->cache);
		flags = 0;
#ifdef BDBSQL_SINGLE_THREAD
		flags |= DB_THREAD;
#endif
		sqlite3_snprintf(
		    BT_MAX_SEQ_NAME, seq_key, "%s_db", cookie->name);
		seq_len = (int)strlen(seq_key);
		memset(&key, 0, sizeof(key));
		key.data = seq_key;
		key.size = key.ulen = seq_len;
		key.flags = DB_DBT_USERMEM;

		if ((ret = seq->open(
		    seq, NULL, &key, flags)) != 0) {
			(void)seq->close(seq, 0);
			return ret;
		}
		cookie->handle = seq;
	}

	return (0);
}
Exemplo n.º 4
0
/* --- Object ctors/dtors */
static DB_SEQUENCE *
rpmseq_init(JSContext *cx, JSObject *obj, DB * db, uint32_t flags)
{
    DB_SEQUENCE * seq = NULL;
    int ret = db_sequence_create(&seq, db, flags);

    if (ret || seq == NULL || !JS_SetPrivate(cx, obj, (void *)seq)) {
	if (db)
	    db->err(db, ret, "db_sequence_create");
        else
            fprintf(stderr, "db_sequence_create: %s\n", db_strerror(ret));
	if (seq)
	    ret = seq->close(seq, 0);
	seq = NULL;
    }

if (_debug)
fprintf(stderr, "<== %s(%p,%p) seq %p\n", __FUNCTION__, cx, obj, seq);

    return seq;
}
static int create_seq_handle(DB_SEQUENCE **seqpp, DB *dbp) {
	int ret;
	if ((ret = db_sequence_create(seqpp, dbp, 0)) == 0)
		info.seqp = *seqpp;
	return ret;
}
Exemplo n.º 6
0
DB_SEQUENCE * db_sequence_create2(DB *db, u_int32_t flags, int *errno) {
  DB_SEQUENCE * seq;
  *errno = db_sequence_create(&seq, db, flags);
  return seq;
}
Exemplo n.º 7
0
static la_storage_open_result_t bdb_la_storage_open(la_storage_env *env, const char *path, int flags, la_storage_object_store **_store)
{
    la_storage_object_store *store = (la_storage_object_store *) malloc(sizeof(struct la_storage_object_store));
    DB_TXN *txn;
    char *seqpath;
    DBT seq_key;
    char seq_name[4];
    int dbflags;
    int ret;

    if (store == NULL)
        return LA_STORAGE_OPEN_ERROR;
    store->env = env;
    if (txn_begin(env->env, NULL, &txn, DB_TXN_WRITE_NOSYNC) != 0)
    {
        free(store);
        return LA_STORAGE_OPEN_ERROR;
    }
    if (db_create(&store->db, env->env, 0) != 0)
    {
        txn_abort(txn);
        free(store);
        return LA_STORAGE_OPEN_ERROR;
    }
    if (db_create(&store->seq_db, env->env, 0) != 0)
    {
        txn_abort(txn);
        free(store);
        return LA_STORAGE_OPEN_ERROR;
    }
    dbflags = 0;
    if (flags & LA_STORAGE_OPEN_FLAG_CREATE)
        dbflags = DB_CREATE;
    if (flags & LA_STORAGE_OPEN_FLAG_EXCL)
        dbflags |= DB_EXCL;
    if ((ret = store->db->open(store->db, txn, path, NULL, DB_BTREE, dbflags | DB_MULTIVERSION | DB_THREAD, 0)) != 0)
    {
        txn_abort(txn);
        free(store);
        if (ret == EEXIST)
            return LA_STORAGE_OPEN_EXISTS;
        if (ret == ENOENT)
            return LA_STORAGE_OPEN_NOT_FOUND;
        return LA_STORAGE_OPEN_ERROR;
    }
    seqpath = string_append(path, ".seq");
    if (seqpath == NULL)
    {
        store->db->close(store->db, 0);
        txn_abort(txn);
        free(store);
        return LA_STORAGE_OPEN_ERROR;
    }
    if (store->seq_db->set_bt_compare(store->seq_db, compare_seq) != 0)
    {
        store->db->close(store->db, 0);
        txn_abort(txn);
        free(store);
        return LA_STORAGE_OPEN_ERROR;
    }
    if (store->seq_db->open(store->seq_db, txn, seqpath, NULL, DB_BTREE, DB_CREATE | DB_THREAD, 0) != 0)
    {
        free(seqpath);
        store->db->close(store->db, 0);
        txn_abort(txn);
        free(store);
        return LA_STORAGE_OPEN_ERROR;
    }
    free(seqpath);
    store->db->associate(store->db, txn, store->seq_db, seqindex, 0);
    if (db_sequence_create(&store->seq, store->db, 0) != 0)
    {
        store->seq_db->close(store->seq_db, 0);
        store->db->close(store->db, 0);
        txn_abort(txn);
        free(store);
        return LA_STORAGE_OPEN_ERROR;
    }
    store->seq->initial_value(store->seq, 1);
    seq_name[0] = '\0';
    seq_name[1] = 'S';
    seq_name[2] = 'E';
    seq_name[3] = 'Q';
    seq_key.data = seq_name;
    seq_key.size = 4;
    seq_key.ulen = 4;
    seq_key.flags = DB_DBT_USERMEM;
    if (store->seq->open(store->seq, txn, &seq_key, DB_CREATE | DB_THREAD) != 0)
    {
        store->seq_db->close(store->seq_db, 0);
        store->db->close(store->db, 0);
        txn_abort(txn);
        free(store);
        return LA_STORAGE_OPEN_ERROR;        
    }
    txn_commit(txn, DB_TXN_NOSYNC);
    *_store = store;
    if ((flags & (LA_STORAGE_OPEN_FLAG_CREATE|LA_STORAGE_OPEN_FLAG_EXCL)) == (LA_STORAGE_OPEN_FLAG_CREATE|LA_STORAGE_OPEN_FLAG_EXCL))
        return LA_STORAGE_OPEN_CREATED;
    return LA_STORAGE_OPEN_OK;
}