step.encoding
Class CacheStrategy

java.lang.Object
  |
  +--step.encoding.Encoder
        |
        +--step.encoding.Strategy
              |
              +--step.encoding.RegularValueStrategy
                    |
                    +--step.encoding.CacheStrategy
Direct Known Subclasses:
PCacheStrategy

public class CacheStrategy
extends RegularValueStrategy

Use a simple caching strategy to replace values with a reference to a cache slot. When a miss occurs, the strategy just replaces the next slot (on a rotational basis) with the new value.

It is possible to chain cache strategies to get a multi-level exclusive cache hierarchy.

NOTE: If the cache has a lot of misses, it will likely have a negative effect on overall reduction; introducing lots of meta-data and artificial randomness (as values get mapped to different slots).


Field Summary
protected  StepObject[] _cache
           
protected  StepInt[] _slots
           
protected  Map _valueToSlot
           
static String STRATEGY_NAME
           
 
Fields inherited from class step.encoding.RegularValueStrategy
_nextIsRegular
 
Fields inherited from class step.encoding.Strategy
_baseEncoder
 
Constructor Summary
CacheStrategy(Encoder baseEncoder, int cacheSize)
          Create a new "cache" strategy.
 
Method Summary
 StepObject decode(DecodeContext context)
          Decode a value using its cache slot ID.
 void dump(int level)
           
 void encode(StepObject o, EncodeContext context)
          Encode a value using its cache slot ID.
protected  StepInt nextSlot()
          Rotate to the next cache slot.
 
Methods inherited from class step.encoding.RegularValueStrategy
setNextIsRegular
 
Methods inherited from class step.encoding.Strategy
baseDecode, baseEncode, eventMessage
 
Methods inherited from class step.encoding.Encoder
getFactory, getName, indent, makeFieldName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STRATEGY_NAME

public static final String STRATEGY_NAME
See Also:
Constant Field Values

_cache

protected StepObject[] _cache

_slots

protected StepInt[] _slots

_valueToSlot

protected Map _valueToSlot
Constructor Detail

CacheStrategy

public CacheStrategy(Encoder baseEncoder,
                     int cacheSize)
Create a new "cache" strategy.

Parameters:
baseEncoder - the encoder to use for cache misses.
cacheSize - the number of elements to keep in the cache.
Method Detail

encode

public void encode(StepObject o,
                   EncodeContext context)
            throws EncoderException,
                   IOException
Encode a value using its cache slot ID.

Specified by:
encode in class Encoder
Throws:
IOException - if the encode operation encountered an I/O failure.
EncoderException - if the encode operation encountered an error.

decode

public StepObject decode(DecodeContext context)
                  throws EncoderException,
                         IOException
Decode a value using its cache slot ID.

Specified by:
decode in class Encoder
Throws:
IOException - if the decode operation encountered an I/O failure.
EncoderException - if the decode operation encountered an error.

nextSlot

protected StepInt nextSlot()
Rotate to the next cache slot.


dump

public void dump(int level)
Specified by:
dump in class Encoder