use of org.h2.store.Page in project h2database by h2database.
the class MVTableEngine method init.
/**
* Initialize the MVStore.
*
* @param db the database
* @return the store
*/
public static Store init(final Database db) {
Store store = db.getMvStore();
if (store != null) {
return store;
}
byte[] key = db.getFileEncryptionKey();
String dbPath = db.getDatabasePath();
MVStore.Builder builder = new MVStore.Builder();
store = new Store();
boolean encrypted = false;
if (dbPath != null) {
String fileName = dbPath + Constants.SUFFIX_MV_FILE;
MVStoreTool.compactCleanUp(fileName);
builder.fileName(fileName);
builder.pageSplitSize(db.getPageSize());
if (db.isReadOnly()) {
builder.readOnly();
} else {
// possibly create the directory
boolean exists = FileUtils.exists(fileName);
if (exists && !FileUtils.canWrite(fileName)) {
// read only
} else {
String dir = FileUtils.getParent(fileName);
FileUtils.createDirectories(dir);
}
}
if (key != null) {
encrypted = true;
char[] password = new char[key.length / 2];
for (int i = 0; i < password.length; i++) {
password[i] = (char) (((key[i + i] & 255) << 16) | ((key[i + i + 1]) & 255));
}
builder.encryptionKey(password);
}
if (db.getSettings().compressData) {
builder.compress();
// use a larger page split size to improve the compression ratio
builder.pageSplitSize(64 * 1024);
}
builder.backgroundExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
db.setBackgroundException(DbException.convert(e));
}
});
}
store.open(db, builder, encrypted);
db.setMvStore(store);
return store;
}
use of org.h2.store.Page in project h2database by h2database.
the class PageDataOverflow method create.
/**
* Create a new overflow page.
*
* @param store the page store
* @param page the page id
* @param type the page type
* @param parentPageId the parent page id
* @param next the next page or 0
* @param all the data
* @param offset the offset within the data
* @param size the number of bytes
* @return the page
*/
static PageDataOverflow create(PageStore store, int page, int type, int parentPageId, int next, Data all, int offset, int size) {
Data data = store.createData();
PageDataOverflow p = new PageDataOverflow(store, page, data);
store.logUndo(p, null);
data.writeByte((byte) type);
data.writeShortInt(0);
data.writeInt(parentPageId);
if (type == Page.TYPE_DATA_OVERFLOW) {
data.writeInt(next);
} else {
data.writeShortInt(size);
}
p.start = data.length();
data.write(all.getBytes(), offset, size);
p.type = type;
p.parentPageId = parentPageId;
p.nextPage = next;
p.size = size;
return p;
}
use of org.h2.store.Page in project ignite by apache.
the class InlineIndexHelperTest method testPutGet.
/**
*/
private void testPutGet(Value v1, Value v2, Value v3) throws Exception {
DataRegionConfiguration plcCfg = new DataRegionConfiguration().setInitialSize(1024 * MB).setMaxSize(1024 * MB);
PageMemory pageMem = new PageMemoryNoStoreImpl(log(), new UnsafeMemoryProvider(log()), null, PAGE_SIZE, plcCfg, new DataRegionMetricsImpl(plcCfg), false);
pageMem.start();
long pageId = 0L;
long page = 0L;
try {
pageId = pageMem.allocatePage(CACHE_ID, 1, PageIdAllocator.FLAG_DATA);
page = pageMem.acquirePage(CACHE_ID, pageId);
long pageAddr = pageMem.readLock(CACHE_ID, pageId, page);
int off = 0;
int max = 255;
InlineIndexHelper ih = new InlineIndexHelper(v1.getType(), 1, 0, CompareMode.getInstance(null, 0));
off += ih.put(pageAddr, off, v1, max - off);
off += ih.put(pageAddr, off, v2, max - off);
off += ih.put(pageAddr, off, v3, max - off);
Value v11 = ih.get(pageAddr, 0, max);
Value v22 = ih.get(pageAddr, ih.fullSize(pageAddr, 0), max);
assertEquals(v1.getObject(), v11.getObject());
assertEquals(v2.getObject(), v22.getObject());
} finally {
if (page != 0L)
pageMem.releasePage(CACHE_ID, pageId, page);
pageMem.stop();
}
}
use of org.h2.store.Page in project ignite by apache.
the class InlineIndexHelperTest method putAndCompare.
/**
* @param v1 Value 1.
* @param v2 Value 2.
* @param maxSize Max inline size.
* @return Compare result.
* @throws Exception If failed.
*/
private int putAndCompare(String v1, String v2, int maxSize) throws Exception {
DataRegionConfiguration plcCfg = new DataRegionConfiguration().setInitialSize(1024 * MB).setMaxSize(1024 * MB);
PageMemory pageMem = new PageMemoryNoStoreImpl(log, new UnsafeMemoryProvider(log), null, PAGE_SIZE, plcCfg, new DataRegionMetricsImpl(plcCfg), false);
pageMem.start();
long pageId = 0L;
long page = 0L;
try {
pageId = pageMem.allocatePage(CACHE_ID, 1, PageIdAllocator.FLAG_DATA);
page = pageMem.acquirePage(CACHE_ID, pageId);
long pageAddr = pageMem.readLock(CACHE_ID, pageId, page);
int off = 0;
InlineIndexHelper ih = new InlineIndexHelper(Value.STRING, 1, 0, CompareMode.getInstance(null, 0));
ih.put(pageAddr, off, v1 == null ? ValueNull.INSTANCE : ValueString.get(v1), maxSize);
Comparator<Value> comp = new Comparator<Value>() {
@Override
public int compare(Value o1, Value o2) {
throw new AssertionError("Optimized algorithm should be used.");
}
};
return ih.compare(pageAddr, off, maxSize, v2 == null ? ValueNull.INSTANCE : ValueString.get(v2), comp);
} finally {
if (page != 0L)
pageMem.releasePage(CACHE_ID, pageId, page);
pageMem.stop();
}
}
use of org.h2.store.Page in project ignite by apache.
the class InlineIndexHelper method compareAsDateTime.
/**
* @param pageAddr Page address.
* @param off Offset.
* @param v Value to compare.
* @param type Highest value type.
* @return Compare result ({@code -2} means we can't compare).
*/
private int compareAsDateTime(long pageAddr, int off, Value v, int type) {
// only compatible types are supported now.
if (PageUtils.getByte(pageAddr, off) == type) {
switch(type) {
case Value.TIME:
long nanos1 = PageUtils.getLong(pageAddr, off + 1);
long nanos2 = ((ValueTime) v.convertTo(type)).getNanos();
return fixSort(Long.signum(nanos1 - nanos2), sortType());
case Value.DATE:
long date1 = PageUtils.getLong(pageAddr, off + 1);
long date2 = ((ValueDate) v.convertTo(type)).getDateValue();
return fixSort(Long.signum(date1 - date2), sortType());
case Value.TIMESTAMP:
ValueTimestamp v0 = (ValueTimestamp) v.convertTo(type);
date1 = PageUtils.getLong(pageAddr, off + 1);
date2 = v0.getDateValue();
int c = Long.signum(date1 - date2);
if (c == 0) {
nanos1 = PageUtils.getLong(pageAddr, off + 9);
nanos2 = v0.getTimeNanos();
c = Long.signum(nanos1 - nanos2);
}
return fixSort(c, sortType());
}
}
return Integer.MIN_VALUE;
}
Aggregations