Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
io.questdb.cairo.vm.MemoryCMORImpl Maven / Gradle / Ivy
package io.questdb.cairo.vm;
import io.questdb.cairo.CairoException;
import io.questdb.cairo.TableUtils;
import io.questdb.cairo.vm.api.MemoryCMOR;
import io.questdb.log.Log;
import io.questdb.log.LogFactory;
import io.questdb.std.Files;
import io.questdb.std.FilesFacade;
import io.questdb.std.str.LPSZ;
public class MemoryCMORImpl extends MemoryCMRImpl implements MemoryCMOR {
private static final Log LOG = LogFactory.getLog(MemoryCMORImpl.class );
private long mapFileOffset;
private long offset;
public MemoryCMORImpl() {
}
@Override
public long addressOf(long fileOffset) {
assert checkOffsetMapped(fileOffset) : "offset=" + offset + ", size=" + size + ", fd=" + fd;
if (pageAddress == 0 ) {
map(ff, size, mapFileOffset);
}
return pageAddress + fileOffset - mapFileOffset;
}
@Override
public boolean checkOffsetMapped(long fileOffset) {
return fileOffset - mapFileOffset <= size;
}
@Override
public void close() {
super .close();
mapFileOffset = 0 ;
offset = 0 ;
}
@Override
public long detachFdClose() {
long lfd = fd;
fd = -1 ;
close();
return lfd;
}
@Override
public void extend(long newSize) {
if (newSize > size) {
setSize0(newSize + offset - mapFileOffset);
}
}
@Override
public long getOffset() {
return offset;
}
@Override
public void growToFileSize() {
long length = getFilesFacade().length(getFd());
if (length < 0 ) {
throw CairoException.critical(ff.errno()).put("could not get length fd: " ).put(fd);
}
extend(length - mapFileOffset);
}
@Override
public void map() {
if (pageAddress == 0 ) {
map(ff, size, mapFileOffset);
}
}
@Override
public void of(FilesFacade ff, LPSZ name, long extendSegmentSize, long size, int memoryTag, long opts) {
ofOffset(ff, name, 0 L, size, memoryTag, opts);
}
@Override
public void ofOffset(FilesFacade ff, long fd, LPSZ name, long lo, long hi, int memoryTag, long opts) {
this .memoryTag = memoryTag;
if (fd > -1 ) {
close();
this .ff = ff;
this .fd = fd;
} else {
openFile(ff, name);
}
mapLazy(lo, hi);
}
@Override
public void ofOffset(FilesFacade ff, LPSZ name, long lo, long hi, int memoryTag, long opts) {
ofOffset(ff, -1 , name, lo, hi, memoryTag, opts);
}
@Override
public long size() {
return size + mapFileOffset - offset;
}
private void mapLazy(long lo, long hi) {
assert hi >= 0 && hi >= lo : "hi : " + hi + " lo : " + lo;
if (hi > lo) {
this .offset = lo;
this .mapFileOffset = Files.PAGE_SIZE * (lo / Files.PAGE_SIZE);
this .size = hi - mapFileOffset;
} else {
this .size = 0 ;
}
}
private void openFile(FilesFacade ff, LPSZ name) {
close();
this .ff = ff;
fd = TableUtils.openRO(ff, name, LOG);
}
private void setSize0(long newSize) {
try {
if (size > 0 && pageAddress != 0 ) {
pageAddress = TableUtils.mremap(ff, fd, pageAddress, size, newSize, mapFileOffset, Files.MAP_RO, memoryTag);
} else {
pageAddress = TableUtils.mapRO(ff, fd, newSize, mapFileOffset, memoryTag);
}
size = newSize;
} catch (Throwable e) {
close();
throw e;
}
}
protected void map(FilesFacade ff, final long size, final long mapOffset) {
this .size = size;
if (size > 0 ) {
try {
this .pageAddress = TableUtils.mapRO(ff, fd, size, mapOffset, memoryTag);
} catch (Throwable e) {
close();
throw e;
}
}
LOG.debug().$("map [fd=" ).$(fd).$(", size=" ).$(this .size).$(']' ).$();
}
}