
import grizzled.slf4j.Logger
import org.apache.ignite.{Ignite, Ignition}
import IgniteConfigGenerator._

import scala.util.Try


object IgniteMain {

  private val logger = Logger[IgniteMain.type]

  var myIgnite : Option[Ignite] = None

    def startIgnite() : Unit = {
      val cfg = basicIgniteConfig("localhost", 47500, 47100, 10)
      logger.info("starting ignite")
      myIgnite = Option(Ignition.start(cfg))
      myIgnite.get.active(true)
    }

      def main(args: Array[String]): Unit = {
        logger.info("start ignite")
        new Thread{override def run() : Unit = startIgnite()}.start()

        while (myIgnite.isEmpty)
          Thread.sleep(1000)  // give ignite a little time to initialize

        val ignite = myIgnite.get
        val longString = "four score and seven years ago our forefathers brought forth onto this " +
          "continent a new nation conceived in liberty and dedicated to the idal that all" +
          "men are created equal abc"
        val longerString =
          longString + longString + longString + longString + longString +
            longString + longString + longString + longString + longString +
            longString + longString + longString + longString + longString +
            longString + longString + longString + longString + longString +
            longString + longString + longString + longString + longString +
            longString + longString

        logger.info("create cache")
        val aCache = initCache[Int, String](ignite, "fubar", persistenceEnabled = true,  Some("RefData"))
        import java.util.{TreeMap => JavaMap}
        val putMap = new JavaMap[Int, String]

        val max = 35000
        val chunkSize = 25
        val chunks = max /chunkSize


        logger.info("load cache via put ")
        for ( a <- 1 to max) {
          putMap.put(a, longerString)
        }

        for(b <-1 to chunks) {
          aCache.putAll(putMap.subMap((b-1)*chunkSize,b*chunkSize))
        }
        aCache.put(max, longerString)

        Thread.sleep(1000)
        logger.info(s"${aCache.metrics}")

        Try {
          for ( a <- 1 to max) {
            aCache.get(a)
          }
        }
        Thread.sleep(1000)
        logger.info(s"${aCache.metrics}")

        logger.info("thats all folks")
      }
  }
