| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.mvstore; |
| 7 |
|
| 8 |
import java.nio.ByteBuffer; |
| 9 |
import java.util.HashMap; |
| 10 |
import java.util.Iterator; |
| 11 |
import java.util.Map.Entry; |
| 12 |
import java.util.TreeMap; |
| 13 |
import java.util.zip.ZipOutputStream; |
| 14 |
|
| 15 |
/** |
| 16 |
* A storage mechanism that "persists" data in the off-heap area of the main memory. |
| 17 |
*/ |
| 18 |
public class OffHeapStore extends RandomAccessStore { |
| 19 |
|
| 20 |
private final TreeMap<Long, ByteBuffer> memory = new TreeMap<>(); |
| 21 |
|
| 22 |
public OffHeapStore() { |
| 23 |
super(new HashMap<>()); |
| 24 |
} |
| 25 |
|
| 26 |
@Override |
| 27 |
public void open(String fileName, boolean readOnly, char[] encryptionKey) { |
| 28 |
init(); |
| 29 |
} |
| 30 |
|
| 31 |
@Override |
| 32 |
public OffHeapStore open(String fileName, boolean readOnly) { |
| 33 |
OffHeapStore result = new OffHeapStore(); |
| 34 |
result.init(); |
| 35 |
return result; |
| 36 |
} |
| 37 |
|
| 38 |
private void init() { |
| 39 |
memory.clear(); |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
public String toString() { |
| 44 |
return memory.toString(); |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
public ByteBuffer readFully(SFChunk chunk, long pos, int len) { |
| 49 |
Entry<Long, ByteBuffer> memEntry = memory.floorEntry(pos); |
| 50 |
if (memEntry == null) { |
| 51 |
throw DataUtils.newMVStoreException( |
| 52 |
DataUtils.ERROR_READING_FAILED, |
| 53 |
"Could not read from position {0}", pos); |
| 54 |
} |
| 55 |
readCount.incrementAndGet(); |
| 56 |
readBytes.addAndGet(len); |
| 57 |
ByteBuffer buff = memEntry.getValue(); |
| 58 |
ByteBuffer read = buff.duplicate(); |
| 59 |
int offset = (int) (pos - memEntry.getKey()); |
| 60 |
read.position(offset); |
| 61 |
read.limit(len + offset); |
| 62 |
return read.slice(); |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public void free(long pos, int length) { |
| 67 |
super.free(pos, length); |
| 68 |
ByteBuffer buff = memory.remove(pos); |
| 69 |
if (buff == null) { |
| 70 |
// nothing was written (just allocated) |
| 71 |
} else if (buff.remaining() != length) { |
| 72 |
throw DataUtils.newMVStoreException( |
| 73 |
DataUtils.ERROR_READING_FAILED, |
| 74 |
"Partial remove is not supported at position {0}", pos); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public void writeFully(SFChunk chunk, long pos, ByteBuffer src) { |
| 80 |
setSize(Math.max(size(), pos + src.remaining())); |
| 81 |
Entry<Long, ByteBuffer> mem = memory.floorEntry(pos); |
| 82 |
if (mem == null) { |
| 83 |
// not found: create a new entry |
| 84 |
writeNewEntry(pos, src); |
| 85 |
return; |
| 86 |
} |
| 87 |
long prevPos = mem.getKey(); |
| 88 |
ByteBuffer buff = mem.getValue(); |
| 89 |
int prevLength = buff.capacity(); |
| 90 |
int length = src.remaining(); |
| 91 |
if (prevPos == pos) { |
| 92 |
if (prevLength != length) { |
| 93 |
throw DataUtils.newMVStoreException( |
| 94 |
DataUtils.ERROR_READING_FAILED, |
| 95 |
"Could not write to position {0}; " + |
| 96 |
"partial overwrite is not supported", pos); |
| 97 |
} |
| 98 |
writeCount.incrementAndGet(); |
| 99 |
writeBytes.addAndGet(length); |
| 100 |
buff.rewind(); |
| 101 |
buff.put(src); |
| 102 |
return; |
| 103 |
} |
| 104 |
if (prevPos + prevLength > pos) { |
| 105 |
throw DataUtils.newMVStoreException( |
| 106 |
DataUtils.ERROR_READING_FAILED, |
| 107 |
"Could not write to position {0}; " + |
| 108 |
"partial overwrite is not supported", pos); |
| 109 |
} |
| 110 |
writeNewEntry(pos, src); |
| 111 |
} |
| 112 |
|
| 113 |
private void writeNewEntry(long pos, ByteBuffer src) { |
| 114 |
int length = src.remaining(); |
| 115 |
writeCount.incrementAndGet(); |
| 116 |
writeBytes.addAndGet(length); |
| 117 |
ByteBuffer buff = ByteBuffer.allocateDirect(length); |
| 118 |
buff.put(src); |
| 119 |
buff.rewind(); |
| 120 |
memory.put(pos, buff); |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public void truncate(long size) { |
| 125 |
writeCount.incrementAndGet(); |
| 126 |
setSize(size); |
| 127 |
if (size == 0) { |
| 128 |
memory.clear(); |
| 129 |
} else { |
| 130 |
for (Iterator<Long> it = memory.keySet().iterator(); it.hasNext(); ) { |
| 131 |
long pos = it.next(); |
| 132 |
if (pos < size) { |
| 133 |
break; |
| 134 |
} |
| 135 |
ByteBuffer buff = memory.get(pos); |
| 136 |
if (buff.capacity() > size) { |
| 137 |
throw DataUtils.newMVStoreException( |
| 138 |
DataUtils.ERROR_READING_FAILED, |
| 139 |
"Could not truncate to {0}; " + |
| 140 |
"partial truncate is not supported", pos); |
| 141 |
} |
| 142 |
it.remove(); |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
@Override |
| 148 |
public int getDefaultRetentionTime() { |
| 149 |
return 0; |
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
public void backup(ZipOutputStream out) { |
| 154 |
throw new UnsupportedOperationException(); |
| 155 |
} |
| 156 |
} |