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

📄 main.c

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

/*
 * This sample demonstrates the use of "compact" declaration.
 *
 * Schema declares 2 classes with the same set of fields, only
 * one is declared with the "compact" quilifier. The sample code
 * writes the same "raw" data into each of the classes and
 * calculates the amount of memory used to store the objects
 * within normal and compact layout.
 *
 * This sample uses data schema from compact.mco file
 */

#include "platform.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "compactdb.h"

/* Total data size is 8 Mbytes, with 100 bytes pages */
#define DBSIZE (1024*8000)
#ifndef MCO_PLATFORM_X64
    #define PAGESIZE 100
#else 
    #define PAGESIZE 200
#endif 


const char dbName[] = "compact";
const int MAP_ADDRESS = 0x20000000;

#define FACTOR   1
#define NOBJECTS 20

void _SH_(void)
{

    char text[] = 
    {
        "\nThis sample demonstrates the use of \"compact\" declaration.\n"
            "Schema declares 2 classes with the same set of fields, only\n"
            "one is declared with the \"compact\" quilifier. The sample code\n"
            "writes the same \"raw\" data into each of the classes and\n"
            "calculates the amount of memory used to store the objects\n""within normal and compact layout.\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();

}


static uint4 show_mem(mco_db_h db)
{
    mco_puint totalpg, freepg;
    uint2 pgsize;

    mco_db_free_pages(db, &freepg);
    mco_db_total_pages(db, &totalpg);
    mco_db_page_size(db, &pgsize);

    printf("\nMem: total pgs=%d, free pgs=%d, used and reserved %d Kb", totalpg, freepg, (totalpg - freepg)* pgsize /
           1024);

    return (uint4)(((totalpg - freepg)* pgsize / 1024));
}


/*
 * Writes NOBJECTS of Small or Large objects into the database, depending
 * on the obj_code.
 */
static int write_objs(mco_db_h db, const int obj_code)
{
    uint2 i, n;
    MCO_RET rc = 0;
    mco_trans_h t;
    Small smallobj;
    Large large;
    Expr optexp;
    char expression[64], text[32];
    //	char            text_in[30] = {0};
    uint4 raw_size = 0;
    uint4 start_mem, end_mem;

    start_mem = show_mem(db);

    for (i = 0; i < NOBJECTS; i++)
    {
        mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t);

        if (obj_code == Small_code)
        {
            rc = Small_new(t, &smallobj);
        }
        else
        {
            rc = Large_new(t, &large);
        }

        if (rc)
        {
            mco_trans_rollback(t);
            return rc;
        }

        // write "structure" data every other itteration
        if (i % 2)
        {
            if (obj_code == Small_code)
            {
                rc = Small_eval_write_handle(&smallobj, &optexp);
            }
            else
            {
                rc = Large_eval_write_handle(&large, &optexp);
            }

            if (rc)
            {
                mco_trans_rollback(t);
                return rc;
            }

            Expr_time_in_put(&optexp, (uint4)time(NULL));
            raw_size += sizeof(uint4);

            sprintf(expression, "if (%c%d%c%d%c%d.GE.%d%c%d%c%d%c)", 'A' - i, (73* i) % 2, 'B' - i, (89* i) % 3, 'C' -
                    i, (31* i) % 7, (37* i) % 2, 'D' - i, (98* i) % 3, 'E' - i, (13* i) % 7, 'F' - i);
            Expr_expression_put(&optexp, expression, (uint2)strlen(expression));
            raw_size += (uint4)strlen(expression);
        }
        // write vector
        if (obj_code == Small_code)
        {
            rc = Small_texts_alloc(&smallobj, (uint2)((i + 1)* FACTOR));
        }
        else
        {
            rc = Large_texts_alloc(&large, (uint2)((i + 1)* FACTOR));
        }

        for (n = 0; n < (i + 1)* FACTOR; n++)
        {
            sprintf(text, "abcdefghigklmnopqrs_etc..._%d", n);
            if (obj_code == Small_code)
            {
                Small_texts_put(&smallobj, n, text, (uint2)strlen(text));
            }
            else
            {
                Large_texts_put(&large, n, text, (uint2)strlen(text));
            }

            raw_size += (uint4)strlen(text);
        }
        rc = mco_trans_commit(t);
    }

    end_mem = show_mem(db);
    printf("\n%dK used to store %d objects, raw_size=%dK, %dK overhead\n", end_mem - start_mem, i, raw_size / 1024,
           end_mem - start_mem - raw_size / 1024);
    return rc;
}


int main(void)
{

    MCO_RET rc;
    mco_db_h db = 0;
    //	uint2		  sensor_num=0,
    //      		  measure_num=0;
    //  int4		  raw=0L,
    //				    dbsize=0L,
    //				    news=0;
    //	unsigned long   tr_total=0L;
    //	uint4           d=0,
    //					s=0;
    //	char            str[1000]={0};
    void* start_mem;
    mco_runtime_info_t info;

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

    mco_runtime_start();

    rc = mco_db_open(dbName, compactdb_get_dictionary(), start_mem, DBSIZE, (uint2)PAGESIZE);

    if (rc)
    {
        printf("\nerror creating database");
        if (!info.mco_shm_supported)
        {
            free(start_mem);
        }
        exit(1);
    }
    /* connect to the database, obtain a database handle */
    mco_db_connect(dbName, &db);
    srand((unsigned)time(NULL));

    printf("Writing compact objects\n");
    write_objs(db, Small_code);
    printf("\n\nWriting normal objects\n");
    write_objs(db, Large_code);

    /* disconnect from the database, db is no longer valid */
    mco_db_disconnect(db);
    /* destroys the db instance */
    mco_db_close(dbName);
    mco_runtime_stop();

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

    printf("\n\nPress any key to exit");
    getchar();

    PROG_EXIT(0);
}

⌨️ 快捷键说明

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