mirror of
https://github.com/mdbtools/mdbtools.git
synced 2026-03-10 00:20:54 +08:00
Some work on indexes/write support. Bug fixes. Preliminary temp table/worktable work.
This commit is contained in:
@@ -221,23 +221,42 @@ int mdb_read_row(MdbTableDef *table, int row)
|
||||
lookupflag ? "[lookup]" : "",
|
||||
delflag ? "[delflag]" : "");
|
||||
#endif
|
||||
num_fields = mdb_crack_row(table, row_start, row_end, &fields);
|
||||
if (!mdb_test_sargs(table, &fields, num_fields)) return 0;
|
||||
for (i=0; i < num_fields; i++) {
|
||||
//col = g_ptr_array_index (table->columns, fields[i].colnum - 1);
|
||||
//rc = _mdb_attempt_bind(mdb, col, fields[i].is_null,
|
||||
//row_start + col_start, col->col_size);
|
||||
}
|
||||
//if (!table->noskip_del && (delflag || lookupflag)) {
|
||||
|
||||
if (!table->noskip_del && delflag) {
|
||||
row_end = row_start-1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
num_fields = mdb_crack_row(table, row_start, row_end, &fields);
|
||||
if (!mdb_test_sargs(table, &fields, num_fields)) return 0;
|
||||
|
||||
#if MDB_DEBUG
|
||||
fprintf(stdout,"sarg test passed row %d \n", row);
|
||||
#endif
|
||||
|
||||
#if MDB_DEBUG
|
||||
buffer_dump(mdb->pg_buf, row_start, row_end);
|
||||
#endif
|
||||
|
||||
#if 1
|
||||
/* take advantage of mdb_crack_row() to clean up binding */
|
||||
for (i = 0; i < num_fields; i++) {
|
||||
col = g_ptr_array_index(table->columns,fields[i].colnum);
|
||||
if (fields[i].is_fixed) {
|
||||
rc = _mdb_attempt_bind(mdb, col,
|
||||
fields[i].is_null,
|
||||
fields[i].start,
|
||||
col->col_size);
|
||||
} else {
|
||||
rc = _mdb_attempt_bind(mdb, col,
|
||||
fields[i].is_null,
|
||||
fields[i].start,
|
||||
fields[i].siz);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
/* find out all the important stuff about the row */
|
||||
if (IS_JET4(mdb)) {
|
||||
num_cols = mdb_pg_get_int16(mdb, row_start);
|
||||
@@ -386,6 +405,7 @@ int mdb_read_row(MdbTableDef *table, int row)
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
static int _mdb_attempt_bind(MdbHandle *mdb,
|
||||
|
||||
@@ -23,6 +23,9 @@
|
||||
#include "dmalloc.h"
|
||||
#endif
|
||||
|
||||
MdbIndexPage *mdb_index_read_bottom_pg(MdbHandle *mdb, MdbIndex *idx, MdbIndexChain *chain);
|
||||
MdbIndexPage *mdb_chain_add_page(MdbHandle *mdb, MdbIndexChain *chain, guint32 pg);
|
||||
|
||||
char idx_to_text[] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0-7 0x00-0x07 */
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8-15 0x09-0x0f */
|
||||
@@ -334,20 +337,19 @@ void mdb_index_page_init(MdbIndexPage *ipg)
|
||||
* pages at the end of the chain have been peeled off before the call.
|
||||
*/
|
||||
MdbIndexPage *
|
||||
mdb_find_next_leaf(MdbHandle *mdb, MdbIndexChain *chain)
|
||||
mdb_find_next_leaf(MdbHandle *mdb, MdbIndex *idx, MdbIndexChain *chain)
|
||||
{
|
||||
MdbIndexPage *ipg, *newipg;
|
||||
guint32 pg;
|
||||
guint passed = 0;
|
||||
|
||||
ipg = &(chain->pages[chain->cur_depth - 1]);
|
||||
ipg = mdb_index_read_bottom_pg(mdb, idx, chain);
|
||||
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
mdb_read_pg(mdb, ipg->pg);
|
||||
if (mdb->pg_buf[0]==MDB_PAGE_LEAF)
|
||||
return ipg;
|
||||
|
||||
@@ -367,15 +369,8 @@ mdb_find_next_leaf(MdbHandle *mdb, MdbIndexChain *chain)
|
||||
* 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", MDB_MAX_INDEX_DEPTH);
|
||||
exit(1);
|
||||
}
|
||||
newipg = &(chain->pages[chain->cur_depth - 1]);
|
||||
mdb_index_page_init(newipg);
|
||||
newipg->pg = pg;
|
||||
newipg = mdb_find_next_leaf(mdb, chain);
|
||||
newipg = mdb_chain_add_page(mdb, chain, pg);
|
||||
newipg = mdb_find_next_leaf(mdb, idx, chain);
|
||||
//printf("returning pg %lu\n",newipg->pg);
|
||||
return newipg;
|
||||
} while (!passed);
|
||||
@@ -383,6 +378,46 @@ mdb_find_next_leaf(MdbHandle *mdb, MdbIndexChain *chain)
|
||||
return NULL;
|
||||
|
||||
}
|
||||
MdbIndexPage *
|
||||
mdb_chain_add_page(MdbHandle *mdb, MdbIndexChain *chain, guint32 pg)
|
||||
{
|
||||
MdbIndexPage *ipg;
|
||||
|
||||
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", MDB_MAX_INDEX_DEPTH);
|
||||
exit(1);
|
||||
}
|
||||
ipg = &(chain->pages[chain->cur_depth - 1]);
|
||||
mdb_index_page_init(ipg);
|
||||
ipg->pg = pg;
|
||||
|
||||
return ipg;
|
||||
}
|
||||
MdbIndexPage *
|
||||
mdb_index_read_bottom_pg(MdbHandle *mdb, MdbIndex *idx, MdbIndexChain *chain)
|
||||
{
|
||||
MdbIndexPage *ipg;
|
||||
|
||||
/*
|
||||
* 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 (!(ipg = mdb_find_next_leaf(mdb, idx, chain)))
|
||||
return 0;
|
||||
} else {
|
||||
ipg = &(chain->pages[chain->cur_depth - 1]);
|
||||
ipg->len = 0;
|
||||
}
|
||||
|
||||
mdb_read_pg(mdb, ipg->pg);
|
||||
|
||||
return ipg;
|
||||
}
|
||||
/*
|
||||
* the main index function.
|
||||
* caller provides an index chain which is the current traversal of index
|
||||
@@ -401,22 +436,8 @@ mdb_index_find_next(MdbHandle *mdb, MdbIndex *idx, MdbIndexChain *chain, guint32
|
||||
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 (!(ipg = mdb_find_next_leaf(mdb, chain)))
|
||||
return 0;
|
||||
} else {
|
||||
ipg = &(chain->pages[chain->cur_depth - 1]);
|
||||
ipg->len = 0;
|
||||
}
|
||||
|
||||
mdb_read_pg(mdb, ipg->pg);
|
||||
ipg = mdb_index_read_bottom_pg(mdb, idx, chain);
|
||||
|
||||
/*
|
||||
* loop while the sargs don't match
|
||||
@@ -436,7 +457,7 @@ mdb_index_find_next(MdbHandle *mdb, MdbIndex *idx, MdbIndexChain *chain, guint32
|
||||
*/
|
||||
while (chain->cur_depth>1) {
|
||||
chain->cur_depth--;
|
||||
if (!(ipg = mdb_find_next_leaf(mdb, chain)))
|
||||
if (!(ipg = mdb_find_next_leaf(mdb, idx, chain)))
|
||||
return 0;
|
||||
mdb_index_find_next_on_page(mdb, ipg);
|
||||
}
|
||||
|
||||
@@ -82,8 +82,8 @@ int rownum, row_start, row_end;
|
||||
|
||||
|
||||
/* now grab the free space page map */
|
||||
#if 0
|
||||
mdb_swap_pgbuf(mdb);
|
||||
#if 1
|
||||
//mdb_swap_pgbuf(mdb);
|
||||
rownum = mdb->pg_buf[fmt->tab_free_map_offset];
|
||||
mdb_read_alt_pg(mdb, mdb_pg_get_int24(mdb, fmt->tab_free_map_offset + 1));
|
||||
mdb_swap_pgbuf(mdb);
|
||||
@@ -92,6 +92,7 @@ int rownum, row_start, row_end;
|
||||
table->freemap_sz = row_end - row_start + 1;
|
||||
table->free_usage_map = malloc(table->freemap_sz);
|
||||
memcpy(table->free_usage_map, &mdb->pg_buf[row_start], table->freemap_sz);
|
||||
mdb_swap_pgbuf(mdb);
|
||||
#endif
|
||||
|
||||
table->first_data_pg = mdb_pg_get_int16(mdb, fmt->tab_first_dpg_offset);
|
||||
|
||||
@@ -246,7 +246,7 @@ int i;
|
||||
/* column is null is bit is clear (0) */
|
||||
if (!fields[i].is_null) {
|
||||
byte |= 1 << bit;
|
||||
printf("%d %d %d %d\n", i, bit, 1 << bit, byte);
|
||||
//printf("%d %d %d %d\n", i, bit, 1 << bit, byte);
|
||||
}
|
||||
bit++;
|
||||
if (bit==8) {
|
||||
@@ -302,9 +302,48 @@ mdb_update_indexes(MdbTableDef *table, int num_fields, MdbField *fields)
|
||||
#if MDB_DEBUG_WRITE
|
||||
fprintf(stderr,"Updating %s (%d).\n", idx->name, idx->index_type);
|
||||
#endif
|
||||
if (idx->index_type==1) {
|
||||
mdb_update_index(table, idx, num_fields, fields);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mdb_init_index_chain(MdbTableDef *table, MdbIndex *idx)
|
||||
{
|
||||
MdbCatalogEntry *entry = table->entry;
|
||||
MdbHandle *mdb = entry->mdb;
|
||||
|
||||
table->scan_idx = idx;
|
||||
table->chain = g_malloc0(sizeof(MdbIndexChain));
|
||||
table->mdbidx = mdb_clone_handle(mdb);
|
||||
mdb_read_pg(table->mdbidx, table->scan_idx->first_pg);
|
||||
}
|
||||
int
|
||||
mdb_update_index(MdbTableDef *table, MdbIndex *idx, int num_fields, MdbField *fields)
|
||||
{
|
||||
int idx_xref[16];
|
||||
int i, j;
|
||||
|
||||
for (i = 0; i < idx->num_keys; i++) {
|
||||
for (j = 0; j < num_fields; j++) {
|
||||
// key_col_num is 1 based, can't remember why though
|
||||
if (fields[j].colnum == idx->key_col_num[i]-1)
|
||||
idx_xref[i] = j;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < idx->num_keys; i++) {
|
||||
fprintf(stdout, "key col %d (%d) is mapped to field %d (%d %d)\n",
|
||||
i, idx->key_col_num[i], idx_xref[i], fields[idx_xref[i]].colnum,
|
||||
fields[idx_xref[i]].siz);
|
||||
}
|
||||
for (i = 0; i < num_fields; i++) {
|
||||
fprintf(stdout, "%d (%d %d)\n",
|
||||
i, fields[i].colnum,
|
||||
fields[i].siz);
|
||||
}
|
||||
//mdb_find_leaf_pg();
|
||||
}
|
||||
|
||||
int
|
||||
mdb_insert_row(MdbTableDef *table, int num_fields, MdbField *fields)
|
||||
{
|
||||
@@ -330,11 +369,39 @@ mdb_insert_row(MdbTableDef *table, int num_fields, MdbField *fields)
|
||||
return 0;
|
||||
}
|
||||
|
||||
mdb_add_row_to_pg(table, row_buffer, new_row_size);
|
||||
|
||||
#if MDB_DEBUG_WRITE
|
||||
buffer_dump(mdb->pg_buf, 0, 39);
|
||||
buffer_dump(mdb->pg_buf, fmt->pg_size - 160, fmt->pg_size-1);
|
||||
fprintf(stdout, "writing page %d\n", pgnum);
|
||||
#endif
|
||||
if (!mdb_write_pg(mdb, pgnum)) {
|
||||
fprintf(stderr, "write failed! exiting...\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
mdb_update_indexes(table, num_fields, fields);
|
||||
}
|
||||
/*
|
||||
* Assumes caller has verfied space is available on page and adds the new
|
||||
* row to the current pg_buf.
|
||||
*/
|
||||
void
|
||||
mdb_add_row_to_pg(MdbTableDef *table, unsigned char *row_buffer, int new_row_size)
|
||||
{
|
||||
unsigned char *new_pg;
|
||||
int num_rows, i, pos, row_start, row_end, row_size;
|
||||
MdbCatalogEntry *entry = table->entry;
|
||||
MdbHandle *mdb = entry->mdb;
|
||||
MdbFormatConstants *fmt = mdb->fmt;
|
||||
|
||||
new_pg = mdb_new_data_pg(entry);
|
||||
|
||||
num_rows = mdb_pg_get_int16(mdb, fmt->row_count_offset);
|
||||
pos = mdb->fmt->pg_size;
|
||||
|
||||
/* copy existing rows */
|
||||
for (i=0;i<num_rows;i++) {
|
||||
row_start = mdb_pg_get_int16(mdb, (fmt->row_count_offset + 2) + (i*2));
|
||||
row_end = mdb_find_end_of_row(mdb, i);
|
||||
@@ -347,19 +414,19 @@ mdb_insert_row(MdbTableDef *table, int num_fields, MdbField *fields)
|
||||
/* add our new row */
|
||||
pos -= new_row_size;
|
||||
memcpy(&new_pg[pos], row_buffer, new_row_size);
|
||||
/* add row to the row offset table */
|
||||
_mdb_put_int16(new_pg, (fmt->row_count_offset + 2) + (num_rows*2), pos);
|
||||
|
||||
/* update number rows on this page */
|
||||
num_rows++;
|
||||
_mdb_put_int16(new_pg, fmt->row_count_offset, num_rows);
|
||||
|
||||
/* copy new page over old */
|
||||
memcpy(mdb->pg_buf, new_pg, fmt->pg_size);
|
||||
g_free(new_pg);
|
||||
#if MDB_DEBUG_WRITE
|
||||
buffer_dump(mdb->pg_buf, 0, 39);
|
||||
buffer_dump(mdb->pg_buf, fmt->pg_size - 160, fmt->pg_size-1);
|
||||
#endif
|
||||
|
||||
mdb_update_indexes(table, num_fields, fields);
|
||||
/* update the freespace */
|
||||
_mdb_put_int16(mdb->pg_buf, 2, mdb_pg_get_freespace(mdb));
|
||||
}
|
||||
int
|
||||
mdb_update_row(MdbTableDef *table)
|
||||
|
||||
Reference in New Issue
Block a user