Search in sources :

Example 1 with MetaDataRecord

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;
}
Also used : PagedFile(org.neo4j.io.pagecache.PagedFile) MetaDataRecord(org.neo4j.kernel.impl.store.record.MetaDataRecord) PageCursor(org.neo4j.io.pagecache.PageCursor)

Example 2 with MetaDataRecord

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);
}
Also used : MetaDataRecord(org.neo4j.kernel.impl.store.record.MetaDataRecord)

Example 3 with MetaDataRecord

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);
    }
}
Also used : IOException(java.io.IOException) MetaDataRecord(org.neo4j.kernel.impl.store.record.MetaDataRecord)

Example 4 with MetaDataRecord

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));
}
Also used : Arrays(java.util.Arrays) PageCursor(org.neo4j.io.pagecache.PageCursor) TransactionIdStore(org.neo4j.kernel.impl.transaction.log.TransactionIdStore) System.currentTimeMillis(java.lang.System.currentTimeMillis) EphemeralFileSystemRule(org.neo4j.test.rule.fs.EphemeralFileSystemRule) PagedFile(org.neo4j.io.pagecache.PagedFile) LogProvider(org.neo4j.logging.LogProvider) NullLogProvider(org.neo4j.logging.NullLogProvider) ArrayList(java.util.ArrayList) DelegatingPageCache(org.neo4j.io.pagecache.DelegatingPageCache) Assert.assertThat(org.junit.Assert.assertThat) PageCacheRule(org.neo4j.test.rule.PageCacheRule) Assert.fail(org.junit.Assert.fail) BASE_TX_COMMIT_TIMESTAMP(org.neo4j.kernel.impl.transaction.log.TransactionIdStore.BASE_TX_COMMIT_TIMESTAMP) Before(org.junit.Before) Standard(org.neo4j.kernel.impl.store.format.standard.Standard) PageCache(org.neo4j.io.pagecache.PageCache) OpenOption(java.nio.file.OpenOption) MetaDataRecord(org.neo4j.kernel.impl.store.record.MetaDataRecord) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) MetaDataStore.versionStringToLong(org.neo4j.kernel.impl.store.MetaDataStore.versionStringToLong) File(java.io.File) EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Rule(org.junit.Rule) DelegatingPageCursor(org.neo4j.io.pagecache.impl.DelegatingPageCursor) Matchers.equalTo(org.hamcrest.Matchers.equalTo) NullLogger(org.neo4j.logging.NullLogger) Matchers.is(org.hamcrest.Matchers.is) DelegatingPagedFile(org.neo4j.io.pagecache.DelegatingPagedFile) Race.throwing(org.neo4j.test.Race.throwing) PageCacheRule.config(org.neo4j.test.rule.PageCacheRule.config) RecordLoad(org.neo4j.kernel.impl.store.record.RecordLoad) Race(org.neo4j.test.Race) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Assert.assertEquals(org.junit.Assert.assertEquals) ArrayList(java.util.ArrayList) MetaDataStore.versionStringToLong(org.neo4j.kernel.impl.store.MetaDataStore.versionStringToLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) PagedFile(org.neo4j.io.pagecache.PagedFile) File(java.io.File) DelegatingPagedFile(org.neo4j.io.pagecache.DelegatingPagedFile) MetaDataRecord(org.neo4j.kernel.impl.store.record.MetaDataRecord) Test(org.junit.Test)

Example 5 with MetaDataRecord

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;
}
Also used : PagedFile(org.neo4j.io.pagecache.PagedFile) MetaDataRecordFormat(org.neo4j.kernel.impl.store.format.standard.MetaDataRecordFormat) MetaDataRecord(org.neo4j.kernel.impl.store.record.MetaDataRecord) PageCursor(org.neo4j.io.pagecache.PageCursor)

Aggregations

MetaDataRecord (org.neo4j.kernel.impl.store.record.MetaDataRecord)5 PageCursor (org.neo4j.io.pagecache.PageCursor)3 PagedFile (org.neo4j.io.pagecache.PagedFile)3 IOException (java.io.IOException)2 File (java.io.File)1 System.currentTimeMillis (java.lang.System.currentTimeMillis)1 OpenOption (java.nio.file.OpenOption)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 SECONDS (java.util.concurrent.TimeUnit.SECONDS)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Collectors (java.util.stream.Collectors)1 Matchers.equalTo (org.hamcrest.Matchers.equalTo)1 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)1 Matchers.is (org.hamcrest.Matchers.is)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertThat (org.junit.Assert.assertThat)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 Assert.fail (org.junit.Assert.fail)1