/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.horizondb.model.protocol;

import io.horizondb.io.Buffer;
import io.horizondb.io.ByteReader;
import io.horizondb.io.ByteWriter;
import io.horizondb.io.buffers.Buffers;
import io.horizondb.io.encoding.VarInts;
import io.horizondb.io.serialization.Parser;

import java.io.IOException;

/**
 */
public final class InsertPayload implements Payload {
   
    /**
     * The parser instance.
     */
    private static final Parser<InsertPayload> PARSER = new Parser<InsertPayload>() {

        /**
         * {@inheritDoc}
         */
        @Override
        public InsertPayload parseFrom(ByteReader reader) throws IOException {

            String database = VarInts.readString(reader);
            String timeSeries = VarInts.readString(reader);
            int recordType = reader.readByte();
            int length = VarInts.readUnsignedInt(reader);
            Buffer buffer = Buffers.allocate(length);
            buffer.transfer(reader.slice(length));
            
            return new InsertPayload(database, timeSeries, recordType, buffer);
        }
    };
    
    /**
     * The name of the database in which the data must be inserted.
     */
    private final String database; 
    
    /**
     * The time series in which the data must be inserted.
     */
    private final String series;

    /**
     * The record type
     */
    private final int recordType;

    /**
     * The record data.
     */
    private final Buffer buffer;

    /**
     * Creates a new <code>InsertPayload</code>. 
     * 
     * @param database the database in which the data must be inserted
     * @param series the time series in which the data must be inserted
     * @param recordType the record type
     * @param buffer the record data
     */
    public InsertPayload(String database, String series, int recordType, Buffer buffer) {
        
        this.database = database;
        this.series = series;
        this.recordType = recordType;
        this.buffer = buffer;
    }

    /**
     * Returns the name of the database in which the data must be inserted.   
     * 
     * @return the name of the database in which the data must be inserted.  
     */
    public String getDatabase() {
        return this.database;
    }

    /**
     * Returns the name of the time series in which the data must be inserted.   
     *
     * @return the name of the time series in which the data must be inserted.  
     */
    public String getSeries() {
        return this.series;
    }

    /**
     * Returns the type of the record that must be inserted.
     * 
     * @return the type of the record that must be inserted.
     */
    public int getRecordType() {
        return this.recordType;
    }

    /**
     * Returns the record data
     *
     * @return the buffer the record data
     */
    public Buffer getBuffer() {
        return this.buffer;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int computeSerializedSize() {

        int length = this.buffer.readableBytes();
        return VarInts.computeStringSize(this.database) 
                + VarInts.computeStringSize(this.series) 
                + 1
                + VarInts.computeUnsignedIntSize(length)
                + length;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void writeTo(ByteWriter writer) throws IOException {

        VarInts.writeString(writer, this.database);
        VarInts.writeString(writer, this.series);
        VarInts.writeByte(writer, this.recordType);
        VarInts.writeUnsignedInt(writer, this.buffer.readableBytes());
        writer.transfer(this.buffer);
    }
    
    /**
     * Creates a new <code>InsertPayload</code> by reading the data from the specified reader.
     * 
     * @param reader the reader to read from.
     * @throws IOException if an I/O problem occurs
     */
    public static InsertPayload parseFrom(ByteReader reader) throws IOException {

        return getParser().parseFrom(reader);
    }

    /**
     * Returns the parser that can be used to deserialize <code>InsertPayload</code> instances.
     * @return the parser that can be used to deserialize <code>InsertPayload</code> instances.
     */
    public static Parser<InsertPayload> getParser() {

        return PARSER;
    }
}

@@ -687,24 +687,63 @@
 ByteWriter;%0A
+import io.horizondb.io.ReadableBuffer;%0A
 import io.ho
@@ -1557,16 +1557,31 @@
 length))
+.readerIndex(0)
 ;%0A      
@@ -3434,16 +3434,24 @@
  public 
+Readable
 Buffer g
@@ -3485,24 +3485,39 @@
  this.buffer
+.readerIndex(0)
 ;%0A    %7D%0A%0A   
@@ -3631,32 +3631,47 @@
 h = this.buffer.
+readerIndex(0).
 readableBytes();
@@ -4003,33 +4003,32 @@
 s IOException %7B%0A
-%0A
         VarInts.
@@ -4236,16 +4236,16 @@
 tes());%0A
-
         
@@ -4267,24 +4267,39 @@
 (this.buffer
+.readerIndex(0)
 );%0A    %7D%0A   

Fix serialization issue with InsertPayload