static void
print_environment(ham_env_t *env)
{
    /*
     * we need a temp. database
     */
    ham_db_t *db;
    ham_status_t st;

    st=ham_new(&db);
    if (st)
        error("ham_new", st);

    st=ham_env_open_db(env, db, 0xf001, 0, 0);
    if (st)
        error("ham_env_open_db", st);

    printf("environment\n");
    printf("    pagesize:                   %u\n", env_get_pagesize(env));
    printf("    version:                    %u.%u.%u.%u\n", 
            env_get_version(env, 0),
            env_get_version(env, 1),
            env_get_version(env, 2),
            env_get_version(env, 3));
    printf("    serialno:                   %u\n", env_get_serialno(env));
    printf("    max databases:              %u\n", env_get_max_databases(env));

    st=ham_close(db, 0);
    if (st)
        error("ham_close", st);
    ham_delete(db);
}
Пример #2
0
int
main(int argc, char **argv)
{
    ham_status_t st;      /* status variable */
    ham_db_t *db;         /* hamsterdb database object */
    ham_cursor_t *cursor; /* a database cursor */
    char line[1024*4];    /* a buffer for reading lines */
    ham_key_t key;
    ham_record_t record;

    memset(&key, 0, sizeof(key));
    memset(&record, 0, sizeof(record));

    printf("This sample uses hamsterdb to list all words in the "
            "original order.\n");
    printf("Reading from stdin...\n");

    /*
     * first step: create a new hamsterdb object
     */
    st=ham_new(&db);
    if (st!=HAM_SUCCESS) {
        printf("ham_new() failed with error %d\n", st);
        return (-1);
    }

    /*
     * second step: create a new hamsterdb "record number" Database
     *
     * we could create an in-memory-database to speed up the sorting.
     */
    st=ham_create(db, "test.db", HAM_RECORD_NUMBER, 0664);
    if (st!=HAM_SUCCESS) {
        printf("ham_create() failed with error %d\n", st);
        return (-1);
    }

    /*
     * now we read each line from stdin and split it in words; then each
     * word is inserted into the database
     */
    while (fgets(line, sizeof(line), stdin)) {
        char *start=line, *p;

        /*
         * strtok is not the best function because it's not threadsafe
         * and not flexible, but it's good enough for this example.
         */
        while ((p=strtok(start, " \t\r\n"))) {
            ham_u64_t recno;

            key.flags=HAM_KEY_USER_ALLOC;
            key.data=&recno;
            key.size=sizeof(recno);

            record.data=p;
            record.size=(ham_size_t)strlen(p)+1; /* also store terminating 0 */

            st=ham_insert(db, 0, &key, &record, 0);
            if (st!=HAM_SUCCESS && st!=HAM_DUPLICATE_KEY) {
                printf("ham_insert() failed with error %d\n", st);
                return (-1);
            }
            printf(".");

            start=0;
        }
    }

    /*
     * create a cursor
     */
    st=ham_cursor_create(db, 0, 0, &cursor);
    if (st!=HAM_SUCCESS) {
        printf("ham_cursor_create() failed with error %d\n", st);
        return (-1);
    }

    /*
     * iterate over all items and print the records
     */
    while (1) {
        st=ham_cursor_move(cursor, &key, &record, HAM_CURSOR_NEXT);
        if (st!=HAM_SUCCESS) {
            /* reached end of the database? */
            if (st==HAM_KEY_NOT_FOUND)
                break;
            else {
                printf("ham_cursor_next() failed with error %d\n", st);
                return (-1);
            }
        }

        /*
         * print the record number and the word
         */
#if WIN32
        printf("%I64u: %s\n", *(ham_u64_t *)key.data,
                (const char *)record.data);
#else
        printf("%llu: %s\n", *(unsigned long long *)key.data,
                (const char *)record.data);
#endif
    }

    /*
     * then close the database handle; the flag
     * HAM_AUTO_CLEANUP will automatically close all cursors, and we
     * do not need to call ham_cursor_close
     */
    st=ham_close(db, HAM_AUTO_CLEANUP);
    if (st!=HAM_SUCCESS) {
        printf("ham_close() failed with error %d\n", st);
        return (-1);
    }

    /*
     * delete the database object to avoid memory leaks
     */
    ham_delete(db);

    /*
     * success!
     */
    return (0);
}
Пример #3
0
int 
main(int argc, char **argv)
{
    int i;
    ham_status_t st;       /* status variable */
    ham_db_t *db;          /* hamsterdb database object */
    ham_key_t key;         /* the structure for a key */
    ham_record_t record;   /* the structure for a record */

    memset(&key, 0, sizeof(key));
    memset(&record, 0, sizeof(record));

    /*
     * first step: create a new hamsterdb object 
     */
    st=ham_new(&db);
    if (st!=HAM_SUCCESS)
        error("ham_new", st);

    /*
     * second step: create a new hamsterdb database
     *
     * we could also use ham_create_ex() if we wanted to specify the 
     * page size, key size or cache size limits
     */
    st=ham_create_ex(db, "test.db", 0, 0664, 0);
    if (st!=HAM_SUCCESS)
        error("ham_create", st);

    /*
     * now we can insert, delete or lookup values in the database
     *
     * for our test program, we just insert a few values, then look them 
     * up, then delete them and try to look them up again (which will fail).
     */
    for (i=0; i<LOOP; i++) {
        key.data=&i;
        key.size=sizeof(i);

        record.data=&i;
        record.size=sizeof(i);

        st=ham_insert(db, 0, &key, &record, 0);
		if (st!=HAM_SUCCESS)
            error("ham_insert", st);
    }

    /*
     * now lookup all values
     *
     * for ham_find(), we could use the flag HAM_RECORD_USER_ALLOC, if WE
     * allocate record.data (otherwise the memory is automatically allocated
     * by hamsterdb)
     */
    for (i=0; i<LOOP; i++) {
        key.data=&i;
        key.size=sizeof(i);

        st=ham_find(db, 0, &key, &record, 0);
        if (st!=HAM_SUCCESS)
            error("ham_find", st);

        /*
         * check if the value is ok
         */
        if (*(int *)record.data!=i) {
            printf("ham_find() ok, but returned bad value\n");
            return (-1);
        }
    }

    /*
     * close the database handle, then re-open it (to demonstrate the 
     * call ham_open)
     */
    st=ham_close(db, 0);
    if (st!=HAM_SUCCESS)
        error("ham_close", st);
    st=ham_open_ex(db, "test.db", 0, 0);
    if (st!=HAM_SUCCESS)
        error("ham_open", st);

    /*
     * now erase all values
     */
    for (i=0; i<LOOP; i++) {
        key.size=sizeof(i);
        key.data=&i;

        st=ham_erase(db, 0, &key, 0);
        if (st!=HAM_SUCCESS)
            error("ham_erase", st);
    }

    /*
     * once more we try to find all values... every ham_find() call must
     * now fail with HAM_KEY_NOT_FOUND
     */
    for (i=0; i<LOOP; i++) {
        key.size=sizeof(i);
        key.data=&i;

        st=ham_find(db, 0, &key, &record, 0);
        if (st!=HAM_KEY_NOT_FOUND)
            error("ham_find", st);
    }

    /*
     * we're done! close the database handle
     */
    st=ham_close(db, 0);
    if (st!=HAM_SUCCESS)
        error("ham_close", st);

    /*
     * delete the database object to avoid memory leaks
     */
    ham_delete(db);

#if UNDER_CE
    error("success", 0);
#endif
    printf("success!\n");
	return (0);
}
int
main(int argc, char **argv)
{
    unsigned opt;
    char *param, *filename=0, *endptr=0;
    unsigned short dbname=0xffff;
    int full=0;

    ham_u16_t names[1024];
    ham_size_t i, names_count=1024;
    ham_status_t st;
    ham_env_t *env;
    ham_db_t *db;

    ham_u32_t maj, min, rev;
    const char *licensee, *product;
    ham_get_license(&licensee, &product);
    ham_get_version(&maj, &min, &rev);

    getopts_init(argc, argv, "ham_info");

    while ((opt=getopts(&opts[0], &param))) {
        switch (opt) {
            case ARG_DBNAME:
                if (!param) {
                    printf("Parameter `dbname' is missing.\n");
                    return (-1);
                }
                dbname=(short)strtoul(param, &endptr, 0);
                if (endptr && *endptr) {
                    printf("Invalid parameter `dbname'; numerical value "
                           "expected.\n");
                    return (-1);
                }
                break;
            case ARG_FULL:
                full=1;
                break;
            case GETOPTS_PARAMETER:
                if (filename) {
                    printf("Multiple files specified. Please specify "
                           "only one filename.\n");
                    return (-1);
                }
                filename=param;
                break;
            case ARG_HELP:
                printf("hamsterdb %d.%d.%d - Copyright (C) 2005-2007 "
                       "Christoph Rupp ([email protected]).\n\n",
                       maj, min, rev);

                if (licensee[0]=='\0')
                    printf(
                       "This program is free software; you can redistribute "
                       "it and/or modify it\nunder the terms of the GNU "
                       "General Public License as published by the Free\n"
                       "Software Foundation; either version 2 of the License,\n"
                       "or (at your option) any later version.\n\n"
                       "See file COPYING.GPL2 and COPYING.GPL3 for License "
                       "information.\n\n");
                else
                    printf("Commercial version; licensed for %s (%s)\n\n",
                            licensee, product);

                printf("usage: ham_info [-db DBNAME] [-f] file\n");
                printf("usage: ham_info -h\n");
                printf("       -h:         this help screen (alias: --help)\n");
                printf("       -db DBNAME: only print info about "
                        "this database (alias: --dbname=<arg>)\n");
                printf("       -f:         print full information "
                        "(alias: --full)\n");
                return (0);
            default:
                printf("Invalid or unknown parameter `%s'. "
                       "Enter `ham_info --help' for usage.", param);
                return (-1);
        }
    }

    if (!filename) {
        printf("Filename is missing. Enter `ham_info --help' for usage.\n");
        return (-1);
    }

    /*
     * open the environment
     */
    st=ham_env_new(&env);
    if (st!=HAM_SUCCESS)
        error("ham_env_new", st);
    st=ham_env_open(env, filename, HAM_READ_ONLY);
    if (st==HAM_FILE_NOT_FOUND) {
        printf("File `%s' not found or unable to open it\n", filename);
        return (-1);
    }
    else if (st!=HAM_SUCCESS)
        error("ham_env_open", st);

    /*
     * print information about the environment
     */
    print_environment(env);

    /*
     * get a list of all databases
     */
    st=ham_env_get_database_names(env, names, &names_count);
    if (st!=HAM_SUCCESS)
        error("ham_env_get_database_names", st);

    /*
     * did the user specify a database name? if yes, show only this database
     */
    if (dbname!=0xffff) {
        st=ham_new(&db);
        if (st)
            error("ham_new", st);
    
        st=ham_env_open_db(env, db, dbname, 0, 0);
        if (st==HAM_DATABASE_NOT_FOUND) {
            printf("Database %u (0x%x) not found\n", dbname, dbname);
            return (-1);
        }
        else if (st)
            error("ham_env_open_db", st);
    
        print_database(db, dbname, full);
    
        st=ham_close(db, 0);
        if (st)
            error("ham_close", st);
        ham_delete(db);
    }
    else {
        /*
         * otherwise: for each database: print information about the database
         */
        for (i=0; i<names_count; i++) {
            st=ham_new(&db);
            if (st)
                error("ham_new", st);
    
            st=ham_env_open_db(env, db, names[i], 0, 0);
            if (st)
                error("ham_env_open_db", st);
    
            print_database(db, names[i], full);
    
            st=ham_close(db, 0);
            if (st)
                error("ham_close", st);
            ham_delete(db);
        }
    } 
    /*
     * clean up
     */
    st=ham_env_close(env, 0);
    if (st!=HAM_SUCCESS)
        error("ham_env_close", st);

    ham_env_delete(env);

    return (0);
}
Пример #5
0
int 
main(void)
{
    ham_db_t *db;
    ham_env_t *env;
    ham_srv_t *srv;
    ham_srv_config_t cfg;
    ham_status_t st;
    char input[1024];

    ham_env_new(&env);
    st=ham_env_create_ex(env, "env1.db", HAM_ENABLE_TRANSACTIONS, 0644, 0);
    if (st) {
        printf("ham_env_create_ex: %d\n", st);
        exit(-1);
    }

    ham_new(&db);
    st=ham_env_create_db(env, db, 12, HAM_ENABLE_DUPLICATES, 0);
    if (st) {
        printf("ham_env_create_db: %d\n", st);
        exit(-1);
    }
    ham_close(db, 0);

    st=ham_env_create_db(env, db, 13, HAM_ENABLE_DUPLICATES, 0);
    if (st) {
        printf("ham_env_create_db: %d\n", st);
        exit(-1);
    }
    ham_close(db, 0);

    st=ham_env_create_db(env, db, 33, 
                HAM_RECORD_NUMBER|HAM_ENABLE_DUPLICATES, 0);
    if (st) {
        printf("ham_env_create_db: %d\n", st);
        exit(-1);
    }
    ham_close(db, 0);

    memset(&cfg, 0, sizeof(cfg));
    cfg.port=8080;
    ham_srv_init(&cfg, &srv);
    ham_srv_add_env(srv, env, "/env1.db");

    printf("server1%s started - please run sample 'client1%s' for a test\n", 
            EXT, EXT);
    printf("type 'exit' to end the server\n");
    
    while (1) {
        printf("> ");
        scanf("%s", &input[0]);
        if (!strcmp(input, "exit")) {
            printf("exiting...\n");
            break;
        }
        printf("unknown command\n");
    }

    ham_srv_close(srv);
    ham_env_close(env, HAM_AUTO_CLEANUP);
    ham_env_delete(env);
    ham_delete(db);

    return (0);
}