From caadfb466cd116adc6fd8945f282b15904392811 Mon Sep 17 00:00:00 2001 From: John Cage <58808125+coshcage@users.noreply.github.com> Date: Thu, 18 Apr 2024 09:51:20 +0000 Subject: [PATCH] Add files via upload --- src/silock.c | 2 +- src/simisc.c | 45 +++++- src/sitable.c | 391 ++++++++++++++++++++++++++++---------------------- src/sitrans.c | 17 ++- src/sixmem.c | 22 +-- src/svimrdb.h | 45 ++++-- 6 files changed, 324 insertions(+), 198 deletions(-) diff --git a/src/silock.c b/src/silock.c index e920825..bdc9d66 100644 --- a/src/silock.c +++ b/src/silock.c @@ -100,7 +100,7 @@ static int _sicbftvsTrans(void * pitem, size_t param) * ptrans Pointer to a transaction. * pobj Pointer to an object. * lt Lock type. - * Return value: TRUE lock acquired. FALSE lock refused. + * Return value: TRUE lock accuired. FALSE lock refused. * Caution: N/A. * Tip: N/A. */ diff --git a/src/simisc.c b/src/simisc.c index 77d14ce..5c9e5ad 100644 --- a/src/simisc.c +++ b/src/simisc.c @@ -2,13 +2,14 @@ * Name: simisc.c * Description: Misc functions. * Author: cosh.cage#hotmail.com - * File ID: 0703231856E1101231110L00287 + * File ID: 0703231856E0418240132L00330 * License: GPLv2. */ #include /* Using function tolower. */ #include /* Using type uint32_t, uint64_t. */ #include /* Using function strcmp. */ +#include /* Using function printf. */ #include "svimrdb.h" int _siCBFCompareNodeDataSPlusA(void * pitem, size_t param); @@ -156,6 +157,18 @@ size_t siPlatformSize(void) return sizeof(size_t); } +/* Function name: siPrintSystemVersion + * Description: Print system version. + * Parameter: N/A. + * Return value: N/A. + * Caution: N/A. + * Tip: N/A. + */ +void siPrintSystemVersion(void) +{ + (void)printf("%s", SYS_VER); +} + /* Attention: This Is An Internal Function. No Interface for Library Users. * Function name: _siCBFCompareNodeDataSPlusA * Description: Used to compare data of NODE_S for strings. @@ -284,3 +297,33 @@ P_NODE_S hshSearchCPlusW(P_HSHTBL_C pht, CBF_HASH cbfhsh, const void * pkey, siz { return _siSearchLinkedListSCPlusW(*(P_NODE_S *)(pht->pdata + (cbfhsh(pkey) % strLevelArrayZ(pht)) * sizeof(P_NODE_S)), pkey, size); } + +/* Function name: sicbftaDefaultIncrease + * Description: Default table increasing function. + * Parameters: + * curln Input the current table length value. + * ln Input total table length value. + * Return value: New table length value. + */ +size_t sicbftaDefaultIncrease(size_t curln, size_t ln) +{ + if (curln == ln) + return ln + TBL_LN_BUF_SIZ; + else + return ln; +} + +/* Function name: sicbftaDefaultDecrease + * Description: Default table decreasing function. + * Parameters: + * curln Input the current table length value. + * ln Input total table length value. + * Return value: New table length value. + */ +size_t sicbftaDefaultDecrease(size_t curln, size_t ln) +{ + if (ln - TBL_LN_BUF_SIZ > curln) + return ln - TBL_LN_BUF_SIZ; + else + return ln; +} diff --git a/src/sitable.c b/src/sitable.c index ca0dee7..5633ae4 100644 --- a/src/sitable.c +++ b/src/sitable.c @@ -2,7 +2,7 @@ * Name: sitable.c * Description: SI functions for tables. * Author: cosh.cage#hotmail.com - * File ID: 0628231947C0130242056L00996 + * File ID: 0628231947C0418240133L01049 * License: GPLv2. */ #define _CRT_SECURE_NO_WARNINGS @@ -168,6 +168,31 @@ void siDestroyView(P_MATRIX pmtx) } } +/* Function name: siDestroyTable + * Description: Uninitialize a table. + * Parameter: + * ptbl Pointer to a table. + * Return value: N/A. + * Caution: N/A. + * Tip: N/A. + */ +void siDestroyTable(P_TABLE ptbl) +{ + if (NULL != ptbl) + { + size_t i, j; + for (i = 0; i < ptbl->curln; ++i) + { + for (j = 0; j < ptbl->tbldata.col; ++j) + { + P_CELL * ppcold = (P_CELL *)strGetValueMatrix(NULL, &ptbl->tbldata, i, j, sizeof(P_CELL)); + if (NULL != *ppcold) + siDeleteCell(ppcold); + } + } + } +} + /* Function name: siPrintView * Description: Print a view to stdout. * Parameter: @@ -262,9 +287,9 @@ ptrdiff_t siGetColumnByString(P_TABLE ptbl, char * strname) */ P_MATRIX siCreateViewOfTable(P_TABLE ptbl) { - P_MATRIX pmtx = strCreateMatrix(ptbl->tbldata.ln, ptbl->tbldata.col, sizeof(P_CELL)); + P_MATRIX pmtx = strCreateMatrix(ptbl->curln, ptbl->tbldata.col, sizeof(P_CELL)); if (NULL != pmtx) - strCopyMatrix(pmtx, &ptbl->tbldata, sizeof(P_CELL)); + strProjectMatrix(pmtx, 0, 0, &ptbl->tbldata, 0, 0, sizeof(P_CELL)); return pmtx; } @@ -340,7 +365,8 @@ P_TABLE siCreateTable(P_TRANS ptrans, char * tblname, P_ARRAY_Z parrhdr) break; } } - strInitMatrix(&ptbl->tbldata, 0, parrhdr->num, sizeof(P_CELL)); + ptbl->curln = 0; + strInitMatrix(&ptbl->tbldata, TBL_LN_BUF_SIZ, parrhdr->num, sizeof(P_CELL)); } if (NULL != ptrans) @@ -373,7 +399,9 @@ P_TABLE siCopyTable(P_TRANS ptrans, P_TABLE ptbl) { size_t i, j; - for (i = 0; i < ptbl->tbldata.ln; ++i) + pr->curln = ptbl->curln; + + for (i = 0; i < ptbl->curln; ++i) { for (j = 0; j < ptbl->tbldata.col; ++j) { @@ -425,7 +453,7 @@ void siDeleteTable(P_TRANS ptrans, P_TABLE ptbl) if (NULL != ptbl->tbldata.arrz.pdata) { - siDestroyView(&ptbl->tbldata); + siDestroyTable(ptbl); strFreeMatrix(&ptbl->tbldata); } free(ptbl); @@ -437,195 +465,209 @@ void siDeleteTable(P_TRANS ptrans, P_TABLE ptbl) * Parameter: * ptrans Pointer to a transaction. * ptbl Pointer to a table you want to insert. + * cbfta Pointer to an increasing function. + * If this parameter equals NULL, default increasing function will be taken. * ... Parameters you want to insert. * Return value: TRUE insertion succeeded. FALSE insertion failed. * Caution: Parameter ptbl must be allocated first. * Tip: N/A. */ -BOOL siInsertIntoTable(P_TRANS ptrans, P_TABLE ptbl, ...) +BOOL siInsertIntoTable(P_TRANS ptrans, P_TABLE ptbl, SICBF_TBLAUG cbfta, ...) { - size_t i, j; + va_list arg; + + size_t i, j, k, l; BOOL bins = TRUE; - j = ptbl->tbldata.ln; - if (NULL != strResizeMatrix(&(ptbl->tbldata), ptbl->tbldata.ln + 1, ptbl->tbldata.col, sizeof(P_CELL))) - { - va_list arg; - va_start(arg, ptbl); + + va_start(arg, cbfta); + + j = ptbl->curln; + + if (NULL == cbfta) + cbfta = sicbftaDefaultIncrease; + + k = cbfta(j + 1, ptbl->tbldata.ln); + l = ptbl->tbldata.ln; + + if (k != ptbl->tbldata.ln) + if (NULL == strResizeMatrix(&(ptbl->tbldata), k, ptbl->tbldata.col, sizeof(P_CELL))) + return FALSE; - for (i = 0; i < strLevelArrayZ(&ptbl->header); ++i) + for (i = 0; i < strLevelArrayZ(&ptbl->header); ++i) + { + union un_CellData { - union un_CellData - { - char c; - short s; - int i; - long l; - float f; - double d; - } cd; - P_CELL pc; - P_TBLHDR pt = (P_TBLHDR)strLocateItemArrayZ(&ptbl->header, sizeof(TBLHDR), i); - switch (pt->ct) + char c; + short s; + int i; + long l; + float f; + double d; + } cd; + P_CELL pc; + P_TBLHDR pt = (P_TBLHDR)strLocateItemArrayZ(&ptbl->header, sizeof(TBLHDR), i); + switch (pt->ct) + { + case CT_CHAR: + cd.c = (char)va_arg(arg, int); + pc = siCreateCell(&cd.c, pt->ct); + if (NULL != pt->phsh) { - case CT_CHAR: - cd.c = (char)va_arg(arg, int); - pc = siCreateCell(&cd.c, pt->ct); - if (NULL != pt->phsh) + switch (pt->cr) { - switch (pt->cr) - { - case CR_UNIQUE: - case CR_PRIMARY_KEY: - if (NULL == hshSearchC(pt->phsh, siHashChar, &cd.c, sizeof(char))) - hshInsertC(pt->phsh, siHashChar, &cd.c, sizeof(char)); - else - bins = FALSE; - break; - } + case CR_UNIQUE: + case CR_PRIMARY_KEY: + if (NULL == hshSearchC(pt->phsh, siHashChar, &cd.c, sizeof(char))) + hshInsertC(pt->phsh, siHashChar, &cd.c, sizeof(char)); + else + bins = FALSE; + break; } - break; - case CT_SHORT: - cd.s = va_arg(arg, int); - pc = siCreateCell(&cd.s, pt->ct); - if (NULL != pt->phsh) + } + break; + case CT_SHORT: + cd.s = va_arg(arg, int); + pc = siCreateCell(&cd.s, pt->ct); + if (NULL != pt->phsh) + { + switch (pt->cr) { - switch (pt->cr) - { - case CR_UNIQUE: - case CR_PRIMARY_KEY: - if (NULL == hshSearchC(pt->phsh, siHashShort, &cd.s, sizeof(short))) - hshInsertC(pt->phsh, siHashShort, &cd.s, sizeof(short)); - else - bins = FALSE; - break; - } + case CR_UNIQUE: + case CR_PRIMARY_KEY: + if (NULL == hshSearchC(pt->phsh, siHashShort, &cd.s, sizeof(short))) + hshInsertC(pt->phsh, siHashShort, &cd.s, sizeof(short)); + else + bins = FALSE; + break; } - break; - case CT_INTEGER: - cd.i = va_arg(arg, int); - pc = siCreateCell(&cd.i, pt->ct); - if (NULL != pt->phsh) + } + break; + case CT_INTEGER: + cd.i = va_arg(arg, int); + pc = siCreateCell(&cd.i, pt->ct); + if (NULL != pt->phsh) + { + switch (pt->cr) { - switch (pt->cr) - { - case CR_UNIQUE: - case CR_PRIMARY_KEY: - if (NULL == hshSearchC(pt->phsh, siHashInt, &cd.i, sizeof(int))) - hshInsertC(pt->phsh, siHashInt, &cd.i, sizeof(int)); - else - bins = FALSE; - break; - } + case CR_UNIQUE: + case CR_PRIMARY_KEY: + if (NULL == hshSearchC(pt->phsh, siHashInt, &cd.i, sizeof(int))) + hshInsertC(pt->phsh, siHashInt, &cd.i, sizeof(int)); + else + bins = FALSE; + break; } - break; - case CT_LONG: - cd.l = va_arg(arg, long); - pc = siCreateCell(&cd.l, pt->ct); - if (NULL != pt->phsh) + } + break; + case CT_LONG: + cd.l = va_arg(arg, long); + pc = siCreateCell(&cd.l, pt->ct); + if (NULL != pt->phsh) + { + switch (pt->cr) { - switch (pt->cr) - { - case CR_UNIQUE: - case CR_PRIMARY_KEY: - if (NULL == hshSearchC(pt->phsh, siHashLong, &cd.l, sizeof(long))) - hshInsertC(pt->phsh, siHashLong, &cd.l, sizeof(long)); - else - bins = FALSE; - break; - } + case CR_UNIQUE: + case CR_PRIMARY_KEY: + if (NULL == hshSearchC(pt->phsh, siHashLong, &cd.l, sizeof(long))) + hshInsertC(pt->phsh, siHashLong, &cd.l, sizeof(long)); + else + bins = FALSE; + break; } - break; - case CT_FLOAT: - cd.f = *(float *)va_arg(arg, float *); - pc = siCreateCell(&cd.f, pt->ct); - if (NULL != pt->phsh) + } + break; + case CT_FLOAT: + cd.f = *(float *)va_arg(arg, float *); + pc = siCreateCell(&cd.f, pt->ct); + if (NULL != pt->phsh) + { + switch (pt->cr) { - switch (pt->cr) - { - case CR_UNIQUE: - case CR_PRIMARY_KEY: - if (NULL == hshSearchC(pt->phsh, siHashFloat, &cd.f, sizeof(float))) - hshInsertC(pt->phsh, siHashFloat, &cd.f, sizeof(float)); - else - bins = FALSE; - break; - } + case CR_UNIQUE: + case CR_PRIMARY_KEY: + if (NULL == hshSearchC(pt->phsh, siHashFloat, &cd.f, sizeof(float))) + hshInsertC(pt->phsh, siHashFloat, &cd.f, sizeof(float)); + else + bins = FALSE; + break; } - break; - case CT_DOUBLE: - cd.d = *(double *)va_arg(arg, double *); - pc = siCreateCell(&cd.d, pt->ct); - if (NULL != pt->phsh) + } + break; + case CT_DOUBLE: + cd.d = *(double *)va_arg(arg, double *); + pc = siCreateCell(&cd.d, pt->ct); + if (NULL != pt->phsh) + { + switch (pt->cr) { - switch (pt->cr) - { - case CR_UNIQUE: - case CR_PRIMARY_KEY: - if (NULL == hshSearchC(pt->phsh, siHashDouble, &cd.d, sizeof(double))) - hshInsertC(pt->phsh, siHashDouble, &cd.d, sizeof(double)); - else - bins = FALSE; - break; - } + case CR_UNIQUE: + case CR_PRIMARY_KEY: + if (NULL == hshSearchC(pt->phsh, siHashDouble, &cd.d, sizeof(double))) + hshInsertC(pt->phsh, siHashDouble, &cd.d, sizeof(double)); + else + bins = FALSE; + break; } - break; - case CT_STRING: - pc = siCreateCell(va_arg(arg, char *), pt->ct); - if (NULL != pt->phsh) + } + break; + case CT_STRING: + pc = siCreateCell(va_arg(arg, char *), pt->ct); + if (NULL != pt->phsh) + { + switch (pt->cr) { - switch (pt->cr) - { - case CR_UNIQUE: - case CR_PRIMARY_KEY: - if (NULL == hshSearchCPlusA(pt->phsh, siHashString, &pc->pdata, sizeof(char *))) - hshInsertC(pt->phsh, siHashString, &pc->pdata, sizeof(char *)); - else - bins = FALSE; - break; - } + case CR_UNIQUE: + case CR_PRIMARY_KEY: + if (NULL == hshSearchCPlusA(pt->phsh, siHashString, &pc->pdata, sizeof(char *))) + hshInsertC(pt->phsh, siHashString, &pc->pdata, sizeof(char *)); + else + bins = FALSE; + break; } - break; - case CT_WSTRING: - pc = siCreateCell(va_arg(arg, wchar_t *), pt->ct); - if (NULL != pt->phsh) + } + break; + case CT_WSTRING: + pc = siCreateCell(va_arg(arg, wchar_t *), pt->ct); + if (NULL != pt->phsh) + { + switch (pt->cr) { - switch (pt->cr) - { - case CR_UNIQUE: - case CR_PRIMARY_KEY: - if (NULL == hshSearchCPlusW(pt->phsh, siHashWString, &pc->pdata, sizeof(wchar_t *))) - hshInsertC(pt->phsh, siHashWString, &pc->pdata, sizeof(wchar_t *)); - else - bins = FALSE; - break; - } + case CR_UNIQUE: + case CR_PRIMARY_KEY: + if (NULL == hshSearchCPlusW(pt->phsh, siHashWString, &pc->pdata, sizeof(wchar_t *))) + hshInsertC(pt->phsh, siHashWString, &pc->pdata, sizeof(wchar_t *)); + else + bins = FALSE; + break; } - break; } - if (bins) - strSetValueMatrix(&ptbl->tbldata, j, i, &pc, sizeof(P_CELL)); - else - break; + break; } + if (bins) + strSetValueMatrix(&ptbl->tbldata, j, i, &pc, sizeof(P_CELL)); + else + break; + } - va_end(arg); + va_end(arg); - if (NULL != ptrans && bins) - { - DATALT da; - da.at = AT_ADD_TUPLE; - da.ptbl = ptbl; - da.data.datpl.sizln = j; + if (NULL != ptrans && bins) + { + DATALT da; + da.at = AT_ADD_TUPLE; + da.ptbl = ptbl; + da.data.datpl.sizln = j; - queInjectDL(&ptrans->qoprlst, &da, sizeof(DATALT)); - } + queInjectDL(&ptrans->qoprlst, &da, sizeof(DATALT)); + } - if (!bins) - strResizeMatrix(&(ptbl->tbldata), j, ptbl->tbldata.col, sizeof(P_CELL)); + if (!bins) + if (l != ptbl->tbldata.ln) + strResizeMatrix(&ptbl->tbldata, l, ptbl->tbldata.col, sizeof(P_CELL)); + if (bins) + ++ptbl->curln; - return bins; - } - return FALSE; + return bins; } /* Function name: siDeleteFromTable @@ -633,14 +675,19 @@ BOOL siInsertIntoTable(P_TRANS ptrans, P_TABLE ptbl, ...) * Parameter: * ptrans Pointer to a transaction. * ptbl Pointer to a table you want to delete from. + * cbfta Pointer to a decreasing function. + * If this parameter equals NULL, default decreasing function will be taken. * ln Row number you want to delete. Starts from 0. * Return value: TRUE deletion succeeded. FALSE deletion failed. * Caution: Parameter ptbl must be allocated first. * Tip: N/A. */ -BOOL siDeleteFromTable(P_TRANS ptrans, P_TABLE ptbl, size_t ln) +BOOL siDeleteFromTable(P_TRANS ptrans, P_TABLE ptbl, SICBF_TBLAUG cbfta, size_t ln) { - if (ln < ptbl->tbldata.ln) + if (NULL == cbfta) + cbfta = sicbftaDefaultDecrease; + + if (ln < ptbl->curln) { size_t i; @@ -712,10 +759,16 @@ BOOL siDeleteFromTable(P_TRANS ptrans, P_TABLE ptbl, size_t ln) ( &ptbl->tbldata.arrz.pdata[ln * sizeof(P_CELL) * ptbl->tbldata.col], &ptbl->tbldata.arrz.pdata[(ln + 1) * sizeof(P_CELL) * ptbl->tbldata.col], - (ptbl->tbldata.ln - 1 - ln) * sizeof(P_CELL) * ptbl->tbldata.col + (ptbl->curln - 1 - ln) * sizeof(P_CELL) * ptbl->tbldata.col ); - strResizeMatrix(&ptbl->tbldata, ptbl->tbldata.ln - 1, ptbl->tbldata.col, sizeof(P_CELL)); + --ptbl->curln; + + i = cbfta(ptbl->curln, ptbl->tbldata.ln); + + if (i != ptbl->tbldata.ln) + strResizeMatrix(&ptbl->tbldata, i, ptbl->tbldata.col, sizeof(P_CELL)); + return TRUE; } return FALSE; @@ -956,8 +1009,8 @@ BOOL siDropTableColumn(P_TRANS ptrans, P_TABLE ptbl, size_t col) da.data.dacol.hdr = *(TBLHDR *)strLocateItemArrayZ(&ptbl->header, sizeof(TBLHDR), col); da.data.dacol.hdr.strname = strdup((*(TBLHDR *)strLocateItemArrayZ(&ptbl->header, sizeof(TBLHDR), col)).strname); - strInitArrayZ(&da.data.dacol.coldat, ptbl->tbldata.ln, sizeof(P_CELL)); - for (i = 0; i < ptbl->tbldata.ln; ++i) + strInitArrayZ(&da.data.dacol.coldat, ptbl->curln, sizeof(P_CELL)); + for (i = 0; i < ptbl->curln; ++i) { P_CELL pc; strGetValueMatrix(&pc, &ptbl->tbldata, i, col, sizeof(P_CELL)); @@ -976,7 +1029,7 @@ BOOL siDropTableColumn(P_TRANS ptrans, P_TABLE ptbl, size_t col) free(((P_TBLHDR)strLocateItemArrayZ(&ptbl->header, sizeof(TBLHDR), col))->strname); strRemoveItemArrayZ(&ptbl->header, sizeof(TBLHDR), col, TRUE); - for (i = 0; i < ptbl->tbldata.ln; ++i) + for (i = 0; i < ptbl->curln; ++i) { siDeleteCell((P_CELL *)strGetValueMatrix(NULL, &ptbl->tbldata, i, col, sizeof(P_CELL))); diff --git a/src/sitrans.c b/src/sitrans.c index de0f14f..010f9b2 100644 --- a/src/sitrans.c +++ b/src/sitrans.c @@ -2,7 +2,7 @@ * Name: sitrans.c * Description: Transaction control. * Author: cosh.cage#hotmail.com - * File ID: 0702231427D0130242055L00360 + * File ID: 0702231427D0418240134L00367 * License: GPLv2. */ @@ -143,10 +143,14 @@ void siRollbackTransaction(P_ARRAY_Z * pparr, P_TRANS ptrans) siDeleteCell(&da.data.dacell.before); break; case AT_ADD_TUPLE: - siDeleteFromTable(NULL, da.ptbl, da.data.datpl.sizln); + siDeleteFromTable(NULL, da.ptbl, NULL, da.data.datpl.sizln); break; case AT_DEL_TUPLE: - strResizeMatrix(&da.ptbl->tbldata, da.ptbl->tbldata.ln + 1, da.ptbl->tbldata.col, sizeof(P_CELL)); + i = sicbftaDefaultIncrease(da.ptbl->curln + 1, da.ptbl->tbldata.ln); + + if (i != da.ptbl->tbldata.ln) + strResizeMatrix(&da.ptbl->tbldata, i, da.ptbl->tbldata.col, sizeof(P_CELL)); + ppc = (P_CELL *)malloc(sizeof(P_CELL) * da.ptbl->tbldata.col); if (ppc) { @@ -200,15 +204,18 @@ void siRollbackTransaction(P_ARRAY_Z * pparr, P_TRANS ptrans) } } + ++da.ptbl->curln; + memmove ( &da.ptbl->tbldata.arrz.pdata[(da.data.datpl.sizln + 1) * sizeof(P_CELL) * da.ptbl->tbldata.col], &da.ptbl->tbldata.arrz.pdata[da.data.datpl.sizln * sizeof(P_CELL) * da.ptbl->tbldata.col], - (da.ptbl->tbldata.ln - 1 - da.data.datpl.sizln) * sizeof(P_CELL) * da.ptbl->tbldata.col + (da.ptbl->curln - 1 - da.data.datpl.sizln) * sizeof(P_CELL) * da.ptbl->tbldata.col ); memcpy(&da.ptbl->tbldata.arrz.pdata[da.data.datpl.sizln * sizeof(P_CELL) * da.ptbl->tbldata.col], ppc, sizeof(P_CELL) * da.ptbl->tbldata.col); free(ppc); + } for (i = 0; i < strLevelArrayZ(&da.data.datpl.tupledat); ++i) @@ -237,7 +244,7 @@ void siRollbackTransaction(P_ARRAY_Z * pparr, P_TRANS ptrans) if (NULL != strResizeMatrix(&da.ptbl->tbldata, da.ptbl->tbldata.ln, da.ptbl->tbldata.col + 1, sizeof(P_CELL))) { size_t i; - for (i = 0; i < da.ptbl->tbldata.ln; ++i) + for (i = 0; i < da.ptbl->curln; ++i) { P_CELL pc; memmove diff --git a/src/sixmem.c b/src/sixmem.c index ef01931..47433be 100644 --- a/src/sixmem.c +++ b/src/sixmem.c @@ -2,12 +2,13 @@ * Name: sixmem.c * Description: SI external memory function. * Author: cosh.cage#hotmail.com - * File ID: 0714231200H0809231000L00388 + * File ID: 0714231200H0418240130L00394 * License: GPLv2. */ #include "sixmem.h" -#include /* Using funtion strdup. */ -#include /* Using function malloc. */ +#include "svimrdb.h" /* Using macro TBL_LN_BUF_SIZ. */ +#include /* Using funtion strdup. */ +#include /* Using function malloc. */ /* DB Table file structure. * platform size for a size_t. @@ -149,18 +150,18 @@ void siSaveTable(FILE * fp, long lpos, P_TABLE ptbl) _siWriteString(((P_TBLHDR)strLocateItemArrayZ(&ptbl->header, sizeof(TBLHDR), i))->strname, fp); /* Write table size. */ - fwrite(&ptbl->tbldata.ln, sizeof(size_t), 1, fp); + fwrite(&ptbl->curln, sizeof(size_t), 1, fp); fwrite(&ptbl->tbldata.col, sizeof(size_t), 1, fp); oldl = ftell(fp); /* Fill xcell. */ - for (i = 0; i < ptbl->tbldata.ln * ptbl->tbldata.col; ++i) + for (i = 0; i < ptbl->curln * ptbl->tbldata.col; ++i) fwrite(&xc, sizeof(XCELL), 1, fp); /* Fill content. */ ppc = (P_CELL *)strGetValueMatrix(NULL, &ptbl->tbldata, 0, 0, sizeof(P_CELL)); - for (i = 0; i < ptbl->tbldata.ln * ptbl->tbldata.col; ++i) + for (i = 0; i < ptbl->curln * ptbl->tbldata.col; ++i) { curl = ftell(fp); @@ -237,6 +238,7 @@ P_TABLE siLoadTable(FILE * fp, long lpos) if ('d' == magic[0] && 'b' == magic[1] && siPlatformSize() == j) { + stdiv_t d; long oldl; XCELL xc; P_CELL * ppc; @@ -275,10 +277,14 @@ P_TABLE siLoadTable(FILE * fp, long lpos) fread(&l, sizeof(size_t), 1, fp); fread(&m, sizeof(size_t), 1, fp); + ptbl->curln = l; + d = stdiv(l, TBL_LN_BUF_SIZ); + l = TBL_LN_BUF_SIZ * d.quot + (0 == d.rem ? 0 : TBL_LN_BUF_SIZ); + strInitMatrix(&ptbl->tbldata, l, m, sizeof(P_CELL)); ppc = (P_CELL *)strGetValueMatrix(NULL, &ptbl->tbldata, 0, 0, sizeof(P_CELL)); - for (i = 0; i < l * m; ++i) + for (i = 0; i < ptbl->curln * m; ++i) { P_CELL pc = NULL; union un_CellData @@ -334,7 +340,7 @@ P_TABLE siLoadTable(FILE * fp, long lpos) *(ppc++) = pc; fseek(fp, oldl, SEEK_SET); } - for (i = 0; i < ptbl->tbldata.ln; ++i) + for (i = 0; i < ptbl->curln; ++i) { for (j = 0; j < ptbl->tbldata.col; ++j) { diff --git a/src/svimrdb.h b/src/svimrdb.h index 931cab0..fccf6e1 100644 --- a/src/svimrdb.h +++ b/src/svimrdb.h @@ -2,7 +2,7 @@ * Name: svimrdb.h * Description: StoneValley in-memory relational database (aka SI). * Author: cosh.cage#hotmail.com - * File ID: 0628231947A0130242055L00197 + * File ID: 0628231947A0418240133L00212 * License: GPLv2. */ #ifndef _SVIMRDB_H_ @@ -50,21 +50,25 @@ typedef enum en_ColRstct /* Table header element. */ typedef struct st_TBLHDR { - CellType ct; /* Data type. */ - char * strname; /* Column name. */ - ColRstct cr; /* Restriction. */ - P_HSHTBL_C phsh; /* Hash table for searching. */ + CellType ct; /* Data type. */ + char * strname; /* Column name. */ + ColRstct cr; /* Restriction. */ + P_HSHTBL_C phsh; /* Hash table for searching. */ } TBLHDR, * P_TBLHDR; /* Selection function. */ typedef BOOL(*SICBF_SELECT)(P_CELL * pitem, size_t param); +/* Table augmentation function. */ +typedef size_t(*SICBF_TBLAUG)(size_t curln, size_t ln); + /* Table structure. */ typedef struct st_TABLE { - char * tblname; /* Table name. */ - ARRAY_Z header; /* Table header. */ - MATRIX tbldata; /* Table data. */ + char * tblname; /* Table name. */ + ARRAY_Z header; /* Table header. */ + size_t curln; /* Current line. */ + MATRIX tbldata; /* Table data. */ } TABLE, * P_TABLE; /* Lock type. */ @@ -129,7 +133,7 @@ typedef struct st_DATALT typedef struct st_TRANS { DEQUE_DL qoprlst; /* Operation list. */ - SET_T setlock; /* Lock set. */ + SET_T setlock; /* Lock set. */ } TRANS, * P_TRANS; /* Table reference. */ @@ -152,6 +156,9 @@ size_t siHashWString(const void * pkey); P_NODE_S hshSearchCPlusA(P_HSHTBL_C pht, CBF_HASH cbfhsh, const void * pkey, size_t size); P_NODE_S hshSearchCPlusW(P_HSHTBL_C pht, CBF_HASH cbfhsh, const void * pkey, size_t size); size_t siPlatformSize(void); +void siPrintSystemVersion(void); +size_t sicbftaDefaultIncrease(size_t curln, size_t ln); +size_t sicbftaDefaultDecrease(size_t curln, size_t ln); /* Relational algebraic functions. */ P_MATRIX siCreateUniqueView(P_MATRIX pmtx); P_MATRIX siCreateUnionView(P_MATRIX pmtxa, P_MATRIX pmtxb); @@ -173,8 +180,8 @@ P_MATRIX siCreateViewOfTable(P_TABLE ptbl); P_TABLE siCreateTable(P_TRANS ptrans, char * tblname, P_ARRAY_Z parrhdr); P_TABLE siCopyTable(P_TRANS ptrans, P_TABLE ptbl); void siDeleteTable(P_TRANS ptrans, P_TABLE ptbl); -BOOL siInsertIntoTable(P_TRANS ptrans, P_TABLE ptbl, ...); -BOOL siDeleteFromTable(P_TRANS ptrans, P_TABLE ptbl, size_t col); +BOOL siInsertIntoTable(P_TRANS ptrans, P_TABLE ptbl, SICBF_TBLAUG cbfta, ...); +BOOL siDeleteFromTable(P_TRANS ptrans, P_TABLE ptbl, SICBF_TBLAUG cbfta, size_t ln); BOOL siUpdateTableCell(P_TRANS ptrans, P_TABLE ptbl, void * pval, size_t ln, size_t col); BOOL siAddTableColumn(P_TRANS ptrans, P_TABLE ptbl, P_TBLHDR phdr); BOOL siDropTableColumn(P_TRANS ptrans, P_TABLE ptbl, size_t col); @@ -187,10 +194,20 @@ void siReleaseAllTransaction(); BOOL siTrylock(P_TRANS ptrans, void * pobj, LockType lt); void siUnlock(P_TRANS ptrans, void * pobj, LockType lt); -#define BKSNUM 1021 /* 1021 is a prime. */ +#define BKSNUM 1021 /* 1021 is a prime. */ +#define TBL_LN_BUF_SIZ 512 + // #define strdup _strdup /* POSIX compliant for visual C compiler. */ -//#define pthread_mutex_lock(abc) -//#define pthread_mutex_unlock(def) +/* The following macros are for single thread debugging. */ +/* +#define pthread_mutex_lock(abc) +#define pthread_mutex_unlock(def) +#define pthread_mutex_t int +#define PTHREAD_MUTEX_INITIALIZER 0 +*/ + +/* System version. */ +#define SYS_VER "1.0.1" #endif