use of java.nio.MappedByteBuffer in project GeoGig by boundlessgeo.
the class MappedIndex method newBuffer.
private void newBuffer() throws IOException {
long position = MAX_BUFF_SIZE * ranges.size();
long size = MAX_BUFF_SIZE;
MappedByteBuffer buff = indexChannel.map(MapMode.READ_WRITE, position, size);
BufferRange range = new BufferRange(buff);
ranges.add(range);
this.currBuffer = range;
}
use of java.nio.MappedByteBuffer in project jdk8u_jdk by JetBrains.
the class MapTest method testWrite.
/**
* Maps blah file with a random offset and checks to see if data
* written out to the file can be read back in
*/
private static void testWrite() throws Exception {
StringBuilder sb = new StringBuilder();
sb.setLength(4);
for (int x = 0; x < 1000; x++) {
try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) {
FileChannel fc = raf.getChannel();
long offset = generator.nextInt(1000);
MappedByteBuffer b = fc.map(MapMode.READ_WRITE, offset, 100);
for (int i = 0; i < 4; i++) {
b.put(i, (byte) ('0' + i));
}
for (int i = 0; i < 4; i++) {
byte aByte = b.get(i);
sb.setCharAt(i, (char) aByte);
}
if (!sb.toString().equals("0123"))
throw new Exception("Write test failed");
}
}
}
use of java.nio.MappedByteBuffer in project jdk8u_jdk by JetBrains.
the class MapTest method testZero.
/**
* Tests zero size file mapping
*/
private static void testZero() throws Exception {
try (FileInputStream fis = new FileInputStream(blah)) {
FileChannel fc = fis.getChannel();
MappedByteBuffer b = fc.map(MapMode.READ_ONLY, 0, 0);
}
}
use of java.nio.MappedByteBuffer in project brisk by riptano.
the class BriskServer method getLocalSubBlock.
/**
* Retrieves a local subBlock
*
* @param blockId row key
* @param sblockId SubBlock column name
* @param offset inside the sblock
* @return a local sublock
* @throws TException
*/
private LocalBlock getLocalSubBlock(String subBlockCFName, ByteBuffer blockId, ByteBuffer sblockId, int offset) throws TException {
DecoratedKey<Token<?>> decoratedKey = new DecoratedKey<Token<?>>(StorageService.getPartitioner().getToken(blockId), blockId);
Table table = Table.open(cfsKeyspace);
ColumnFamilyStore sblockStore = table.getColumnFamilyStore(subBlockCFName);
Collection<SSTableReader> sstables = sblockStore.getSSTables();
for (SSTableReader sstable : sstables) {
long position = sstable.getPosition(decoratedKey, Operator.EQ);
if (position == -1)
continue;
String filename = sstable.descriptor.filenameFor(Component.DATA);
RandomAccessFile raf = null;
int mappedLength = -1;
MappedByteBuffer mappedData = null;
MappedFileDataInput file = null;
try {
raf = new RandomAccessFile(filename, "r");
assert position < raf.length();
mappedLength = (raf.length() - position) < Integer.MAX_VALUE ? (int) (raf.length() - position) : Integer.MAX_VALUE;
mappedData = raf.getChannel().map(FileChannel.MapMode.READ_ONLY, position, mappedLength);
file = new MappedFileDataInput(mappedData, filename, 0);
if (file == null)
continue;
// Verify key was found in data file
DecoratedKey keyInDisk = SSTableReader.decodeKey(sstable.partitioner, sstable.descriptor, ByteBufferUtil.readWithShortLength(file));
assert keyInDisk.equals(decoratedKey) : String.format("%s != %s in %s", keyInDisk, decoratedKey, file.getPath());
long rowSize = SSTableReader.readRowSize(file, sstable.descriptor);
assert rowSize > 0;
assert rowSize < mappedLength;
Filter bf = IndexHelper.defreezeBloomFilter(file, sstable.descriptor.usesOldBloomFilter);
// verify this column in in this version of the row.
if (!bf.isPresent(sblockId))
continue;
List<IndexHelper.IndexInfo> indexList = IndexHelper.deserializeIndex(file);
// we can stop early if bloom filter says none of the
// columns actually exist -- but,
// we can't stop before initializing the cf above, in
// case there's a relevant tombstone
ColumnFamilySerializer serializer = ColumnFamily.serializer();
try {
ColumnFamily cf = serializer.deserializeFromSSTableNoColumns(ColumnFamily.create(sstable.metadata), file);
if (cf.isMarkedForDelete())
continue;
} catch (Exception e) {
e.printStackTrace();
throw new IOException(serializer + " failed to deserialize " + sstable.getColumnFamilyName() + " with " + sstable.metadata + " from " + file, e);
}
Integer sblockLength = null;
if (indexList == null)
sblockLength = seekToSubColumn(sstable.metadata, file, sblockId);
else
sblockLength = seekToSubColumn(sstable.metadata, file, sblockId, indexList);
if (sblockLength == null || sblockLength < 0)
continue;
int bytesReadFromStart = mappedLength - (int) file.bytesRemaining();
if (logger.isDebugEnabled())
logger.debug("BlockLength = " + sblockLength + " Availible " + file.bytesRemaining());
assert offset <= sblockLength : String.format("%d > %d", offset, sblockLength);
long dataOffset = position + bytesReadFromStart;
if (file.bytesRemaining() == 0 || sblockLength == 0)
continue;
return new LocalBlock(file.getPath(), dataOffset + offset, sblockLength - offset);
} catch (IOException e) {
throw new TException(e);
} finally {
FileUtils.closeQuietly(raf);
}
}
return null;
}
use of java.nio.MappedByteBuffer in project JGroups by belaban.
the class ShmTest method start.
protected static void start(boolean server) throws Exception {
RandomAccessFile file = new RandomAccessFile("/tmp/shm", "rwd");
FileChannel channel = file.getChannel();
MappedByteBuffer shared_buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, SIZE + 10);
file.close();
// messages written (server) or read (client)
int count = 0;
int busy_spin = 0;
if (server) {
while (count < NUM) {
byte input = shared_buffer.get(0);
// System.out.println("input = " + input);
if (input == 0) {
// write
byte[] buf = new byte[SIZE];
shared_buffer.put(buf, 0, buf.length);
shared_buffer.rewind();
shared_buffer.put((byte) 1).rewind();
count++;
if (count % PRINT == 0)
System.out.println("wrote " + count);
} else {
busy_spin = 0;
while ((input = shared_buffer.get(0)) != 0) {
if (busy_spin++ < MAX_BUSY_SPIN)
;
else
Thread.yield();
}
}
}
} else {
long start = System.nanoTime();
while (count < NUM) {
byte input = shared_buffer.get(0);
// System.out.println("input = " + input);
if (input == 1) {
// write
byte[] buf = new byte[SIZE];
shared_buffer.get(buf, 0, buf.length);
shared_buffer.rewind();
shared_buffer.put((byte) 0).rewind();
count++;
if (count % PRINT == 0)
System.out.println("read " + count);
} else {
busy_spin = 0;
while ((input = shared_buffer.get(0)) != 1) {
if (busy_spin++ < MAX_BUSY_SPIN)
;
else
Thread.yield();
}
}
}
long time = System.nanoTime() - start;
long reads_sec = (long) (NUM / (time / 1000.0 / 1000.0 / 1000.0));
double ns_per_msg = time / NUM;
double ms_per_msg = (time / 1000000.0) / NUM;
double throughput = (NUM * SIZE / 1000000) / (time / 1000000000.0);
System.out.println((time / 1000000.0) + " ms, " + reads_sec + " reads/sec, " + ns_per_msg + " ns/msg, " + ms_per_msg + " ms/msg, " + throughput + " MB/sec");
}
}
Aggregations