use of org.neo4j.exceptions.UnderlyingStorageException in project neo4j by neo4j.
the class MetaDataStore method transactionCommitted.
@Override
public void transactionCommitted(long transactionId, int checksum, long commitTimestamp, CursorContext cursorContext) {
assertNotClosed();
if (highestCommittedTransaction.offer(transactionId, checksum, commitTimestamp)) {
// 3. some other thread might kick in while we have been written only one record
synchronized (transactionCommittedLock) {
// acquiring this monitor.
if (highestCommittedTransaction.get().transactionId() == transactionId) {
long pageId = pageIdForRecord(Position.LAST_TRANSACTION_ID.id);
assert pageId == pageIdForRecord(Position.LAST_TRANSACTION_CHECKSUM.id);
try (PageCursor cursor = pagedFile.io(pageId, PF_SHARED_WRITE_LOCK, cursorContext)) {
if (cursor.next()) {
setRecord(cursor, Position.LAST_TRANSACTION_ID, transactionId);
setRecord(cursor, Position.LAST_TRANSACTION_CHECKSUM, checksum);
setRecord(cursor, Position.LAST_TRANSACTION_COMMIT_TIMESTAMP, commitTimestamp);
}
} catch (IOException e) {
throw new UnderlyingStorageException(e);
}
}
}
}
}
use of org.neo4j.exceptions.UnderlyingStorageException in project neo4j by neo4j.
the class MetaDataStore method setRecord.
/**
* Writes a record in a neostore file.
* This method only works for neostore files of the current version.
*
* @param pageCache {@link PageCache} the {@code neostore} file lives in.
* @param neoStore {@link Path} pointing to the neostore.
* @param position record {@link Position}.
* @param value value to write in that record.
* @param databaseName name of database this metadata store belongs to.
* @param cursorContext underlying page cursor context.
* @return the previous value before writing.
* @throws IOException if any I/O related error occurs.
*/
public static long setRecord(PageCache pageCache, Path neoStore, Position position, long value, String databaseName, CursorContext cursorContext) throws IOException {
long previousValue = FIELD_NOT_INITIALIZED;
int pageSize = pageCache.pageSize();
try (PagedFile pagedFile = pageCache.map(neoStore, pageSize, databaseName, immutable.empty())) {
int offset = offset(position);
try (PageCursor cursor = pagedFile.io(0, PagedFile.PF_SHARED_WRITE_LOCK, cursorContext)) {
if (cursor.next()) {
// We're overwriting a record, get the previous value
cursor.setOffset(offset);
byte inUse = cursor.getByte();
long record = cursor.getLong();
if (inUse == Record.IN_USE.byteValue()) {
previousValue = record;
}
// Write the value
cursor.setOffset(offset);
cursor.putByte(Record.IN_USE.byteValue());
cursor.putLong(value);
if (cursor.checkAndClearBoundsFlag()) {
MetaDataRecord neoStoreRecord = new MetaDataRecord();
neoStoreRecord.setId(position.id);
throw new UnderlyingStorageException(buildOutOfBoundsExceptionMessage(neoStoreRecord, 0, offset, RECORD_SIZE, pageSize, neoStore.toAbsolutePath().toString()));
}
}
}
}
return previousValue;
}
use of org.neo4j.exceptions.UnderlyingStorageException in project neo4j by neo4j.
the class MetaDataStore method getRecord.
/**
* Reads a record from a neostore file.
*
* @param pageCache {@link PageCache} the {@code neostore} file lives in.
* @param neoStore {@link Path} pointing to the neostore.
* @param position record {@link Position}.
* @param cursorContext underlying page cursor context.
* @return the read record value specified by {@link Position}.
*/
public static long getRecord(PageCache pageCache, Path neoStore, Position position, String databaseName, CursorContext cursorContext) throws IOException {
var recordFormat = new MetaDataRecordFormat();
int pageSize = pageCache.pageSize();
long value = FIELD_NOT_PRESENT;
try (PagedFile pagedFile = pageCache.map(neoStore, pageSize, databaseName, immutable.empty(), DISABLED)) {
if (pagedFile.getLastPageId() >= 0) {
try (PageCursor cursor = pagedFile.io(0, PF_SHARED_READ_LOCK, cursorContext)) {
if (cursor.next()) {
MetaDataRecord record = new MetaDataRecord();
record.setId(position.id);
do {
recordFormat.read(record, cursor, RecordLoad.CHECK, RECORD_SIZE, pageSize / RECORD_SIZE);
if (record.inUse()) {
value = record.getValue();
} else {
value = FIELD_NOT_PRESENT;
}
} while (cursor.shouldRetry());
if (cursor.checkAndClearBoundsFlag()) {
int offset = offset(position);
throw new UnderlyingStorageException(buildOutOfBoundsExceptionMessage(record, 0, offset, RECORD_SIZE, pageSize, neoStore.toAbsolutePath().toString()));
}
}
}
}
}
return value;
}
use of org.neo4j.exceptions.UnderlyingStorageException in project neo4j by neo4j.
the class MetaDataStore method getRecordValue.
private long getRecordValue(PageCursor cursor, Position position, long defaultValue) {
MetaDataRecord record = newRecord();
try {
record.setId(position.id);
recordFormat.read(record, cursor, ALWAYS, RECORD_SIZE, getRecordsPerPage());
if (record.inUse()) {
return record.getValue();
}
return defaultValue;
} catch (IOException e) {
throw new UnderlyingStorageException(e);
}
}
use of org.neo4j.exceptions.UnderlyingStorageException in project neo4j by neo4j.
the class MetaDataStore method incrementAndGetVersion.
private long incrementAndGetVersion(CursorContext cursorContext, Object lock, Position position) {
// This method can expect synchronisation at a higher level,
// and be effectively single-threaded.
long pageId = pageIdForRecord(position.id);
long version;
synchronized (lock) {
try (PageCursor cursor = pagedFile.io(pageId, PF_SHARED_WRITE_LOCK, cursorContext)) {
if (cursor.next()) {
version = incrementVersion(cursor, position);
} else {
throw new IllegalStateException("Filed " + position + "missing in metadata store. Page " + pageId + "not found.");
}
} catch (IOException e) {
throw new UnderlyingStorageException(e);
}
}
// make sure the new version value is persisted
flush(cursorContext);
return version;
}
Aggregations