All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.neo4j.kernel.impl.index.schema.NativeIndexUpdater Maven / Gradle / Ivy

Go to download

Neo4j kernel is a lightweight, embedded Java database designed to store data structured as graphs rather than tables. For more information, see http://neo4j.org.

There is a newer version: 5.25.1
Show newest version
/*
 * Copyright (c) "Neo4j"
 * Neo4j Sweden AB [https://neo4j.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package org.neo4j.kernel.impl.index.schema;

import static org.neo4j.kernel.impl.index.schema.NativeIndexKey.Inclusion.NEUTRAL;

import org.neo4j.index.internal.gbptree.Writer;
import org.neo4j.io.IOUtils;
import org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException;
import org.neo4j.kernel.api.index.IndexUpdater;
import org.neo4j.storageengine.api.IndexEntryUpdate;
import org.neo4j.storageengine.api.ValueIndexEntryUpdate;
import org.neo4j.values.storable.Value;

class NativeIndexUpdater> implements IndexUpdater {
    private final KEY treeKey;
    private final IndexUpdateIgnoreStrategy ignoreStrategy;
    private final ConflictDetectingValueMerger conflictDetectingValueMerger;
    private Writer writer;

    private boolean closed = true;

    NativeIndexUpdater(
            KEY treeKey,
            IndexUpdateIgnoreStrategy ignoreStrategy,
            ConflictDetectingValueMerger conflictDetectingValueMerger) {
        this.treeKey = treeKey;
        this.ignoreStrategy = ignoreStrategy;
        this.conflictDetectingValueMerger = conflictDetectingValueMerger;
    }

    NativeIndexUpdater initialize(Writer writer) {
        if (!closed) {
            throw new IllegalStateException("Updater still open");
        }

        this.writer = writer;
        closed = false;
        return this;
    }

    @Override
    public void process(IndexEntryUpdate update) throws IndexEntryConflictException {
        assertOpen();
        ValueIndexEntryUpdate valueUpdate = asValueUpdate(update);
        processUpdate(treeKey, valueUpdate, writer, conflictDetectingValueMerger, ignoreStrategy);
    }

    @Override
    public void close() {
        closed = true;
        IOUtils.closeAllUnchecked(writer);
    }

    private void assertOpen() {
        if (closed) {
            throw new IllegalStateException("Updater has been closed");
        }
    }

    @Override
    public void yield() {
        writer.yield();
    }

    static > void processUpdate(
            KEY treeKey,
            ValueIndexEntryUpdate update,
            Writer writer,
            ConflictDetectingValueMerger conflictDetectingValueMerger,
            IndexUpdateIgnoreStrategy ignoreStrategy)
            throws IndexEntryConflictException {
        switch (update.updateMode()) {
            case REMOVED:
                processRemove(treeKey, update.getEntityId(), update.values(), writer, ignoreStrategy);
                break;
            case CHANGED:
                processRemove(treeKey, update.getEntityId(), update.beforeValues(), writer, ignoreStrategy);
                // fallthrough
            case ADDED:
                processAdd(
                        treeKey,
                        update.getEntityId(),
                        update.values(),
                        writer,
                        conflictDetectingValueMerger,
                        ignoreStrategy);
                break;
            default:
                throw new IllegalArgumentException();
        }
    }

    private static > void processRemove(
            KEY treeKey,
            long entityId,
            Value[] values,
            Writer writer,
            IndexUpdateIgnoreStrategy ignoreStrategy) {
        if (ignoreStrategy.ignore(values)) {
            return;
        }
        // todo Do we need to verify that we actually removed something at all?
        // todo Difference between online and recovery?
        initializeKeyFromUpdate(treeKey, entityId, values);
        writer.remove(treeKey);
    }

    private static > void processAdd(
            KEY treeKey,
            long entityId,
            Value[] values,
            Writer writer,
            ConflictDetectingValueMerger conflictDetectingValueMerger,
            IndexUpdateIgnoreStrategy ignoreStrategy)
            throws IndexEntryConflictException {
        if (ignoreStrategy.ignore(values)) {
            return;
        }
        initializeKeyFromUpdate(treeKey, entityId, values);
        conflictDetectingValueMerger.controlConflictDetection(treeKey);
        writer.merge(treeKey, NullValue.INSTANCE, conflictDetectingValueMerger);
        conflictDetectingValueMerger.checkConflict(values);
    }

    static > void initializeKeyFromUpdate(KEY treeKey, long entityId, Value[] values) {
        treeKey.initialize(entityId);
        for (int i = 0; i < values.length; i++) {
            treeKey.initFromValue(i, values[i], NEUTRAL);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy