Align persisted Chronicle Map creation with Size Prefixed Blob and Self-Bootstrapping Header specifications

This commit is contained in:
leventov
2016-02-05 16:46:12 +07:00
parent 01f15fb9b1
commit 7b9ccac147
5 changed files with 290 additions and 77 deletions
@@ -0,0 +1,42 @@
/*
* Copyright (C) 2015 higherfrequencytrading.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.openhft.chronicle.hash.impl;
public final class SizePrefixedBlob {
public static final int HEADER_OFFSET = 0;
public static final int SIZE_WORD_OFFSET = 8;
public static final int SELF_BOOTSTRAPPING_HEADER_OFFSET = 12;
public static final int READY = 0;
public static final int NOT_READY = 0x80000000;
public static final int DATA = 0;
public static final int META_DATA = 0x40000000;
public static final int SIZE_MASK = (1 << 30) - 1;
public static boolean isReady(int sizeWord) {
return sizeWord > 0;
}
public static int extractSize(int sizeWord) {
return sizeWord & SIZE_MASK;
}
private SizePrefixedBlob() {}
}
@@ -378,9 +378,9 @@ public abstract class VanillaChronicleHash<K,
}
}
public final void initBeforeMapping(File file, FileChannel ch, long headerSize)
public final void initBeforeMapping(File file, FileChannel ch, long headerEnd)
throws IOException {
this.headerSize = roundUpMapHeaderSize(headerSize);
this.headerSize = roundUpMapHeaderSize(headerEnd);
if (!createdOrInMemory) {
// This block is for reading segmentHeadersOffset before main mapping
// After the mapping globalMutableState value's bytes are reassigned
@@ -774,6 +774,10 @@ public abstract class VanillaChronicleHash<K,
globalMutableState.setFirstFreeTierIndex(firstTierIndex);
}
public void msync(RandomAccessFile raf) throws IOException {
msync(raf, bsAddress(), bs.capacity());
}
private static void msync(RandomAccessFile raf, long address, long length) throws IOException {
// address should be a multiple of page size
if (OS.pageAlign(address) != address) {
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2015 higherfrequencytrading.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.openhft.chronicle.hash.impl.util;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public final class FileIOUtils {
private FileIOUtils() {}
public static void readFully(FileChannel fileChannel, long filePosition, ByteBuffer buffer)
throws IOException {
int startBufferPosition = buffer.position();
while (buffer.remaining() > 0 &&
buffer.position() < fileChannel.size()) {
int bytesRead = fileChannel.read(buffer,
filePosition + buffer.position() - startBufferPosition);
if (bytesRead == -1)
break;
}
}
public static void writeFully(FileChannel fileChannel, long filePosition, ByteBuffer buffer)
throws IOException {
int startBufferPosition = buffer.position();
while (buffer.remaining() > 0) {
fileChannel.write(buffer, filePosition + buffer.position() - startBufferPosition);
}
}
}