Exemplo n.º 1
0
 @Override
 public StreamSqlResult execute(ExecutionContext c) throws StreamSqlException {
   YarchDatabase dict = YarchDatabase.getInstance(c.getDbName());
   // locking of the dictionary is performed inside the close
   Stream s = dict.getStream(name);
   if (s == null) throw new ResourceNotFoundException(name);
   s.close();
   return new StreamSqlResult();
 }
Exemplo n.º 2
0
  private WebSocketReplyData processPublishRequest(
      WebSocketDecodeContext ctx, WebSocketDecoder decoder) throws WebSocketException {
    YarchDatabase ydb = YarchDatabase.getInstance(processor.getInstance());

    StreamData req = decoder.decodeMessageData(ctx, SchemaArchive.StreamData.MERGE).build();
    Stream stream = ydb.getStream(req.getStream());
    if (stream == null) {
      throw new WebSocketException(
          ctx.getRequestId(), "Cannot find stream '" + req.getStream() + "'");
    }

    TupleDefinition tdef = stream.getDefinition();
    List<Object> tupleColumns = new ArrayList<>();

    // 'fixed' colums
    for (ColumnDefinition cdef : stream.getDefinition().getColumnDefinitions()) {
      ColumnData providedField = findColumnValue(req, cdef.getName());
      if (providedField == null) continue;
      if (!providedField.hasValue()) {
        throw new WebSocketException(
            ctx.getRequestId(), "No value was provided for column " + cdef.getName());
      }
      Object column =
          makeTupleColumn(ctx, cdef.getName(), providedField.getValue(), cdef.getType());
      tupleColumns.add(column);
    }

    // 'dynamic' columns
    for (ColumnData val : req.getColumnList()) {
      if (stream.getDefinition().getColumn(val.getName()) == null) {
        DataType type = dataTypeFromValue(val.getValue());
        tdef.addColumn(val.getName(), type);
        Object column = makeTupleColumn(ctx, val.getName(), val.getValue(), type);
        tupleColumns.add(column);
      }
    }

    Tuple t = new Tuple(tdef, tupleColumns);
    log.info("Emitting tuple {} to {}", t, stream.getName());
    stream.emitTuple(t);
    return toAckReply(ctx.getRequestId());
  }
Exemplo n.º 3
0
  private WebSocketReplyData processSubscribeRequest(
      WebSocketDecodeContext ctx, WebSocketDecoder decoder) throws WebSocketException {
    YarchDatabase ydb = YarchDatabase.getInstance(processor.getInstance());

    // Optionally read body. If it's not provided, suppose the subscription concerns
    // the stream of the current processor (TODO currently doesn't work with JSON).
    Stream stream;
    if (ctx.getData() != null) { // Check doesn't work with JSON, always returns JsonParser
      StreamSubscribeRequest req =
          decoder.decodeMessageData(ctx, SchemaRest.StreamSubscribeRequest.MERGE).build();
      if (req.hasStream()) {
        stream = ydb.getStream(req.getStream());
      } else {
        throw new WebSocketException(ctx.getRequestId(), "No stream was provided");
      }
    } else {
      stream = ydb.getStream(processor.getName());
    }

    StreamSubscriber subscriber =
        new StreamSubscriber() {

          @Override
          public void onTuple(Stream stream, Tuple tuple) {
            StreamData data = ArchiveHelper.toStreamData(stream, tuple);
            try {
              wsHandler.sendData(ProtoDataType.STREAM_DATA, data, SchemaArchive.StreamData.WRITE);
            } catch (IOException e) {
              log.debug("Could not send tuple data", e);
            }
          }

          @Override
          public void streamClosed(Stream stream) {}
        };

    stream.addSubscriber(subscriber);
    subscriptions.add(new Subscription(stream, subscriber));
    return toAckReply(ctx.getRequestId());
  }