use of org.neo4j.kernel.impl.store.record.MetaDataRecord 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 File} pointing to the neostore.
* @param position record {@link Position}.
* @param value value to write in that record.
* @return the previous value before writing.
* @throws IOException if any I/O related error occurs.
*/
public static long setRecord(PageCache pageCache, File neoStore, Position position, long value) throws IOException {
long previousValue = FIELD_NOT_INITIALIZED;
int pageSize = getPageSize(pageCache);
try (PagedFile pagedFile = pageCache.map(neoStore, pageSize)) {
int offset = offset(position);
try (PageCursor cursor = pagedFile.io(0, PagedFile.PF_SHARED_WRITE_LOCK)) {
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.getAbsolutePath()));
}
}
}
}
return previousValue;
}
use of org.neo4j.kernel.impl.store.record.MetaDataRecord in project neo4j by neo4j.
the class MetaDataStore method setRecord.
private void setRecord(Position position, long value) {
long id = position.id;
// We need to do a little special handling of high id in neostore since it's not updated in the same
// way as other stores. Other stores always gets updates via commands where records are updated and
// the one making the update can also track the high id in the event of recovery.
// Here methods can be called directly, for example setLatestConstraintIntroducingTx where it's
// unclear from the outside which record id that refers to, so here we need to manage high id ourselves.
setHighestPossibleIdInUse(id);
MetaDataRecord record = new MetaDataRecord();
record.initialize(true, value);
record.setId(id);
updateRecord(record);
}
use of org.neo4j.kernel.impl.store.record.MetaDataRecord 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, FORCE, RECORD_SIZE);
if (record.inUse()) {
return record.getValue();
}
return defaultValue;
} catch (IOException e) {
throw new UnderlyingStorageException(e);
}
}
use of org.neo4j.kernel.impl.store.record.MetaDataRecord in project neo4j by neo4j.
the class MetaDataStoreTest method mustSupportScanningAllRecordsWithRecordCursor.
@Test
public void mustSupportScanningAllRecordsWithRecordCursor() throws Exception {
File file = createMetaDataFile();
MetaDataStore.Position[] positions = MetaDataStore.Position.values();
long storeVersion = versionStringToLong(Standard.LATEST_RECORD_FORMATS.storeVersion());
writeCorrectMetaDataRecord(file, positions, storeVersion);
List<Long> actualValues = new ArrayList<>();
try (MetaDataStore store = newMetaDataStore()) {
MetaDataRecord record = store.newRecord();
try (RecordCursor<MetaDataRecord> cursor = store.newRecordCursor(record)) {
cursor.acquire(0, RecordLoad.NORMAL);
long highId = store.getHighId();
for (long id = 0; id < highId; id++) {
if (cursor.next(id)) {
actualValues.add(record.getValue());
}
}
}
}
List<Long> expectedValues = Arrays.stream(positions).map(p -> {
if (p == MetaDataStore.Position.STORE_VERSION) {
return storeVersion;
} else {
return p.ordinal() + 1L;
}
}).collect(Collectors.toList());
assertThat(actualValues, is(expectedValues));
}
use of org.neo4j.kernel.impl.store.record.MetaDataRecord 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 File} pointing to the neostore.
* @param position record {@link Position}.
* @return the read record value specified by {@link Position}.
*/
public static long getRecord(PageCache pageCache, File neoStore, Position position) throws IOException {
MetaDataRecordFormat format = new MetaDataRecordFormat();
int pageSize = getPageSize(pageCache);
long value = FIELD_NOT_PRESENT;
try (PagedFile pagedFile = pageCache.map(neoStore, pageSize)) {
if (pagedFile.getLastPageId() >= 0) {
try (PageCursor cursor = pagedFile.io(0, PF_SHARED_READ_LOCK)) {
if (cursor.next()) {
MetaDataRecord record = new MetaDataRecord();
record.setId(position.id);
do {
format.read(record, cursor, RecordLoad.CHECK, 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.getAbsolutePath()));
}
}
}
}
}
return value;
}
Aggregations