InStream* CFReader_Local_Open_In_IMP(CompoundFileReader *self, String *name) { CompoundFileReaderIVARS *const ivars = CFReader_IVARS(self); Hash *entry = (Hash*)Hash_Fetch(ivars->records, name); if (!entry) { InStream *instream = Folder_Local_Open_In(ivars->real_folder, name); if (!instream) { ERR_ADD_FRAME(Err_get_error()); } return instream; } else { Obj *len = Hash_Fetch_Utf8(entry, "length", 6); Obj *offset = Hash_Fetch_Utf8(entry, "offset", 6); if (!len || !offset) { Err_set_error(Err_new(Str_newf("Malformed entry for '%o' in '%o'", name, Folder_Get_Path(ivars->real_folder)))); return NULL; } else if (Str_Get_Size(ivars->path)) { String *fullpath = Str_newf("%o/%o", ivars->path, name); InStream *instream = InStream_Reopen(ivars->instream, fullpath, Obj_To_I64(offset), Obj_To_I64(len)); DECREF(fullpath); return instream; } else { return InStream_Reopen(ivars->instream, name, Obj_To_I64(offset), Obj_To_I64(len)); } } }
InStream* CFReader_local_open_in(CompoundFileReader *self, const CharBuf *name) { Hash *entry = (Hash*)Hash_Fetch(self->records, (Obj*)name); if (!entry) { InStream *instream = Folder_Local_Open_In(self->real_folder, name); if (!instream) { ERR_ADD_FRAME(Err_get_error()); } return instream; } else { Obj *len = Hash_Fetch_Str(entry, "length", 6); Obj *offset = Hash_Fetch_Str(entry, "offset", 6); if (!len || !offset) { Err_set_error(Err_new(CB_newf("Malformed entry for '%o' in '%o'", name, Folder_Get_Path(self->real_folder)))); return NULL; } else if (CB_Get_Size(self->path)) { CharBuf *fullpath = CB_newf("%o/%o", self->path, name); InStream *instream = InStream_Reopen(self->instream, fullpath, Obj_To_I64(offset), Obj_To_I64(len)); DECREF(fullpath); return instream; } else { return InStream_Reopen(self->instream, name, Obj_To_I64(offset), Obj_To_I64(len)); } } }
static void S_fresh_flip(PostingPool *self, InStream *lex_temp_in, InStream *post_temp_in) { PostingPoolIVARS *const ivars = PostPool_IVARS(self); if (ivars->flipped) { THROW(ERR, "Can't Flip twice"); } ivars->flipped = true; // Sort RawPostings in buffer, if any. PostPool_Sort_Buffer(self); // Bail if never flushed. if (ivars->lex_end == 0) { return; } // Get a Lexicon. String *lex_alias = Str_newf("%o-%i64-to-%i64", InStream_Get_Filename(lex_temp_in), ivars->lex_start, ivars->lex_end); InStream *lex_temp_in_dupe = InStream_Reopen( lex_temp_in, lex_alias, ivars->lex_start, ivars->lex_end - ivars->lex_start); ivars->lexicon = (Lexicon*)RawLex_new( ivars->schema, ivars->field, lex_temp_in_dupe, 0, ivars->lex_end - ivars->lex_start); DECREF(lex_alias); DECREF(lex_temp_in_dupe); // Get a PostingList. String *post_alias = Str_newf("%o-%i64-to-%i64", InStream_Get_Filename(post_temp_in), ivars->post_start, ivars->post_end); InStream *post_temp_in_dupe = InStream_Reopen(post_temp_in, post_alias, ivars->post_start, ivars->post_end - ivars->post_start); ivars->plist = (PostingList*)RawPList_new(ivars->schema, ivars->field, post_temp_in_dupe, 0, ivars->post_end - ivars->post_start); DECREF(post_alias); DECREF(post_temp_in_dupe); }
static void test_Clone_and_Reopen(TestBatchRunner *runner) { String *foo = SSTR_WRAP_C("foo"); String *bar = SSTR_WRAP_C("bar"); RAMFile *file = RAMFile_new(NULL, false); OutStream *outstream = OutStream_open((Obj*)file); RAMFileHandle *fh; InStream *instream; InStream *clone; InStream *reopened; for (uint8_t i = 0; i < 26; i++) { OutStream_Write_U8(outstream, 'a' + i); } OutStream_Close(outstream); fh = RAMFH_open(foo, FH_READ_ONLY, file); instream = InStream_open((Obj*)fh); InStream_Seek(instream, 1); TEST_TRUE(runner, Str_Equals(InStream_Get_Filename(instream), (Obj*)foo), "Get_Filename"); clone = InStream_Clone(instream); TEST_TRUE(runner, Str_Equals(InStream_Get_Filename(clone), (Obj*)foo), "Clones have same filename"); TEST_TRUE(runner, InStream_Length(instream) == InStream_Length(clone), "Clones have same length"); TEST_TRUE(runner, InStream_Read_U8(instream) == InStream_Read_U8(clone), "Clones start at same file position"); reopened = InStream_Reopen(instream, bar, 25, 1); TEST_TRUE(runner, Str_Equals(InStream_Get_Filename(reopened), (Obj*)bar), "Reopened InStreams take new filename"); TEST_TRUE(runner, InStream_Read_U8(reopened) == 'z', "Reopened stream starts at supplied offset"); TEST_TRUE(runner, InStream_Length(reopened) == 1, "Reopened stream uses supplied length"); TEST_TRUE(runner, InStream_Tell(reopened) == 1, "Tell() uses supplied offset for reopened stream"); InStream_Seek(reopened, 0); TEST_TRUE(runner, InStream_Read_U8(reopened) == 'z', "Seek() uses supplied offset for reopened stream"); DECREF(reopened); DECREF(clone); DECREF(instream); DECREF(outstream); DECREF(fh); DECREF(file); }
static void S_flip_run(SortFieldWriter *run, size_t sub_thresh, InStream *ord_in, InStream *ix_in, InStream *dat_in) { SortFieldWriterIVARS *const run_ivars = SortFieldWriter_IVARS(run); if (run_ivars->flipped) { THROW(ERR, "Can't Flip twice"); } run_ivars->flipped = true; // Get our own slice of mem_thresh. DECREF(run_ivars->counter); run_ivars->counter = Counter_new(); run_ivars->mem_thresh = sub_thresh; // Done if we already have a SortCache to read from. if (run_ivars->sort_cache) { return; } // Open the temp files for reading. String *seg_name = Seg_Get_Name(run_ivars->segment); String *ord_alias = Str_newf("%o/sort_ord_temp-%i64-to-%i64", seg_name, run_ivars->ord_start, run_ivars->ord_end); InStream *ord_in_dupe = InStream_Reopen(ord_in, ord_alias, run_ivars->ord_start, run_ivars->ord_end - run_ivars->ord_start); DECREF(ord_alias); InStream *ix_in_dupe = NULL; if (run_ivars->var_width) { String *ix_alias = Str_newf("%o/sort_ix_temp-%i64-to-%i64", seg_name, run_ivars->ix_start, run_ivars->ix_end); ix_in_dupe = InStream_Reopen(ix_in, ix_alias, run_ivars->ix_start, run_ivars->ix_end - run_ivars->ix_start); DECREF(ix_alias); } String *dat_alias = Str_newf("%o/sort_dat_temp-%i64-to-%i64", seg_name, run_ivars->dat_start, run_ivars->dat_end); InStream *dat_in_dupe = InStream_Reopen(dat_in, dat_alias, run_ivars->dat_start, run_ivars->dat_end - run_ivars->dat_start); DECREF(dat_alias); // Get a SortCache. String *field = Seg_Field_Name(run_ivars->segment, run_ivars->field_num); switch (run_ivars->prim_id & FType_PRIMITIVE_ID_MASK) { case FType_TEXT: run_ivars->sort_cache = (SortCache*)TextSortCache_new( field, run_ivars->type, run_ivars->run_cardinality, run_ivars->run_max, run_ivars->null_ord, run_ivars->ord_width, ord_in_dupe, ix_in_dupe, dat_in_dupe); break; case FType_INT32: run_ivars->sort_cache = (SortCache*)I32SortCache_new( field, run_ivars->type, run_ivars->run_cardinality, run_ivars->run_max, run_ivars->null_ord, run_ivars->ord_width, ord_in_dupe, dat_in_dupe); break; case FType_INT64: run_ivars->sort_cache = (SortCache*)I64SortCache_new( field, run_ivars->type, run_ivars->run_cardinality, run_ivars->run_max, run_ivars->null_ord, run_ivars->ord_width, ord_in_dupe, dat_in_dupe); break; case FType_FLOAT32: run_ivars->sort_cache = (SortCache*)F32SortCache_new( field, run_ivars->type, run_ivars->run_cardinality, run_ivars->run_max, run_ivars->null_ord, run_ivars->ord_width, ord_in_dupe, dat_in_dupe); break; case FType_FLOAT64: run_ivars->sort_cache = (SortCache*)F64SortCache_new( field, run_ivars->type, run_ivars->run_cardinality, run_ivars->run_max, run_ivars->null_ord, run_ivars->ord_width, ord_in_dupe, dat_in_dupe); break; default: THROW(ERR, "No SortCache class for %o", run_ivars->type); } DECREF(ord_in_dupe); DECREF(ix_in_dupe); DECREF(dat_in_dupe); }