<commit_before>/**
 * Copyright (C) 2014 Stratio (http://stratio.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.stratio.streaming.api

import java.util.List

import scala.collection.JavaConversions.asScalaBuffer
import scala.collection.JavaConversions.bufferAsJavaList

import org.apache.curator.framework.CuratorFramework
import org.apache.curator.framework.CuratorFrameworkFactory
import org.apache.curator.framework.api.CuratorEvent
import org.apache.curator.framework.api.CuratorEventType.WATCHED
import org.apache.curator.framework.api.CuratorListener
import org.apache.curator.retry.RetryOneTime
import org.slf4j.LoggerFactory

import com.stratio.streaming.api.messaging.ColumnNameType
import com.stratio.streaming.api.messaging.ColumnNameValue
import com.stratio.streaming.api.messaging.MessageBuilder.builder
import com.stratio.streaming.api.messaging.MessageBuilderWithColumns
import com.stratio.streaming.commons.constants.InternalTopic
import com.stratio.streaming.commons.constants.STREAMING.ZK_EPHEMERAL_NODE_PATH
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.ACTION.INDEX
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.ACTION.LISTEN
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.ACTION.SAVETO_CASSANDRA
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.ACTION.SAVETO_MONGO
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.ACTION.STOP_INDEX
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.ACTION.STOP_LISTEN
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.ACTION.STOP_SAVETO_CASSANDRA
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.ACTION.STOP_SAVETO_MONGO
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.DEFINITION
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.DEFINITION.ADD_QUERY
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.DEFINITION.ALTER
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.DEFINITION.DROP
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.DEFINITION.REMOVE_QUERY
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.MANIPULATION.LIST
import com.stratio.streaming.commons.exceptions.StratioEngineConnectionException
import com.stratio.streaming.commons.exceptions.StratioEngineOperationException
import com.stratio.streaming.commons.exceptions.StratioEngineStatusException
import com.stratio.streaming.commons.kafka.service.KafkaTopicService
import com.stratio.streaming.commons.kafka.service.TopicService
import com.stratio.streaming.commons.messages.ColumnNameTypeValue
import com.stratio.streaming.commons.streams.StratioStream
import com.stratio.streaming.dto.StratioQueryStream
import com.stratio.streaming.kafka.KafkaConsumer
import com.stratio.streaming.kafka.KafkaProducer
import com.stratio.streaming.messaging.InsertMessageBuilder
import com.stratio.streaming.messaging.QueryMessageBuilder
import com.stratio.streaming.messaging.StreamMessageBuilder
import com.stratio.streaming.zookeeper.ZookeeperConsumer

import StratioStreamingAPI.config

class StratioStreamingAPI
  extends IStratioStreamingAPI {
  import StratioStreamingAPI._

  def createStream(streamName: String, columns: List[ColumnNameType]) = {
    checkStreamingStatus()
    val operation = DEFINITION.CREATE.toLowerCase
    val creationStreamMessage = MessageBuilderWithColumns(sessionId, operation).build(streamName, columns)
    syncOperation.performSyncOperation(creationStreamMessage)
  }

  def alterStream(streamName: String, columns: List[ColumnNameType]) = {
    checkStreamingStatus()
    val operation = ALTER.toLowerCase
    val alterStreamMessage = MessageBuilderWithColumns(sessionId, operation).build(streamName, columns)
    syncOperation.performSyncOperation(alterStreamMessage)
  }

  def insertData(streamName: String, data: List[ColumnNameValue]) = {
    checkStreamingStatus()
    val insertStreamMessage = InsertMessageBuilder(sessionId).build(streamName, data)
    asyncOperation.performAsyncOperation(insertStreamMessage)
  }

  def addQuery(streamName: String, query: String): String = {
    checkStreamingStatus()
    val operation = ADD_QUERY.toLowerCase
    val addQueryStreamMessage = QueryMessageBuilder(sessionId).build(streamName, query, operation)
    syncOperation.performSyncOperation(addQueryStreamMessage)
    getQueryId(streamName, query)
  }

  def getQueryId(streamName: String, query: String): String = {
    val queries = queriesFromStream(streamName)
    val addedQuery = queries.find(theQuery => theQuery.query.equals(query))
    addedQuery match {
      case Some(q) => q.queryId
      case _ => ""
    }
  }

  def removeQuery(streamName: String, queryId: String) = {
    checkStreamingStatus()
    val operation = REMOVE_QUERY.toLowerCase
    val removeQueryMessage = QueryMessageBuilder(sessionId).build(streamName, queryId, operation)
    syncOperation.performSyncOperation(removeQueryMessage)
  }

  def dropStream(streamName: String) = {
    checkStreamingStatus()
    val operation = DROP.toLowerCase
    val dropStreamMessage = StreamMessageBuilder(sessionId).build(streamName, operation)
    syncOperation.performSyncOperation(dropStreamMessage)
  }

  def listenStream(streamName: String) = {
    checkStreamingStatus()
    val operation = LISTEN.toLowerCase
    val listenStreamMessage = StreamMessageBuilder(sessionId).build(streamName, operation)
    syncOperation.performSyncOperation(listenStreamMessage)
    val kafkaConsumer = new KafkaConsumer(streamName, zookeeperCluster)
    streamingListeners.put(streamName, kafkaConsumer)
    kafkaConsumer.stream
  }
<commit_msg>Listen operation now read from te last event readed.<commit_after>/**
 * Copyright (C) 2014 Stratio (http://stratio.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.stratio.streaming.api

import java.util.List

import scala.collection.JavaConversions.asScalaBuffer
import scala.collection.JavaConversions.bufferAsJavaList

import org.apache.curator.framework.CuratorFramework
import org.apache.curator.framework.CuratorFrameworkFactory
import org.apache.curator.framework.api.CuratorEvent
import org.apache.curator.framework.api.CuratorEventType.WATCHED
import org.apache.curator.framework.api.CuratorListener
import org.apache.curator.retry.RetryOneTime
import org.slf4j.LoggerFactory

import com.stratio.streaming.api.messaging.ColumnNameType
import com.stratio.streaming.api.messaging.ColumnNameValue
import com.stratio.streaming.api.messaging.MessageBuilder.builder
import com.stratio.streaming.api.messaging.MessageBuilderWithColumns
import com.stratio.streaming.commons.constants.InternalTopic
import com.stratio.streaming.commons.constants.STREAMING.ZK_EPHEMERAL_NODE_PATH
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.ACTION.INDEX
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.ACTION.LISTEN
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.ACTION.SAVETO_CASSANDRA
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.ACTION.SAVETO_MONGO
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.ACTION.STOP_INDEX
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.ACTION.STOP_LISTEN
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.ACTION.STOP_SAVETO_CASSANDRA
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.ACTION.STOP_SAVETO_MONGO
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.DEFINITION
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.DEFINITION.ADD_QUERY
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.DEFINITION.ALTER
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.DEFINITION.DROP
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.DEFINITION.REMOVE_QUERY
import com.stratio.streaming.commons.constants.STREAM_OPERATIONS.MANIPULATION.LIST
import com.stratio.streaming.commons.exceptions.StratioEngineConnectionException
import com.stratio.streaming.commons.exceptions.StratioEngineOperationException
import com.stratio.streaming.commons.exceptions.StratioEngineStatusException
import com.stratio.streaming.commons.kafka.service.KafkaTopicService
import com.stratio.streaming.commons.kafka.service.TopicService
import com.stratio.streaming.commons.messages.ColumnNameTypeValue
import com.stratio.streaming.commons.streams.StratioStream
import com.stratio.streaming.dto.StratioQueryStream
import com.stratio.streaming.kafka.KafkaConsumer
import com.stratio.streaming.kafka.KafkaProducer
import com.stratio.streaming.messaging.InsertMessageBuilder
import com.stratio.streaming.messaging.QueryMessageBuilder
import com.stratio.streaming.messaging.StreamMessageBuilder
import com.stratio.streaming.zookeeper.ZookeeperConsumer

import StratioStreamingAPI.config

class StratioStreamingAPI
  extends IStratioStreamingAPI {
  import StratioStreamingAPI._

  def createStream(streamName: String, columns: List[ColumnNameType]) = {
    checkStreamingStatus()
    val operation = DEFINITION.CREATE.toLowerCase
    val creationStreamMessage = MessageBuilderWithColumns(sessionId, operation).build(streamName, columns)
    syncOperation.performSyncOperation(creationStreamMessage)
  }

  def alterStream(streamName: String, columns: List[ColumnNameType]) = {
    checkStreamingStatus()
    val operation = ALTER.toLowerCase
    val alterStreamMessage = MessageBuilderWithColumns(sessionId, operation).build(streamName, columns)
    syncOperation.performSyncOperation(alterStreamMessage)
  }

  def insertData(streamName: String, data: List[ColumnNameValue]) = {
    checkStreamingStatus()
    val insertStreamMessage = InsertMessageBuilder(sessionId).build(streamName, data)
    asyncOperation.performAsyncOperation(insertStreamMessage)
  }

  def addQuery(streamName: String, query: String): String = {
    checkStreamingStatus()
    val operation = ADD_QUERY.toLowerCase
    val addQueryStreamMessage = QueryMessageBuilder(sessionId).build(streamName, query, operation)
    syncOperation.performSyncOperation(addQueryStreamMessage)
    getQueryId(streamName, query)
  }

  def getQueryId(streamName: String, query: String): String = {
    val queries = queriesFromStream(streamName)
    val addedQuery = queries.find(theQuery => theQuery.query.equals(query))
    addedQuery match {
      case Some(q) => q.queryId
      case _ => ""
    }
  }

  def removeQuery(streamName: String, queryId: String) = {
    checkStreamingStatus()
    val operation = REMOVE_QUERY.toLowerCase
    val removeQueryMessage = QueryMessageBuilder(sessionId).build(streamName, queryId, operation)
    syncOperation.performSyncOperation(removeQueryMessage)
  }

  def dropStream(streamName: String) = {
    checkStreamingStatus()
    val operation = DROP.toLowerCase
    val dropStreamMessage = StreamMessageBuilder(sessionId).build(streamName, operation)
    syncOperation.performSyncOperation(dropStreamMessage)
  }

  def listenStream(streamName: String) = {
    checkStreamingStatus()
    val operation = LISTEN.toLowerCase
    val listenStreamMessage = StreamMessageBuilder(sessionId).build(streamName, operation)
    syncOperation.performSyncOperation(listenStreamMessage)
    val kafkaConsumer = new KafkaConsumer(streamName, zookeeperCluster, readFromStartOfStream = false)
    streamingListeners.put(streamName, kafkaConsumer)
    kafkaConsumer.stream
  }

{'fraction_non_alphanumeric': 0.07962548015364916, 'fraction_numerical': 0.001440460947503201, 'mean_word_length': 6.096536059057354, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1220019', 'n_tokens_mistral': 3574, 'n_tokens_neox': 3551, 'n_words': 624}