package krati.cds.impl.segment;

import java.io.File;
import java.io.IOException;
import java.nio.channels.WritableByteChannel;

/**
 * Segment.
 * 
 * The segment header uses the first 128 bytes.
 * Currently, only the first 16 bytes are used.
 * <pre>
 *   long    -- 8 bytes for lastForcedTime
 *   long    -- 8 bytes for storageVersion
 * </pre>
 * 
 * @author jwu
 *
 */
public interface Segment
{
    public final static long STORAGE_VERSION = 1;
    
    public final static int defaultSegmentFileSizeMB = 512;
    public final static int maxSegmentFileSizeMB = 2048;
    public final static int minSegmentFileSizeMB = 32;
    
    public final static int posLastForcedTime = 0;
    public final static int posStorageVersion = 8;
    
    /**
     * The data section starts at offset 128.
     */
    public final static int dataStartPosition = 128;
    
    public Mode getMode();
    
    public int getSegmentId();
    
    public File getSegmentFile();
    
    public long getInitialSize();
    
    public int getInitialSizeMB();
    
    public int getLoadSize();
    
    public double getLoadFactor();
    
    public void incrLoadSize(int byteCnt);
    
    public void decrLoadSize(int byteCnt);
    
    public long getAppendPosition() throws IOException;
    
    public void setAppendPosition(long pos) throws IOException;
    
    public int readInt(int pos) throws IOException;
    
    public long readLong(int pos) throws IOException;
    
    public short readShort(int pos) throws IOException;
    
    public void read(int pos, byte[] dst) throws IOException;
    
    public void read(int pos, byte[] dst, int offset, int length) throws IOException;
    
    public int appendInt(int value) throws IOException;
    
    public int appendLong(long value) throws IOException;
    
    public int appendShort(short value) throws IOException;
    
    public int append(byte[] data) throws IOException;
    
    public int append(byte[] data, int offset, int length) throws IOException;
    
    public long transferTo(long pos, int length, WritableByteChannel targetChannel) throws IOException;
    
    public boolean isReadOnly();
    
    public void asReadOnly() throws IOException;
    
    public void load() throws IOException;
    
    public void force() throws IOException;
    
    public void close(boolean force) throws IOException;
    
    /**
     * @return the last forced time in milliseconds since the epoch, January 01, 1970, 00:00:00 GMT.
     */
    public long getLastForcedTime();
    
    /**
     * @return the storage version of this segment.
     */
    public long getStorageVersion();
    
    public static enum Mode
    {
        READ_ONLY,
        READ_WRITE;
    }
}

@@ -136,17 +136,35 @@
  Segment
-.
+ Storage Version: 1
 %0A * %0A * 
@@ -182,16 +182,24 @@
  header 
+section 
 uses the
@@ -218,23 +218,13 @@
 ytes
-.%0A * Currently,
+, but
  onl
@@ -261,19 +261,148 @@
 %0A * 
-%3Cpr
+The segment data section stores varying-length data in the format of repeated %5Blength%5D%5Bdata ... ...%5D.%0A * %0A * Header Section:%0A * %3Ccod
 e%3E%0A *   
 long
@@ -393,24 +393,30 @@
 %3Ccode%3E%0A *   
+0x00  
 long    -- 8
@@ -422,20 +422,16 @@
 8 bytes 
-for 
 lastForc
@@ -446,53 +446,793 @@
 *   
-long    -- 8 bytes for storageVersion
+0x08  long    -- 8 bytes storageVersion%0A *   0x10  long    -- 8 bytes reserved%0A *   0x18  long    -- 8 bytes reserved%0A *   0x20  long    -- 8 bytes reserved%0A *   0x28  long    -- 8 bytes reserved%0A *   0x30  long    -- 8 bytes reserved%0A *   0x38  long    -- 8 bytes reserved%0A *   0x40  long    -- 8 bytes reserved%0A *   0x48  long    -- 8 bytes reserved%0A *   0x50  long    -- 8 bytes reserved%0A *   0x58  long    -- 8 bytes reserved%0A *   0x60  long    -- 8 bytes reserved%0A *   0x68  long    -- 8 bytes reserved%0A *   0x70  long    -- 8 bytes reserved%0A *   0x78  long    -- 8 bytes reserved%0A * %3C/code%3E%0A * %0A * Data Section:%0A * %3Ccode%3E%0A *   %5Blength1%5D%5Bdata1 ... ...%5D%0A *   %5Blength2%5D%5Bdata2 ... ... ... ... ... ...%5D%0A *   %5Blength3%5D%5Bdata3 ... ... ... ...%5D%0A *   %5Blength4%5D%5Bdata4 ...%5D%0A *   ...
 %0A * %3C/
-pr
+cod
 e%3E%0A 

document Segment Storage Version: 1