下面列出了java.nio.MappedByteBuffer#putLong ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
static LargeLongArray create(Iterator<Long> iterator, int size) {
try {
File file = File.createTempFile("list", ".tmp");
file.deleteOnExit();
try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) {
FileChannel channel = raf.getChannel();
MappedByteBuffer map = channel
.map(MapMode.READ_WRITE, 0, size * 8L);
for (int i = 0; i < size; i++) {
long x = iterator.next();
map.putLong(x);
}
return new LargeLongArray(size, file, channel, map);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void addTrough(String name)
{
if (name.length() > MAX_TROUGH_NAME_LEN) throw new RuntimeException("OVER MAX NAME LENGTH");
Map<String, Integer> troughs = getTroughMap();
if(troughs.containsKey(name)) return;
if (!troughs.containsKey("__FREE"))
throw new RuntimeException("TOO MANY TROUGHS");
int trough_idx = troughs.get("__FREE");
long hash_loc = makeNewHashTable(HASH_INITAL_SIZE);
MappedByteBuffer mbb = getBufferMap(0);
synchronized(mbb)
{
mbb.position( (int) (LOCATION_TROUGH_TABLE_START + (8 + MAX_TROUGH_NAME_LEN) * trough_idx));
mbb.putLong(hash_loc);
mbb.put(name.getBytes());
}
trough_map = null;
}
@Override
public void writeLittleEndianLong(long value) throws IOException {
MappedByteBuffer curChunk = getCurChunk();
if (curChunk.remaining() >= 8) {
curChunk.putLong(value);
return;
}
// Value is on the chunk boundary - edge case so it is ok if it's a bit slower.
writeUnsignedByte((int) ((value) & 0xFF));
writeUnsignedByte((int) ((value >>> 8) & 0xFF));
writeUnsignedByte((int) ((value >>> 16) & 0xFF));
writeUnsignedByte((int) ((value >>> 24) & 0xFF));
writeUnsignedByte((int) ((value >>> 32) & 0xFF));
writeUnsignedByte((int) ((value >>> 40) & 0xFF));
writeUnsignedByte((int) ((value >>> 48) & 0xFF));
writeUnsignedByte((int) ((value >>> 56) & 0xFF));
}
public static void writeChannel(
final SocketChannel destination,
final FileChannel source,
final MappedByteBuffer buffer,
final long time) throws IOException
{
buffer.putLong(0, time);
int position = 0;
while (position < MESSAGE_SIZE)
{
position += source.transferTo(position, MESSAGE_SIZE - position, destination);
}
}
protected void writeLong(long position, long value)
{
int file = (int) (position / SEGMENT_FILE_SIZE);
int offset_in_file = (int) (position % SEGMENT_FILE_SIZE);
MappedByteBuffer mbb = getBufferMap(file);
synchronized(mbb)
{
mbb.position(offset_in_file);
mbb.putLong(value);
}
}
@Ignore("HCOLL-422")
@Test
public void recoverTest() throws IOException, ExecutionException, InterruptedException {
File mapFile = File.createTempFile("recoverTestFile", ".map");
mapFile.deleteOnExit();
ChronicleMapBuilder<Integer, Integer> builder = ChronicleMap
.of(Integer.class, Integer.class)
.entries(2)
.actualSegments(1)
.checksumEntries(true);
ChronicleHashBuilderPrivateAPI<?, ?> privateAPI =
(ChronicleHashBuilderPrivateAPI<?, ?>) builder.privateAPI();
privateAPI.replication((byte) 1);
privateAPI.cleanupRemovedEntries(false);
map = (ReplicatedChronicleMap<Integer, Integer, ?>) builder.createPersistedTo(mapFile);
map.acquireModificationIterator((byte) 2);
// acquires read lock successfully
assertNull(map.get(0));
ExecutorService executorService = Executors.newSingleThreadExecutor(
new NamedThreadFactory("recoverTest"));
executorService.submit(() -> {
ExternalMapQueryContext<Integer, Integer, ?> c = map.queryContext(0);
c.writeLock().lock();
}).get();
try {
map.get(0);
throw new AssertionError("Expected dead lock exception");
} catch (Exception expected) {
// do nothing
}
map.close();
map = (ReplicatedChronicleMap<Integer, Integer, ?>)
builder.recoverPersistedTo(mapFile, true);
// acquires read lock successfully
assertNull(map.get(0));
map.put(1, 1);
map.put(2, 2);
map.remove(1);
long segmentHeadersOffset = this.map.segmentHeadersOffset;
map.close();
try (RandomAccessFile raf = new RandomAccessFile(mapFile, "rw")) {
FileChannel ch = raf.getChannel();
MappedByteBuffer mapBB = ch.map(FileChannel.MapMode.READ_WRITE, 0, mapFile.length());
for (long offset = segmentHeadersOffset; offset < mapFile.length();
offset += 8) {
for (int bit = 0; bit < 64; bit++) {
LOG.error("flip bit {} of word at {}", bit, offset);
mapBB.putLong((int) offset, mapBB.getLong((int) offset) ^ (1L << bit));
ChronicleMapBuilder<Integer, Integer> recoverBuilder = ChronicleMap
.of(Integer.class, Integer.class);
ChronicleHashBuilderPrivateAPI<?, ?> recoverPrivateAPI =
(ChronicleHashBuilderPrivateAPI<?, ?>) recoverBuilder.privateAPI();
recoverPrivateAPI.replication((byte) 1);
recoverPrivateAPI.cleanupRemovedEntries(false);
try (ChronicleMap<Integer, Integer> recovered =
recoverBuilder.recoverPersistedTo(mapFile, false)) {
recovered.put(1, 1);
recovered.put(2, 2);
recovered.remove(1);
}
}
}
}
}
private void setCurrentWriteLocation(long v)
{
MappedByteBuffer mbb = getBufferMap(0);
mbb.putLong((int)LOCATION_NEXT_FREE, v);
}
protected void putKeyValueTable(long table_pos, RecordEntry put_re)
{
long t1=System.nanoTime();
int hash_file = (int) (table_pos / SEGMENT_FILE_SIZE);
MappedByteBuffer hash_mbb = getBufferMap(hash_file);
int file_offset = (int) (table_pos % SEGMENT_FILE_SIZE);
int max;
int items;
long next_ptr;
synchronized(hash_mbb)
{
hash_mbb.position(file_offset + (int) LOCATION_HASH_MAX);
max = hash_mbb.getInt();
items = hash_mbb.getInt();
next_ptr = hash_mbb.getLong();
}
Assert.assertTrue("Max " + max + " items " + items + " table at " + table_pos + " file " + hash_file + " file offset " + file_offset ,max > items);
Assert.assertTrue(max > 4);
Assert.assertTrue(items >= 0);
//DeterministicStream det_stream = new DeterministicStream(key);
//int loc = det_stream.nextInt(max);
int hash = put_re.getKey().hashCode();
int loc = Math.abs(hash % max);
if (loc < 0) loc = 0;
double full = (double) items / (double) max;
while(true)
{
Assert.assertTrue(loc >= 0);
Assert.assertTrue(loc < max);
synchronized(hash_mbb)
{
long t1_check = System.nanoTime();
hash_mbb.position(file_offset + LOCATION_HASH_START + loc * 8);
long ptr = hash_mbb.getLong();
TimeRecord.record(t1_check, "slop_get_ptr");
if ((ptr == 0) && (full >= HASH_FULL))
{
// It isn't here and the hash is full, move on to next table
if (next_ptr == 0)
{
next_ptr = makeNewHashTable(max * HASH_MULTIPLCATION);
hash_mbb.position(file_offset + (int) LOCATION_HASH_NEXT);
hash_mbb.putLong(next_ptr);
}
TimeRecord.record(t1, "slop_put_key_value_table_rec");
putKeyValueTable(next_ptr, put_re);
return;
}
RecordEntry re = null;
if (ptr != 0)
{
re = new RecordEntry(ptr);
if (!re.getKey().equals(put_re.getKey()))
{
re = null;
}
}
if ((ptr == 0) || (re!=null))
{
//If we have an empty or a key match
long data_loc = put_re.storeItem(ptr);
hash_mbb.position(file_offset + LOCATION_HASH_START + loc * 8);
hash_mbb.putLong(data_loc);
if (ptr == 0)
{
hash_mbb.position(file_offset + LOCATION_HASH_ITEMS);
items++;
hash_mbb.putInt(items);
}
TimeRecord.record(t1, "slop_put_key_value_table_add");
return;
}
}
//loc = det_stream.nextInt(max);
loc = (loc + 131 ) % max;
}
}