updates for rpms lots o bug fixes, index stuff

This commit is contained in:
brianb
2003-01-12 22:59:41 +00:00
parent f35c3f05b9
commit 2af5847c6f
63 changed files with 2392 additions and 241 deletions

View File

@@ -19,7 +19,8 @@
#include "mdbtools.h"
char *mdb_get_objtype_string(int obj_type)
char *
mdb_get_objtype_string(int obj_type)
{
static char *type_name[] = {"Form",
"Table",
@@ -43,13 +44,10 @@ static char *type_name[] = {"Form",
}
/* new method */
#if 1
GPtrArray *mdb_read_catalog (MdbHandle *mdb, int objtype)
{
int i, j, k;
MdbCatalogEntry entry, msysobj, *data;
MdbTableDef *table;
MdbColumn *col;
char parentid[256];
char objname[256];
char tobjtype[256];
@@ -98,7 +96,7 @@ int type;
/* old method */
#else
#if 0
MdbCatalogEntry *mdb_read_catalog_entry(MdbHandle *mdb, int rowid, MdbCatalogEntry *entry)
{
@@ -146,7 +144,8 @@ int mdb_catalog_rows(MdbHandle *mdb)
{
return mdb_get_int16(mdb, mdb->row_count_offset);
}
GArray *mdb_read_catalog(MdbHandle *mdb, int obj_type)
GPtrArray *
mdb_read_catalog(MdbHandle *mdb, int obj_type)
{
int i;
int rows;
@@ -192,7 +191,7 @@ int next_pg, next_pg_off;
mdb_free_catalog(mdb);
mdb->num_catalog = 0;
mdb->catalog = g_array_new(FALSE,FALSE,sizeof(MdbCatalogEntry));
mdb->catalog = g_ptr_array_new();
next_pg=0;
while (mdb_read_pg(mdb,next_pg)) {
if (mdb->pg_buf[0]==0x01 &&
@@ -205,7 +204,7 @@ int next_pg, next_pg_off;
if (mdb_read_catalog_entry(mdb, i, &entry)) {
//printf("page %d\n",next_pg);
mdb->num_catalog++;
mdb->catalog = g_array_append_val(mdb->catalog, entry);
g_ptr_array_add(mdb->catalog, entry);
}
}
}
@@ -213,10 +212,11 @@ int next_pg, next_pg_off;
}
}
#endif
void mdb_dump_catalog(MdbHandle *mdb, int obj_type)
void
mdb_dump_catalog(MdbHandle *mdb, int obj_type)
{
int rows, i;
MdbCatalogEntry *entry;
int i;
MdbCatalogEntry *entry;
mdb_read_catalog(mdb, obj_type);
for (i=0;i<mdb->num_catalog;i++) {
@@ -225,8 +225,8 @@ MdbCatalogEntry *entry;
fprintf(stdout,"Type: %-10s Name: %-18s T pg: %04x KKD pg: %04x row: %2d\n",
mdb_get_objtype_string(entry->object_type),
entry->object_name,
entry->table_pg,
entry->kkd_pg,
(unsigned int) entry->table_pg,
(unsigned int) entry->kkd_pg,
entry->kkd_rowid);
}
}

View File

@@ -46,10 +46,11 @@ MdbColumn *col;
col=g_ptr_array_index(table->columns, col_num - 1);
col->len_ptr = len_ptr;
}
int mdb_find_end_of_row(MdbHandle *mdb, int row)
int
mdb_find_end_of_row(MdbHandle *mdb, int row)
{
MdbFormatConstants *fmt = mdb->fmt;
int row_end;
MdbFormatConstants *fmt = mdb->fmt;
int row_end;
/* Search the previous "row start" values for the first non-deleted one.
* If we don't find one, then the end of the page is the correct value.

View File

@@ -64,7 +64,7 @@ int ret;
return 0;
}
g_free(tmpfname);
} while (dir = strtok(NULL, ":"));
} while ((dir = strtok(NULL, ":")));
return -1;
}
MdbHandle *_mdb_open(char *filename, gboolean writable)
@@ -154,10 +154,10 @@ MdbHandle *mdb_clone_handle(MdbHandle *mdb)
newmdb = mdb_alloc_handle();
memcpy(newmdb, mdb, sizeof(MdbHandle));
newmdb->stats = NULL;
newmdb->catalog = g_array_new(FALSE,FALSE,sizeof(MdbCatalogEntry));
newmdb->catalog = g_ptr_array_new();
for (i=0;i<mdb->num_catalog;i++) {
entry = g_ptr_array_index(mdb->catalog,i);
newmdb->catalog = g_array_append_val(newmdb->catalog, entry);
g_ptr_array_add(newmdb->catalog, entry);
}
mdb->backend_name = NULL;
if (mdb->f) {
@@ -227,13 +227,14 @@ unsigned char c;
mdb->cur_pos++;
return c;
}
int _mdb_get_int16(unsigned char *buf, int offset)
int
_mdb_get_int16(unsigned char *buf, int offset)
{
return buf[offset+1]*256+buf[offset];
}
int mdb_get_int16(MdbHandle *mdb, int offset)
int
mdb_get_int16(MdbHandle *mdb, int offset)
{
unsigned char *c;
int i;
if (offset < 0 || offset+2 > mdb->fmt->pg_size) return -1;
@@ -244,7 +245,8 @@ int i;
return i;
}
gint32 mdb_get_int24_msb(MdbHandle *mdb, int offset)
gint32
mdb_get_int24_msb(MdbHandle *mdb, int offset)
{
gint32 l;
unsigned char *c;
@@ -295,11 +297,15 @@ long l;
mdb->cur_pos+=4;
return l;
}
float mdb_get_single(MdbHandle *mdb, int offset)
float
mdb_get_single(MdbHandle *mdb, int offset)
{
float f, f2;
unsigned char *c;
int i;
#ifdef WORDS_BIGENDIAN
float f2;
int i;
unsigned char *c;
#endif
float f;
if (offset <0 || offset+4 > mdb->fmt->pg_size) return -1;
@@ -316,11 +322,15 @@ int i;
return f;
}
double mdb_get_double(MdbHandle *mdb, int offset)
double
mdb_get_double(MdbHandle *mdb, int offset)
{
double d, d2;
unsigned char *c;
int i;
#ifdef WORDS_BIGENDIAN
double d2;
int i;
unsigned char *c;
#endif
double d;
if (offset <0 || offset+4 > mdb->fmt->pg_size) return -1;
@@ -337,7 +347,8 @@ int i;
return d;
}
int mdb_set_pos(MdbHandle *mdb, int pos)
int
mdb_set_pos(MdbHandle *mdb, int pos)
{
if (pos<0 || pos >= mdb->fmt->pg_size) return 0;

View File

@@ -53,7 +53,8 @@ char idx_to_text[] = {
0x81, 0x00, 0x00, 0x00, 'x', 0x00, 0x00, 0x00, /* 0xf8-0xff */
};
GPtrArray *mdb_read_indices(MdbTableDef *table)
GPtrArray *
mdb_read_indices(MdbTableDef *table)
{
MdbHandle *mdb = table->entry->mdb;
MdbIndex idx, *pidx;
@@ -170,7 +171,7 @@ void mdb_index_cache_sarg(MdbColumn *col, MdbSarg *sarg, MdbSarg *idx_sarg)
//cache_int = sarg->value.i * -1;
c = &(idx_sarg->value.i);
c[0] |= 0x80;
printf("int %08x %02x %02x %02x %02x\n", sarg->value.i, c[0], c[1], c[2], c[3]);
//printf("int %08x %02x %02x %02x %02x\n", sarg->value.i, c[0], c[1], c[2], c[3]);
break;
case MDB_INT:
@@ -200,7 +201,7 @@ mdb_index_test_sargs(MdbHandle *mdb, MdbIndex *idx, int offset, int len)
c_len = strlen(&mdb->pg_buf[offset + c_offset]);
} else {
c_len = col->col_size;
fprintf(stderr,"Only text types currently supported. How did we get here?\n");
//fprintf(stderr,"Only text types currently supported. How did we get here?\n");
}
/*
* If we have no cached index values for this column,
@@ -211,7 +212,7 @@ mdb_index_test_sargs(MdbHandle *mdb, MdbIndex *idx, int offset, int len)
for (j=0;j<col->num_sargs;j++) {
sarg = g_ptr_array_index (col->sargs, j);
idx_sarg = g_memdup(sarg,sizeof(MdbSarg));
printf("calling mdb_index_cache_sarg\n");
//printf("calling mdb_index_cache_sarg\n");
mdb_index_cache_sarg(col, sarg, idx_sarg);
g_ptr_array_add(col->idx_sarg_cache, idx_sarg);
}
@@ -227,6 +228,10 @@ mdb_index_test_sargs(MdbHandle *mdb, MdbIndex *idx, int offset, int len)
}
return 1;
}
/*
* find the next entry on a page (either index or leaf). Uses state information
* stored in the MdbIndexPage across calls.
*/
int
mdb_index_find_next_on_page(MdbHandle *mdb, MdbIndexPage *ipg)
{
@@ -249,45 +254,93 @@ mdb_index_find_next_on_page(MdbHandle *mdb, MdbIndexPage *ipg)
}
void mdb_index_page_init(MdbIndexPage *ipg)
{
memset(ipg, 0, sizeof(MdbIndexPage));
ipg->offset = 0xf8; /* start byte of the index entries */
ipg->mask_pos = 0x16;
ipg->mask_bit=0;
ipg->len = 0;
}
int
/*
* find the next leaf page if any given a chain. Assumes any exhausted leaf
* pages at the end of the chain have been peeled off before the call.
*/
MdbIndexPage *
mdb_find_next_leaf(MdbHandle *mdb, MdbIndexChain *chain)
{
MdbIndexPage *ipg;
MdbIndexPage *ipg, *newipg;
guint32 pg;
guint passed = 0;
ipg = &(chain->pages[chain->cur_depth - 1]);
/*
* If we are at the first page deep and it's not an index page then
* we are simply done. (there is no page to find
*/
if (chain->cur_depth==1) {
ipg = &(chain->pages[0]);
if (mdb->pg_buf[0]==MDB_PAGE_LEAF ||
mdb->pg_buf[0]==MDB_PAGE_DATA) {
return ipg->pg;
mdb_read_pg(mdb, ipg->pg);
if (mdb->pg_buf[0]==MDB_PAGE_LEAF)
return ipg;
/*
* apply sargs here, currently we don't
*/
do {
ipg->len = 0;
//printf("finding next on pg %lu\n", ipg->pg);
if (!mdb_index_find_next_on_page(mdb, ipg))
return 0;
pg = mdb_get_int24_msb(mdb, ipg->offset + ipg->len - 3);
//printf("Looking at pg %lu at %lu %d\n", pg, ipg->offset, ipg->len);
ipg->offset += ipg->len;
/*
* add to the chain and call this function
* recursively.
*/
chain->cur_depth++;
if (chain->cur_depth > MDB_MAX_INDEX_DEPTH) {
fprintf(stderr,"Error! maximum index depth of %d exceeded. This is probably due to a programming bug, If you are confident that your indexes really are this deep, adjust MDB_MAX_INDEX_DEPTH in mdbtools.h and recompile.\n");
exit(1);
}
}
ipg = &(chain->pages[chain->cur_depth - 1]);
newipg = &(chain->pages[chain->cur_depth - 1]);
mdb_index_page_init(newipg);
newipg->pg = pg;
newipg = mdb_find_next_leaf(mdb, chain);
//printf("returning pg %lu\n",newipg->pg);
return newipg;
} while (!passed);
/* no more pages */
return 0;
return NULL;
}
/*
* the main index function.
* caller provides an index chain which is the current traversal of index
* pages from the root page to the leaf. Initially passed as blank,
* mdb_index_find_next will store it's state information here. Each invocation
* then picks up where the last one left off, allowing us to scroll through
* the index one by one.
*
* Sargs are applied here but also need to be applied on the whole row b/c
* text columns may return false positives due to hashing and non-index
* columns with sarg values can't be tested here.
*/
int
mdb_index_find_next(MdbHandle *mdb, MdbIndex *idx, MdbIndexChain *chain, guint32 *pg, guint16 *row)
{
MdbIndexPage *ipg;
int passed = 0;
/*
* if it's new use the root index page (idx->first_pg)
*/
if (!chain->cur_depth) {
ipg = &(chain->pages[0]);
mdb_index_page_init(ipg);
chain->cur_depth = 1;
ipg->pg = idx->first_pg;
if (!mdb_find_next_leaf(mdb, chain))
if (!(ipg = mdb_find_next_leaf(mdb, chain)))
return 0;
} else {
ipg = &(chain->pages[chain->cur_depth - 1]);
@@ -296,10 +349,31 @@ mdb_index_find_next(MdbHandle *mdb, MdbIndex *idx, MdbIndexChain *chain, guint32
mdb_read_pg(mdb, ipg->pg);
/*
* loop while the sargs don't match
*/
do {
ipg->len = 0;
if (!mdb_index_find_next_on_page(mdb, ipg))
return 0;
/*
* if no more rows on this leaf, try to find a new leaf
*/
if (!mdb_index_find_next_on_page(mdb, ipg)) {
//printf("page %lu finished\n",ipg->pg);
if (chain->cur_depth==1)
return 0;
/*
* unwind the stack until we find something or reach
* the top.
*/
while (chain->cur_depth>1) {
chain->cur_depth--;
if (!(ipg = mdb_find_next_leaf(mdb, chain)))
return 0;
mdb_index_find_next_on_page(mdb, ipg);
}
if (chain->cur_depth==1)
return 0;
}
*row = mdb->pg_buf[ipg->offset + ipg->len - 1];
*pg = mdb_get_int24_msb(mdb, ipg->offset + ipg->len - 4);
@@ -332,10 +406,11 @@ int i;
printf("column %d coltype %d col_size %d (%d)\n",i,col->col_type, mdb_col_fixed_size(col), col->col_size);
}
}
void mdb_index_dump(MdbTableDef *table, MdbIndex *idx)
void
mdb_index_dump(MdbTableDef *table, MdbIndex *idx)
{
int i;
MdbColumn *col;
int i;
MdbColumn *col;
fprintf(stdout,"index number %d\n", idx->index_num);
fprintf(stdout,"index name %s\n", idx->name);

View File

@@ -27,10 +27,9 @@
GArray *mdb_get_column_props(MdbCatalogEntry *entry, int start)
{
int i, j=0, pos, cnt=0;
int pos, cnt=0;
int len, tmp, cplen;
MdbColumnProp prop;
char name[MDB_MAX_OBJ_NAME+1];
MdbHandle *mdb = entry->mdb;
entry->props = g_array_new(FALSE,FALSE,sizeof(MdbColumnProp));
@@ -100,13 +99,8 @@ void mdb_kkd_dump(MdbCatalogEntry *entry)
{
int rows;
int kkd_start, kkd_end;
int i, j, tmp, pos, row_type, hdrpos=0, datapos=0;
int len;
int col_type, col_num, val_len;
int start;
unsigned char c;
int i, tmp, pos, row_type, datapos=0;
MdbColumnProp prop;
char name[MDB_MAX_OBJ_NAME+1];
MdbHandle *mdb = entry->mdb;
int rowid = entry->kkd_rowid;

View File

@@ -35,20 +35,24 @@ MdbStatistics *mdb_alloc_stats(MdbHandle *mdb)
mdb->stats = g_malloc0(sizeof(MdbStatistics));
return mdb->stats;
}
void mdb_free_stats(MdbHandle *mdb)
void
mdb_free_stats(MdbHandle *mdb)
{
g_free(mdb->stats);
mdb->stats = NULL;
}
MdbFile *mdb_alloc_file()
MdbFile *
mdb_alloc_file()
{
MdbHandle *f;
MdbFile *f;
f = (MdbFile *) malloc(sizeof(MdbFile));
memset(f, '\0', sizeof(MdbFile));
return f;
}
void mdb_free_file(MdbFile *f)
void
mdb_free_file(MdbFile *f)
{
if (!f) return;
if (f->fd) close(f->fd);
@@ -82,9 +86,6 @@ void mdb_alloc_catalog(MdbHandle *mdb)
}
void mdb_free_catalog(MdbHandle *mdb)
{
GList *l;
MdbCatalogEntry entry;
//g_ptr_array_free(mdb->catalog, FALSE);
mdb->catalog = NULL;
}
@@ -99,11 +100,13 @@ MdbTableDef *table;
return table;
}
void mdb_free_tabledef(MdbTableDef *table)
void
mdb_free_tabledef(MdbTableDef *table)
{
if (table->usage_map) free(table->usage_map);
if (table) free(table);
}
void
mdb_append_column(GPtrArray *columns, MdbColumn *in_col)
{
MdbColumn *col;
@@ -111,10 +114,12 @@ MdbColumn *col;
col = g_memdup(in_col,sizeof(MdbColumn));
g_ptr_array_add(columns, col);
}
void
mdb_free_columns(GPtrArray *columns)
{
g_ptr_array_free(columns, TRUE);
}
void
mdb_append_index(GPtrArray *indices, MdbIndex *in_idx)
{
MdbIndex *idx;
@@ -122,6 +127,7 @@ MdbIndex *idx;
idx = g_memdup(in_idx,sizeof(MdbIndex));
g_ptr_array_add(indices, idx);
}
void
mdb_free_indices(GPtrArray *indices)
{
g_ptr_array_free(indices, TRUE);

View File

@@ -45,7 +45,7 @@ MdbTableDef *mdb_read_table(MdbCatalogEntry *entry)
MdbTableDef *table;
MdbHandle *mdb = entry->mdb;
MdbFormatConstants *fmt = mdb->fmt;
int len, i;
int len;
int rownum, row_start, row_end;
table = mdb_alloc_tabledef(entry);
@@ -104,9 +104,7 @@ MdbColumn col, *pcol;
int len, i,j;
unsigned char low_byte, high_byte;
int cur_col, cur_name;
int col_type, col_size;
char name[MDB_MAX_OBJ_NAME+1];
int name_sz, col_num;
int name_sz;
GSList *slist = NULL;
table->columns = g_ptr_array_new();
@@ -238,11 +236,11 @@ int i,bitn;
int pgnum;
table = mdb_read_table(entry);
fprintf(stdout,"definition page = %d\n",entry->table_pg);
fprintf(stdout,"definition page = %lu\n",entry->table_pg);
fprintf(stdout,"number of datarows = %d\n",table->num_rows);
fprintf(stdout,"number of columns = %d\n",table->num_cols);
fprintf(stdout,"number of indices = %d\n",table->num_real_idxs);
fprintf(stdout,"first data page = %d\n",table->first_data_pg);
fprintf(stdout,"first data page = %lu\n",table->first_data_pg);
mdb_read_columns(table);
mdb_read_indices(table);