Search in sources :

Example 1 with IndexEntryConflictException

use of org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException in project neo4j by neo4j.

the class LuceneSchemaIndexUniquenessVerificationIT method verifyUniqueness.

private void verifyUniqueness(Collection<PropertyValue> data) throws IOException, IndexEntryConflictException {
    Object[] propertyValues = data.stream().map(property -> property.value).toArray();
    PropertyAccessor propertyAccessor = new TestPropertyAccessor(propertyValues);
    index.verifyUniqueness(propertyAccessor, new int[] { PROPERTY_KEY_ID });
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) SchemaIndex(org.neo4j.kernel.api.impl.schema.SchemaIndex) IOUtils(org.neo4j.io.IOUtils) Exceptions(org.neo4j.helpers.Exceptions) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) Property(org.neo4j.kernel.api.properties.Property) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) LuceneDocumentStructure(org.neo4j.kernel.api.impl.schema.LuceneDocumentStructure) Document(org.apache.lucene.document.Document) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException) Randoms(org.neo4j.test.Randoms) LuceneSchemaIndexBuilder(org.neo4j.kernel.api.impl.schema.LuceneSchemaIndexBuilder) After(org.junit.After) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Strings(org.neo4j.helpers.Strings) Assert.fail(org.junit.Assert.fail) ArrayUtil(org.neo4j.helpers.ArrayUtil) Parameterized(org.junit.runners.Parameterized) Collectors.toSet(java.util.stream.Collectors.toSet) Before(org.junit.Before) Parameter(org.junit.runners.Parameterized.Parameter) Collection(java.util.Collection) Set(java.util.Set) NewIndexDescriptorFactory(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptorFactory) TestDirectory(org.neo4j.test.rule.TestDirectory) Test(org.junit.Test) IOException(java.io.IOException) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) PropertyAccessor(org.neo4j.kernel.api.index.PropertyAccessor) Objects(java.util.Objects) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) Rule(org.junit.Rule) DefaultFileSystemRule(org.neo4j.test.rule.fs.DefaultFileSystemRule) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) PropertyAccessor(org.neo4j.kernel.api.index.PropertyAccessor)

Example 2 with IndexEntryConflictException

use of org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException in project neo4j by neo4j.

the class SimpleUniquenessVerifier method verify.

@Override
public void verify(PropertyAccessor accessor, int[] propKeyIds) throws IndexEntryConflictException, IOException {
    try {
        DuplicateCheckingCollector collector = DuplicateCheckingCollector.forProperties(accessor, propKeyIds);
        IndexSearcher searcher = indexSearcher();
        for (LeafReaderContext leafReaderContext : searcher.getIndexReader().leaves()) {
            Fields fields = leafReaderContext.reader().fields();
            for (String field : fields) {
                if (LuceneDocumentStructure.NODE_ID_KEY.equals(field)) {
                    continue;
                }
                TermsEnum terms = LuceneDocumentStructure.originalTerms(fields.terms(field), field);
                BytesRef termsRef;
                while ((termsRef = terms.next()) != null) {
                    if (terms.docFreq() > 1) {
                        collector.reset();
                        searcher.search(new TermQuery(new Term(field, termsRef)), collector);
                    }
                }
            }
        }
    } catch (IOException e) {
        Throwable cause = e.getCause();
        if (cause instanceof IndexEntryConflictException) {
            throw (IndexEntryConflictException) cause;
        }
        throw e;
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) Fields(org.apache.lucene.index.Fields) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException) BytesRef(org.apache.lucene.util.BytesRef) TermsEnum(org.apache.lucene.index.TermsEnum)

Example 3 with IndexEntryConflictException

use of org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException in project neo4j by neo4j.

the class ConstraintEnforcingEntityOperations method validateNoExistingNodeWithExactValues.

private void validateNoExistingNodeWithExactValues(KernelStatement state, UniquenessConstraintDescriptor constraint, ExactPredicate[] propertyValues, long modifiedNode) throws ConstraintValidationException {
    try {
        NewIndexDescriptor index = constraint.ownedIndexDescriptor();
        assertIndexOnline(state, index);
        int labelId = index.schema().getLabelId();
        state.locks().optimistic().acquireExclusive(state.lockTracer(), INDEX_ENTRY, indexEntryResourceId(labelId, propertyValues));
        long existing = entityReadOperations.nodeGetFromUniqueIndexSeek(state, index, propertyValues);
        if (existing != NO_SUCH_NODE && existing != modifiedNode) {
            throw new UniquePropertyValueValidationException(constraint, VALIDATION, new IndexEntryConflictException(existing, NO_SUCH_NODE, OrderedPropertyValues.of(propertyValues)));
        }
    } catch (IndexNotFoundKernelException | IndexBrokenKernelException | IndexNotApplicableKernelException e) {
        throw new UnableToValidateConstraintException(constraint, e);
    }
}
Also used : UniquePropertyValueValidationException(org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException) IndexNotApplicableKernelException(org.neo4j.kernel.api.exceptions.index.IndexNotApplicableKernelException) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) UnableToValidateConstraintException(org.neo4j.kernel.api.exceptions.schema.UnableToValidateConstraintException) IndexBrokenKernelException(org.neo4j.kernel.api.exceptions.schema.IndexBrokenKernelException) IndexNotFoundKernelException(org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException)

Example 4 with IndexEntryConflictException

use of org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException in project neo4j by neo4j.

the class BatchInserterImpl method shutdown.

@Override
public void shutdown() {
    if (isShutdown) {
        throw new IllegalStateException("Batch inserter already has shutdown");
    }
    isShutdown = true;
    flushStrategy.forceFlush();
    rebuildCounts();
    try {
        repopulateAllIndexes();
    } catch (IOException | IndexEntryConflictException e) {
        throw new RuntimeException(e);
    } finally {
        cursors.close();
        neoStores.close();
        try {
            storeLocker.close();
        } catch (IOException e) {
            throw new UnderlyingStorageException("Could not release store lock", e);
        }
        msgLog.info(Thread.currentThread() + " Clean shutdown on BatchInserter(" + this + ")", true);
        life.shutdown();
    }
}
Also used : IOException(java.io.IOException) UnderlyingStorageException(org.neo4j.kernel.impl.store.UnderlyingStorageException) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException)

Example 5 with IndexEntryConflictException

use of org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException in project neo4j by neo4j.

the class Operations method validateNoExistingNodeWithExactValues.

/**
 * Check so that there is not an existing node with the exact match of label and property
 */
private void validateNoExistingNodeWithExactValues(IndexBackedConstraintDescriptor constraint, PropertyIndexQuery.ExactPredicate[] propertyValues, long modifiedNode) throws UniquePropertyValueValidationException, UnableToValidateConstraintException {
    IndexDescriptor index = allStoreHolder.indexGetForName(constraint.getName());
    try (FullAccessNodeValueIndexCursor valueCursor = cursors.allocateFullAccessNodeValueIndexCursor(ktx.cursorContext(), memoryTracker);
        IndexReaders indexReaders = new IndexReaders(index, allStoreHolder)) {
        assertIndexOnline(index);
        SchemaDescriptor schema = index.schema();
        long[] labelIds = schema.lockingKeys();
        if (labelIds.length != 1) {
            throw new UnableToValidateConstraintException(constraint, new AssertionError(format("Constraint indexes are not expected to be multi-token indexes, " + "but the constraint %s was referencing an index with the following schema: %s.", constraint.userDescription(token), schema.userDescription(token))), token);
        }
        // Take a big fat lock, and check for existing node in index
        ktx.lockClient().acquireExclusive(ktx.lockTracer(), INDEX_ENTRY, indexEntryResourceId(labelIds[0], propertyValues));
        allStoreHolder.nodeIndexSeekWithFreshIndexReader(valueCursor, indexReaders.createReader(), propertyValues);
        if (valueCursor.next() && valueCursor.nodeReference() != modifiedNode) {
            throw new UniquePropertyValueValidationException(constraint, VALIDATION, new IndexEntryConflictException(valueCursor.nodeReference(), NO_SUCH_NODE, PropertyIndexQuery.asValueTuple(propertyValues)), token);
        }
    } catch (IndexNotFoundKernelException | IndexBrokenKernelException | IndexNotApplicableKernelException e) {
        throw new UnableToValidateConstraintException(constraint, e, token);
    }
}
Also used : RelationTypeSchemaDescriptor(org.neo4j.internal.schema.RelationTypeSchemaDescriptor) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) UniquePropertyValueValidationException(org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException) UnableToValidateConstraintException(org.neo4j.kernel.api.exceptions.schema.UnableToValidateConstraintException) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) IndexNotApplicableKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotApplicableKernelException) IndexBrokenKernelException(org.neo4j.kernel.api.exceptions.schema.IndexBrokenKernelException) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException)

Aggregations

IndexEntryConflictException (org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException)21 IOException (java.io.IOException)9 Test (org.junit.jupiter.api.Test)5 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)5 UniquePropertyValueValidationException (org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException)5 TermQuery (org.apache.lucene.search.TermQuery)4 IndexUpdater (org.neo4j.kernel.api.index.IndexUpdater)4 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)4 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 Collection (java.util.Collection)3 List (java.util.List)3 Test (org.junit.Test)3 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 Set (java.util.Set)2 Query (org.apache.lucene.search.Query)2