2019-03-07 16:19:53 +01:00
|
|
|
package it.cavallium.strangedb.database.references;
|
|
|
|
|
|
|
|
import it.cavallium.strangedb.database.IReferencesMetadata;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.nio.channels.AsynchronousFileChannel;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.file.StandardOpenOption;
|
|
|
|
import java.util.concurrent.ExecutionException;
|
2019-04-20 22:18:34 +02:00
|
|
|
import java.util.concurrent.atomic.AtomicLong;
|
2019-04-20 15:54:40 +02:00
|
|
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
2019-03-07 16:19:53 +01:00
|
|
|
|
2019-04-20 15:54:40 +02:00
|
|
|
import static it.cavallium.strangedb.database.IDatabase.DISK_BLOCK_SIZE;
|
2019-03-07 16:19:53 +01:00
|
|
|
|
|
|
|
public class DatabaseReferencesMetadata implements IReferencesMetadata {
|
2019-04-20 15:54:40 +02:00
|
|
|
public static final byte ERRORED_CLEANER = (byte) -1;
|
|
|
|
public static final byte BLANK_DATA_CLEANER = (byte) -2;
|
2019-04-22 12:18:25 +02:00
|
|
|
public static final ReferenceInfo NONEXISTENT_REFERENCE_INFO = new ReferenceInfo(-1, -1, ERRORED_CLEANER);
|
|
|
|
private static final int REF_META_BYTES_COUNT = Long.BYTES + Integer.BYTES + Byte.BYTES;
|
2019-04-20 15:54:40 +02:00
|
|
|
public static final int REF_META_READS_AT_EVERY_READ = (DISK_BLOCK_SIZE - DISK_BLOCK_SIZE % REF_META_BYTES_COUNT) / REF_META_BYTES_COUNT;
|
|
|
|
|
2019-03-07 16:19:53 +01:00
|
|
|
private final AsynchronousFileChannel metaFileChannel;
|
|
|
|
private final DatabaseReferencesMetadataCache cache;
|
2019-04-20 22:18:34 +02:00
|
|
|
private AtomicLong firstFreeReference;
|
2019-04-20 15:54:40 +02:00
|
|
|
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(false);
|
2019-03-07 16:19:53 +01:00
|
|
|
|
|
|
|
public DatabaseReferencesMetadata(Path refMetaFile) throws IOException {
|
|
|
|
metaFileChannel = AsynchronousFileChannel.open(refMetaFile, StandardOpenOption.READ, StandardOpenOption.WRITE);
|
2019-04-20 22:18:34 +02:00
|
|
|
firstFreeReference = new AtomicLong(metaFileChannel.size() / REF_META_BYTES_COUNT);
|
2019-04-22 12:18:25 +02:00
|
|
|
this.cache = new DatabaseReferencesMetadataCache(new Flusher());
|
2019-03-07 16:19:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-04-22 12:18:25 +02:00
|
|
|
public ReferenceInfo getReferenceInfo(long reference) throws IOException {
|
|
|
|
ReferenceInfo block;
|
|
|
|
long[] allReferences;
|
|
|
|
ReferenceInfo[] allInfo;
|
2019-04-20 15:54:40 +02:00
|
|
|
lock.readLock().lock();
|
|
|
|
try {
|
2019-04-22 12:18:25 +02:00
|
|
|
long firstFreeReference = this.firstFreeReference.get();
|
|
|
|
if (reference >= firstFreeReference) {
|
|
|
|
return NONEXISTENT_REFERENCE_INFO;
|
|
|
|
}
|
|
|
|
if ((block = cache.getInfo(reference)) != NONEXISTENT_REFERENCE_INFO) {
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
long position = reference * REF_META_BYTES_COUNT;
|
|
|
|
int size = REF_META_READS_AT_EVERY_READ * REF_META_BYTES_COUNT;
|
|
|
|
if (reference + (REF_META_READS_AT_EVERY_READ - 1) >= firstFreeReference) {
|
|
|
|
size = (int) ((firstFreeReference - reference) * REF_META_BYTES_COUNT);
|
|
|
|
}
|
|
|
|
int referencesCount = size / REF_META_BYTES_COUNT;
|
|
|
|
|
|
|
|
ByteBuffer buffer = ByteBuffer.allocate(size);
|
|
|
|
try {
|
|
|
|
metaFileChannel.read(buffer, position).get();
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
throw new IOException(e);
|
|
|
|
} catch (ExecutionException e) {
|
|
|
|
throw new IOException(e.getCause());
|
|
|
|
}
|
|
|
|
buffer.flip();
|
2019-04-20 15:54:40 +02:00
|
|
|
|
2019-04-22 12:18:25 +02:00
|
|
|
if (referencesCount < 1) {
|
|
|
|
throw new IOException("Trying to read <1 references");
|
|
|
|
}
|
|
|
|
if (buffer.limit() % REF_META_BYTES_COUNT != 0 || buffer.limit() < REF_META_BYTES_COUNT) {
|
|
|
|
throw new IOException("The buffer is smaller than the data requested.");
|
|
|
|
} else if (buffer.limit() != size) {
|
|
|
|
size = buffer.limit();
|
|
|
|
referencesCount = size / REF_META_BYTES_COUNT;
|
|
|
|
}
|
2019-04-20 15:54:40 +02:00
|
|
|
|
2019-04-22 12:18:25 +02:00
|
|
|
allReferences = new long[referencesCount];
|
|
|
|
allInfo = new ReferenceInfo[referencesCount];
|
|
|
|
|
|
|
|
block = NONEXISTENT_REFERENCE_INFO;
|
|
|
|
for (int delta = 0; delta < referencesCount; delta++) {
|
|
|
|
long referenceToLoad = reference + delta;
|
|
|
|
long currentIndex = buffer.getLong();
|
|
|
|
int currentSize = buffer.getInt();
|
|
|
|
byte cleanerId = buffer.get();
|
|
|
|
ReferenceInfo refInfo = new ReferenceInfo(currentIndex, currentSize, cleanerId);
|
|
|
|
if (buffer.limit() != 0 && currentIndex != 0xFFFFFFFFFFFFFFFFL) {
|
|
|
|
allReferences[delta] = referenceToLoad;
|
|
|
|
allInfo[delta] = refInfo;
|
|
|
|
if (referenceToLoad == reference) {
|
|
|
|
block = refInfo;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
allReferences[delta] = referenceToLoad;
|
|
|
|
allInfo[delta] = refInfo;
|
|
|
|
if (referenceToLoad == reference) {
|
|
|
|
block = NONEXISTENT_REFERENCE_INFO;
|
|
|
|
}
|
2019-04-20 15:54:40 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-22 12:18:25 +02:00
|
|
|
} finally {
|
|
|
|
lock.readLock().unlock();
|
2019-04-20 15:54:40 +02:00
|
|
|
}
|
2019-04-22 12:18:25 +02:00
|
|
|
lock.writeLock().lock();
|
2019-04-20 15:54:40 +02:00
|
|
|
try {
|
2019-04-22 12:18:25 +02:00
|
|
|
cache.putAll(allReferences, allInfo);
|
2019-04-20 15:54:40 +02:00
|
|
|
} finally {
|
2019-04-22 12:18:25 +02:00
|
|
|
lock.writeLock().unlock();
|
2019-03-07 16:19:53 +01:00
|
|
|
}
|
2019-04-22 12:18:25 +02:00
|
|
|
return block;
|
2019-03-07 16:19:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-04-22 12:18:25 +02:00
|
|
|
public long newReference(long index, int size) throws IOException {
|
2019-04-20 22:18:34 +02:00
|
|
|
long newReference;
|
2019-04-24 01:10:43 +02:00
|
|
|
newReference = firstFreeReference.getAndIncrement();
|
|
|
|
cache.put(newReference, new ReferenceInfo(index, size, BLANK_DATA_CLEANER), true, true);
|
2019-04-20 22:18:34 +02:00
|
|
|
return newReference;
|
2019-04-20 15:54:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-04-22 12:18:25 +02:00
|
|
|
public void editReference(long reference, ReferenceInfo info) throws IOException {
|
2019-04-20 15:54:40 +02:00
|
|
|
lock.writeLock().lock();
|
|
|
|
try {
|
2019-04-24 01:10:43 +02:00
|
|
|
cache.put(reference, info, true, false);
|
2019-04-20 15:54:40 +02:00
|
|
|
} finally {
|
|
|
|
lock.writeLock().unlock();
|
|
|
|
}
|
2019-03-07 16:19:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-04-20 15:54:40 +02:00
|
|
|
public void deleteReference(long reference) throws IOException {
|
|
|
|
lock.writeLock().lock();
|
|
|
|
try {
|
2019-04-24 01:10:43 +02:00
|
|
|
cache.put(reference, NONEXISTENT_REFERENCE_INFO, true, false);
|
2019-04-20 15:54:40 +02:00
|
|
|
} finally {
|
|
|
|
lock.writeLock().unlock();
|
|
|
|
}
|
2019-03-07 16:19:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() throws IOException {
|
2019-04-20 15:54:40 +02:00
|
|
|
lock.writeLock().lock();
|
|
|
|
try {
|
|
|
|
cache.close();
|
|
|
|
metaFileChannel.close();
|
|
|
|
} finally {
|
|
|
|
lock.writeLock().unlock();
|
|
|
|
}
|
2019-03-07 16:19:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getFirstFreeReference() {
|
2019-04-20 22:18:34 +02:00
|
|
|
lock.readLock().lock();
|
|
|
|
try {
|
|
|
|
return firstFreeReference.get();
|
|
|
|
} finally {
|
|
|
|
lock.readLock().unlock();
|
2019-04-20 15:54:40 +02:00
|
|
|
}
|
2019-03-07 16:19:53 +01:00
|
|
|
}
|
|
|
|
|
2019-04-22 12:18:25 +02:00
|
|
|
private class Flusher implements DatabaseReferencesMetadataCacheFlusher {
|
|
|
|
private Flusher() {
|
|
|
|
|
2019-04-20 15:54:40 +02:00
|
|
|
}
|
2019-04-22 12:18:25 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void flush(long reference, ReferenceInfo info, boolean closing) throws IOException {
|
|
|
|
if (info.getCleanerId() == ERRORED_CLEANER) {
|
|
|
|
throw new IOException("Passing a cleaner with the id of ERRORED_CLIENT");
|
|
|
|
}
|
|
|
|
ByteBuffer data = ByteBuffer.allocate(REF_META_BYTES_COUNT);
|
|
|
|
data.putLong(info.getIndex());
|
|
|
|
data.putInt(info.getSize());
|
|
|
|
data.put(info.getCleanerId());
|
|
|
|
data.flip();
|
|
|
|
try {
|
|
|
|
metaFileChannel.write(data, reference * REF_META_BYTES_COUNT).get();
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
throw new IOException(e);
|
|
|
|
} catch (ExecutionException e) {
|
|
|
|
throw new IOException(e.getCause());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void flushMultiple(long referenceStart, ReferenceInfo[] infos) throws IOException {
|
|
|
|
ByteBuffer data = ByteBuffer.allocate(infos.length * REF_META_BYTES_COUNT);
|
|
|
|
for (ReferenceInfo info : infos) {
|
|
|
|
if (info.getCleanerId() == ERRORED_CLEANER) {
|
|
|
|
throw new IOException("Passing a cleaner with the id of ERRORED_CLIENT");
|
|
|
|
}
|
|
|
|
data.putLong(info.getIndex());
|
|
|
|
data.putInt(info.getSize());
|
|
|
|
data.put(info.getCleanerId());
|
|
|
|
}
|
|
|
|
data.flip();
|
|
|
|
try {
|
|
|
|
metaFileChannel.write(data, referenceStart * REF_META_BYTES_COUNT).get();
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
throw new IOException(e);
|
|
|
|
} catch (ExecutionException e) {
|
|
|
|
throw new IOException(e.getCause());
|
|
|
|
}
|
2019-04-20 15:54:40 +02:00
|
|
|
}
|
2019-03-07 16:19:53 +01:00
|
|
|
}
|
|
|
|
|
2019-04-20 22:18:34 +02:00
|
|
|
/*
|
|
|
|
private void writeReferenceToDisk(long reference, byte cleanerId, long blockId) throws IOException {
|
|
|
|
if (cleanerId == ERRORED_CLEANER) {
|
|
|
|
throw new IOException("Passing a cleaner with the id of ERRORED_CLIENT");
|
|
|
|
}
|
|
|
|
long firstReferenceToWrite = 1 + lastWrittenReference.getAndUpdate((lastWrittenReferenceVal) -> reference > lastWrittenReferenceVal ? reference : lastWrittenReferenceVal);
|
|
|
|
if (firstReferenceToWrite > reference) {
|
|
|
|
firstReferenceToWrite = reference;
|
|
|
|
}
|
|
|
|
ByteBuffer data = ByteBuffer.allocate((int) ((reference + 1 - firstReferenceToWrite) * REF_META_BYTES_COUNT));
|
|
|
|
for (long i = firstReferenceToWrite; i < reference - 1; i++) {
|
|
|
|
data.putLong(ERROR_BLOCK_ID);
|
|
|
|
data.putInt(ERRORED_CLEANER & 0xFF);
|
|
|
|
}
|
|
|
|
data.putLong(blockId);
|
|
|
|
data.putInt(cleanerId & 0xFF);
|
|
|
|
data.flip();
|
|
|
|
try {
|
|
|
|
metaFileChannel.write(data, firstReferenceToWrite * REF_META_BYTES_COUNT).get();
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
throw new IOException(e);
|
|
|
|
} catch (ExecutionException e) {
|
|
|
|
throw new IOException(e.getCause());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2019-03-07 16:19:53 +01:00
|
|
|
}
|