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

org.neo4j.kernel.impl.index.schema.BlockEntry 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 java.lang.String.format;
import static org.neo4j.util.Preconditions.checkState;

import org.neo4j.index.internal.gbptree.Layout;
import org.neo4j.io.pagecache.PageCursor;

/**
 * A {@link BlockEntry} is a key-value mapping and the smallest unit in the {@link BlockStorage} and {@link IndexUpdateStorage} hierarchy. Except for being a
 * container class for key-value pairs, it also provide static methods for serializing and deserializing {@link BlockEntry} instances and calculating total
 * store size of them.
 */
record BlockEntry(KEY key, VALUE value) {

    private static final int ENTRY_OVERHEAD = Short.BYTES * 2;

    @Override
    public String toString() {
        return format("[%s=%s]", key, value);
    }

    static  int entrySize(Layout layout, KEY key, VALUE value) {
        int keySize = layout.keySize(key);
        int valueSize = layout.valueSize(value);
        return keySize + valueSize + ENTRY_OVERHEAD;
    }

    static  int keySize(Layout layout, KEY key) {
        int keySize = layout.keySize(key);
        return keySize + ENTRY_OVERHEAD;
    }

    static  BlockEntry read(PageCursor pageCursor, Layout layout) {
        KEY key = layout.newKey();
        VALUE value = layout.newValue();
        read(pageCursor, layout, key, value);
        return new BlockEntry<>(key, value);
    }

    static  void read(PageCursor pageCursor, Layout layout, KEY key, VALUE value) {
        var keySize = pageCursor.getShort();
        var valueSize = pageCursor.getShort();
        layout.readKey(pageCursor, key, keySize);
        layout.readValue(pageCursor, value, valueSize);
    }

    static  void read(PageCursor pageCursor, Layout layout, KEY key) {
        var keySize = pageCursor.getShort();
        var valueSize = pageCursor.getShort();
        checkState(valueSize == 0, "Expected 0 value size");
        layout.readKey(pageCursor, key, keySize);
    }

    static  void write(PageCursor pageCursor, Layout layout, KEY key, VALUE value) {
        int keySize = layout.keySize(key);
        int valueSize = layout.valueSize(value);
        checkState(((short) keySize) == keySize, "Key size overflow");
        checkState(((short) valueSize) == valueSize, "Value size overflow");
        pageCursor.putShort((short) keySize);
        pageCursor.putShort((short) valueSize);
        layout.writeKey(pageCursor, key);
        layout.writeValue(pageCursor, value);
    }

    static  void write(PageCursor pageCursor, Layout layout, KEY key) {
        int keySize = layout.keySize(key);
        checkState(((short) keySize) == keySize, "Key size overflow");
        pageCursor.putShort((short) keySize);
        pageCursor.putShort((short) 0);
        layout.writeKey(pageCursor, key);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy