void test__BufferedReadUseBeforeBuffer__IsNextReadLenZero(void **state) { BufferedRead *bufferedRead = palloc(sizeof(BufferedRead)); int32 memoryLen = 512; /* maxBufferLen + largeReadLen */ uint8 *memory = malloc(sizeof(memoryLen)); char *relname = "test"; int32 maxBufferLen = 128; int32 maxLargeReadLen = 128; int32 nextBufferLen; int32 maxReadAheadLen = 64; memset(bufferedRead, 0 , sizeof(BufferedRead)); /* * Initialize the buffer */ BufferedReadInit(bufferedRead, memory, memoryLen, maxBufferLen, maxLargeReadLen, relname); /* * filling up the bufferedRead struct */ bufferedRead->largeReadLen=100; bufferedRead->bufferOffset=0; bufferedRead->fileLen=200; bufferedRead->temporaryLimitFileLen=200; bufferedRead->largeReadPosition=50; bufferedRead->maxLargeReadLen = 0; /* this will get assigned to nextReadLen(=0) */ PG_TRY(); { /* * This will throw a ereport(ERROR). */ BufferedReadUseBeforeBuffer(bufferedRead, maxReadAheadLen, &nextBufferLen); } PG_CATCH(); { CurrentMemoryContext = 1; //To be fixed ErrorData *edata = CopyErrorData(); /* * Validate the expected error */ assert_true(edata->sqlerrcode == ERRCODE_INTERNAL_ERROR); assert_true(edata->elevel == ERROR); } PG_END_TRY(); }
void test__BufferedReadInit__IsConsistent(void **state) { BufferedRead *bufferedRead = palloc(sizeof(BufferedRead)); int32 memoryLen = 512; /* maxBufferLen + largeReadLen */ uint8 *memory = malloc(sizeof(memoryLen)); char *relname = "test"; int32 maxBufferLen = 128; int32 maxLargeReadLen = 128; memset(bufferedRead, 0 , sizeof(BufferedRead)); /* * Call the function so as to set the above values. */ BufferedReadInit(bufferedRead, memory, memoryLen, maxBufferLen, maxLargeReadLen, relname); /* * Check for consistency */ assert_int_equal(bufferedRead->maxBufferLen,maxBufferLen); assert_int_equal(bufferedRead->maxLargeReadLen,maxLargeReadLen); assert_string_equal(bufferedRead->relationName, relname); assert_memory_equal(bufferedRead->memory, memory, memoryLen); assert_int_equal(bufferedRead->memoryLen, memoryLen); }
/* * Initialize AppendOnlyStorageRead. * * The AppendOnlyStorageRead data structure is initialized once for a read * "session" and can be used to read Append-Only Storage Blocks from 1 or * more segment files. * * The current file to read to is opened with the * AppendOnlyStorageRead_OpenFile routine. * * storageRead - data structure to initialize * memoryContext - memory context to use for buffers and other memory * needs. When NULL, the current memory context is used. * maxBufferLen - maximum Append-Only Storage Block length including all * storage headers. * relationName - name of the relation to use in system logging and * error messages. * title - A phrase that better describes the purpose of this open. * The caller manages the storage for this. * storageAttributes - Append-Only Storage Attributes from relation creation. */ void AppendOnlyStorageRead_Init(AppendOnlyStorageRead *storageRead, MemoryContext memoryContext, int32 maxBufferLen, char *relationName, char *title, AppendOnlyStorageAttributes *storageAttributes) { int relationNameLen; uint8 *memory; int32 memoryLen; MemoryContext oldMemoryContext; Assert(storageRead != NULL); /* UNDONE: Range check maxBufferLen */ Assert(relationName != NULL); Assert(storageAttributes != NULL); /* UNDONE: Range check fields in storageAttributes */ MemSet(storageRead, 0, sizeof(AppendOnlyStorageRead)); storageRead->maxBufferLen = maxBufferLen; if (memoryContext == NULL) storageRead->memoryContext = CurrentMemoryContext; else storageRead->memoryContext = memoryContext; oldMemoryContext = MemoryContextSwitchTo(storageRead->memoryContext); memcpy(&storageRead->storageAttributes, storageAttributes, sizeof(AppendOnlyStorageAttributes)); relationNameLen = strlen(relationName); storageRead->relationName = (char *) palloc(relationNameLen + 1); memcpy(storageRead->relationName, relationName, relationNameLen + 1); storageRead->title = title; storageRead->minimumHeaderLen = AppendOnlyStorageFormat_RegularHeaderLenNeeded( storageRead->storageAttributes.checksum); /* * Initialize BufferedRead. */ storageRead->largeReadLen = 2 * storageRead->maxBufferLen; memoryLen = BufferedReadMemoryLen(storageRead->maxBufferLen, storageRead->largeReadLen); Assert(CurrentMemoryContext == storageRead->memoryContext); memory = (uint8 *) palloc(memoryLen); BufferedReadInit(&storageRead->bufferedRead, memory, memoryLen, storageRead->maxBufferLen, storageRead->largeReadLen, relationName); elogif(Debug_appendonly_print_scan || Debug_appendonly_print_read_block, LOG, "Append-Only Storage Read initialize for table '%s' " "(compression = %s, compression level %d, maximum buffer length %d, large read length %d)", storageRead->relationName, (storageRead->storageAttributes.compress ? "true" : "false"), storageRead->storageAttributes.compressLevel, storageRead->maxBufferLen, storageRead->largeReadLen); storageRead->file = -1; storageRead->formatVersion = -1; MemoryContextSwitchTo(oldMemoryContext); storageRead->isActive = true; }
/* * Initialize AppendOnlyStorageRead. * * The AppendOnlyStorageRead data structure is initialized * once for a read "session" and can be used to read * Append-Only Storage Blocks from 1 or more segment files. * * The current file to read to is opened with the * AppendOnlyStorageRead_OpenFile routine. */ void AppendOnlyStorageRead_Init( AppendOnlyStorageRead *storageRead, /* The data structure to initialize. */ MemoryContext memoryContext, /* * The memory context to use for buffers and * other memory needs. When NULL, the * current memory context is used. */ int32 maxBufferLen, /* * The maximum Append-Only Storage Block * length including all storage headers. */ char *relationName, /* * Name of the relation to use in system * logging and error messages. */ char *title, /* * A phrase that better describes the purpose of the this open. * * The caller manages the storage for this. */ AppendOnlyStorageAttributes *storageAttributes) /* * The Append-Only Storage Attributes * from relation creation. */ { int relationNameLen; uint8 *memory; int32 memoryLen; MemoryContext oldMemoryContext; Assert(storageRead != NULL); // UNDONE: Range check maxBufferLen Assert(relationName != NULL); Assert(storageAttributes != NULL); // UNDONE: Range check fields in storageAttributes MemSet(storageRead, 0, sizeof(AppendOnlyStorageRead)); storageRead->maxBufferLen = maxBufferLen; if (memoryContext == NULL) storageRead->memoryContext = CurrentMemoryContext; else storageRead->memoryContext = memoryContext; oldMemoryContext = MemoryContextSwitchTo(storageRead->memoryContext); memcpy( &storageRead->storageAttributes, storageAttributes, sizeof(AppendOnlyStorageAttributes)); relationNameLen = strlen(relationName); storageRead->relationName = (char *) palloc(relationNameLen + 1); memcpy(storageRead->relationName, relationName, relationNameLen + 1); storageRead->title = title; storageRead->minimumHeaderLen = AppendOnlyStorageFormat_RegularHeaderLenNeeded( storageRead->storageAttributes.checksum); /* * Initialize BufferedRead. */ storageRead->largeReadLen = 2 * storageRead->maxBufferLen; memoryLen = BufferedReadMemoryLen( storageRead->maxBufferLen, storageRead->largeReadLen); Assert(CurrentMemoryContext == storageRead->memoryContext); memory = (uint8*)palloc(memoryLen); BufferedReadInit(&storageRead->bufferedRead, memory, memoryLen, storageRead->maxBufferLen, storageRead->largeReadLen, relationName); elogif(Debug_appendonly_print_scan || Debug_appendonly_print_read_block, LOG, "Append-Only Storage Read initialize for table '%s' " "(compression = %s, compression level %d, maximum buffer length %d, large read length %d)", storageRead->relationName, (storageRead->storageAttributes.compress ? "true" : "false"), storageRead->storageAttributes.compressLevel, storageRead->maxBufferLen, storageRead->largeReadLen); storageRead->file = -1; MemoryContextSwitchTo(oldMemoryContext); storageRead->isActive = true; }