Ejemplo n.º 1
0
void
gdbWriteBlockHeader(GdbBlock *block)
{
	GDatabase *db;
	char header[GDB_BLOCK_HEADER_SIZE];
	int   counter = 0;

	if (block == NULL || !GDB_IS_DIRTY(block))
		return;

	db = block->db;

	if (block->offset == 0)
		return;

	/* Write the header to a buffer. */
	gdbPut8(header,  &counter, block->type);
	gdbPut32(header, &counter, block->dataSize);
	gdbPut16(header, &counter, block->flags);
	gdbPut32(header, &counter, block->next);
	gdbPut32(header, &counter, block->listNext);

	/* Write the header to disk. */
	fseek(db->fp, block->offset, SEEK_SET);

	fwrite(header, GDB_BLOCK_HEADER_SIZE, 1, db->fp);

	fflush(db->fp);

	GDB_CLEAR_DIRTY(block);

	if (block->inList == 0)
		gdbCacheAddBlock(block->db, block);
}
Ejemplo n.º 2
0
void
gdbWriteFreeBlockList(GDatabase *db, GdbFreeBlock *blocks, uint32_t count)
{
    uint32_t listSize;
    uint8_t *buffer;
    uint32_t i, counter = 0;
    offset_t __offset;

    if (db == NULL || blocks == NULL)
    {
        return;
    }
    /* Get the total size of the list. */
    listSize = sizeof(uint32_t) + count * (sizeof(uint16_t) + sizeof(offset_t));

    /* Allocate the buffer for the block list. */
    MEM_CHECK(buffer = (uint8_t *)SAFE_MALLOC(listSize, LOC_DB_0006));

    gdbPut32(buffer, &counter, count);

    for (i = 0; i < count; i++)
    {
        gdbPut16(buffer, &counter, blocks[i].size);
        gdbPutOffset(buffer, &counter, blocks[i].offset);
    }

    rawFileSeek(db->idxRawFile, DB_FREE_BLOCK_LIST_OFFSET, SEEK_SET);
    __offset = DB_FREE_BLOCK_LIST_OFFSET;

    rawFileWrite(db->idxRawFile, __offset, buffer, listSize, 1, LOC_DB_0007);

    SAFE_FREE(buffer, LOC_DB_0008);
}
Ejemplo n.º 3
0
/*comment: write BTree Node into buffer*/
void
btreeWriteNodeBlock(GdbBlock *block, uint8_t **buffer, uint32_t *size)
{
    BTreeNode *node;
    uint8_t    i;
    uint32_t   counter = 0;
    uint8_t ** newKeys;
    uint16_t * newKeySizes;

    node = (BTreeNode *)block->detail;
#if (SWITCH_ON == COMPRESS_MODE)
    /* Compress the node. */
    if (node->keyCount >= 2)
    {
        __compressNode(node, &newKeys, &newKeySizes);
    }
    else
    {
        newKeys     = node->keys;
        newKeySizes = node->keySizes;
    }
#endif/*(SWITCH_ON == COMPRESS_MODE)*/
#if (SWITCH_OFF == COMPRESS_MODE)
    newKeys     = node->keys;
    newKeySizes = node->keySizes;
#endif/*(SWITCH_OFF == COMPRESS_MODE)*/

    *size = __getNodeSize(node, newKeySizes);

    MEM_CHECK(*buffer = (uint8_t *)SAFE_MALLOC(*size, LOC_BTREE_0162));

    gdbPut8(*buffer, &counter, node->keyCount);

    for (i = 0; i < node->tree->order; i++)
    {
        gdbPutOffset(*buffer, &counter, node->children[i]);
    }
    for (i = 0; i < node->tree->order - 1; i++)
    {
        gdbPut16(*buffer, &counter, newKeySizes[i]);
    }
    for (i = 0; i < node->tree->order - 1; i++)
    {
        if (newKeySizes[i] > 0)
        {
            memcpy(*buffer + counter, newKeys[i], newKeySizes[i]);

            counter += newKeySizes[i];
        }
    }
#if (SWITCH_ON == COMPRESS_MODE)
    if (node->keyCount >= 2)
    {
        /* Free up the arrays. */
        for (i = 0; i < node->keyCount; i++)
        {
            if (newKeys[i] != NULL)
            {
                keyFree(newKeys[i], LOC_BTREE_0163);
            }
        }

        SAFE_FREE(newKeys, LOC_BTREE_0164);
        SAFE_FREE(newKeySizes, LOC_BTREE_0165);
    }
#endif/*(SWITCH_ON == COMPRESS_MODE)*/
}