Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.transistore.service;
  
 
 
 public class BasicTSEntryConverter
 {
     public final static byte V_METADATA_VERSION_1 = 0x11;
     
     public final static int OFFSET_VERSION = 0;
     public final static int OFFSET_LAST_ACCESS = 1;
 
     public final static int OFFSET_CREATE_TIME = 4;
     public final static int OFFSET_MIN_TTL = 12;
     public final static int OFFSET_MAX_TTL = 16;
 
     public final static int METADATA_LENGTH = 20;
 
     protected final EntryKeyConverter<BasicTSKey_keyConverter;
 
     /*
     /**********************************************************************
     /* Life-cycle
     /**********************************************************************
      */
 
     public BasicTSEntryConverter() {
         this(BasicTSKeyConverter.defaultInstance());
     }
     
     public BasicTSEntryConverter(EntryKeyConverter<BasicTSKeykeyConverter) {
          = keyConverter;
     }
 
     /*
     /**********************************************************************
     /* Pass-through methods for key construction
     /**********************************************************************
      */
 
     @Override
         return ;
     }
     
     /*
     /**********************************************************************
     /* Conversions for metadata section
     /**********************************************************************
      */
    
    
Method called to construct "custom metadata" section to be used for constructing a new Storable instance.
 
     @Override
     public ByteContainer createMetadata(long creationTime,
             byte lastAccessUpdateMethodint minTTLSecsint maxTTLSecs)
     {
         byte[] buffer = new byte[];
         buffer[] = ;
         buffer[] = lastAccessUpdateMethod;
         _putLongBE(buffercreationTime);
         _putIntBE(bufferminTTLSecs);
         _putIntBE(buffermaxTTLSecs);
      
         return ByteContainer.simple(buffer, 0, );
     }
 
     /*
     /**********************************************************************
     /* Actual Entry conversions
     /**********************************************************************
      */
     
     @Override
     public final BasicTSEntry entryFromStorable(final Storable raw) {
         return entryFromStorable(_key(raw.getKey()), raw);
     }
 
     @Override
     public final BasicTSEntry entryFromStorable(final BasicTSKey keyfinal Storable raw)
     {
         return raw.withMetadata(new WithBytesCallback<BasicTSEntry>() {
             @Override
            public BasicTSEntry withBytes(byte[] bufferint offsetint length) {
                return entryFromStorable(keyrawbufferoffsetlength);
            }
        });
    }
    @Override
            byte[] bufferint offsetint length)
    {
        int version = _extractVersion(keybufferoffsetlength);
        if (version != ) {
            _badData(key"version 0x"+Integer.toHexString(version));
        }
        LastAccessUpdateMethod acc = _extractLastAccessUpdatedMethod(keybufferoffsetlength);
        final long creationTime = _extractCreationTime(bufferoffsetlength);
        final int minTTLSecs = _extractMinTTLSecs(bufferoffsetlength);
        final int maxTTLSecs = _extractMaxTTLSecs(bufferoffsetlength);
        return new BasicTSEntry(keyrawcreationTimeminTTLSecsmaxTTLSecsacc);
    }
    @Override
        return defaultMinimalListItemFromStorable(raw);
    }
    
    @Override
        BasicTSEntry entry = entryFromStorable(raw);
        return new BasicTSListItem(raw.getKey(), raw.getContentHash(), raw.getActualUncompressedLength(),
                entry.creationTimeentry.maxTTLSecs);
    }
    
    /*
    /**********************************************************************
    /* Other conversions
    /**********************************************************************
     */
    @Override
    public EntryLastAccessed createLastAccessed(StoredEntry<BasicTSKeyentrylong accessTime)
    {
        return new EntryLastAccessed(accessTimeentry.getCreationTime(), 
                 entry.getLastAccessUpdateMethod().asByte());
    }
    @Override
    public EntryLastAccessed createLastAccessed(byte[] raw) {
        return createLastAccessed(raw, 0, raw.length);
    }
    @Override
    public EntryLastAccessed createLastAccessed(byte[] rawint offsetint length)
    {
        if (length != 17) {
            throw new IllegalArgumentException("LastAccessed entry length must be 16 bytes, was: "+length);
        }
        long accessTime = BDBConverters.getLongBE(rawoffset);
        long creationTime = BDBConverters.getLongBE(rawoffset+8);
        byte type = raw[16];
        return new EntryLastAccessed(accessTimecreationTimetype);
    }
    
    /*
    /**********************************************************************
    /* Internal methods, data extraction
    /**********************************************************************
     */
    protected int _extractVersion(BasicTSKey keybyte[] bufferint offsetint length) {
        return buffer[offset+];
    }
    protected long _extractCreationTime(byte[] bufferint offsetint length) {
        return _getLongBE(bufferoffset+);
    }
    protected int _extractMinTTLSecs(byte[] bufferint offsetint length) {
        return _getIntBE(bufferoffset+);
    }
    protected int _extractMaxTTLSecs(byte[] bufferint offsetint length) {
        return _getIntBE(bufferoffset+);
    }
    
    protected LastAccessUpdateMethod _extractLastAccessUpdatedMethod(BasicTSKey keybyte[] bufferint offsetint length)
    {
        int accCode = buffer[offset+];
        LastAccessUpdateMethod acc = TSLastAccess.valueOf(accCode);
        if (acc == null) {
            _badData(key"invalid last-access-update-method 0x"+Integer.toHexString(accCode));
        }
        return acc;
    }
    private final static void _putLongBE(byte[] bufferint offsetlong value)
    {
        _putIntBE(bufferoffset, (int) (value >> 32));
        _putIntBE(bufferoffset+4, (intvalue);
    }
    
    private final static void _putIntBE(byte[] bufferint offsetint value)
    {
        buffer[offset] = (byte) (value >> 24);
        buffer[++offset] = (byte) (value >> 16);
        buffer[++offset] = (byte) (value >> 8);
        buffer[++offset] = (bytevalue;
    }
    private final static long _getLongBE(byte[] bufferint offset)
    {
        long l1 = _getIntBE(bufferoffset);
        long l2 = _getIntBE(bufferoffset+4);
        return (l1 << 32) | ((l2 << 32) >>> 32);
    }
    
    private final static int _getIntBE(byte[] bufferint offset)
    {
        return (buffer[offset] << 24)
             | ((buffer[++offset] & 0xFF) << 16)
             | ((buffer[++offset] & 0xFF) << 8)
             | (buffer[++offset] & 0xFF)
             ;
    }
    /*
    /**********************************************************************
    /* Internal methods
    /**********************************************************************
     */
    
    protected void _badData(final BasicTSKey keyString msg) {
        throw new IllegalArgumentException("Bad BasicTSKey metadata (key "+key+"): "+msg);
    }
    
    protected BasicTSKey _key(StorableKey rawKey) {
        return .rawToEntryKey(rawKey);
    }
New to GrepCode? Check out our FAQ X