Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Cache Java type -> Thrift type conversions

This improves serialization and deserialization perf a lot:

```
                                -------- old --------   -------- new --------     ------ win % ------
Benchmark                          ns/iter      intvl      ns/iter      intvl      ns/iter      intvl
=====================================================================================================
deserialize_Empty                      700         12          106          1          560       1100
deserialize_SmallInt                   756         10          133          2          468        400
deserialize_BigInt                     782          9          155          2          404        350
deserialize_SmallString                822         19          202          3          306        533
deserialize_BigString                 9152         53         8450         99            8        -46
deserialize_BigBinary                 3011        189         2137         24           40        687
deserialize_LargeBinary            5289470    3173098      5285250    3260233            0         -2
deserialize_Mixed                      874          8          269          3          224        166
deserialize_SmallListInt              1010         19          386          4          161        375
deserialize_BigListInt              304536       6177       309774       3594           -1         71
deserialize_BigListMixed           1848336      13685      1942206     119850           -4        -88
deserialize_LargeListMixed       224981060   83408124    221868657   75328617            1         10
serialize_Empty                        903         17          131          3          589        466
serialize_SmallInt                     961         14          173          3          455        366
serialize_BigInt                      1039         11          240          4          332        175
serialize_SmallString                 1145         22          301          4          280        450
serialize_BigString                  51215        208        49916        467            2        -55
serialize_BigBinary                   5595        125         4788        132           16         -5
serialize_LargeBinary              7592627    6189458      7723409    5639016           -1          9
serialize_Mixed                       1200         18          425          6          182        200
serialize_SmallListInt                1322         31          516          9          156        244
serialize_BigListInt                253678       1988       249520       3737            1        -46
serialize_BigListMixed             2424505      36120      2408576      22974            0         57
serialize_LargeListMixed         269418097   69367304    268303289   70283444            0         -1
```
latest commit b477095d2d
alandau alandau authored

README.md

Swift Codec

Swift Codec is a simple library specifying how Java objects are converted to and from Thrift. This library is similar to JaxB (XML) and Jackson (JSON), but for Thrift. Swift codec supports field, method, constructor, and builder injection.

Structs

To make a Java class a Thrift struct simply add the @ThriftStruct annotation. Swift will assume the Java class and the Thrift struct have the same name, so if the Thrift struct has a different name, you will need to add a value to annotation like this: @ThriftStruct("MyStructName").

Field

The simplest way to add a Thrift field is to annotate a public Java field with @ThriftField(42). As with structs, Swift will assume the Java field and Thrift field have the same name, so if they don't just add a name to the annotation like this: @ThriftField(value = 1, name="myFieldName").

@ThriftStruct
public class Bonk
{
    @ThriftField(1)
    public String message;

    @ThriftField(2)
    public int type;

    public BonkField()
    {
    }
} 

Beans

Traditional Java beans can easily be converted to Thrift structs by annotating the getters and setters. Swift will link the getter and setter by name, so you only need to specify the Thrift field id on one of them. You can override the Thrift field name in the annotation if necessary.

@ThriftStruct
public class Bonk
{
    private String message;
    private int type;

    @ThriftField(1)
    public String getMessage()
    {
        return message;
    }

    @ThriftField
    public void setMessage(String message)
    {
        this.message = message;
    }

    @ThriftField(2)
    public int getType()
    {
        return type;
    }

    @ThriftField
    public void setType(int type)
    {
        this.type = type;
    }
}

Constructor

Swift support immutable Java objects using constructor injection. Simply, annotate the constructor you want Swift to use with @ThriftConstructor, and Swift will automatically supply the constructor with the specified fields. Assuming you have compiled with debug symbols on, the parameters are automatically matched to a Thrift field (getter or Java field) by name. Otherwise, you will need to annotate the parameters with @ThriftField(name = "myName").

@Immutable
@ThriftStruct
public class Bonk
{
    private final String message;
    private final int type;

    @ThriftConstructor
    public Bonk(String message, int type)
    {
        this.message = message;
        this.type = type;
    }

    @ThriftField(1)
    public String getMessage()
    {
        return message;
    }

    @ThriftField(2)
    public int getType()
    {
        return type;
    }
}

Builder

For larger immutable objects, Swift supports the builder pattern. The Thrift struct is linked to the builder class using the builder property on the @ThriftStruct annotation. Swift will look for a factory method annotated with @ThriftConstructor on the builder class. The builder can use field, method and/or constructor injection in addition to injection into the factory method itself.

@Immutable
@ThriftStruct(builder = Builder.class)
public class Bonk
{
    private final String message;
    private final int type;

    public Bonk(String message, int type)
    {
        this.message = message;
        this.type = type;
    }

    @ThriftField(1)
    public String getMessage()
    {
        return message;
    }

    @ThriftField(2)
    public int getType()
    {
        return type;
    }

    public static class Builder
    {
        private String message;
        private int type;

        @ThriftField
        public Builder setMessage(String message)
        {
            this.message = message;
            return this;
        }

        @ThriftField
        public Builder setType(int type)
        {
            this.type = type;
            return this;
        }

        @ThriftConstructor
        public Bonk create()
        {
            return new Bonk(message, type);
        }
    }
}

Enumerations

Swift automatically maps Java enumerations to a Thrift int.

Implicit Value

Swift supports standard Java enumerations directly as a Thrift enumeration using the Java ordinal value as the Thrift enum value.

public enum Fruit
{
    APPLE, BANANA, CHERRY
}

Explicit Value

For custom enumerations, you can annotate a method on the enumeration to supply an int value.

public enum Letter
{
    A(65), B(66), C(67), D(68);

    private final int asciiValue;

    Letter(int asciiValue)
    {
        this.asciiValue = asciiValue;
    }

    @ThriftEnumValue
    public int getAsciiValue()
    {
        return asciiValue;
    }
}

Guice Support

A ThriftCodec can be bound into Guice adding the ThriftCodecModule to the injector and bind the codec with the fluent ThriftCodecBinder as follows:

Injector injector = Guice.createInjector(Stage.PRODUCTION,
        new ThriftCodecModule(),
        new Module()
        {
            @Override
            public void configure(Binder binder)
            {
                thriftServerBinder(binder).bindThriftCodec(Bonk.class);
            }
        });

Then, simply add the ThriftCodec type to any @Inject annotated field or method. Like this:

@Inject
private ThriftCodec<Bonk> bonkCodec;

public void write(Bonk bonk, TProtocol protocol) throws Exception
{
    bonkCodec.write(nwq TProtocolWriter(protocol));
}
Something went wrong with that request. Please try again.