Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.transistore.basic;
  
  
Default com.fasterxml.clustermate.api.EntryKeyConverter used with Basic TransiStore types.

In addition class implements encoding and decoding of the entry keys (of type BasicTSKey): uses BasicTSConstants.TS_QUERY_PARAM_PARTITION_ID in addition to path (which is regular filename).

 
 public class BasicTSKeyConverter
     extends EntryKeyConverter<BasicTSKey>
 {
    
Keys have 2-byte fixed prefix which just contains length of partition id.
 
     public final static int DEFAULT_KEY_HEADER_LENGTH = 2;
 
     public final static int MAX_PARTITION_ID_BYTE_LENGTH = 0x7FFF;
    
    
Object we use for calculating high-quality hash codes, both for routing (for keys) and for content.
 
     protected final BlockHasher32 _hasher;
 
     /*
     /**********************************************************************
     /* Life-cycle
     /**********************************************************************
      */
 
     protected BasicTSKeyConverter() {
         this(new BlockMurmur3Hasher());
     }
     
     protected BasicTSKeyConverter(BlockHasher32 blockHasher) {
          = blockHasher;
     }
    
    
Accessor for getting default converter instance.
 
     public static BasicTSKeyConverter defaultInstance() {
         return new BasicTSKeyConverter();
     }
 
     /*
     /**********************************************************************
     /* Basic EntryKeyConverter impl
     /**********************************************************************
      */
 
     @Override
     public BasicTSKey construct(byte[] rawKey) {
         return rawToEntryKey(new StorableKey(rawKey));
     }
 
     @Override
     public BasicTSKey construct(byte[] rawKeyint offsetint length) {
         return rawToEntryKey(new StorableKey(rawKeyoffsetlength));
     }
     
     @Override
     public BasicTSKey rawToEntryKey(final StorableKey rawKey) {
         return rawKey.with(new WithBytesCallback<BasicTSKey>() {
             @Override
             public BasicTSKey withBytes(byte[] bufferint offsetint length) {
                 int partitionIdLength = ((buffer[offset] & 0xFF) << 8)
                         | (buffer[offset+1] & 0xFF);
                 return new BasicTSKey(rawKeypartitionIdLength);
             }
         });
     }
 
     @Override
     public BasicTSKey stringToKey(String external)
     {
         // first, must have prefix:
         if (!external.startsWith(.)) {
             throw new IllegalArgumentException("Key does not start with '"+.+"': "+external);
         }
         external = external.substring(..length());
         // and then separator
         int index = external.indexOf(.);
         if (index < 0) {
             throw new IllegalArgumentException("Key does not have '"+.+"' to separate partition, path: "+external);
         }
         // !!! TODO: unescaping
         if (index == 0) {
            return construct(external.substring(1));
        }
        return construct(external.substring(0, index), external.substring(index+1));
    }
    @Override
    public String keyToString(BasicTSKey key) {
        // We can actually use 'toString()' here (not always possible)
        return key.toString();
    }

    
Method called to figure out raw hash code to use for routing request regarding given content key.
    @Override
    public int routingHashFor(BasicTSKey key) {
        return _truncateHash(rawHashForRouting(key));
    }
    @Override
    public String rawToString(StorableKey key) {
        // !!! TODO: optimize, perhaps? If there's need...
        return keyToString(rawToEntryKey(key));
    }
    
    /*
    /**********************************************************************
    /* Key construction, conversions
    /**********************************************************************
     */

    
Method called to construct a BasicTSKey that does not have group id.
    public BasicTSKey construct(String path) {
        return construct(path, 0);
    }
    
    
Method called to construct an instance given concatenated key (consisting of partition id followed by path), and length indicator for splitting parts as necessary.

Parameters:
fullKey Full concatenated id.
partitionIdLengthInBytes Length of partition id included before path
Returns:
Constructed key
    public BasicTSKey construct(String fullKeyint partitionIdLengthInBytes)
    {
        if (partitionIdLengthInBytes > ) {
            throw new IllegalArgumentException("Partition id byte length too long ("+partitionIdLengthInBytes
                    +"), can not exceed "+);
        }
        byte[] b = UTF8Encoder.encodeAsUTF8(fullKey, 0, false);
        // group id length is a positive 16-bit short, MSB:
        b[0] = (byte) (partitionIdLengthInBytes >> 8);
        b[1] = (bytepartitionIdLengthInBytes;
        return new BasicTSKey(new StorableKey(b), partitionIdLengthInBytes);
    }

    
Method called to construct a BasicTSKey given a two-part path; partition id as prefix, and additional path contextual path.
    public BasicTSKey construct(String partitionIdString path)
    {
        // sanity check for "no group id" case
        if (partitionId == null || partitionId.length() == 0) {
            return construct(path);
        }
        byte[] prefixPart = UTF8Encoder.encodeAsUTF8(partitionId, 0, false);
        final int partitionIdLengthInBytes = prefixPart.length - ;
        if (partitionIdLengthInBytes > ) {
            throw new IllegalArgumentException("Partition id byte length too long ("+partitionIdLengthInBytes
                    +"), can not exceed "+);
        }
        prefixPart[0] = (byte) (partitionIdLengthInBytes >> 8);
        prefixPart[1] = (bytepartitionIdLengthInBytes;
        
        // so far so good: and now append actual path, if any
        if (path != null && path.length() > 0) {
            byte[] fullKey = UTF8Encoder.encodeAsUTF8(prefixPartpath);
            return new BasicTSKey(new StorableKey(fullKey), partitionIdLengthInBytes);
        }
        return new BasicTSKey(new StorableKey(prefixPart), partitionIdLengthInBytes);
    }
    public StorableKey storableKey(String fullPathint partitionIdLengthInBytes)
    {
        if (partitionIdLengthInBytes > ) {
            throw new IllegalArgumentException("Partition id byte length too long ("+partitionIdLengthInBytes
                    +"), can not exceed "+);
        }
        byte[] b = UTF8Encoder.encodeAsUTF8(fullPath, 0, false);
        // group id length is a positive 16-bit short, MSB:
        b[0] = (byte) (partitionIdLengthInBytes >> 8);
        b[1] = (bytepartitionIdLengthInBytes;
        return new StorableKey(b);
    }
    
    /*
    /**********************************************************************
    /* Path handling
    /**********************************************************************
     */
    
    @SuppressWarnings("unchecked")
    @Override
    public <B extends RequestPathBuilder> B appendToPath(B bBasicTSKey key)
    {
        // Partition id: could be added as path segment; but for now we'll use query param instead
        int partitionIdLen = key.getPartitionIdLength();
        if (partitionIdLen > 0) {
            String partitionId = key.getPartitionId();
            b = (B) b.addParameter(.partitionId);
        }
        // also: while not harmful, let's avoid escaping embedded slashes (slightly more compact)
        b = (B) b.addPathSegmentsRaw(key.getPath());
        return b;
    }
    @Override
    public <P extends DecodableRequestPathBasicTSKey extractFromPath(P path)
    {
        final String partitionId = path.getQueryParameter(.);
        // but ignore empty one
        final String filename = path.getDecodedPath();
        if (partitionId != null) {
            if (partitionId.length() > 0) {
                return construct(partitionIdfilename);
            }
        }
        return construct(filename);
    }
    
    /*
    /**********************************************************************
    /* Hash code calculation
    /**********************************************************************
     */
    protected int rawHashForRouting(BasicTSKey keyBlockHasher32 hasher)
    {
        // first: skip metadata (group id length indicator)
        int offset = .;
        StorableKey rawKey = key.asStorableKey();
        int length = rawKey.length() - offset;
        // second include only group id (if got one), or full thing
        if (key.hasPartitionId()) {
            length = key.getPartitionIdLength();
        }
        return rawKey.hashCode(hasheroffsetlength);
    }
    @Override
    public int contentHashFor(ByteContainer bytes) {
        return bytes.hash(.);
    }
    @Override
        return new IncrementalMurmur3Hasher();
    }
New to GrepCode? Check out our FAQ X