Interface ValueCoder<T>

  • Type Parameters:
    T - The target type for the encoding.

    public interface ValueCoder<T>
    A ValueCoder is used encode and decode values to and from ByteStrings. In Antidote every value in registers and sets is stored as a ByteString, but in Java it is often desirable to use other types to get more type safety and avoid calling conversion functions all the time.

    As an example consider the following UserId class:

    
     class UserId {
         private String id;
    
         public UserId(String id) {
             this.id = id;
         }
    
         public String getId() {
             return id;
         }
    
         // hashCode, equals, etc.
     }
     

    We can create a ValueCoder for this class using the static method stringCoder(Function, Function):

    
         ValueCoder<UserId> userIdCoder = ValueCoder.stringCoder(UserId::getId, UserId::new);
     

    Then this ValueCoder can be used to get a key to a set of UserIds:

    
         SetKey<UserId> userSet = Key.set("users", userIdCoder);
     

    Now the userSet can be updated and read without converting UserIds to a lower level type like ByteString:

    
         UserId user1 = new UserId("user1");
         UserId user2 = new UserId("user2");
         // update the user set
         bucket.update(tx, userSet.addAll(user1, user2));
    
         // read the user set
         List<UserId> value = bucket.read(tx, userSet);
     
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      static <T> ValueCoder<T> byteCoder​(java.util.function.Function<T,​com.google.protobuf.ByteString> toBytestring, java.util.function.Function<com.google.protobuf.ByteString,​T> fromBytestring)
      A helper method to create a custom ValueCoder.
      T decode​(com.google.protobuf.ByteString bytes)  
      default java.util.List<T> decodeList​(java.util.List<com.google.protobuf.ByteString> byteStringList)  
      com.google.protobuf.ByteString encode​(T value)  
      default java.util.List<com.google.protobuf.ByteString> encodeList​(java.util.List<T> valueList)  
      static <T> ValueCoder<T> stringCoder​(java.util.function.Function<T,​java.lang.String> toString, java.util.function.Function<java.lang.String,​T> fromString)
      A helper method to create a custom ValueCoder based on a string encoding.
    • Field Detail

      • utf8String

        static final ValueCoder<java.lang.String> utf8String
        Stores Strings in utf8 encoding
      • bytestringEncoder

        static final ValueCoder<com.google.protobuf.ByteString> bytestringEncoder
        Stores plain ByteStrings

        This is the identity-coder

      • integerCoder

        static final ValueCoder<java.lang.Integer> integerCoder
        Stores integers
      • longCoder

        static final ValueCoder<java.lang.Long> longCoder
        Stores longs
      • floatCoder

        static final ValueCoder<java.lang.Float> floatCoder
        Stores floats
      • doubleCoder

        static final ValueCoder<java.lang.Double> doubleCoder
        Stores doubles
    • Method Detail

      • encode

        com.google.protobuf.ByteString encode​(T value)
      • decode

        T decode​(com.google.protobuf.ByteString bytes)
      • decodeList

        default java.util.List<T> decodeList​(java.util.List<com.google.protobuf.ByteString> byteStringList)
      • encodeList

        default java.util.List<com.google.protobuf.ByteString> encodeList​(java.util.List<T> valueList)
      • byteCoder

        static <T> ValueCoder<T> byteCoder​(java.util.function.Function<T,​com.google.protobuf.ByteString> toBytestring,
                                           java.util.function.Function<com.google.protobuf.ByteString,​T> fromBytestring)
        A helper method to create a custom ValueCoder.
        Type Parameters:
        T - the target type for the coder
        Parameters:
        toBytestring - A function to convert T to ByteString.
        fromBytestring - A function to convert a ByteString to T.
        Returns:
        A coder for T.
      • stringCoder

        static <T> ValueCoder<T> stringCoder​(java.util.function.Function<T,​java.lang.String> toString,
                                             java.util.function.Function<java.lang.String,​T> fromString)
        A helper method to create a custom ValueCoder based on a string encoding. Internally this will convert the strings to ByteStrings with utf8 encoding.
        Type Parameters:
        T - the target type for the coder
        Parameters:
        toString - A function to convert T to String.
        fromString - A function to convert a String to T.
        Returns:
        A coder for T.