⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 delete.c

📁 PB 熟悉的哥们希望大家可以互相学习一下
💻 C
字号:
/***************************************************************
 *                                                             *
 * Copyright (c) 2001-2007 McObject LLC. All Right Reserved.   *
 *                                                             *
 ***************************************************************/

/*
 * This example demonstrates hash-based serach functionality,
 * erase/delete interfaces and database statistics methods.
 */
#include <platform.h>
#include <stdio.h>
#include <stdlib.h>

#include "samples.h"

static const char* dbname = "SimpleDb";
static const int FIRST_SEGSZ = 1024 * 8000UL;
#ifndef MCO_PLATFORM_X64
    static const int PAGESIZE = 96;
#else 
    static const int PAGESIZE = 192;
#endif 

const int MAP_ADDRESS = 0x20000000;
void _SH_(void)
{

    char text[] = 
    {
        "\nThis example demonstrates eXtremeDB hash-based search interfaces,\n"
            "erase and delete interfaces and database statistics methods.\n"
    };
    char text1[] = 
    {
        "Copyright (c) 2001-2007 McObject LLC. All Right Reserved.\n\n"
    };

    printf("%s\neXtremeDB runtime version %d.%d, build %d\n%s\n\nPress Enter to start", text, MCO_COMP_VER_MAJOR,
           MCO_COMP_VER_MINOR, MCO_COMP_BUILD_NUM, text1);

    getchar();
}


/* Display database statistics
 */
void showStat(mco_db_h db, uint2 class_code)
{
    MCO_RET rc = 0;
    mco_trans_h t;
    mco_class_stat_t stat;

    mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t);
    rc = mco_class_stat_get(t, class_code, &stat);
    mco_trans_commit(t);

    if (rc == MCO_S_OK)
    {
        printf(
               "\n\tStatistics for the class with code %d:\n\tNumber of objects:\t%ld\n\tTotal pages used:\t%ld\n\tTotal core used:\t%ld\n", class_code, stat.objects_num, stat.core_pages + stat.blob_pages, stat.core_space);
    }
}


/* Find and object based on hash index created for the class. Ones found
 * delete the object and return all memory back to the pool.
 */
int find_and_delete(mco_db_h db, uint4 vh)
{
    MCO_RET rc;
    mco_trans_h t;
    SimpleClass hClass;
    simple_oid id;

    mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t);

    rc = SimpleClass_SimpleKey_find(t, vh, &hClass);

    if (rc == MCO_S_OK)
    {
        /* delete it!*/
        rc = SimpleClass_oid_get(&hClass, &id);
        printf("\n\tobject with OID=%d about to be deleted...", id.seq);
        rc = SimpleClass_delete(&hClass);
        if (rc == MCO_S_OK)
        {
            printf("okay\n");
            mco_trans_commit(t);
        }
        else
        {
            printf("failed (%d)\n", rc);
            mco_trans_rollback(t);
        }
    }
    else
    {
        printf("\n\terror(%d) searching for the object with hash value %d", rc, vh);
        mco_trans_rollback(t);
    }

    return (int)rc;
}


int main(void)
{
    MCO_RET rc;
    mco_db_h db = 0;
    int num = NUMOBJECTS, one_id[NUMOBJECTS], vhash[NUMOBJECTS];
    char* start_mem;
    mco_runtime_info_t info;

    _SH_();
    mco_get_runtime_info(&info);
    if (info.mco_shm_supported)
    {
        start_mem = (char*)MAP_ADDRESS;
    }
    else
    {
        start_mem = (char*)malloc(FIRST_SEGSZ);
        if (!start_mem)
        {
            printf("Couldn't allocated memory\n");
            exit(1);
        }
    };

    mco_runtime_start();
    rc = mco_db_open(dbname, simple_get_dictionary(), start_mem, FIRST_SEGSZ, (uint2)PAGESIZE);

    if (rc)
    {
        printf("\nerror %d creating database", rc);
        if (!info.mco_shm_supported)
        {
            free(start_mem);
        }
        exit(1);
    }

    /* connect to the database, obtain a database handle */
    mco_db_connect(dbname, &db);
    printf("\ndb handle = %d\n", db);

    srand((unsigned)time(NULL));
    while (0 < num--)
    {
        /* get some random values */
        one_id[num] = rand();
        vhash[num] = (uint4)rand();

        /* create a new object */
        newobj(db, one_id[num], vhash[num]);
    }

    printf("\n");

    /* print out statistics for SimpleClass */
    showStat(db, SimpleClass_code);

    printf("\n\nHash-based search & delete\n");

    for (num = NUMOBJECTS; 0 < num; num--)
    {
        find_and_delete(db, vhash[num - 1]);
    }

    showStat(db, SimpleClass_code);

    /* disconnect from the database, db is no longer valid */
    mco_db_disconnect(db);

    /* destroy the db */
    mco_db_close(dbname);
    mco_runtime_stop();

    if (!info.mco_shm_supported)
    {
        free(start_mem);
    }

    printf("Press Enter to finish\n");
    getchar();
    PROG_EXIT(0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -