All Downloads are FREE. Search and download functionalities are using the official Maven repository.

scray.service.qservice.thriftjava.ScrayStatefulTService Maven / Gradle / Ivy

There is a newer version: 1.1.2
Show newest version
/**
 * generated by Scrooge ${project.version}
 */
package scray.service.qservice.thriftjava;

import com.twitter.scrooge.Option;
import com.twitter.scrooge.ThriftStruct;
import com.twitter.scrooge.ThriftStructCodec;
import com.twitter.scrooge.ThriftStructCodec3;
import com.twitter.scrooge.Utilities;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;
import org.apache.thrift.protocol.*;
import org.apache.thrift.TApplicationException;
import com.twitter.util.Future;
import com.twitter.util.FutureEventListener;
import com.twitter.finagle.SourcedException;
import com.twitter.finagle.stats.NullStatsReceiver;
import com.twitter.finagle.stats.StatsReceiver;
import com.twitter.finagle.thrift.ThriftClientRequest;
import java.util.Arrays;
import org.apache.thrift.TException;
import com.twitter.finagle.Service;
import com.twitter.finagle.stats.Counter;
import com.twitter.util.Function;
import com.twitter.util.Function2;
import org.apache.thrift.transport.TMemoryBuffer;
import org.apache.thrift.transport.TMemoryInputTransport;
import org.apache.thrift.transport.TTransport;

/**
 * Stateful query service with continuous result pages
 */
@javax.annotation.Generated(value = "com.twitter.scrooge.Compiler", date = "2017-04-25T16:52:50.412+0200")
public class ScrayStatefulTService {
  public interface Iface {
    /**
    	 * Submit query
    	 */
    public scray.service.qmodel.thriftjava.ScrayUUID query(scray.service.qmodel.thriftjava.ScrayTQuery query) throws ScrayTException;
    /**
    	 * Fetch query results
    	 * Paging state is maintained on server side.
    	 * The operation is neither idempotent nor safe.
    	 */
    public ScrayTResultFrame getResults(scray.service.qmodel.thriftjava.ScrayUUID queryId) throws ScrayTException;
  }

  public interface FutureIface {
    /**
    	 * Submit query
    	 */
    public Future query(scray.service.qmodel.thriftjava.ScrayTQuery query);
    /**
    	 * Fetch query results
    	 * Paging state is maintained on server side.
    	 * The operation is neither idempotent nor safe.
    	 */
    public Future getResults(scray.service.qmodel.thriftjava.ScrayUUID queryId);
  }

  static class query$args implements ThriftStruct {
    private static final TStruct STRUCT = new TStruct("query_args");
    private static final TField QueryField = new TField("query", TType.STRUCT, (short) 1);
    final scray.service.qmodel.thriftjava.ScrayTQuery query;
  
    public static class Builder {
      private scray.service.qmodel.thriftjava.ScrayTQuery _query = null;
      private Boolean _got_query = false;
  
      public Builder query(scray.service.qmodel.thriftjava.ScrayTQuery value) {
        this._query = value;
        this._got_query = true;
        return this;
      }
  
      public Builder unsetQuery() {
        this._query = null;
        this._got_query = false;
        return this;
      }
  
      public query$args build() {
        return new query$args(
          this._query    );
      }
    }
  
    public Builder copy() {
      Builder builder = new Builder();
      builder.query(this.query);
      return builder;
    }
  
    public static ThriftStructCodec CODEC = new ThriftStructCodec3() {
      @Override
      public query$args decode(TProtocol _iprot) throws org.apache.thrift.TException {
        Builder builder = new Builder();
        scray.service.qmodel.thriftjava.ScrayTQuery query = null;
        Boolean _done = false;
        _iprot.readStructBegin();
        while (!_done) {
          TField _field = _iprot.readFieldBegin();
          if (_field.type == TType.STOP) {
            _done = true;
          } else {
            switch (_field.id) {
              case 1: /* query */
                switch (_field.type) {
                  case TType.STRUCT:
                    scray.service.qmodel.thriftjava.ScrayTQuery query_item;
                    query_item = scray.service.qmodel.thriftjava.ScrayTQuery.decode(_iprot);
                    query = query_item;
                    break;
                  default:
                    TProtocolUtil.skip(_iprot, _field.type);
                }
                builder.query(query);
                break;
              default:
                TProtocolUtil.skip(_iprot, _field.type);
            }
            _iprot.readFieldEnd();
          }
        }
        _iprot.readStructEnd();
        try {
          return builder.build();
        } catch (IllegalStateException stateEx) {
          throw new TProtocolException(stateEx.getMessage());
        }
      }
  
      @Override
      public void encode(query$args struct, TProtocol oprot) throws org.apache.thrift.TException {
        struct.write(oprot);
      }
    };
  
    public static query$args decode(TProtocol _iprot) throws org.apache.thrift.TException {
      return CODEC.decode(_iprot);
    }
  
    public static void encode(query$args struct, TProtocol oprot) throws org.apache.thrift.TException {
      CODEC.encode(struct, oprot);
    }
  
    public query$args(
      scray.service.qmodel.thriftjava.ScrayTQuery query
    ) {
      this.query = query;
    }
  
  
    public scray.service.qmodel.thriftjava.ScrayTQuery getQuery() {
      return this.query;
    }
    public boolean isSetQuery() {
      return this.query != null;
    }
  
    public void write(TProtocol _oprot) throws org.apache.thrift.TException {
      validate();
      _oprot.writeStructBegin(STRUCT);
        _oprot.writeFieldBegin(QueryField);
        scray.service.qmodel.thriftjava.ScrayTQuery query_item = query;
        query_item.write(_oprot);
        _oprot.writeFieldEnd();
      _oprot.writeFieldStop();
      _oprot.writeStructEnd();
    }
  
    private void validate() throws org.apache.thrift.protocol.TProtocolException {
    }
  
  
    @Override
    public boolean equals(Object other) {
      if (!(other instanceof query$args)) return false;
      query$args that = (query$args) other;
      return
  this.query.equals(that.query);
    }
  
    @Override
    public String toString() {
      return "query$args(" + this.query + ")";
    }
  
    @Override
    public int hashCode() {
      int hash = 1;
      hash = hash * (this.query == null ? 0 : this.query.hashCode());
      return hash;
    }
  }
  static class query$result implements ThriftStruct {
    private static final TStruct STRUCT = new TStruct("query_result");
    private static final TField SuccessField = new TField("success", TType.STRUCT, (short) 0);
    final Option success;
    private static final TField ExField = new TField("ex", TType.STRUCT, (short) 1);
    final Option ex;
  
    public static class Builder {
      private scray.service.qmodel.thriftjava.ScrayUUID _success = null;
      private Boolean _got_success = false;
  
      public Builder success(scray.service.qmodel.thriftjava.ScrayUUID value) {
        this._success = value;
        this._got_success = true;
        return this;
      }
  
      public Builder unsetSuccess() {
        this._success = null;
        this._got_success = false;
        return this;
      }
      private ScrayTException _ex = null;
      private Boolean _got_ex = false;
  
      public Builder ex(ScrayTException value) {
        this._ex = value;
        this._got_ex = true;
        return this;
      }
  
      public Builder unsetEx() {
        this._ex = null;
        this._got_ex = false;
        return this;
      }
  
      public query$result build() {
        return new query$result(
        Option.make(this._got_success, this._success),
        Option.make(this._got_ex, this._ex)    );
      }
    }
  
    public Builder copy() {
      Builder builder = new Builder();
      if (this.success.isDefined()) builder.success(this.success.get());
      if (this.ex.isDefined()) builder.ex(this.ex.get());
      return builder;
    }
  
    public static ThriftStructCodec CODEC = new ThriftStructCodec3() {
      @Override
      public query$result decode(TProtocol _iprot) throws org.apache.thrift.TException {
        Builder builder = new Builder();
        scray.service.qmodel.thriftjava.ScrayUUID success = null;
        ScrayTException ex = null;
        Boolean _done = false;
        _iprot.readStructBegin();
        while (!_done) {
          TField _field = _iprot.readFieldBegin();
          if (_field.type == TType.STOP) {
            _done = true;
          } else {
            switch (_field.id) {
              case 0: /* success */
                switch (_field.type) {
                  case TType.STRUCT:
                    scray.service.qmodel.thriftjava.ScrayUUID success_item;
                    success_item = scray.service.qmodel.thriftjava.ScrayUUID.decode(_iprot);
                    success = success_item;
                    break;
                  default:
                    TProtocolUtil.skip(_iprot, _field.type);
                }
                builder.success(success);
                break;
              case 1: /* ex */
                switch (_field.type) {
                  case TType.STRUCT:
                    ScrayTException ex_item;
                    ex_item = ScrayTException.decode(_iprot);
                    ex = ex_item;
                    break;
                  default:
                    TProtocolUtil.skip(_iprot, _field.type);
                }
                builder.ex(ex);
                break;
              default:
                TProtocolUtil.skip(_iprot, _field.type);
            }
            _iprot.readFieldEnd();
          }
        }
        _iprot.readStructEnd();
        try {
          return builder.build();
        } catch (IllegalStateException stateEx) {
          throw new TProtocolException(stateEx.getMessage());
        }
      }
  
      @Override
      public void encode(query$result struct, TProtocol oprot) throws org.apache.thrift.TException {
        struct.write(oprot);
      }
    };
  
    public static query$result decode(TProtocol _iprot) throws org.apache.thrift.TException {
      return CODEC.decode(_iprot);
    }
  
    public static void encode(query$result struct, TProtocol oprot) throws org.apache.thrift.TException {
      CODEC.encode(struct, oprot);
    }
  
    public query$result(
      Option success, 
      Option ex
    ) {
      this.success = success;
      this.ex = ex;
    }
  
  
    public scray.service.qmodel.thriftjava.ScrayUUID getSuccess() {
      return this.success.get();
    }
    public boolean isSetSuccess() {
      return this.success.isDefined();
    }
    public ScrayTException getEx() {
      return this.ex.get();
    }
    public boolean isSetEx() {
      return this.ex.isDefined();
    }
  
    public void write(TProtocol _oprot) throws org.apache.thrift.TException {
      validate();
      _oprot.writeStructBegin(STRUCT);
      if (success.isDefined()) {  _oprot.writeFieldBegin(SuccessField);
        scray.service.qmodel.thriftjava.ScrayUUID success_item = success.get();
        success_item.write(_oprot);
        _oprot.writeFieldEnd();
      }
      if (ex.isDefined()) {  _oprot.writeFieldBegin(ExField);
        ScrayTException ex_item = ex.get();
        ex_item.write(_oprot);
        _oprot.writeFieldEnd();
      }
      _oprot.writeFieldStop();
      _oprot.writeStructEnd();
    }
  
    private void validate() throws org.apache.thrift.protocol.TProtocolException {
    }
  
  
    @Override
    public boolean equals(Object other) {
      if (!(other instanceof query$result)) return false;
      query$result that = (query$result) other;
      return
  this.success.equals(that.success) &&
  this.ex.equals(that.ex);
    }
  
    @Override
    public String toString() {
      return "query$result(" + this.success + "," + this.ex + ")";
    }
  
    @Override
    public int hashCode() {
      int hash = 1;
      hash = hash * (this.success.isDefined() ? 0 : this.success.get().hashCode());
      hash = hash * (this.ex.isDefined() ? 0 : this.ex.get().hashCode());
      return hash;
    }
  }
  static class getResults$args implements ThriftStruct {
    private static final TStruct STRUCT = new TStruct("getResults_args");
    private static final TField QueryIdField = new TField("queryId", TType.STRUCT, (short) 1);
    final scray.service.qmodel.thriftjava.ScrayUUID queryId;
  
    public static class Builder {
      private scray.service.qmodel.thriftjava.ScrayUUID _queryId = null;
      private Boolean _got_queryId = false;
  
      public Builder queryId(scray.service.qmodel.thriftjava.ScrayUUID value) {
        this._queryId = value;
        this._got_queryId = true;
        return this;
      }
  
      public Builder unsetQueryId() {
        this._queryId = null;
        this._got_queryId = false;
        return this;
      }
  
      public getResults$args build() {
        return new getResults$args(
          this._queryId    );
      }
    }
  
    public Builder copy() {
      Builder builder = new Builder();
      builder.queryId(this.queryId);
      return builder;
    }
  
    public static ThriftStructCodec CODEC = new ThriftStructCodec3() {
      @Override
      public getResults$args decode(TProtocol _iprot) throws org.apache.thrift.TException {
        Builder builder = new Builder();
        scray.service.qmodel.thriftjava.ScrayUUID queryId = null;
        Boolean _done = false;
        _iprot.readStructBegin();
        while (!_done) {
          TField _field = _iprot.readFieldBegin();
          if (_field.type == TType.STOP) {
            _done = true;
          } else {
            switch (_field.id) {
              case 1: /* queryId */
                switch (_field.type) {
                  case TType.STRUCT:
                    scray.service.qmodel.thriftjava.ScrayUUID queryId_item;
                    queryId_item = scray.service.qmodel.thriftjava.ScrayUUID.decode(_iprot);
                    queryId = queryId_item;
                    break;
                  default:
                    TProtocolUtil.skip(_iprot, _field.type);
                }
                builder.queryId(queryId);
                break;
              default:
                TProtocolUtil.skip(_iprot, _field.type);
            }
            _iprot.readFieldEnd();
          }
        }
        _iprot.readStructEnd();
        try {
          return builder.build();
        } catch (IllegalStateException stateEx) {
          throw new TProtocolException(stateEx.getMessage());
        }
      }
  
      @Override
      public void encode(getResults$args struct, TProtocol oprot) throws org.apache.thrift.TException {
        struct.write(oprot);
      }
    };
  
    public static getResults$args decode(TProtocol _iprot) throws org.apache.thrift.TException {
      return CODEC.decode(_iprot);
    }
  
    public static void encode(getResults$args struct, TProtocol oprot) throws org.apache.thrift.TException {
      CODEC.encode(struct, oprot);
    }
  
    public getResults$args(
      scray.service.qmodel.thriftjava.ScrayUUID queryId
    ) {
      this.queryId = queryId;
    }
  
  
    public scray.service.qmodel.thriftjava.ScrayUUID getQueryId() {
      return this.queryId;
    }
    public boolean isSetQueryId() {
      return this.queryId != null;
    }
  
    public void write(TProtocol _oprot) throws org.apache.thrift.TException {
      validate();
      _oprot.writeStructBegin(STRUCT);
        _oprot.writeFieldBegin(QueryIdField);
        scray.service.qmodel.thriftjava.ScrayUUID queryId_item = queryId;
        queryId_item.write(_oprot);
        _oprot.writeFieldEnd();
      _oprot.writeFieldStop();
      _oprot.writeStructEnd();
    }
  
    private void validate() throws org.apache.thrift.protocol.TProtocolException {
    }
  
  
    @Override
    public boolean equals(Object other) {
      if (!(other instanceof getResults$args)) return false;
      getResults$args that = (getResults$args) other;
      return
  this.queryId.equals(that.queryId);
    }
  
    @Override
    public String toString() {
      return "getResults$args(" + this.queryId + ")";
    }
  
    @Override
    public int hashCode() {
      int hash = 1;
      hash = hash * (this.queryId == null ? 0 : this.queryId.hashCode());
      return hash;
    }
  }
  static class getResults$result implements ThriftStruct {
    private static final TStruct STRUCT = new TStruct("getResults_result");
    private static final TField SuccessField = new TField("success", TType.STRUCT, (short) 0);
    final Option success;
    private static final TField ExField = new TField("ex", TType.STRUCT, (short) 1);
    final Option ex;
  
    public static class Builder {
      private ScrayTResultFrame _success = null;
      private Boolean _got_success = false;
  
      public Builder success(ScrayTResultFrame value) {
        this._success = value;
        this._got_success = true;
        return this;
      }
  
      public Builder unsetSuccess() {
        this._success = null;
        this._got_success = false;
        return this;
      }
      private ScrayTException _ex = null;
      private Boolean _got_ex = false;
  
      public Builder ex(ScrayTException value) {
        this._ex = value;
        this._got_ex = true;
        return this;
      }
  
      public Builder unsetEx() {
        this._ex = null;
        this._got_ex = false;
        return this;
      }
  
      public getResults$result build() {
        return new getResults$result(
        Option.make(this._got_success, this._success),
        Option.make(this._got_ex, this._ex)    );
      }
    }
  
    public Builder copy() {
      Builder builder = new Builder();
      if (this.success.isDefined()) builder.success(this.success.get());
      if (this.ex.isDefined()) builder.ex(this.ex.get());
      return builder;
    }
  
    public static ThriftStructCodec CODEC = new ThriftStructCodec3() {
      @Override
      public getResults$result decode(TProtocol _iprot) throws org.apache.thrift.TException {
        Builder builder = new Builder();
        ScrayTResultFrame success = null;
        ScrayTException ex = null;
        Boolean _done = false;
        _iprot.readStructBegin();
        while (!_done) {
          TField _field = _iprot.readFieldBegin();
          if (_field.type == TType.STOP) {
            _done = true;
          } else {
            switch (_field.id) {
              case 0: /* success */
                switch (_field.type) {
                  case TType.STRUCT:
                    ScrayTResultFrame success_item;
                    success_item = ScrayTResultFrame.decode(_iprot);
                    success = success_item;
                    break;
                  default:
                    TProtocolUtil.skip(_iprot, _field.type);
                }
                builder.success(success);
                break;
              case 1: /* ex */
                switch (_field.type) {
                  case TType.STRUCT:
                    ScrayTException ex_item;
                    ex_item = ScrayTException.decode(_iprot);
                    ex = ex_item;
                    break;
                  default:
                    TProtocolUtil.skip(_iprot, _field.type);
                }
                builder.ex(ex);
                break;
              default:
                TProtocolUtil.skip(_iprot, _field.type);
            }
            _iprot.readFieldEnd();
          }
        }
        _iprot.readStructEnd();
        try {
          return builder.build();
        } catch (IllegalStateException stateEx) {
          throw new TProtocolException(stateEx.getMessage());
        }
      }
  
      @Override
      public void encode(getResults$result struct, TProtocol oprot) throws org.apache.thrift.TException {
        struct.write(oprot);
      }
    };
  
    public static getResults$result decode(TProtocol _iprot) throws org.apache.thrift.TException {
      return CODEC.decode(_iprot);
    }
  
    public static void encode(getResults$result struct, TProtocol oprot) throws org.apache.thrift.TException {
      CODEC.encode(struct, oprot);
    }
  
    public getResults$result(
      Option success, 
      Option ex
    ) {
      this.success = success;
      this.ex = ex;
    }
  
  
    public ScrayTResultFrame getSuccess() {
      return this.success.get();
    }
    public boolean isSetSuccess() {
      return this.success.isDefined();
    }
    public ScrayTException getEx() {
      return this.ex.get();
    }
    public boolean isSetEx() {
      return this.ex.isDefined();
    }
  
    public void write(TProtocol _oprot) throws org.apache.thrift.TException {
      validate();
      _oprot.writeStructBegin(STRUCT);
      if (success.isDefined()) {  _oprot.writeFieldBegin(SuccessField);
        ScrayTResultFrame success_item = success.get();
        success_item.write(_oprot);
        _oprot.writeFieldEnd();
      }
      if (ex.isDefined()) {  _oprot.writeFieldBegin(ExField);
        ScrayTException ex_item = ex.get();
        ex_item.write(_oprot);
        _oprot.writeFieldEnd();
      }
      _oprot.writeFieldStop();
      _oprot.writeStructEnd();
    }
  
    private void validate() throws org.apache.thrift.protocol.TProtocolException {
    }
  
  
    @Override
    public boolean equals(Object other) {
      if (!(other instanceof getResults$result)) return false;
      getResults$result that = (getResults$result) other;
      return
  this.success.equals(that.success) &&
  this.ex.equals(that.ex);
    }
  
    @Override
    public String toString() {
      return "getResults$result(" + this.success + "," + this.ex + ")";
    }
  
    @Override
    public int hashCode() {
      int hash = 1;
      hash = hash * (this.success.isDefined() ? 0 : this.success.get().hashCode());
      hash = hash * (this.ex.isDefined() ? 0 : this.ex.get().hashCode());
      return hash;
    }
  }
  /**
   * Stateful query service with continuous result pages
   */
  public static class FinagledClient implements FutureIface {
    private com.twitter.finagle.Service service;
    private String serviceName;
    private TProtocolFactory protocolFactory /* new TBinaryProtocol.Factory */;
    private StatsReceiver scopedStats;
  
    public FinagledClient(
      com.twitter.finagle.Service service,
      TProtocolFactory protocolFactory /* new TBinaryProtocol.Factory */,
      String serviceName,
      StatsReceiver stats
    ) {
      this.service = service;
      this.serviceName = serviceName;
      this.protocolFactory = protocolFactory;
      if (serviceName != "") {
        this.scopedStats = stats.scope(serviceName);
      } else {
        this.scopedStats = stats;
      }
    }
  
    // ----- boilerplate that should eventually be moved into finagle:
  
    protected ThriftClientRequest encodeRequest(String name, ThriftStruct args) {
      TMemoryBuffer buf = new TMemoryBuffer(512);
      TProtocol oprot = protocolFactory.getProtocol(buf);
  
      try {
        oprot.writeMessageBegin(new TMessage(name, TMessageType.CALL, 0));
        args.write(oprot);
        oprot.writeMessageEnd();
      } catch (TException e) {
        // not real.
      }
  
      byte[] bytes = Arrays.copyOfRange(buf.getArray(), 0, buf.length());
      return new ThriftClientRequest(bytes, false);
    }
  
    protected  T decodeResponse(byte[] resBytes, ThriftStructCodec codec) throws TException {
      TProtocol iprot = protocolFactory.getProtocol(new TMemoryInputTransport(resBytes));
      TMessage msg = iprot.readMessageBegin();
      try {
        if (msg.type == TMessageType.EXCEPTION) {
          TException exception = TApplicationException.read(iprot);
          if (exception instanceof SourcedException) {
            if (this.serviceName != "") ((SourcedException) exception).serviceName_$eq(this.serviceName);
          }
          throw exception;
        } else {
          return codec.decode(iprot);
        }
      } finally {
        iprot.readMessageEnd();
      }
    }
  
    protected Exception missingResult(String name) {
      return new TApplicationException(
        TApplicationException.MISSING_RESULT,
        "`" + name + "` failed: unknown result"
      );
    }
  
    protected class __Stats {
      public Counter requestsCounter, successCounter, failuresCounter;
      public StatsReceiver failuresScope;
  
      public __Stats(String name) {
        StatsReceiver scope = FinagledClient.this.scopedStats.scope(name);
        this.requestsCounter = scope.counter0("requests");
        this.successCounter = scope.counter0("success");
        this.failuresCounter = scope.counter0("failures");
        this.failuresScope = scope.scope("failures");
      }
    }
  
    // ----- end boilerplate.
  
    private __Stats ___stats_query;
  
    private __Stats __stats_query() {
      if (___stats_query == null) {
        ___stats_query = new __Stats("query");
      }
      return ___stats_query;
    }
  
    /**
    	 * Submit query
    	 */
    public Future query(scray.service.qmodel.thriftjava.ScrayTQuery query) {
      __stats_query().requestsCounter.incr();
  
      Future rv = this.service.apply(encodeRequest("query", new query$args(query))).flatMap(new Function>() {
        public Future apply(byte[] in) {
          try {
            query$result result = decodeResponse(in, query$result.CODEC);
  
            Exception exception = null;
            if (exception == null && result.ex.isDefined()) exception = result.ex.get();
            if (exception != null) return Future.exception(exception);
  
            if (result.success.isDefined()) return Future.value(result.success.get());
            return Future.exception(missingResult("query"));
          } catch (TException e) {
            return Future.exception(e);
          }
        }
      }).rescue(new Function>() {
        public Future apply(Throwable t) {
          if (t instanceof SourcedException) {
            ((SourcedException) t).serviceName_$eq(FinagledClient.this.serviceName);
          }
          return Future.exception(t);
        }
      });
  
      rv.addEventListener(new FutureEventListener() {
        public void onSuccess(scray.service.qmodel.thriftjava.ScrayUUID result) {
          __stats_query().successCounter.incr();
        }
  
        public void onFailure(Throwable t) {
          __stats_query().failuresCounter.incr();
          __stats_query().failuresScope.counter0(t.getClass().getName()).incr();
        }
      });
  
      return rv;
    }
    private __Stats ___stats_getResults;
  
    private __Stats __stats_getResults() {
      if (___stats_getResults == null) {
        ___stats_getResults = new __Stats("getResults");
      }
      return ___stats_getResults;
    }
  
    /**
    	 * Fetch query results
    	 * Paging state is maintained on server side.
    	 * The operation is neither idempotent nor safe.
    	 */
    public Future getResults(scray.service.qmodel.thriftjava.ScrayUUID queryId) {
      __stats_getResults().requestsCounter.incr();
  
      Future rv = this.service.apply(encodeRequest("getResults", new getResults$args(queryId))).flatMap(new Function>() {
        public Future apply(byte[] in) {
          try {
            getResults$result result = decodeResponse(in, getResults$result.CODEC);
  
            Exception exception = null;
            if (exception == null && result.ex.isDefined()) exception = result.ex.get();
            if (exception != null) return Future.exception(exception);
  
            if (result.success.isDefined()) return Future.value(result.success.get());
            return Future.exception(missingResult("getResults"));
          } catch (TException e) {
            return Future.exception(e);
          }
        }
      }).rescue(new Function>() {
        public Future apply(Throwable t) {
          if (t instanceof SourcedException) {
            ((SourcedException) t).serviceName_$eq(FinagledClient.this.serviceName);
          }
          return Future.exception(t);
        }
      });
  
      rv.addEventListener(new FutureEventListener() {
        public void onSuccess(ScrayTResultFrame result) {
          __stats_getResults().successCounter.incr();
        }
  
        public void onFailure(Throwable t) {
          __stats_getResults().failuresCounter.incr();
          __stats_getResults().failuresScope.counter0(t.getClass().getName()).incr();
        }
      });
  
      return rv;
    }
  }
  /**
   * Stateful query service with continuous result pages
   */
  public static class FinagledService extends Service {
    final private FutureIface iface;
    final private TProtocolFactory protocolFactory;
  
    public FinagledService(final FutureIface iface, final TProtocolFactory protocolFactory) {
      this.iface = iface;
      this.protocolFactory = protocolFactory;
  
      addFunction("query", new Function2>() {
        public Future apply(TProtocol iprot, final Integer seqid) {
          try {
            query$args args = query$args.decode(iprot);
            iprot.readMessageEnd();
            Future result;
            try {
              result = iface.query(args.query);
            } catch (Throwable t) {
              result = Future.exception(t);
            }
            return result.flatMap(new Function>() {
              public Future apply(scray.service.qmodel.thriftjava.ScrayUUID value){
                return reply("query", seqid, new query$result.Builder().success(value).build());
              }
            }).rescue(new Function>() {
              public Future apply(Throwable t) {
                if (t instanceof ScrayTException) {
                  return reply("ScrayStatefulTService", seqid, new query$result.Builder().ex((ScrayTException) t).build());
                }
                return Future.exception(t);
              }
            });
          } catch (TProtocolException e) {
            try {
              iprot.readMessageEnd();
              return exception("query", seqid, TApplicationException.PROTOCOL_ERROR, e.getMessage());
            } catch (Exception unrecoverable) {
              return Future.exception(unrecoverable);
            }
          } catch (Throwable t) {
            return Future.exception(t);
          }
        }
      });
      addFunction("getResults", new Function2>() {
        public Future apply(TProtocol iprot, final Integer seqid) {
          try {
            getResults$args args = getResults$args.decode(iprot);
            iprot.readMessageEnd();
            Future result;
            try {
              result = iface.getResults(args.queryId);
            } catch (Throwable t) {
              result = Future.exception(t);
            }
            return result.flatMap(new Function>() {
              public Future apply(ScrayTResultFrame value){
                return reply("getResults", seqid, new getResults$result.Builder().success(value).build());
              }
            }).rescue(new Function>() {
              public Future apply(Throwable t) {
                if (t instanceof ScrayTException) {
                  return reply("ScrayStatefulTService", seqid, new getResults$result.Builder().ex((ScrayTException) t).build());
                }
                return Future.exception(t);
              }
            });
          } catch (TProtocolException e) {
            try {
              iprot.readMessageEnd();
              return exception("getResults", seqid, TApplicationException.PROTOCOL_ERROR, e.getMessage());
            } catch (Exception unrecoverable) {
              return Future.exception(unrecoverable);
            }
          } catch (Throwable t) {
            return Future.exception(t);
          }
        }
      });
    }
  
    // ----- boilerplate that should eventually be moved into finagle:
  
    protected Map>> functionMap =
      new HashMap>>();
  
    protected void addFunction(String name, Function2> fn) {
      functionMap.put(name, fn);
    }
  
    protected Function2> getFunction(String name) {
      return functionMap.get(name);
    }
  
    protected Future exception(String name, int seqid, int code, String message) {
      try {
        TApplicationException x = new TApplicationException(code, message);
        TMemoryBuffer memoryBuffer = new TMemoryBuffer(512);
        TProtocol oprot = protocolFactory.getProtocol(memoryBuffer);
  
        oprot.writeMessageBegin(new TMessage(name, TMessageType.EXCEPTION, seqid));
        x.write(oprot);
        oprot.writeMessageEnd();
        oprot.getTransport().flush();
        return Future.value(Arrays.copyOfRange(memoryBuffer.getArray(), 0, memoryBuffer.length()));
      } catch (Exception e) {
        return Future.exception(e);
      }
    }
  
    protected Future reply(String name, int seqid, ThriftStruct result) {
      try {
        TMemoryBuffer memoryBuffer = new TMemoryBuffer(512);
        TProtocol oprot = protocolFactory.getProtocol(memoryBuffer);
  
        oprot.writeMessageBegin(new TMessage(name, TMessageType.REPLY, seqid));
        result.write(oprot);
        oprot.writeMessageEnd();
  
        return Future.value(Arrays.copyOfRange(memoryBuffer.getArray(), 0, memoryBuffer.length()));
      } catch (Exception e) {
        return Future.exception(e);
      }
    }
  
    public final Future apply(byte[] request) {
      TTransport inputTransport = new TMemoryInputTransport(request);
      TProtocol iprot = protocolFactory.getProtocol(inputTransport);
  
      try {
        TMessage msg = iprot.readMessageBegin();
        Function2> f = functionMap.get(msg.name);
        if (f != null) {
          return f.apply(iprot, msg.seqid);
        } else {
          TProtocolUtil.skip(iprot, TType.STRUCT);
          return exception(msg.name, msg.seqid, TApplicationException.UNKNOWN_METHOD, "Invalid method name: '" + msg.name + "'");
        }
      } catch (Exception e) {
        return Future.exception(e);
      }
    }
  
    // ---- end boilerplate.
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy