コード例 #1
0
/*
  Initialize database.
  Returns: pointer to database handle on success, NULL otherwise.
*/
void *db_open(void * dummy, bfpath *bfp, dbmode_t open_mode)
{
    dbh_t *handle;

    int open_flags;
    VILLA *dbp;

    UNUSED(dummy);

    if (open_mode & DS_WRITE)
	open_flags = VL_OWRITER;
    else
	open_flags = VL_OREADER;

    handle = dbh_init(bfp);

    if (handle == NULL) return NULL;

    dbp = handle->dbp = vlopen(handle->name, open_flags, cmpkey);

    if ((dbp == NULL) && (open_mode & DS_WRITE)) {
	dbp = handle->dbp = vlopen(handle->name, open_flags|VL_OCREAT, cmpkey);
	if (dbp != NULL)
	    handle->created = true;
    }

    if (dbp == NULL)
	goto open_err;

    if (DEBUG_DATABASE(1))
	fprintf(dbgout, "(qdbm) vlopen( %s, %d )\n", handle->name, open_mode);

    return handle;

 open_err:
    print_error(__FILE__, __LINE__, "(qdbm) vlopen(%s, %d), err: %d, %s",
		handle->name, open_flags, 
		dpecode, dperrmsg(dpecode));
    dbh_free(handle);

    return NULL;
}
コード例 #2
0
ファイル: hcache.c プロジェクト: srg-imperial/mutt
static int
hcache_open_qdbm (struct header_cache* h, const char* path)
{
  int    flags = VL_OWRITER | VL_OCREAT;

  if (option(OPTHCACHECOMPRESS))
    flags |= VL_OZCOMP;

  h->db = vlopen (path, flags, VL_CMPLEX);
  if (h->db)
    return 0;
  else
    return -1;
}
コード例 #3
0
ファイル: JsonDb.cpp プロジェクト: kleunen/JsonDb
JsonDb::Transaction::Transaction(std::string const &filename, ValuePointer const &_null_element)
{
	/* The null element */
	null_element = _null_element;

  /* open the database */
	db = StorageDbPointer(vlopen(filename.c_str(), VL_OWRITER | VL_OCREAT, VL_CMPINT), CloseDatabase);

	/* Start the transaction */
	vltranbegin(db.get());

	if(db.get() == NULL)
    throw std::runtime_error((boost::format("Failed to open database: %s") % dperrmsg(dpecode)).str().c_str());
	
	ValuePointer root = Retrieve(root_key);
	if(root.get() == NULL)
	{
		root = ValuePointer(new ValueObject(root_key));
		Store(root->GetKey(), root);
	}

	ValuePointer next_id_value = Retrieve(next_id_key);
	if(next_id_value.get() == NULL)
	{
		// Initialize values
		next_id = initial_next_id;
		start_next_id = next_id;
	} else
	{
		// Retrieve values from database
		next_id = next_id_value->GetValueInt();
		start_next_id = next_id;
	}

	// std::cout << "Start transaction, next id: " << next_id << std::endl;
}
コード例 #4
0
ファイル: test.c プロジェクト: ADTSH/io
int main(void)
{
	VILLA *villa;
	int size;
	char *s;
	
	printf("compareFunc(\"a\", \"b\") = %i\n", compareFunc("a", 1, "b", 1));
	printf("compareFunc(\"b\", \"a\") = %i\n", compareFunc("b", 1, "a", 1));
	printf("compareFunc(\"a\", \"a\") = %i\n", compareFunc("a", 1, "a", 1));
	printf("compareFunc(\"b\", \"b\") = %i\n", compareFunc("b", 1, "b", 1));
	
	remove("test.qdbm");
	
	if(!(villa = vlopen("test.qdbm", VL_OWRITER | VL_OCREAT, compareFunc)))
	{
		printf("vlopen failed %s\n", dperrmsg(dpecode));
		return 1;
	}

	if(!vltranbegin(villa))
	{
		printf("vltranbegin failed %s\n", dperrmsg(dpecode));
		return 1;	
	}
		
	if(!vlput(villa, "a", 1, "1", 1, DP_DOVER))
	{
		printf("vlput failed %s\n", dperrmsg(dpecode));
		return 1;
	}
	
	if(!vlput(villa, "b", 1, "2", 1, DP_DOVER))
	{
		printf("vlput failed %s\n", dperrmsg(dpecode));
		return 1;
	}
	
	if(!vltrancommit(villa))
	{
		printf("vltrancommit failed %s\n", dperrmsg(dpecode));
		return 1;	
	}
	
	if(!vlclose(villa))
	{
		printf("vlclose failed %s\n", dperrmsg(dpecode));
		return 1;
	}
	
	if(!(villa = vlopen("test.qdbm", VL_OWRITER | VL_OCREAT, compareFunc)))
	{
		printf("vlopen failed %s\n", dperrmsg(dpecode));
		return 1;
	}
	
	s = vlget(villa, "a", 1, &size);
	printf("a = '%s'\n", s ? s : "NULL");

	s = vlget(villa, "b", 1, &size);
	printf("b = '%s'\n", s ? s : "NULL");

	if(!vlclose(villa))
	{
		printf("vlclose failed %s\n", dperrmsg(dpecode));
		return 1;
	}
		
	return 0;
}
コード例 #5
0
ファイル: mkcensusdb.cpp プロジェクト: WPettersson/regina
int main(int argc, char* argv[]) {
    // Parse the command line.
    if (argc != 3)
        usage(argv[0]);
    std::string outputFile = argv[2];

    // Open the input file.
    std::cout << "Processing: " << argv[1] << std::endl;
    std::ifstream file(argv[1], std::ios_base::in | std::ios_base::binary);
    if (! file) {
        std::cerr << "ERROR: Could not open input file: " << argv[1]
            << std::endl;
        std::exit(1);
    }

    ::boost::iostreams::filtering_istream in;
    if (file.peek() == 0x1f)
        in.push(::boost::iostreams::gzip_decompressor());
    in.push(file);

    // Initialise the database.
#ifdef QDBM_AS_TOKYOCABINET
    VILLA* db;
    if (! (db = vlopen(outputFile.c_str(),
            VL_OWRITER | VL_OCREAT | VL_OTRUNC | VL_OZCOMP, VL_CMPLEX))) {
        std::cerr << "ERROR: Could not open QDBM database: "
            << outputFile << std::endl;
        std::exit(1);
    }
#else
    TCBDB* db = tcbdbnew();
    if (! tcbdbopen(db, outputFile.c_str(),
            BDBOWRITER | BDBOCREAT | BDBOTRUNC)) {
        std::cerr << "ERROR: Could not open Tokyo Cabinet database: "
            << outputFile << std::endl;
        std::exit(1);
    }
#endif

    // Fill the database with the user-supplied key-value pairs.
    std::string sig, name;
    const char* pos;
    unsigned long tot = 0;
    while (true) {
        in >> sig;
        if (in.eof())
            break;

        std::getline(in, name);
        if (in.eof()) {
            std::cerr << "ERROR: Signature " << sig
                << " is missing a corresponding name.\n\n";
            DB_CLOSE(db);
            usage(argv[0]);
        }

        // Skip initial whitespace in the manifold name (which will
        // always be present, since the previous in >> sig
        // does not eat the separating whitespace).
        pos = name.c_str();
        while (*pos && std::isspace(*pos))
            ++pos;
        if (! *pos) {
            std::cerr << "ERROR: Signature " << sig
                << " has an empty name.\n\n";
            DB_CLOSE(db);
            usage(argv[0]);
        }

#ifdef QDBM_AS_TOKYOCABINET
        if (! vlput(db, sig.c_str(), sig.length(),
                pos, -1 /* strlen */, VL_DDUP)) {
#else
        if (! tcbdbputdup2(db, sig.c_str(), pos)) {
#endif
            std::cerr << "ERROR: Could not store the record for "
                << sig << " in the database." << std::endl;
            DB_CLOSE(db);
            std::exit(1);
        }
        ++tot;
    }

    // Close and tidy up.
#ifdef QDBM_AS_TOKYOCABINET
    if (! vloptimize(db)) {
        std::cerr << "ERROR: Could not optimise QDBM database: "
            << outputFile << std::endl;
        DB_CLOSE(db);
        std::exit(1);
    }

    if (! vlclose(db)) {
        std::cerr << "ERROR: Could not close QDBM database: "
            << outputFile << std::endl;
        std::exit(1);
    }
#else
    // The following call to tcbdboptimise() does not change any options
    // other than the bitwise compression option given in the final argument.
    if (! tcbdboptimize(db, 0, 0, 0, -1, -1, BDBTBZIP)) {
        std::cerr << "ERROR: Could not optimise Tokyo Cabinet database: "
            << outputFile << std::endl;
        std::cerr << "Tokyo cabinet error: " << tcerrmsg(tcbdbecode(db))
            << std::endl;
        DB_CLOSE(db);
        std::exit(1);
    }

    if (! tcbdbclose(db)) {
        std::cerr << "ERROR: Could not close Tokyo Cabinet database: "
            << outputFile << std::endl;
        tcbdbdel(db);
        std::exit(1);
    }
    tcbdbdel(db);
#endif

    std::cout << "Success: " << tot << " records." << std::endl;
    return 0;
}