⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datapage.java

📁 非常棒的java数据库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
 * (http://h2database.com/html/license.html).
 * Initial Developer: H2 Group
 */
package org.h2.store;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;

import org.h2.constant.SysProperties;
import org.h2.engine.Constants;
import org.h2.message.Message;
import org.h2.util.MathUtils;
import org.h2.value.Value;
import org.h2.value.ValueArray;
import org.h2.value.ValueBoolean;
import org.h2.value.ValueByte;
import org.h2.value.ValueBytes;
import org.h2.value.ValueDate;
import org.h2.value.ValueDecimal;
import org.h2.value.ValueDouble;
import org.h2.value.ValueFloat;
import org.h2.value.ValueInt;
import org.h2.value.ValueJavaObject;
import org.h2.value.ValueLob;
import org.h2.value.ValueLong;
import org.h2.value.ValueNull;
import org.h2.value.ValueShort;
import org.h2.value.ValueString;
import org.h2.value.ValueStringFixed;
import org.h2.value.ValueStringIgnoreCase;
import org.h2.value.ValueTime;
import org.h2.value.ValueTimestamp;
import org.h2.value.ValueUuid;

/**
 * A data page is a byte buffer that contains persistent data of a row or index
 * page.
 */
public abstract class DataPage {

    static final boolean CHECKSUM = true;

    /**
     * The data handler responsible for lob objects.
     */
    protected DataHandler handler;

    /**
     * The data itself.
     */
    protected byte[] data;

    /**
     * The current write or read position.
     */
    protected int pos;

    /**
     * Calculate the checksum and write.
     *
     */
    public abstract void updateChecksum();

    /**
     * Test if the checksum is correct.
     *
     * @param len the number of bytes
     * @throws SQLException if the checksum does not match
     */
    public abstract void check(int len) throws SQLException;

    /**
     * The space required for the checksum and additional fillers.
     *
     * @return the size
     */
    public abstract int getFillerLength();

    /**
     * Update an integer at the given position.
     * The current position is not change.
     *
     * @param pos the position
     * @param x the value
     */
    public abstract void setInt(int pos, int x);

    /**
     * Write an integer at the current position.
     * The current position is incremented.
     *
     * @param x the value
     */
    public abstract void writeInt(int x);

    /**
     * Read an integer at the current position.
     * The current position is incremented.
     *
     * @return the value
     */
    public abstract int readInt();

    /**
     * Get the length of an integer value.
     *
     * @return the length
     */
    public abstract int getIntLen();

    /**
     * Get the length of a long value.
     *
     * @param x the value
     * @return the length
     */
    public abstract int getLongLen(long x);

    /**
     * Get the length of a String value.
     *
     * @param s the value
     * @return the length
     */
    public abstract int getStringLen(String s);

    /**
     * Read a String value.
     * The current position is incremented.
     *
     * @return the value
     */
    public abstract String readString();

    /**
     * Write a String value.
     * The current position is incremented.
     *
     * @param s the value
     */
    public abstract void writeString(String s);

    /**
     * Increase the size to the given length.
     * The current position is set to the given value.
     *
     * @param len the new length
     */
    public abstract void fill(int len);

    public static DataPage create(DataHandler handler, int capacity) {
        if (handler.getTextStorage()) {
            return new DataPageText(handler, new byte[capacity]);
        } else {
            return new DataPageBinary(handler, new byte[capacity]);
        }
    }

    public static DataPage create(DataHandler handler, byte[] buff) {
        if (handler.getTextStorage()) {
            return new DataPageText(handler, buff);
        } else {
            return new DataPageBinary(handler, buff);
        }
    }

    protected DataPage(DataHandler handler, byte[] data) {
        this.handler = handler;
        this.data = data;
    }

    public void checkCapacity(int plus) {
        if (pos + plus >= data.length) {
            byte[] d = new byte[(data.length + plus) * 2];
            // must copy everything, because pos could be 0 and data may be
            // still required
            System.arraycopy(data, 0, d, 0, data.length);
            data = d;
        }
    }

    public int length() {
        return pos;
    }

    public byte[] getBytes() {
        return data;
    }

    public void reset() {
        pos = 0;
    }

    public void writeDataPageNoSize(DataPage page) {
        checkCapacity(page.pos);
        // don't write filler
        int len = page.pos - getFillerLength();
        System.arraycopy(page.data, 0, data, pos, len);
        pos += len;
    }

    public DataPage readDataPageNoSize() {
        int len = data.length - pos;
        DataPage page = DataPage.create(handler, len);
        System.arraycopy(data, pos, page.data, 0, len);
        page.pos = len;
        return page;
    }

    public void write(byte[] buff, int off, int len) {
        checkCapacity(len);
        System.arraycopy(buff, 0, data, pos, len);
        pos += len;
    }

    public void read(byte[] buff, int off, int len) {
        System.arraycopy(data, pos, buff, off, len);
        pos += len;
    }

    public void writeByte(byte x) {
        data[pos++] = x;
    }

    public int readByte() {
        return data[pos++];
    }

    public long readLong() {
        return ((long) (readInt()) << 32) + (readInt() & 0xffffffffL);
    }

    public void writeLong(long x) {
        writeInt((int) (x >>> 32));
        writeInt((int) x);
    }

    public void writeValue(Value v) throws SQLException {
        if (SysProperties.CHECK) {
            checkCapacity(8);
        }
        // TODO text output: could be in the Value... classes
        if (v == ValueNull.INSTANCE) {
            data[pos++] = '-';
            return;
        }
        int start = pos;
        data[pos++] = (byte) (v.getType() + 'a');
        switch (v.getType()) {
        case Value.BOOLEAN:
        case Value.BYTE:
        case Value.SHORT:
        case Value.INT:
            writeInt(v.getInt());

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -