Skip to content

Commit

Permalink
TEIID-3640 fixing initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Aug 17, 2015
1 parent abdc288 commit 7f5fa6d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
Expand Up @@ -50,30 +50,36 @@ private static class BlockByteBufferData {
* @param blockAddressBits
* @param direct
*/
public BlockByteBuffer(int segmentAddressBits, int blockCount, int blockAddressBits, boolean direct) {
public BlockByteBuffer(int segmentAddressBits, int blockCount, int blockAddressBits, boolean direct, boolean allocate) {
this.data = new BlockByteBufferData();
this.data.segmentAddressBits = segmentAddressBits;
this.data.blockAddressBits = blockAddressBits;
this.data.blockSize = 1 << blockAddressBits;
this.data.segmentSize = 1 << this.data.segmentAddressBits;
this.data.blockCount = blockCount;
long size = ((long)blockCount)<<blockAddressBits;
int fullSegments = (int)size>>segmentAddressBits;
int fullSegments = (int)(size>>segmentAddressBits);
int lastSegmentSize = (int) (size&(data.segmentSize-1));
int segments = fullSegments;
if (lastSegmentSize > 0) {
segments++;
}
origBuffers = new ByteBuffer[segments];
buffers = new ByteBuffer[segments];
for (int i = 0; i < fullSegments; i++) {
origBuffers[i] = allocate(data.segmentSize, direct);
}
if (lastSegmentSize > 0) {
origBuffers[fullSegments] = allocate(lastSegmentSize, direct);
if (allocate) {
for (int i = 0; i < fullSegments; i++) {
origBuffers[i] = allocate(data.segmentSize, direct);
}
if (lastSegmentSize > 0) {
origBuffers[fullSegments] = allocate(lastSegmentSize, direct);
}
}
}

public ByteBuffer[] getBuffers() {
return buffers;
}

private BlockByteBuffer() {

}
Expand Down
Expand Up @@ -569,11 +569,9 @@ void initialize(boolean allocateMemory) throws TeiidComponentException {
blocks = (int) Math.min(Integer.MAX_VALUE, (memoryBufferSpace>>LOG_BLOCK_SIZE)*ADDRESSES_PER_BLOCK/(ADDRESSES_PER_BLOCK+1));
inodesInuse = new ConcurrentBitSet(blocks+1, BufferManagerImpl.CONCURRENCY_LEVEL);
blocksInuse = new ConcurrentBitSet(blocks, BufferManagerImpl.CONCURRENCY_LEVEL);
if (allocateMemory) {
this.blockByteBuffer = new BlockByteBuffer(30, blocks, LOG_BLOCK_SIZE, direct);
//ensure that we'll run out of blocks first
this.inodeByteBuffer = new BlockByteBuffer(30, blocks+1, LOG_INODE_SIZE, direct);
}
this.blockByteBuffer = new BlockByteBuffer(30, blocks, LOG_BLOCK_SIZE, direct, allocateMemory);
//ensure that we'll run out of blocks first
this.inodeByteBuffer = new BlockByteBuffer(30, blocks+1, LOG_INODE_SIZE, direct, allocateMemory);
memoryWritePermits = new Semaphore(blocks);
maxMemoryBlocks = Math.min(MAX_DOUBLE_INDIRECT, blocks);
maxMemoryBlocks = Math.min(maxMemoryBlocks, (maxStorageObjectSize>>LOG_BLOCK_SIZE) + ((maxStorageObjectSize&BufferFrontedFileStoreCache.BLOCK_MASK)>0?1:0));
Expand Down
Expand Up @@ -22,13 +22,20 @@

package org.teiid.common.buffer.impl;

import static org.junit.Assert.*;

import org.junit.Test;

public class TestBlockByteBuffer {

@Test public void testBufferSizing() {
BlockByteBuffer bbb = new BlockByteBuffer(4, 100, 4, false);
BlockByteBuffer bbb = new BlockByteBuffer(4, 100, 4, false, true);
bbb.getByteBuffer(1);
}

@Test public void testLargeSizing() {
BlockByteBuffer bbb = new BlockByteBuffer(30, 1000000, 13, false, false);
assertEquals(8, bbb.getBuffers().length);
}

}

0 comments on commit 7f5fa6d

Please sign in to comment.