int mdb_read_next_dpg(MdbTableDef *table) { MdbCatalogEntry *entry = table->entry; MdbHandle *mdb = entry->mdb; int next_pg; #ifndef SLOW_READ next_pg = mdb_map_find_next(mdb, table->usage_map, table->map_sz, table->cur_phys_pg); if (next_pg >= 0) { if (mdb_read_pg(mdb, next_pg)) { table->cur_phys_pg = next_pg; return table->cur_phys_pg; } else { return 0; } } fprintf(stderr, "Warning: defaulting to brute force read\n"); #endif /* can't do a fast read, go back to the old way */ do { if (!mdb_read_pg(mdb, table->cur_phys_pg++)) return 0; } while (mdb->pg_buf[0]!=0x01 || mdb_get_int32(mdb->pg_buf, 4)!=entry->table_pg); /* fprintf(stderr,"returning new page %ld\n", table->cur_phys_pg); */ return table->cur_phys_pg; }
guint32 mdb_map_find_next_freepage(MdbTableDef *table, int row_size) { MdbCatalogEntry *entry = table->entry; MdbHandle *mdb = entry->mdb; guint32 pgnum; guint32 cur_pg = 0; int free_space; do { pgnum = mdb_map_find_next(mdb, table->free_usage_map, table->freemap_sz, cur_pg); //printf("looking at page %d\n", pgnum); if (!pgnum) { /* allocate new page */ pgnum = mdb_alloc_page(table); return pgnum; } else if (pgnum==-1) { mdb->fatal_error_handler("Error: mdb_map_find_next_freepage error while reading maps.\n"); exit(1); } cur_pg = pgnum; mdb_read_pg(mdb, pgnum); free_space = mdb_pg_get_freespace(mdb); } while (free_space < row_size); //printf("page %d has %d bytes left\n", pgnum, free_space); return pgnum; }
guint32 mdb_map_find_next_freepage(MdbTableDef *table, int row_size) { MdbCatalogEntry *entry = table->entry; MdbHandle *mdb = entry->mdb; guint32 pgnum; guint32 cur_pg = 0; int free_space; do { pgnum = mdb_map_find_next(mdb, table->free_usage_map, table->freemap_sz, cur_pg); if (!pgnum) { /* allocate new page */ pgnum = mdb_alloc_page(table); return pgnum; } cur_pg = pgnum; mdb_read_pg(mdb, pgnum); free_space = mdb_pg_get_freespace(mdb); } while (free_space < row_size); return pgnum; }
/* Read next data page into mdb->pg_buf */ int mdb_read_next_dpg(MdbTableDef *table) { MdbCatalogEntry *entry = table->entry; MdbHandle *mdb = entry->mdb; int next_pg; #ifndef SLOW_READ while (1) { next_pg = mdb_map_find_next(mdb, table->usage_map, table->map_sz, table->cur_phys_pg); if (next_pg < 0) break; /* unknow map type: goto fallback */ if (!next_pg) return 0; if (!mdb_read_pg(mdb, next_pg)) { fprintf(stderr, "error: reading page %d failed.\n", next_pg); return 0; } table->cur_phys_pg = next_pg; if (mdb->pg_buf[0]==MDB_PAGE_DATA && mdb_get_int32(mdb->pg_buf, 4)==entry->table_pg) return table->cur_phys_pg; /* On rare occasion, mdb_map_find_next will return a wrong page */ /* Found in a big file, over 4,000,000 records */ fprintf(stderr, "warning: page %d from map doesn't match: Type=%d, buf[4..7]=%ld Expected table_pg=%ld\n", next_pg, mdb->pg_buf[0], mdb_get_int32(mdb->pg_buf, 4), entry->table_pg); } fprintf(stderr, "Warning: defaulting to brute force read\n"); #endif /* can't do a fast read, go back to the old way */ do { if (!mdb_read_pg(mdb, table->cur_phys_pg++)) return 0; } while (mdb->pg_buf[0]!=MDB_PAGE_DATA || mdb_get_int32(mdb->pg_buf, 4)!=entry->table_pg); /* fprintf(stderr,"returning new page %ld\n", table->cur_phys_pg); */ return table->cur_phys_pg; }