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

📄 dbinit.c

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

#include "load.h"

uint2 fv = 0;
uint2 dv = 0;
uint2 bv = 0;
uint2 bh = 0;
uint2 iv = 0;

uint2 nh = 0;
uint2 nb = 0;
uint2 nd = 0;
uint2 nf = 0;
uint2 ni = 0;

static int random_string(int len, char* str)
{
    int ind;

    for (ind = 0; ind < len; ind++)
    {
        int num = 'A' + rand() % ('Z' - 'A');
        str[ind] = (char)num;
    }
    return 0;

}


static uint2 rand2(uint2 lowlimit, uint2 uplimit)
{
    int n = rand();
    return (uint2)((n % (uplimit - lowlimit + 1)) + lowlimit);
}


int new_fixed(mco_db_h db, int count)
{
    Fixed fixed;
    MCO_RET rc = 0;
    char txt[] = "abcd";
    mco_trans_h t;
    dbtest_oid oid;

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

    oid.seq = count;
    rc = Fixed_new(t, &oid, &fixed);
    if (rc)
    {
        return rc;
    }

    Fixed_oid_get(&fixed, &oid);
    //printf ("OID= %ld\n", oid.seq);

    Fixed_u4_put(&fixed, (uint4)count);
    Fixed_f_put(&fixed, (float)count);
    Fixed_u2_put(&fixed, (uint2)count);

    rc = Fixed_c4_put(&fixed, txt, (uint2)strlen(txt));
    if (rc)
    {
        mco_trans_rollback(t);
    }
    else
    {
        rc = mco_trans_commit(t);
        nf++;
    }
    return rc;

}


int new_dynamic(mco_db_h db, int cnt)
{
    Dynamic dyn;
    MCO_RET rc = 0;
    mco_trans_h t;

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

    if (MCO_S_OK != (rc = Dynamic_new(t, &dyn)))
    {
        return rc;
    }

    {
        uint2 i;
        char* s = malloc(STEP *(cnt + 1));
        if (!s)
        {
            return  - 1;
        }

        for (i = 0; i < STEP *(cnt + 1); i++)
        {
            s[i] = 'A';
        }

        rc = Dynamic_str_put(&dyn, s, (uint2)(STEP *(cnt + 1)));
        free(s);

        if (rc)
        {
            mco_trans_rollback(t);
            return rc;
        }
    }
    {
        uint2 vsize = rand2(0, 50);
        uint2 i;

        Dynamic_vu4_alloc(&dyn, vsize);

        for (i = 0; i < vsize; i++)
        {
            rc |= Dynamic_vu4_put(&dyn, i, rand2(100, 1000));
        }

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

    if (cnt % 2)
    {
        Item item;
        uint2 i;
        char* s = malloc(STEP *(cnt + 1));
        if (!s)
        {
            return  - 1;
        }

        Dynamic_item_write_handle(&dyn, &item);
        Item_id_put(&item, rand());

        for (i = 0; i < STEP *(cnt + 1); i++)
        {
            s[i] = 'A';
        }

        rc = Item_name_put(&item, s, (uint2)(STEP *(cnt + 1)));
        free(s);
        if (rc)
        {
            mco_trans_rollback(t);
            return rc;
        }
    }
    rc = mco_trans_commit(t);
    return rc;

}


int new_blobs(mco_db_h db, int cnt)
{
    Blobs blob;
    MCO_RET rc = 0;
    mco_trans_h t;
    char* txt = 0;
    uint4 ind;
    int8 u8;
    static uint8 u8v = 
    {
        0
    };
    uint4 this_blob_size = 10 * STEP *(cnt + 1);

    if ((txt = malloc(this_blob_size)) == 0)
    {
        return  - 1;
    }

    for (ind = 0; ind < this_blob_size; ind++)
    {
        txt[ind] = 'A';
    }

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

    rc = Blobs_new(t, &blob);
    if (rc)
    {
        goto err;
    }

    rc = Blobs_blo_put(&blob, txt, this_blob_size);
    if (rc)
    {
        goto err;
    }
    rc = Blobs_num_put(&blob, (uint2)rand());
    if (rc)
    {
        goto err;
    }

    mco_uquad_increment(&u8v);
    Blobs_u8_put(&blob, u8v);

    rc = Blobs_autoid_get(&blob, &u8);
    if (rc)
    {
        goto err;
    }

    free(txt);
    rc = mco_trans_commit(t);
    if (SUCCESS(rc))
    {
        nb++;
    }

    /* Perform the update for ever other object */
    /* Since the class is declared with history, such
    updates create it */

    if (info.mco_versioning_supported)
    {
        for (ind = 0; ind < Blobs_versions_limit - 1; ind++)
        {
            uint4 bsz = 0, real_bsz = 0;
            uint4 i4;
            char* dest = 0;

            mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t);
            rc = Blobs_autoid_find(t, u8, &blob);
            if (rc)
            {
                goto err;
            }
            Blobs_blo_size(&blob, &bsz);

            if ((dest = malloc(bsz)) == 0)
            {
                mco_trans_rollback(t);
                break;
            }
            Blobs_blo_get(&blob, 0, dest, bsz, &real_bsz);
            for (i4 = 0; i4 < real_bsz; i4++)
            {
                dest[i4]++;
            }
            Blobs_blo_put(&blob, dest, real_bsz);

            rc = mco_trans_commit(t);
            if (rc)
            {
                break;
            }
            else
            {
                nh++;
            }
        };
    };

    return rc;

    err: free(txt);
    mco_trans_rollback(t);
    return rc;

}


int new_idx(mco_db_h db, int count)
{
    MCO_RET rc = 0;
    Idxs iobj;
    dbtest_oid oid;
    mco_trans_h t;
    Item item;
    uint2 i, vsize = rand2(0, 100);
    char itemname[30];

    rc = mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t);
    if (!SUCCESS(rc))
    {
        return rc;
    }
    oid.seq = OBJCOUNT + count; // don't get fancy, just make sure the OID is unique
    rc = Idxs_new(t, &oid, &iobj);
    if (!SUCCESS(rc))
    {
        goto end;
    }

    Idxs_u2_put(&iobj, (uint2)(count));

    rc = Idxs_items_alloc(&iobj, vsize);
    if (!SUCCESS(rc))
    {
        goto end;
    }

    for (i = 0; i < vsize; i++)
    {

        rc = Idxs_items_put(&iobj, i, &item);
        if (!SUCCESS(rc))
        {
            goto end;
        }

        sprintf(itemname, "item_%d", next_item_id);
        Item_id_put(&item, next_item_id++);
        rc = Item_name_put(&item, itemname, (uint2)strlen(itemname));
        if (!SUCCESS(rc))
        {
            goto end;
        }
        ni++;
    }

    rc = mco_trans_commit(t);
    return rc;

    end: mco_trans_rollback(t);
    return rc;

}


int init_database(mco_db_h db)
{
    MCO_RET rc = 0;
    int j;

    //printf("\nReading original OIDs from Fixed\n");
    for (j = 0; j < OBJCOUNT; j++)
    {

        if (MCO_S_OK != (rc = new_fixed(db, j)))
        {
            break;
        }
        if (MCO_S_OK != (rc = new_dynamic(db, j)))
        {
            break;
        }
        if (MCO_S_OK != (rc = new_blobs(db, j)))
        {
            break;
        }
        if (MCO_S_OK != (rc = new_idx(db, j)))
        {
            break;
        }

    }
    return rc;

}

⌨️ 快捷键说明

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