h2/src/main/org/h2/mvstore/Page.java
/**
     * Store the page and update the position.
     *
     * @param chunk the chunk
     * @param buff the target buffer
     */
Store the page and update the position.
@param chunk the chunk
@param buff the target buffer
Store the page and update the position . @param chunk the chunk @param buff the target buffer
private void write(Chunk chunk, WriteBuffer buff) {
        int start = buff.position();
        int len = keyCount;
        int type = children != null ? DataUtils.PAGE_TYPE_NODE
                : DataUtils.PAGE_TYPE_LEAF;
        buff.putInt(0).
            putShort((byte) 0).
            putVarInt(map.getId()).
            putVarInt(len).
            put((byte) type);
        int compressStart = buff.position();
        DataType keyType = map.getKeyType();
        for (int i = 0; i < len; i++) {
            keyType.write(buff, keys[i]);
        }
        if (type == DataUtils.PAGE_TYPE_NODE) {
            for (int i = 0; i <= len; i++) {
                buff.putLong(children[i]);
            }
            for (int i = 0; i <= len; i++) {
                buff.putVarLong(counts[i]);
            }
        } else {
            DataType valueType = map.getValueType();
            for (int i = 0; i < len; i++) {
                valueType.write(buff, values[i]);
            }
        }
        MVStore store = map.getStore();
        if (store.getCompress()) {
            Compressor compressor = map.getStore().getCompressor();
            int expLen = buff.position() - compressStart;
            byte[] exp = new byte[expLen];
            buff.position(compressStart).
                get(exp);
            byte[] comp = new byte[exp.length * 2];
            int compLen = compressor.compress(exp, exp.length, comp, 0);
            if (compLen + DataUtils.getVarIntLen(compLen - expLen) < expLen) {
                buff.position(compressStart - 1).
                    put((byte) (type + DataUtils.PAGE_COMPRESSED)).
                    putVarInt(expLen - compLen).
                    put(comp, 0, compLen);
            }
        }
        int pageLength = buff.position() - start;
        int chunkId = chunk.id;
        int check = DataUtils.getCheckValue(chunkId)
                ^ DataUtils.getCheckValue(start)
                ^ DataUtils.getCheckValue(pageLength);
        buff.putInt(start, pageLength).
            putShort(start + 4, (short) check);
        if (pos != 0) {
            throw DataUtils.newIllegalStateException(
                    DataUtils.ERROR_INTERNAL, "Page already stored");
        }
        pos = DataUtils.getPagePos(chunkId, start, pageLength, type);
        store.cachePage(pos, this, getMemory());
        long max = DataUtils.getPageMaxLength(pos);
        chunk.maxLength += max;
        chunk.maxLengthLive += max;
        chunk.pageCount++;
        chunk.pageCountLive++;
    }
private void write ( Chunk chunk , WriteBuffer buff ) { int start = buff . position ( ) ; int len = keyCount ; int type = children != null ? DataUtils . PAGE_TYPE_NODE : DataUtils . PAGE_TYPE_LEAF ; buff . putInt ( 0 ) . putShort ( ( byte ) 0 ) . putVarInt ( map . getId ( ) ) . putVarInt ( len ) . put ( ( byte ) type ) ; int compressStart = buff . position ( ) ; DataType keyType = map . getKeyType ( ) ; for ( int i = 0 ; i < len ; i ++ ) { keyType . write ( buff , keys [ i ] ) ; } if ( type == DataUtils . PAGE_TYPE_NODE ) { for ( int i = 0 ; i <= len ; i ++ ) { buff . putLong ( children [ i ] ) ; } for ( int i = 0 ; i <= len ; i ++ ) { buff . putVarLong ( counts [ i ] ) ; } } else { DataType valueType = map . getValueType ( ) ; for ( int i = 0 ; i < len ; i ++ ) { valueType . write ( buff , values [ i ] ) ; } } MVStore store = map . getStore ( ) ; if ( store . getCompress ( ) ) { Compressor compressor = map . getStore ( ) . getCompressor ( ) ; int expLen = buff . position ( ) - compressStart ; byte [ ] exp = new byte [ expLen ] ; buff . position ( compressStart ) . get ( exp ) ; byte [ ] comp = new byte [ exp . length * 2 ] ; int compLen = compressor . compress ( exp , exp . length , comp , 0 ) ; if ( compLen + DataUtils . getVarIntLen ( compLen - expLen ) < expLen ) { buff . position ( compressStart - 1 ) . put ( ( byte ) ( type + DataUtils . PAGE_COMPRESSED ) ) . putVarInt ( expLen - compLen ) . put ( comp , 0 , compLen ) ; } } int pageLength = buff . position ( ) - start ; int chunkId = chunk . id ; int check = DataUtils . getCheckValue ( chunkId ) ^ DataUtils . getCheckValue ( start ) ^ DataUtils . getCheckValue ( pageLength ) ; buff . putInt ( start , pageLength ) . putShort ( start + 4 , ( short ) check ) ; if ( pos != 0 ) { throw DataUtils . newIllegalStateException ( DataUtils . ERROR_INTERNAL , "Page already stored" ) ; } pos = DataUtils . getPagePos ( chunkId , start , pageLength , type ) ; store . cachePage ( pos , this , getMemory ( ) ) ; long max = DataUtils . getPageMaxLength ( pos ) ; chunk . maxLength += max ; chunk . maxLengthLive += max ; chunk . pageCount ++ ; chunk . pageCountLive ++ ; }
Store the page and update the position.
Store the page and update the position .