<commit_before>package com.ubirch.avatar.core.device

import com.typesafe.scalalogging.slf4j.StrictLogging
import com.ubirch.avatar.config.Config
import com.ubirch.avatar.model.db.device.Device
import com.ubirch.avatar.model.rest.device.{AvatarState, DeviceStateUpdate}
import com.ubirch.avatar.util.model.DeviceUtil
import com.ubirch.server.util.ServerKeys
import com.ubirch.util.elasticsearch.client.binary.storage.ESBulkStorage
import com.ubirch.util.json.{Json4sUtil, MyJsonProtocol}
import com.ubirch.util.uuid.UUIDUtil
import org.json4s._

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

/**
  * Created by derMicha on 09/11/16.
  */
object DeviceStateManager extends MyJsonProtocol with StrictLogging {

  private val index = Config.esDeviceStateIndex
  private val esType = Config.esDeviceStateType

  def createNewDeviceState(device: Device, avatarState: AvatarState): DeviceStateUpdate = {

    val payload = avatarState.delta.getOrElse(Json4sUtil.string2JValue("{}").get)

    val s = DeviceUtil.sign(payload)

    DeviceStateUpdate(
      id = UUIDUtil.uuid,
      k = ServerKeys.pubKeyB64,
      s = s,
      ds = avatarState.currentDeviceSignature,
      p = payload
    )
  }

  /**
    * Insert or update an [[DeviceStateUpdate]].
    *
    * @param state object to upsert
    * @return json of what we stored; None if something went wrong
    */
  def upsert(state: DeviceStateUpdate): Future[Option[DeviceStateUpdate]] = {

    Json4sUtil.any2jvalue(state) match {

      case Some(doc) =>

        val id = state.id.toString
        ESBulkStorage.storeDocBulk(
          docIndex = index,
          docType = esType,
          docId = id,
          doc = doc
        ) map (_.extractOpt[DeviceStateUpdate])

      case None => Future(None)

    }
  }
}<commit_msg>test a concurrency fix for the blocking es store<commit_after>package com.ubirch.avatar.core.device

import com.typesafe.scalalogging.slf4j.StrictLogging
import com.ubirch.avatar.config.Config
import com.ubirch.avatar.model.db.device.Device
import com.ubirch.avatar.model.rest.device.{AvatarState, DeviceStateUpdate}
import com.ubirch.avatar.util.model.DeviceUtil
import com.ubirch.server.util.ServerKeys
import com.ubirch.util.elasticsearch.client.binary.storage.ESBulkStorage
import com.ubirch.util.json.{Json4sUtil, MyJsonProtocol}
import com.ubirch.util.uuid.UUIDUtil
import org.json4s._

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

/**
  * Created by derMicha on 09/11/16.
  */
object DeviceStateManager extends MyJsonProtocol with StrictLogging {

  private val index = Config.esDeviceStateIndex
  private val esType = Config.esDeviceStateType

  def createNewDeviceState(device: Device, avatarState: AvatarState): DeviceStateUpdate = {

    val payload = avatarState.delta.getOrElse(Json4sUtil.string2JValue("{}").get)

    val s = DeviceUtil.sign(payload)

    DeviceStateUpdate(
      id = UUIDUtil.uuid,
      k = ServerKeys.pubKeyB64,
      s = s,
      ds = avatarState.currentDeviceSignature,
      p = payload
    )
  }

  /**
    * Insert or update an [[DeviceStateUpdate]].
    *
    * @param state object to upsert
    * @return json of what we stored; None if something went wrong
    */
  def upsert(state: DeviceStateUpdate): Future[Option[DeviceStateUpdate]] = {

    Json4sUtil.any2jvalue(state) match {

      case Some(doc) =>

        val id = state.id.toString
        Future {
          val start = System.currentTimeMillis()
          ESBulkStorage.storeDocBulk(
            docIndex = index,
            docType = esType,
            docId = id,
            doc = doc
          )
          logger.debug(s"DeviceStateUpdate: took ${System.currentTimeMillis() - start}ms")
        }

        Future(doc.extractOpt[DeviceStateUpdate])

      case None => Future(None)

    }
  }
}
{'fraction_non_alphanumeric': 0.09724013412432293, 'fraction_numerical': 0.007737941707505804, 'mean_word_length': 3.275633958103638, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3958608', 'n_tokens_mistral': 1253, 'n_tokens_neox': 1212, 'n_words': 247}