/*
 * Copyright (c) 2016. emengjzs. All rights reserved.
 */

package emengjzs.emengdb.db;

/**
 * Created by emengjzs on 2016/12/20.
 */

import emengjzs.emengdb.util.Bits;

/**
 * InternalKey is used for the putting low-level key in skip list,
 * which contains of original user key and other meta data such as
 * the length of the key, seq number, etc.
 *
 * Here defines the encode and decode of the InternalKey.
 *
 */
public class InternalKeyCoder {

    /* if need to compress the metadata of key and value, e.g. key.length*/
    private boolean useZipMetaData = true;

    /* if need to add crc code of data */
    private boolean useCRCCheckData = true;


    public byte[] construct(long seq, ValueType type, byte[] key) {
        int byteLength = Integer.BYTES + key.length + Long.BYTES;
        byte[]  metaDataWithKeyBytes = new byte[byteLength];

        Bits.putInt(metaDataWithKeyBytes, 0, key.length);
        Bits.putBytes(metaDataWithKeyBytes, 0 + Long.BYTES, key);
        Bits.putLong(metaDataWithKeyBytes, 0 + key.length + Long.BYTES, seq << 8 | type.toByte());

        return metaDataWithKeyBytes;
    }


    public byte[] extractUserKey(byte[] internalKey) {
        return null;
    }

    long extractSeqAndType() {
        return 0;
    }

    long extractSeq() {
        return 0;
    }

}

@@ -168,16 +168,42 @@
 .Bits;%0A%0A
+import java.util.Arrays;%0A%0A
 /**%0A * I
@@ -549,16 +549,17 @@
 y.length
+ 
 */%0A    p
@@ -688,36 +688,413 @@
 e;%0A%0A
-%0A    public byte%5B%5D construct
+    private static final int NO_ZIP_USER_KEY_OFFSET = 0 + Integer.BYTES;%0A%0A    public InternalKeyCoder() %7B%0A%0A    %7D%0A%0A    /**%0A     * encode the key, the format shows as below%0A     * +-----------------+--------+------+-------+%0A     * %7C  key.length + 8 %7C   key  %7C  seq %7C  type %7C%0A     * +-----------------+--------+------+-------+%0A     *          4          key.len    7       1%0A     **/%0A    public byte%5B%5D encode
 (lon
@@ -1364,22 +1364,30 @@
 es, 
-0 + Long.BYTES
+NO_ZIP_USER_KEY_OFFSET
 , ke
@@ -1437,35 +1437,43 @@
 es, 
-0 + key.length + Long.BYTES
+NO_ZIP_USER_KEY_OFFSET + key.length
 , se
@@ -1565,22 +1565,270 @@
 e%5B%5D 
-extrac
+decodeUserKey(byte%5B%5D internalKey) %7B%0A        return Arrays.copyOfRange(%0A                internalKey,%0A                NO_ZIP_USER_KEY_OFFSET,%0A                NO_ZIP_USER_KEY_OFFSET + Bits.getInt(internalKey, 0)%0A                );%0A    %7D%0A%0A    public Slice ge
 tUserKey
 (byt
@@ -1819,24 +1819,29 @@
 e getUserKey
+Slice
 (byte%5B%5D inte
@@ -1871,116 +1871,516 @@
 rn n
-ull;%0A    %7D%0A%0A    long extractSeqAndType() %7B%0A        return 0;%0A    %7D%0A%0A    long extractSeq() %7B%0A        return 0
+ew Slice(internalKey, NO_ZIP_USER_KEY_OFFSET, Bits.getInt(internalKey, 0));%0A    %7D%0A%0A    public long decodeSeqAndType(byte%5B%5D internalKey) %7B%0A        return decodeSeqAndType(internalKey, Bits.getInt(internalKey, 0));%0A    %7D%0A%0A    long decodeSeqAndType(byte%5B%5D internalKey, int keyLength) %7B%0A        return Bits.getLong(internalKey, NO_ZIP_USER_KEY_OFFSET + keyLength);%0A    %7D%0A%0A    byte decodeTypeByte(byte%5B%5D internalKey, int keyLength) %7B%0A        return internalKey%5BNO_ZIP_USER_KEY_OFFSET + keyLength + Long.BYTES - 1%5D
 ;%0A  
@@ -2384,10 +2384,11 @@
 %0A    %7D%0A%0A
+%0A
 %7D%0A

Add decode method