// read directory info out of // pandalog file void read_dir(void) { assert (thePandalog->file != NULL); assert(in_read_mode()); PlHeader *plh = read_header(); thePandalog->chunk.size = plh->chunk_size * SLACK_MULT; thePandalog->chunk.zsize = plh->chunk_size * SLACK_MULT; // realloc those chunk bufs thePandalog->chunk.buf = (unsigned char *) realloc(thePandalog->chunk.buf, thePandalog->chunk.size); thePandalog->chunk.buf_p = thePandalog->chunk.buf; thePandalog->chunk.zbuf = (unsigned char *) realloc(thePandalog->chunk.zbuf, thePandalog->chunk.zsize); fseek(thePandalog->file, plh->dir_pos, SEEK_SET); uint32_t nc; int n = fread(&(nc), 1, sizeof(nc), thePandalog->file); assert (n == sizeof(nc)); PandalogDir *dir = &(thePandalog->dir); dir->max_chunks = nc; dir->instr = (uint64_t *) malloc(sizeof(uint64_t) * nc); dir->pos = (uint64_t *) malloc(sizeof(uint64_t) * (1+nc)); dir->num_entries = (uint64_t *) malloc(sizeof(uint64_t) * nc); uint32_t i; for (i=0; i<nc; i++) { int n = fread(&(dir->instr[i]), 1, sizeof(dir->instr[i]), thePandalog->file); assert (n == sizeof(dir->instr[i])); n = fread(&(dir->pos[i]), 1, sizeof(dir->pos[i]), thePandalog->file); assert (n == sizeof(dir->pos[i])); n = fread(&(dir->num_entries[i]), 1, sizeof(dir->num_entries[i]), thePandalog->file); assert (n == sizeof(dir->num_entries[i])); } // a little hack so unmarshall_chunk will work dir->pos[nc] = plh->dir_pos; }
void pandalog_open_read(const char *path, uint32_t pl_mode) { // NB: 0 chunk size for now -- read_dir will figure this out pandalog_create(0); thePandalog->mode = (PlMode) pl_mode; assert (in_read_mode()); thePandalog->filename = strdup(path); thePandalog->file = fopen(path, "r"); // read directory (and header) read_dir(); thePandalog->chunk_num = 0; if (pl_mode == PL_MODE_READ_FWD) { // seek to first chunk and unmarshall it pandalog_seek(0); } if (pl_mode == PL_MODE_READ_BWD) { // seek to last chunk and unmarshall it pandalog_seek(-1); } }
void pandalog_seek(uint64_t instr) { assert(in_read_mode()); // figure out which chunk this instr in is uint32_t c = find_chunk(instr, 0, thePandalog->dir.max_chunks-1); thePandalog->chunk_num = c; unmarshall_chunk(c); // figure out ind uint32_t ind = find_ind(instr, 0, thePandalog->dir.num_entries[c]-1); if (thePandalog->mode == PL_MODE_READ_BWD) { // need *last* entry with that instr for backward mode uint32_t i; // uint8_t found_entry = 0; for (i=ind; i<thePandalog->dir.num_entries[c]; i++) { Panda__LogEntry *ple = thePandalog->chunk.entry[i]; if (ple->instr != instr || instr > ple->instr) { ind --; break; } } } thePandalog->chunk.ind_entry = ind; }