📄 treeindex.c
字号:
/****************************************************************
* *
* Copyright (c) 2001-2007 McObject LLC. All Right Reserved. *
* *
****************************************************************/
#include <platform.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "randdb.h"
#define DB_MEMORY_SIZE 4 * 1024 * 1024
#ifndef MCO_PLATFORM_X64
#define PAGESIZE 96
#else
#define PAGESIZE 192
#endif
const int MAP_ADDRESS = 0x20000000;
#define SERIES_SIZE 5
#define DELTA 5
#define RANGE_DIFF (float)0.9
#define TARGET_SERIES 5
#define _RGB_(r,g,b) ((uint4)(((uint1)(r)|((uint2)((uint1)(g))<<8))|(((uint4)(uint1)(b))<<16)))
/* Load a data to database */
int LoadData(mco_db_h db)
{
MCO_RET rc;
mco_trans_h t;
int i;
uint2 j;
Measurement Obj;
/* let's make a 1000 measurements */
for (i = 0; i < 1000; i++)
{
/* reset random-mechanism for new series of measurements */
if (i % SERIES_SIZE == 0)
{
srand((int)time(0));
}
/* begin the transaction */
rc = mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t);
if (rc == MCO_S_OK)
{
/* allocate a new object */
rc = Measurement_new(t, &Obj);
if (rc == MCO_S_OK)
{
/* some trivial math */
float ft1, ft2;
int idx, series;
idx = (i - (i / SERIES_SIZE)* SERIES_SIZE) + 1;
series = (i / SERIES_SIZE) + 1;
ft1 = ((float)rand() / (float)RAND_MAX);
ft2 = ((float)rand() / (float)RAND_MAX);
/* fill attributes */
Measurement_iSeries_put(&Obj, series);
Measurement_iIdx_put(&Obj, idx);
Measurement_tTime_put(&Obj, (mco_time)time(0));
Measurement_fValue1_put(&Obj, ft1);
Measurement_fValue2_put(&Obj, ft2);
Measurement_fDiff_put(&Obj, (float)fabs(ft2 - ft1));
Measurement_vVis_alloc(&Obj, DELTA);
for (j = 0; j < DELTA; j++)
{
Vis vis;
float f = (float)j *((float)fabs(ft2 - ft1) / (float)DELTA);
uint4 c = (uint4)(255-f * 255);
Measurement_vVis_put(&Obj, j, &vis);
Vis_fValue_put(&vis, f);
Vis_iRGB_put(&vis, _RGB_(c, c, c));
};
rc = mco_trans_commit(t);
}
else
{
printf("Couldn't allocate a new object\n");
mco_trans_rollback(t);
};
}
else
{
printf("Unable to start a transaction\n");
};
};
return 1;
}
/* Show attrs of one measurement */
void ShowObj(Measurement* hObj)
{
char stub[] = "0:0\n";
int i, j;
float ft1, ft2, ft3;
time_t tm;
mco_time _tm;
char* p1 = stub, * p2;
/* get attr's values */
Measurement_iSeries_get(hObj, &i);
Measurement_iIdx_get(hObj, &j);
Measurement_tTime_get(hObj, (mco_time*) &_tm);
tm = _tm;
Measurement_fValue1_get(hObj, &ft1);
Measurement_fValue2_get(hObj, &ft2);
Measurement_fDiff_get(hObj, &ft3);
/* format & print it */
p1 = (char*)ctime((time_t*) &tm);
p2 = strchr(p1, '\n');
*p2 = (char)0;
printf("%3dx%d. Measurement from %s: %f %f %f\n", i, j, p1, ft1, ft2, ft3);
};
/* Show data using tree-indexes routine */
void ShowData(mco_db_h db)
{
MCO_RET rc;
mco_trans_h t;
mco_cursor_t csr, csr2;
Measurement Obj;
int i, j;
/* navigate downward thru the items from first to last */
printf("List of the first 10 measurements:\n");
/* open a ransaction */
rc = mco_trans_start(db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &t);
if (rc == MCO_S_OK)
{
/* open the cursor */
rc = Measurement_I_Time_index_cursor(t, &csr);
if (rc == MCO_S_OK)
{
/* jump to first measurement */
rc = mco_cursor_first(t, &csr);
/* take 10 first items */
for (i = 0; i < 10 && rc == MCO_S_OK; i++)
{
/* get measurement from cursor */
rc = Measurement_from_cursor(t, &csr, &Obj);
if (rc == MCO_S_OK)
{
/* show it */
ShowObj(&Obj);
/* move to next item */
rc = mco_cursor_next(t, &csr);
};
};
}
else
{
printf("Unable to open a cursor");
};
/* close the transaction */
rc = mco_trans_commit(t);
}
else
{
printf("Unable to open a transaction");
};
printf("\n");
/* navigate upward thru the items from last to first */
printf("List of the last 10 measurements:\n");
/* open a transaction */
rc = mco_trans_start(db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &t);
if (rc == MCO_S_OK)
{
/* open the cursor */
rc = Measurement_I_Time_index_cursor(t, &csr);
if (rc == MCO_S_OK)
{
/* move to last item */
rc = mco_cursor_last(t, &csr);
/* show last 10 items */
for (i = 0; i < 10 && rc == MCO_S_OK; i++)
{
/* get the measurement */
rc = Measurement_from_cursor(t, &csr, &Obj);
if (rc == MCO_S_OK)
{
/* show it */
ShowObj(&Obj);
/* move to next item */
rc = mco_cursor_prev(t, &csr);
};
};
}
else
{
printf("Unable to a open cursor");
};
rc = mco_trans_commit(t);
}
else
{
printf("Unable to open a transaction");
};
printf("\n");
/* looking for specified item */
printf("Search for the first measurement, where the difference is equal to 0.5\n");
/* open a transaction */
rc = mco_trans_start(db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &t);
if (rc == MCO_S_OK)
{
/* open the cursor */
rc = Measurement_I_Diff_index_cursor(t, &csr);
if (rc == MCO_S_OK)
{
/* search an item by key-value */
rc = Measurement_I_Diff_search(t, &csr, MCO_EQ, 0.5);
/* analyze the results */
switch (rc)
{
case MCO_S_OK:
{
/* Item's found. Display it. */
rc = Measurement_from_cursor(t, &csr, &Obj);
if (rc == MCO_S_OK)
{
ShowObj(&Obj);
/* Locate it in the I_Index cursor */
rc = Measurement_I_Index_locate(t, &csr2, &Obj);
if (rc == MCO_S_OK)
{
/* And show next one */
if (mco_cursor_next(t, &csr2) == MCO_S_OK && Measurement_from_cursor(t, &csr2, &Obj) ==
MCO_S_OK)
{
ShowObj(&Obj);
}
}
;
}
;
}
;
break;
case MCO_S_NOTFOUND:
printf("Difference 0.5 is not found.\n");
break;
default:
printf("Error while searching.\n");
break;
};
}
else
{
printf("Unable to open a cursor");
};
/* close the transaction */
rc = mco_trans_commit(t);
}
else
{
printf("Unable to open a transaction");
};
printf("\n");
/* ranged search */
printf("List of measurements with differencial greater or equal to %f\n", RANGE_DIFF);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -