krati.core.array
Class SimpleDataArray

java.lang.Object
  extended by krati.core.array.SimpleDataArray
All Implemented Interfaces:
Array, DataArray, Persistable

public class SimpleDataArray
extends Object
implements DataArray, Persistable

SimpleDataArray: Simple Persistent DataArray. This class is not thread-safe by design. It is expected that the conditions below hold within one JVM.

    1. There is one and only one instance of SimpleDataArray for a given home directory.
    2. There is one and only one thread calling setData and transferTo methods at any given time. 
 
It is expected that this class is used in the case of multiple readers and single writer.

Author:
jwu

Field Summary
protected  AddressArray _addressArray
           
protected  AddressFormat _addressFormat
           
protected  krati.core.array.SimpleDataArrayCompactor _compactor
           
protected  Segment _segment
           
protected  double _segmentCompactFactor
           
protected  SegmentManager _segmentManager
           
 
Constructor Summary
SimpleDataArray(AddressArray addressArray, SegmentManager segmentManager)
          Constructs a DataArray with Segment Compact Factor default to 0.5.
SimpleDataArray(AddressArray addressArray, SegmentManager segmentManager, double segmentCompactFactor)
          Constructs a DataArray.
 
Method Summary
 void clear()
          Clears this Array.
protected  boolean consumeCompactionBatch()
           
protected  void consumeCompactionBatches()
           
protected  void decrOriginalSegmentLoad(int index)
           
 byte[] get(int index)
          Gets data at a given index.
 int get(int index, byte[] data)
          Gets data at a given index.
 int get(int index, byte[] data, int offset)
          Gets data at a given index.
protected  long getAddress(int index)
           
protected  LongArray getAddressArray()
           
protected  Segment getCurrentSegment()
           
 long getHWMark()
          Gets the high water mark.
 int getLength(int index)
           
 long getLWMark()
          Gets the low water mark.
protected  double getSegmentCompactFactor()
           
protected  SegmentManager getSegmentManager()
           
 boolean hasData(int index)
           
 boolean hasIndex(int index)
           
protected  void init()
           
 int length()
           
 void persist()
          Persist all updates from memory buffer into redo log files in non-blocking mode.
 void saveHWMark(long endOfPeriod)
          Save the high water mark.
 void set(int index, byte[] data, int offset, int length, long scn)
          Sets data at a given index.
 void set(int index, byte[] data, long scn)
          Sets data at a given index.
protected  void setAddress(int index, long value, long scn)
           
protected  void setCompactionAddress(int index, long value, long scn)
           
 void sync()
          Force all updates from memory buffer and redo log files to synchronize with the underlying persistent file in blocking mode.
protected  void syncCompactor()
           
 int transferTo(int index, WritableByteChannel channel)
          Transfers data at a given index to a writable channel.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_segment

protected volatile Segment _segment

_addressFormat

protected final AddressFormat _addressFormat

_addressArray

protected final AddressArray _addressArray

_segmentManager

protected final SegmentManager _segmentManager

_compactor

protected final krati.core.array.SimpleDataArrayCompactor _compactor

_segmentCompactFactor

protected final double _segmentCompactFactor
Constructor Detail

SimpleDataArray

public SimpleDataArray(AddressArray addressArray,
                       SegmentManager segmentManager)
Constructs a DataArray with Segment Compact Factor default to 0.5.

Parameters:
addressArray - the array of addresses (i.e. pointers to Segment).
segmentManager - the segment manager for loading, creating, freeing, maintaining segments.

SimpleDataArray

public SimpleDataArray(AddressArray addressArray,
                       SegmentManager segmentManager,
                       double segmentCompactFactor)
Constructs a DataArray.

Parameters:
addressArray - the array of addresses (i.e. pointers to Segment).
segmentManager - the segment manager for loading, creating, freeing, maintaining segments.
segmentCompactFactor - the load factor below which a segment is eligible for compaction. The recommended value is 0.5.
Method Detail

consumeCompactionBatch

protected boolean consumeCompactionBatch()

consumeCompactionBatches

protected void consumeCompactionBatches()

syncCompactor

protected void syncCompactor()

init

protected void init()

getAddress

protected long getAddress(int index)

setAddress

protected void setAddress(int index,
                          long value,
                          long scn)
                   throws Exception
Throws:
Exception

setCompactionAddress

protected void setCompactionAddress(int index,
                                    long value,
                                    long scn)
                             throws Exception
Throws:
Exception

getAddressArray

protected LongArray getAddressArray()

getSegmentCompactFactor

protected double getSegmentCompactFactor()

getSegmentManager

protected SegmentManager getSegmentManager()

getCurrentSegment

protected Segment getCurrentSegment()

decrOriginalSegmentLoad

protected void decrOriginalSegmentLoad(int index)

hasData

public boolean hasData(int index)
Specified by:
hasData in interface DataArray
Returns:
true if this array has data at the given index. Otherwise, false.
Throws:
ArrayIndexOutOfBoundsException - if the index is out of range.

getLength

public int getLength(int index)
Specified by:
getLength in interface DataArray
Returns:
the length of data at the given index. If the given index is out of the array index range, -1 is returned.

get

public byte[] get(int index)
Gets data at a given index.

Specified by:
get in interface DataArray
Parameters:
index - the array index
Returns:
the data at a given index.
Throws:
ArrayIndexOutOfBoundsException - if the index is out of range.

get

public int get(int index,
               byte[] data)
Gets data at a given index.

Specified by:
get in interface DataArray
Parameters:
index - the array index
data - the byte array to fill in
Returns:
the length of data at the given index.
Throws:
ArrayIndexOutOfBoundsException - if the index is out of range or if the byte array does not have enough space to hold the read data.

get

public int get(int index,
               byte[] data,
               int offset)
Gets data at a given index.

Specified by:
get in interface DataArray
Parameters:
index - the array index
data - the byte array to fill in
offset - the offset of the byte array where data is filled in
Returns:
the length of data at the given index.
Throws:
ArrayIndexOutOfBoundsException - if the index is out of range or if the byte array does not have enough space to hold the read data.

transferTo

public int transferTo(int index,
                      WritableByteChannel channel)
Transfers data at a given index to a writable channel.

Specified by:
transferTo in interface DataArray
Parameters:
index - the array index
channel - channel to transfer data to
Returns:
the amount of bytes transferred (the length of data at the given index).
Throws:
ArrayIndexOutOfBoundsException - if the index is out of range.

set

public void set(int index,
                byte[] data,
                long scn)
         throws Exception
Sets data at a given index.

Specified by:
set in interface DataArray
Parameters:
index - the array index
data - the data (byte array). If null, the data at the given index will be removed.
scn - the global scn indicating the sequence of this change
Throws:
ArrayIndexOutOfBoundsException - if the index is out of range.
Exception

set

public void set(int index,
                byte[] data,
                int offset,
                int length,
                long scn)
         throws Exception
Sets data at a given index.

Specified by:
set in interface DataArray
Parameters:
index - the array index
data - the data (byte array) If null, the data at the given index will be removed.
offset - the offset of byte array where data is read
length - the length of data to read from the byte array
scn - the global scn indicating the sequence of this change
Throws:
ArrayIndexOutOfBoundsException - if the index is out of range or if the offset and length is not properly specified.
Exception

hasIndex

public boolean hasIndex(int index)
Specified by:
hasIndex in interface Array
Returns:
a boolean indicating an index is in the range of this Array.

length

public int length()
Specified by:
length in interface Array
Returns:
the current length of this Array.

getHWMark

public long getHWMark()
Description copied from interface: Persistable
Gets the high water mark.

Specified by:
getHWMark in interface Persistable

getLWMark

public long getLWMark()
Description copied from interface: Persistable
Gets the low water mark.

Specified by:
getLWMark in interface Persistable

saveHWMark

public void saveHWMark(long endOfPeriod)
                throws Exception
Description copied from interface: Persistable
Save the high water mark.

Specified by:
saveHWMark in interface Persistable
Throws:
Exception

sync

public void sync()
          throws IOException
Description copied from interface: Persistable
Force all updates from memory buffer and redo log files to synchronize with the underlying persistent file in blocking mode.

Specified by:
sync in interface Persistable
Throws:
IOException

persist

public void persist()
             throws IOException
Description copied from interface: Persistable
Persist all updates from memory buffer into redo log files in non-blocking mode.

Specified by:
persist in interface Persistable
Throws:
IOException

clear

public void clear()
Description copied from interface: Array
Clears this Array.

Specified by:
clear in interface Array


Copyright © 2011. All Rights Reserved.