@Override public DataAccess createDataAccess(String name) { return new UnsafeDataAccess(name, directory, defaultOrder).setSegmentSize(128); }
@Override public boolean loadExisting() { if (isClosed()) throw new IllegalStateException("already closed"); File file = new File(getFullName()); if (!file.exists() || file.length() == 0) return false; RandomAccessFile raFile = new RandomAccessFile(getFullName(), "r"); try { long byteCount = readHeader(raFile) - HEADER_OFFSET; if (byteCount < 0) return false; segmentCount++; ensureCapacity(byteCount, false); byte[] bytes = new byte[segmentSizeInBytes]; for (int s = 0; s < segmentCount; s++) { int read = raFile.read(bytes); if (read <= 0) throw new IllegalStateException("segment " + s + " is empty? " + toString()); setBytes(s * segmentSizeInBytes, bytes, segmentSizeInBytes); throw new RuntimeException("Problem while loading " + getFullName(), ex);
@Override public void flush() { if (isClosed()) throw new IllegalStateException("already closed"); try { RandomAccessFile raFile = new RandomAccessFile(getFullName(), "rw"); try { long len = getCapacity(); writeHeader(raFile, len, segmentSizeInBytes); raFile.seek(HEADER_OFFSET); byte bytes[] = new byte[segmentSizeInBytes]; int segs = getSegments(); for (int s = 0; s < segs; s++) { getBytes(s * segmentSizeInBytes, bytes, segmentSizeInBytes); raFile.write(bytes); } } finally { raFile.close(); } } catch (Exception ex) { throw new RuntimeException("Couldn't store bytes to " + toString(), ex); } }
@Override public UnsafeDataAccess create(long bytes) { // TODO use unsafe.pageSize() instead segmentSizeInBytes? // e.g. on my system pageSize is only 4096 setSegmentSize(segmentSizeInBytes); ensureCapacity(bytes); return this; }
@Override public DataAccess find(String name, DAType type) { if (!name.equals(toLowerCase(name))) throw new IllegalArgumentException("Since 0.7 DataAccess objects does no longer accept upper case names"); DataAccess da = map.get(name); if (da != null) { if (!type.equals(da.getType())) throw new IllegalStateException("Found existing DataAccess object '" + name + "' but types did not match. Requested:" + type + ", was:" + da.getType()); return da; } if (type.isInMemory()) { if (type.isInteg()) { if (type.isStoring()) da = new RAMIntDataAccess(name, location, true, byteOrder); else da = new RAMIntDataAccess(name, location, false, byteOrder); } else if (type.isStoring()) da = new RAMDataAccess(name, location, true, byteOrder); else da = new RAMDataAccess(name, location, false, byteOrder); } else if (type.isMMap()) { da = new MMapDataAccess(name, location, byteOrder, type.isAllowWrites()); } else { da = new UnsafeDataAccess(name, location, byteOrder); } map.put(name, da); return da; }
@Override public final boolean ensureCapacity(long bytes) { return ensureCapacity(bytes, true); }
final boolean ensureCapacity(long bytes, boolean clearNewMem) { long oldCap = getCapacity(); long newBytes = bytes - oldCap; if (newBytes <= 0) return false; // avoid frequent increase of allocation area, instead increase by segment size int allSegments = (int) (bytes / segmentSizeInBytes); if (bytes % segmentSizeInBytes != 0) allSegments++; capacity = allSegments * segmentSizeInBytes; try { address = UNSAFE.reallocateMemory(address, capacity); } catch (OutOfMemoryError err) { throw new OutOfMemoryError(err.getMessage() + " - problem when allocating new memory. Old capacity: " + oldCap + ", new bytes:" + newBytes + ", segmentSizeIntsPower:" + segmentSizePower); } if (clearNewMem) UNSAFE.setMemory(address + oldCap, capacity - oldCap, (byte) 0); return true; }
@Override public UnsafeDataAccess create( long bytes ) { // TODO use unsafe.pageSize() instead segmentSizeInBytes? // e.g. on my system pageSize is only 4096 setSegmentSize(segmentSizeInBytes); ensureCapacity(bytes); return this; }
@Override public DataAccess find(String name, DAType type) { if (!name.equals(toLowerCase(name))) throw new IllegalArgumentException("Since 0.7 DataAccess objects does no longer accept upper case names"); DataAccess da = map.get(name); if (da != null) { if (!type.equals(da.getType())) throw new IllegalStateException("Found existing DataAccess object '" + name + "' but types did not match. Requested:" + type + ", was:" + da.getType()); return da; } if (type.isInMemory()) { if (type.isInteg()) { if (type.isStoring()) da = new RAMIntDataAccess(name, location, true, byteOrder); else da = new RAMIntDataAccess(name, location, false, byteOrder); } else if (type.isStoring()) da = new RAMDataAccess(name, location, true, byteOrder); else da = new RAMDataAccess(name, location, false, byteOrder); } else if (type.isMMap()) { da = new MMapDataAccess(name, location, byteOrder, type.isAllowWrites()); } else { da = new UnsafeDataAccess(name, location, byteOrder); } map.put(name, da); return da; }
@Override public final boolean ensureCapacity( long bytes ) { return ensureCapacity(bytes, true); }
final boolean ensureCapacity(long bytes, boolean clearNewMem) { long oldCap = getCapacity(); long newBytes = bytes - oldCap; if (newBytes <= 0) return false; // avoid frequent increase of allocation area, instead increase by segment size int allSegments = (int) (bytes / segmentSizeInBytes); if (bytes % segmentSizeInBytes != 0) allSegments++; capacity = allSegments * segmentSizeInBytes; try { address = UNSAFE.reallocateMemory(address, capacity); } catch (OutOfMemoryError err) { throw new OutOfMemoryError(err.getMessage() + " - problem when allocating new memory. Old capacity: " + oldCap + ", new bytes:" + newBytes + ", segmentSizeIntsPower:" + segmentSizePower); } if (clearNewMem) UNSAFE.setMemory(address + oldCap, capacity - oldCap, (byte) 0); return true; }
@Override public void flush() { if (isClosed()) throw new IllegalStateException("already closed"); try { RandomAccessFile raFile = new RandomAccessFile(getFullName(), "rw"); try { long len = getCapacity(); writeHeader(raFile, len, segmentSizeInBytes); raFile.seek(HEADER_OFFSET); byte bytes[] = new byte[segmentSizeInBytes]; int segs = getSegments(); for (int s = 0; s < segs; s++) { getBytes(s * segmentSizeInBytes, bytes, segmentSizeInBytes); raFile.write(bytes); } } finally { raFile.close(); } } catch (Exception ex) { throw new RuntimeException("Couldn't store bytes to " + toString(), ex); } }
@Override public boolean loadExisting() { if (isClosed()) throw new IllegalStateException("already closed"); File file = new File(getFullName()); if (!file.exists() || file.length() == 0) return false; RandomAccessFile raFile = new RandomAccessFile(getFullName(), "r"); try { long byteCount = readHeader(raFile) - HEADER_OFFSET; if (byteCount < 0) return false; segmentCount++; ensureCapacity(byteCount, false); byte[] bytes = new byte[segmentSizeInBytes]; for (int s = 0; s < segmentCount; s++) { int read = raFile.read(bytes); if (read <= 0) throw new IllegalStateException("segment " + s + " is empty? " + toString()); setBytes(s * segmentSizeInBytes, bytes, segmentSizeInBytes); throw new RuntimeException("Problem while loading " + getFullName(), ex);
@Override public UnsafeDataAccess create(long bytes) { // TODO use unsafe.pageSize() instead segmentSizeInBytes? // e.g. on my system pageSize is only 4096 setSegmentSize(segmentSizeInBytes); ensureCapacity(bytes); return this; }
} else da = new UnsafeDataAccess(name, location, byteOrder);
@Override public final boolean ensureCapacity(long bytes) { return ensureCapacity(bytes, true); }
final boolean ensureCapacity(long bytes, boolean clearNewMem) { long oldCap = getCapacity(); long newBytes = bytes - oldCap; if (newBytes <= 0) return false; // avoid frequent increase of allocation area, instead increase by segment size int allSegments = (int) (bytes / segmentSizeInBytes); if (bytes % segmentSizeInBytes != 0) allSegments++; capacity = allSegments * segmentSizeInBytes; try { address = UNSAFE.reallocateMemory(address, capacity); } catch (OutOfMemoryError err) { throw new OutOfMemoryError(err.getMessage() + " - problem when allocating new memory. Old capacity: " + oldCap + ", new bytes:" + newBytes + ", segmentSizeIntsPower:" + segmentSizePower); } if (clearNewMem) UNSAFE.setMemory(address + oldCap, capacity - oldCap, (byte) 0); return true; }
@Override public void flush() { if (isClosed()) throw new IllegalStateException("already closed"); try { RandomAccessFile raFile = new RandomAccessFile(getFullName(), "rw"); try { long len = getCapacity(); writeHeader(raFile, len, segmentSizeInBytes); raFile.seek(HEADER_OFFSET); byte bytes[] = new byte[segmentSizeInBytes]; int segs = getSegments(); for (int s = 0; s < segs; s++) { getBytes(s * segmentSizeInBytes, bytes, segmentSizeInBytes); raFile.write(bytes); } } finally { raFile.close(); } } catch (Exception ex) { throw new RuntimeException("Couldn't store bytes to " + toString(), ex); } }
@Override public boolean loadExisting() { if (isClosed()) throw new IllegalStateException("already closed"); File file = new File(getFullName()); if (!file.exists() || file.length() == 0) return false; RandomAccessFile raFile = new RandomAccessFile(getFullName(), "r"); try { long byteCount = readHeader(raFile) - HEADER_OFFSET; if (byteCount < 0) return false; segmentCount++; ensureCapacity(byteCount, false); byte[] bytes = new byte[segmentSizeInBytes]; for (int s = 0; s < segmentCount; s++) { int read = raFile.read(bytes); if (read <= 0) throw new IllegalStateException("segment " + s + " is empty? " + toString()); setBytes(s * segmentSizeInBytes, bytes, segmentSizeInBytes); throw new RuntimeException("Problem while loading " + getFullName(), ex);
@Override public UnsafeDataAccess create(long bytes) { // TODO use unsafe.pageSize() instead segmentSizeInBytes? // e.g. on my system pageSize is only 4096 setSegmentSize(segmentSizeInBytes); ensureCapacity(bytes); return this; }