use of org.neo4j.kernel.impl.util.StringLogger in project graphdb by neo4j-attic.
the class AbstractDynamicStore method rebuildIdGenerator.
/**
* Rebuilds the internal id generator keeping track of what blocks are free
* or taken.
*
* @throws IOException
* If unable to rebuild the id generator
*/
protected void rebuildIdGenerator() {
if (getBlockSize() <= 0) {
throw new InvalidRecordException("Illegal blockSize: " + getBlockSize());
}
logger.fine("Rebuilding id generator for[" + getStorageFileName() + "] ...");
closeIdGenerator();
File file = new File(getStorageFileName() + ".id");
if (file.exists()) {
boolean success = file.delete();
assert success;
}
createIdGenerator(getStorageFileName() + ".id");
openIdGenerator();
// nextBlockId(); // reserved first block containing blockSize
setHighId(1);
FileChannel fileChannel = getFileChannel();
long highId = 0;
long defraggedCount = 0;
try {
long fileSize = fileChannel.size();
boolean fullRebuild = true;
if (getConfig() != null) {
String mode = (String) getConfig().get("rebuild_idgenerators_fast");
if (mode != null && mode.toLowerCase().equals("true")) {
fullRebuild = false;
highId = findHighIdBackwards();
}
}
ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[1]);
LinkedList<Long> freeIdList = new LinkedList<Long>();
if (fullRebuild) {
for (long i = 1; i * getBlockSize() < fileSize; i++) {
fileChannel.position(i * getBlockSize());
fileChannel.read(byteBuffer);
byteBuffer.flip();
byte inUse = byteBuffer.get();
byteBuffer.flip();
nextBlockId();
if (inUse == Record.NOT_IN_USE.byteValue()) {
freeIdList.add(i);
} else {
highId = i;
while (!freeIdList.isEmpty()) {
freeBlockId(freeIdList.removeFirst());
defraggedCount++;
}
}
}
}
} catch (IOException e) {
throw new UnderlyingStorageException("Unable to rebuild id generator " + getStorageFileName(), e);
}
setHighId(highId + 1);
logger.fine("[" + getStorageFileName() + "] high id=" + getHighId() + " (defragged=" + defraggedCount + ")");
if (getConfig() != null) {
String storeDir = (String) getConfig().get("store_dir");
StringLogger msgLog = StringLogger.getLogger(storeDir);
msgLog.logMessage(getStorageFileName() + " rebuild id generator, highId=" + getHighId() + " defragged count=" + defraggedCount, true);
}
closeIdGenerator();
openIdGenerator();
}
use of org.neo4j.kernel.impl.util.StringLogger in project graphdb by neo4j-attic.
the class AbstractDynamicStore method loadStorage.
// public AbstractDynamicStore( String fileName )
// {
// super( fileName );
// }
/**
* Loads this store validating version and id generator. Also the block size
* is loaded (contained in first block)
*/
protected void loadStorage() {
try {
long fileSize = getFileChannel().size();
String expectedVersion = getTypeAndVersionDescriptor();
byte[] version = new byte[UTF8.encode(expectedVersion).length];
ByteBuffer buffer = ByteBuffer.wrap(version);
getFileChannel().position(fileSize - version.length);
getFileChannel().read(buffer);
buffer = ByteBuffer.allocate(4);
getFileChannel().position(0);
getFileChannel().read(buffer);
buffer.flip();
blockSize = buffer.getInt();
if (blockSize <= 0) {
throw new InvalidRecordException("Illegal block size: " + blockSize + " in " + getStorageFileName());
}
if (!expectedVersion.equals(UTF8.decode(version))) {
if (!versionFound(UTF8.decode(version)) && !isReadOnly()) {
setStoreNotOk();
}
}
if ((fileSize - version.length) % blockSize != 0 && !isReadOnly()) {
setStoreNotOk();
}
if (getStoreOk() && !isReadOnly()) {
getFileChannel().truncate(fileSize - version.length);
}
} catch (IOException e) {
throw new UnderlyingStorageException("Unable to load storage " + getStorageFileName(), e);
}
try {
if (!isReadOnly() || isBackupSlave()) {
openIdGenerator();
} else {
openReadOnlyIdGenerator(getBlockSize());
}
} catch (InvalidIdGeneratorException e) {
setStoreNotOk();
} finally {
if (!getStoreOk()) {
if (getConfig() != null) {
String storeDir = (String) getConfig().get("store_dir");
StringLogger msgLog = StringLogger.getLogger(storeDir);
msgLog.logMessage(getStorageFileName() + " non clean shutdown detected", true);
}
}
}
setWindowPool(new PersistenceWindowPool(getStorageFileName(), getBlockSize(), getFileChannel(), getMappedMem(), getIfMemoryMapped(), isReadOnly() && !isBackupSlave()));
}
use of org.neo4j.kernel.impl.util.StringLogger in project graphdb by neo4j-attic.
the class AbstractStore method loadStorage.
// public AbstractStore( String fileName )
// {
// super( fileName );
// }
protected void loadStorage() {
try {
long fileSize = getFileChannel().size();
String expectedVersion = getTypeAndVersionDescriptor();
byte[] version = new byte[UTF8.encode(expectedVersion).length];
ByteBuffer buffer = ByteBuffer.wrap(version);
if (fileSize >= version.length) {
getFileChannel().position(fileSize - version.length);
} else if (!isReadOnly()) {
setStoreNotOk();
}
getFileChannel().read(buffer);
if (!expectedVersion.equals(UTF8.decode(version)) && !isReadOnly()) {
if (!versionFound(UTF8.decode(version))) {
setStoreNotOk();
}
}
if (getRecordSize() != 0 && (fileSize - version.length) % getRecordSize() != 0 && !isReadOnly()) {
setStoreNotOk();
}
if (getStoreOk() && !isReadOnly()) {
getFileChannel().truncate(fileSize - version.length);
}
} catch (IOException e) {
throw new UnderlyingStorageException("Unable to load store " + getStorageFileName(), e);
}
try {
if (!isReadOnly() || isBackupSlave()) {
openIdGenerator();
} else {
openReadOnlyIdGenerator(getRecordSize());
}
} catch (InvalidIdGeneratorException e) {
setStoreNotOk();
} finally {
if (!getStoreOk()) {
if (getConfig() != null) {
String storeDir = (String) getConfig().get("store_dir");
StringLogger msgLog = StringLogger.getLogger(storeDir);
msgLog.logMessage(getStorageFileName() + " non clean shutdown detected", true);
}
}
}
setWindowPool(new PersistenceWindowPool(getStorageFileName(), getRecordSize(), getFileChannel(), getMappedMem(), getIfMemoryMapped(), isReadOnly() && !isBackupSlave()));
}
use of org.neo4j.kernel.impl.util.StringLogger in project graphdb by neo4j-attic.
the class AbstractStore method rebuildIdGenerator.
/**
* Rebuilds the {@link IdGenerator} by looping through all records and
* checking if record in use or not.
*
* @throws IOException
* if unable to rebuild the id generator
*/
protected void rebuildIdGenerator() {
if (isReadOnly() && !isBackupSlave()) {
throw new ReadOnlyDbException();
}
logger.fine("Rebuilding id generator for[" + getStorageFileName() + "] ...");
closeIdGenerator();
File file = new File(getStorageFileName() + ".id");
if (file.exists()) {
boolean success = file.delete();
assert success;
}
createIdGenerator(getStorageFileName() + ".id");
openIdGenerator();
FileChannel fileChannel = getFileChannel();
long highId = 1;
long defraggedCount = 0;
try {
long fileSize = fileChannel.size();
int recordSize = getRecordSize();
boolean fullRebuild = true;
if (getConfig() != null) {
String mode = (String) getConfig().get("rebuild_idgenerators_fast");
if (mode != null && mode.toLowerCase().equals("true")) {
fullRebuild = false;
highId = findHighIdBackwards();
}
}
ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[1]);
// Duplicated code block
LinkedList<Long> freeIdList = new LinkedList<Long>();
if (fullRebuild) {
for (long i = 0; i * recordSize < fileSize && recordSize > 0; i++) {
fileChannel.position(i * recordSize);
fileChannel.read(byteBuffer);
byteBuffer.flip();
byte inUse = byteBuffer.get();
byteBuffer.flip();
nextId();
if ((inUse & 0x1) == Record.NOT_IN_USE.byteValue()) {
freeIdList.add(i);
} else {
highId = i;
while (!freeIdList.isEmpty()) {
freeId(freeIdList.removeFirst());
defraggedCount++;
}
}
}
}
} catch (IOException e) {
throw new UnderlyingStorageException("Unable to rebuild id generator " + getStorageFileName(), e);
}
setHighId(highId + 1);
if (getConfig() != null) {
String storeDir = (String) getConfig().get("store_dir");
StringLogger msgLog = StringLogger.getLogger(storeDir);
msgLog.logMessage(getStorageFileName() + " rebuild id generator, highId=" + getHighId() + " defragged count=" + defraggedCount, true);
}
logger.fine("[" + getStorageFileName() + "] high id=" + getHighId() + " (defragged=" + defraggedCount + ")");
closeIdGenerator();
openIdGenerator();
}
Aggregations