text:
package org.apache.spark.storage

import java.nio.{ByteBuffer, MappedByteBuffer}
import java.util.Arrays
import java.util.concurrent.TimeUnit

import scala.collection.mutable.ArrayBuffer
import scala.concurrent.Await
import scala.concurrent.duration._
import scala.language.implicitConversions
import scala.language.postfixOps

import akka.actor._
import akka.pattern.ask
import akka.util.Timeout

import org.mockito.Mockito.{mock, when}

import org.scalatest._
import org.scalatest.concurrent.Eventually._
import org.scalatest.concurrent.Timeouts._

import org.apache.spark.{MapOutputTrackerMaster, SparkConf, SparkContext, SecurityManager}
import org.apache.spark.executor.DataReadMethod
import org.apache.spark.network.nio.NioBlockTransferService
import org.apache.spark.scheduler.LiveListenerBus
import org.apache.spark.serializer.{JavaSerializer, KryoSerializer}
import org.apache.spark.shuffle.hash.HashShuffleManager
import org.apache.spark.storage.BlockManagerMessages.BlockManagerHeartbeat
import org.apache.spark.util._

class BlockManagerSuite extends FunSuite with Matchers with BeforeAndAfterEach
  with PrivateMethodTester with ResetSystemProperties {

  private val conf = new SparkConf(false)
  var store: BlockManager = null
  var store2: BlockManager = null
  var actorSystem: ActorSystem = null
  var master: BlockManagerMaster = null
  conf.set("spark.authenticate", "false")
  val securityMgr = new SecurityManager(conf)
  val mapOutputTracker = new MapOutputTrackerMaster(conf)
  val shuffleManager = new HashShuffleManager(conf)

  // Reuse a serializer across tests to avoid creating a new thread-local buffer on each test
  conf.set("spark.kryoserializer.buffer.mb", "1")
  val serializer = new KryoSerializer(conf)

  // Implicitly convert strings to BlockIds for test clarity.
  implicit def StringToBlockId(value: String): BlockId = new TestBlockId(value)
  def rdd(rddId: Int, splitId: Int) = RDDBlockId(rddId, splitId)

  private def makeBlockManager(
      maxMem: Long,
      name: String = SparkContext.DRIVER_IDENTIFIER): BlockManager = {
    val transfer = new NioBlockTransferService(conf, securityMgr)
    val manager = new BlockManager(name, actorSystem, master, serializer, maxMem, conf,
      mapOutputTracker, shuffleManager, transfer, securityMgr, 0)
    manager.initialize("app-id")
    manager
  }

  override def beforeEach(): Unit = {
    val (actorSystem, boundPort) = AkkaUtils.createActorSystem(
      "test", "localhost", 0, conf = conf, securityManager = securityMgr)
    this.actorSystem = actorSystem

    // Set the arch to 64-bit and compressedOops to true to get a deterministic test-case
    System.setProperty("os.arch", "amd64")
    conf.set("os.arch", "amd64")
    conf.set("spark.test.useCompressedOops", "true")
    conf.set("spark.driver.port", boundPort.toString)
    conf.set("spark.storage.unrollFraction", "0.4")
    conf.set("spark.storage.unrollMemoryThreshold", "512")

    master = new BlockManagerMaster(
      actorSystem.actorOf(Props(new BlockManagerMasterActor(true, conf, new LiveListenerBus))),
      conf, true)

    val initialize = PrivateMethod[Unit]('initialize)
    SizeEstimator invokePrivate initialize()
  }

  override def afterEach(): Unit = {
    if (store != null) {
      store.stop()
      store = null
    }
    if (store2 != null) {
      store2.stop()
      store2 = null
    }
    actorSystem.shutdown()
    actorSystem.awaitTermination()
    actorSystem = null
    master = null
  }

  test("StorageLevel object caching") {
    val level1 = StorageLevel(false, false, false, false, 3)
    val level2 = StorageLevel(false, false, false, false, 3) // this should return the same object as level1
    val level3 = StorageLevel(false, false, false, false, 2) // this should return a different object
    assert(level2 === level1, "level2 is not same as level1")
    assert(level2.eq(level1), "level2 is not the same object as level1")
    assert(level3 != level1, "level3 is same as level1")
    val bytes1 = Utils.serialize(level1)
    val level1_ = Utils.deserialize[StorageLevel](bytes1)
    val bytes2 = Utils.serialize(level2)
    val level2_ = Utils.deserialize[StorageLevel](bytes2)
    assert(level1_ === level1, "Deserialized level1 not same as original level1")
    assert(level1_.eq(level1), "Deserialized level1 not the same object as original level2")
    assert(level2_ === level2, "Deserialized level2 not same as original level2")
    assert(level2_.eq(level1), "Deserialized level2 not the same object as original level1")
  }

  test("BlockManagerId object caching") {
    val id1 = BlockManagerId("e1", "XXX", 1)
    val id2 = BlockManagerId("e1", "XXX", 1) // this should return the same object as id1
    val id3 = BlockManagerId("e1", "XXX", 2) // this should return a different object
    assert(id2 === id1, "id2 is not same as id1")
    assert(id2.eq(id1), "id2 is not the same object as id1")
    assert(id3 != id1, "id3 is same as id1")
    val bytes1 = Utils.serialize(id1)
    val id1_ = Utils.deserialize[BlockManagerId](bytes1)
    val bytes2 = Utils.serialize(id2)
    val id2_ = Utils.deserialize[BlockManagerId](bytes2)
    assert(id1_ === id1, "Deserialized id1 is not same as original id1")
    assert(id1_.eq(id1), "Deserialized id1 is not the same object as original id1")
    assert(id2_ === id2, "Deserialized id2 is not same as original id2")
    assert(id2_.eq(id1), "Deserialized id2 is not the same object as original id1")
  }

  test("master + 1 manager interaction") {
    store = makeBlockManager(20000)
    val a1 = new Array[Byte](4000)
    val a2 = new Array[Byte](4000)
    val a3 = new Array[Byte](4000)

    // Putting a1, a2  and a3 in memory and telling master only about a1 and a2
    store.putSingle("a1", a1, StorageLevel.MEMORY_ONLY)
    store.putSingle("a2", a2, StorageLevel.MEMORY_ONLY)
    store.putSingle("a3", a3, StorageLevel.MEMORY_ONLY, tellMaster = false)

    // Checking whether blocks are in memory
    assert(store.getSingle("a1").isDefined, "a1 was not in store")
    assert(store.getSingle("a2").isDefined, "a2 was not in store")
    assert(store.getSingle("a3").isDefined, "a3 was not in store")

    // Checking whether master knows about the blocks or not
    assert(master.getLocations("a1").size > 0, "master was not told about a1")
    assert(master.getLocations("a2").size > 0, "master was not told about a2")
    assert(master.getLocations("a3").size === 0, "master was told about a3")

    // Drop a1 and a2 from memory; this should be reported back to the master
    store.dropFromMemory("a1", null)
    store.dropFromMemory("a2", null)
    assert(store.getSingle("a1") === None, "a1 not removed from store")
    assert(store.getSingle("a2") === None, "a2 not removed from store")
    assert(master.getLocations("a1").size === 0, "master did not remove a1")
    assert(master.getLocations("a2").size === 0, "master did not remove a2")
  }

  test("master + 2 managers interaction") {
    store = makeBlockManager(2000, "exec1")
    store2 = makeBlockManager(2000, "exec2")

    val peers = master.getPeers(store.blockManagerId)
    assert(peers.size === 1, "master did not return the other manager as a peer")
    assert(peers.head === store2.blockManagerId, "peer returned by master is not the other manager")

    val a1 = new Array[Byte](400)
    val a2 = new Array[Byte](400)
    store.putSingle("a1", a1, StorageLevel.MEMORY_ONLY_2)
    store2.putSingle("a2", a2, StorageLevel.MEMORY_ONLY_2)
    assert(master.getLocations("a1").size === 2, "master did not report 2 locations for a1")
    assert(master.getLocations("a2").size === 2, "master did not report 2 locations for a2")
  }

  test("removing block") {
    store = makeBlockManager(20000)
    val a1 = new Array[Byte](4000)
    val a2 = new Array[Byte](4000)
    val a3 = new Array[Byte](4000)

    // Putting a1, a2 and a3 in memory and telling master only about a1 and a2
    store.putSingle("a1-to-remove", a1, StorageLevel.MEMORY_ONLY)
    store.putSingle("a2-to-remove", a2, StorageLevel.MEMORY_ONLY)
    store.putSingle("a3-to-remove", a3, StorageLevel.MEMORY_ONLY, tellMaster = false)

    // Checking whether blocks are in memory and memory size
    val memStatus = master.getMemoryStatus.head._2
    assert(memStatus._1 == 20000L, "total memory " + memStatus._1 + " should equal 20000")
    assert(memStatus._2 <= 12000L, "remaining memory " + memStatus._2 + " should <= 12000")
    assert(store.getSingle("a1-to-remove").isDefined, "a1 was not in store")
    assert(store.getSingle("a2-to-remove").isDefined, "a2 was not in store")
    assert(store.getSingle("a3-to-remove").isDefined, "a3 was not in store")

    // Checking whether master knows about the blocks or not
    assert(master.getLocations("a1-to-remove").size > 0, "master was not told about a1")
    assert(master.getLocations("a2-to-remove").size > 0, "master was not told about a2")
    assert(master.getLocations("a3-to-remove").size === 0, "master was told about a3")

    // Remove a1 and a2 and a3. Should be no-op for a3.
    master.removeBlock("a1-to-remove")
    master.removeBlock("a2-to-remove")
    master.removeBlock("a3-to-remove")

    eventually(timeout(1000 milliseconds), interval(10 milliseconds)) {
      store.getSingle("a1-to-remove") should be (None)
      master.getLocations("a1-to-remove") should have size 0
    }
    eventually(timeout(1000 milliseconds), interval(10 milliseconds)) {
      store.getSingle("a2-to-remove") should be (None)
      master.getLocations("a2-to-remove") should have size 0
    }
    eventually(timeout(1000 milliseconds), interval(10 milliseconds)) {
      store.getSingle("a3-to-remove") should not be (None)
      master.getLocations("a3-to-remove") should have size 0
    }
    eventually(timeout(1000 milliseconds), interval(10 milliseconds)) {
      val memStatus = master.getMemoryStatus.head._2
      memStatus._1 should equal (20000L)
      memStatus._2 should equal (20000L)
    }
  }

  test("removing rdd") {
    store = makeBlockManager(20000)
    val a1 = new Array[Byte](4000)
    val a2 = new Array[Byte](4000)
    val a3 = new Array[Byte](4000)
    // Putting a1, a2 and a3 in memory.
    store.putSingle(rdd(0, 0), a1, StorageLevel.MEMORY_ONLY)
    store.putSingle(rdd(0, 1), a2, StorageLevel.MEMORY_ONLY)
    store.putSingle("nonrddblock", a3, StorageLevel.MEMORY_ONLY)
    master.removeRdd(0, blocking = false)

    eventually(timeout(1000 milliseconds), interval(10 milliseconds)) {
      store.getSingle(rdd(0, 0)) should be (None)
      master.getLocations(rdd(0, 0)) should have size 0
    }
    eventually(timeout(1000 milliseconds), interval(10 milliseconds)) {
      store.getSingle(rdd(0, 1)) should be (None)
      master.getLocations(rdd(0, 1)) should have size 0
    }
    eventually(timeout(1000 milliseconds), interval(10 milliseconds)) {
      store.getSingle("nonrddblock") should not be (None)
      master.getLocations("nonrddblock") should have size (1)
    }

    store.putSingle(rdd(0, 0), a1, StorageLevel.MEMORY_ONLY)
    store.putSingle(rdd(0, 1), a2, StorageLevel.MEMORY_ONLY)
    master.removeRdd(0, blocking = true)
    store.getSingle(rdd(0, 0)) should be (None)
    master.getLocations(rdd(0, 0)) should have size 0
    store.getSingle(rdd(0, 1)) should be (None)
    master.getLocations(rdd(0, 1)) should have size 0
  }

  test("removing broadcast") {
    store = makeBlockManager(2000)
    val driverStore = store
    val executorStore = makeBlockManager(2000, "executor")
    val a1 = new Array[Byte](400)
    val a2 = new Array[Byte](400)
    val a3 = new Array[Byte](400)
    val a4 = new Array[Byte](400)

    val broadcast0BlockId = BroadcastBlockId(0)
    val broadcast1BlockId = BroadcastBlockId(1)
    val broadcast2BlockId = BroadcastBlockId(2)
    val broadcast2BlockId2 = BroadcastBlockId(2, "_")

    // insert broadcast blocks in both the stores
    Seq(driverStore, executorStore).foreach { case s =>
      s.putSingle(broadcast0BlockId, a1, StorageLevel.DISK_ONLY)
      s.putSingle(broadcast1BlockId, a2, StorageLevel.DISK_ONLY)
      s.putSingle(broadcast2BlockId, a3, StorageLevel.DISK_ONLY)
      s.putSingle(broadcast2BlockId2, a4, StorageLevel.DISK_ONLY)
    }

    // verify whether the blocks exist in both the stores
    Seq(driverStore, executorStore).foreach { case s =>
      s.getLocal(broadcast0BlockId) should not be (None)
      s.getLocal(broadcast1BlockId) should not be (None)
      s.getLocal(broadcast2BlockId) should not be (None)
      s.getLocal(broadcast2BlockId2) should not be (None)
    }

    // remove broadcast 0 block only from executors
    master.removeBroadcast(0, removeFromMaster = false, blocking = true)

    // only broadcast 0 block should be removed from the executor store
    executorStore.getLocal(broadcast0BlockId) should be (None)
    executorStore.getLocal(broadcast1BlockId) should not be (None)
    executorStore.getLocal(broadcast2BlockId) should not be (None)

    // nothing should be removed from the driver store
    driverStore.getLocal(broadcast0BlockId) should not be (None)
    driverStore.getLocal(broadcast1BlockId) should not be (None)
    driverStore.getLocal(broadcast2BlockId) should not be (None)

    // remove broadcast 0 block from the driver as well
    master.removeBroadcast(0, removeFromMaster = true, blocking = true)
    driverStore.getLocal(broadcast0BlockId) should be (None)
    driverStore.getLocal(broadcast1BlockId) should not be (None)

    // remove broadcast 1 block from both the stores asynchronously
    // and verify all broadcast 1 blocks have been removed
    master.removeBroadcast(1, removeFromMaster = true, blocking = false)
    eventually(timeout(1000 milliseconds), interval(10 milliseconds)) {
      driverStore.getLocal(broadcast1BlockId) should be (None)
      executorStore.getLocal(broadcast1BlockId) should be (None)
    }

    // remove broadcast 2 from both the stores asynchronously
    // and verify all broadcast 2 blocks have been removed
    master.removeBroadcast(2, removeFromMaster = true, blocking = false)
    eventually(timeout(1000 milliseconds), interval(10 milliseconds)) {
      driverStore.getLocal(broadcast2BlockId) should be (None)
      driverStore.getLocal(broadcast2BlockId2) should be (None)
      executorStore.getLocal(broadcast2BlockId) should be (None)
      executorStore.getLocal(broadcast2BlockId2) should be (None)
    }
    executorStore.stop()
    driverStore.stop()
    store = null
  }

  test("reregistration on heart beat") {
    store = makeBlockManager(2000)
    val a1 = new Array[Byte](400)

    store.putSingle("a1", a1, StorageLevel.MEMORY_ONLY)

    assert(store.getSingle("a1").isDefined, "a1 was not in store")
    assert(master.getLocations("a1").size > 0, "master was not told about a1")

    master.removeExecutor(store.blockManagerId.executorId)
    assert(master.getLocations("a1").size == 0, "a1 was not removed from master")

    implicit val timeout = Timeout(30, TimeUnit.SECONDS)
    val reregister = !Await.result(
      master.driverActor ? BlockManagerHeartbeat(store.blockManagerId),
      timeout.duration).asInstanceOf[Boolean]
    assert(reregister == true)
  }

  test("reregistration on block update") {
    store = makeBlockManager(2000)
    val a1 = new Array[Byte](400)
    val a2 = new Array[Byte](400)

    store.putSingle("a1", a1, StorageLevel.MEMORY_ONLY)
    assert(master.getLocations("a1").size > 0, "master was not told about a1")

    master.removeExecutor(store.blockManagerId.executorId)
    assert(master.getLocations("a1").size == 0, "a1 was not removed from master")

    store.putSingle("a2", a2, StorageLevel.MEMORY_ONLY)
    store.waitForAsyncReregister()

    assert(master.getLocations("a1").size > 0, "a1 was not reregistered with master")
    assert(master.getLocations("a2").size > 0, "master was not told about a2")
  }

  test("reregistration doesn't dead lock") {
    store = makeBlockManager(2000)
    val a1 = new Array[Byte](400)
    val a2 = List(new Array[Byte](400))

    // try many times to trigger any deadlocks
    for (i <- 1 to 100) {
      master.removeExecutor(store.blockManagerId.executorId)
      val t1 = new Thread {
        override def run() {
          store.putIterator("a2", a2.iterator, StorageLevel.MEMORY_ONLY, tellMaster = true)
        }
      }
      val t2 = new Thread {
        override def run() {
          store.putSingle("a1", a1, StorageLevel.MEMORY_ONLY)
        }
      }
      val t3 = new Thread {
        override def run() {
          store.reregister()
        }
      }

      t1.start()
      t2.start()
      t3.start()
      t1.join()
      t2.join()
      t3.join()

      store.dropFromMemory("a1", null)
      store.dropFromMemory("a2", null)
      store.waitForAsyncReregister()
    }
  }

  test("correct BlockResult returned from get() calls") {
    store = makeBlockManager(12000)
    val list1 = List(new Array[Byte](2000), new Array[Byte](2000))
    val list2 = List(new Array[Byte](500), new Array[Byte](1000), new Array[Byte](1500))
    val list1SizeEstimate = SizeEstimator.estimate(list1.iterator.toArray)
    val list2SizeEstimate = SizeEstimator.estimate(list2.iterator.toArray)
    store.putIterator("list1", list1.iterator, StorageLevel.MEMORY_ONLY, tellMaster = true)
    store.putIterator("list2memory", list2.iterator, StorageLevel.MEMORY_ONLY, tellMaster = true)
    store.putIterator("list2disk", list2.iterator, StorageLevel.DISK_ONLY, tellMaster = true)
    val list1Get = store.get("list1")
    assert(list1Get.isDefined, "list1 expected to be in store")
    assert(list1Get.get.data.size === 2)
    assert(list1Get.get.inputMetrics.bytesRead === list1SizeEstimate)
    assert(list1Get.get.inputMetrics.readMethod === DataReadMethod.Memory)
    val list2MemoryGet = store.get("list2memory")
    assert(list2MemoryGet.isDefined, "list2memory expected to be in store")
    assert(list2MemoryGet.get.data.size === 3)
    assert(list2MemoryGet.get.inputMetrics.bytesRead === list2SizeEstimate)
    assert(list2MemoryGet.get.inputMetrics.readMethod === DataReadMethod.Memory)
    val list2DiskGet = store.get("list2disk")
    assert(list2DiskGet.isDefined, "list2memory expected to be in store")
    assert(list2DiskGet.get.data.size === 3)
    // We don't know the exact size of the data on disk, but it should certainly be > 0.
    assert(list2DiskGet.get.inputMetrics.bytesRead > 0)
    assert(list2DiskGet.get.inputMetrics.readMethod === DataReadMethod.Disk)
  }

  test("in-memory LRU storage") {
    store = makeBlockManager(12000)
    val a1 = new Array[Byte](4000)
    val a2 = new Array[Byte](4000)
    val a3 = new Array[Byte](4000)
    store.putSingle("a1", a1, StorageLevel.MEMORY_ONLY)
    store.putSingle("a2", a2, StorageLevel.MEMORY_ONLY)
    store.putSingle("a3", a3, StorageLevel.MEMORY_ONLY)
    assert(store.getSingle("a2").isDefined, "a2 was not in store")
    assert(store.getSingle("a3").isDefined, "a3 was not in store")
    assert(store.getSingle("a1") === None, "a1 was in store")
    assert(store.getSingle("a2").isDefined, "a2 was not in store")
    // At this point a2 was gotten last, so LRU will getSingle rid of a3
    store.putSingle("a1", a1, StorageLevel.MEMORY_ONLY)
    assert(store.getSingle("a1").isDefined, "a1 was not in store")
    assert(store.getSingle("a2").isDefined, "a2 was not in store")
    assert(store.getSingle("a3") === None, "a3 was in store")
  }

  test("in-memory LRU storage with serialization") {
    store = makeBlockManager(12000)
    val a1 = new Array[Byte](4000)
    val a2 = new Array[Byte](4000)
    val a3 = new Array[Byte](4000)
    store.putSingle("a1", a1, StorageLevel.MEMORY_ONLY_SER)
    store.putSingle("a2", a2, StorageLevel.MEMORY_ONLY_SER)
    store.putSingle("a3", a3, StorageLevel.MEMORY_ONLY_SER)
    assert(store.getSingle("a2").isDefined, "a2 was not in store")
    assert(store.getSingle("a3").isDefined, "a3 was not in store")
    assert(store.getSingle("a1") === None, "a1 was in store")
    assert(store.getSingle("a2").isDefined, "a2 was not in store")
    // At this point a2 was gotten last, so LRU will getSingle rid of a3
    store.putSingle("a1", a1, StorageLevel.MEMORY_ONLY_SER)
    assert(store.getSingle("a1").isDefined, "a1 was not in store")
    assert(store.getSingle("a2").isDefined, "a2 was not in store")
    assert(store.getSingle("a3") === None, "a3 was in store")
  }

  test("in-memory LRU for partitions of same RDD") {
    store = makeBlockManager(12000)
    val a1 = new Array[Byte](4000)
    val a2 = new Array[Byte](4000)
    val a3 = new Array[Byte](4000)
    store.putSingle(rdd(0, 1), a1, StorageLevel.MEMORY_ONLY)
    store.putSingle(rdd(0, 2), a2, StorageLevel.MEMORY_ONLY)
    store.putSingle(rdd(0, 3), a3, StorageLevel.MEMORY_ONLY)
    // Even though we accessed rdd_0_3 last, it should not have replaced partitions 1 and 2
    // from the same RDD
    assert(store.getSingle(rdd(0, 3)) === None, "rdd_0_3 was in store")
    assert(store.getSingle(rdd(0, 2)).isDefined, "rdd_0_2 was not in store")
    assert(store.getSingle(rdd(0, 1)).isDefined, "rdd_0_1 was not in store")
    // Check that rdd_0_3 doesn't replace them even after further accesses
    assert(store.getSingle(rdd(0, 3)) === None, "rdd_0_3 was in store")
    assert(store.getSingle(rdd(0, 3)) === None, "rdd_0_3 was in store")
    assert(store.getSingle(rdd(0, 3)) === None, "rdd_0_3 was in store")
  }

  test("in-memory LRU for partitions of multiple RDDs") {
    store = makeBlockManager(12000)
    store.putSingle(rdd(0, 1), new Array[Byte](4000), StorageLevel.MEMORY_ONLY)
    store.putSingle(rdd(0, 2), new Array[Byte](4000), StorageLevel.MEMORY_ONLY)
    store.putSingle(rdd(1, 1), new Array[Byte](4000), StorageLevel.MEMORY_ONLY)
    // At this point rdd_1_1 should've replaced rdd_0_1
    assert(store.memoryStore.contains(rdd(1, 1)), "rdd_1_1 was not in store")
    assert(!store.memoryStore.contains(rdd(0, 1)), "rdd_0_1 was in store")
    assert(store.memoryStore.contains(rdd(0, 2)), "rdd_0_2 was not in store")
    // Do a get() on rdd_0_2 so that it is the most recently used item
    assert(store.getSingle(rdd(0, 2)).isDefined, "rdd_0_2 was not in store")
    // Put in more partitions from RDD 0; they should replace rdd_1_1
    store.putSingle(rdd(0, 3), new Array[Byte](4000), StorageLevel.MEMORY_ONLY)
    store.putSingle(rdd(0, 4), new Array[Byte](4000), StorageLevel.MEMORY_ONLY)
    // Now rdd_1_1 should be dropped to add rdd_0_3, but then rdd_0_2 should *not* be dropped
    // when we try to add rdd_0_4.
    assert(!store.memoryStore.contains(rdd(1, 1)), "rdd_1_1 was in store")
    assert(!store.memoryStore.contains(rdd(0, 1)), "rdd_0_1 was in store")
    assert(!store.memoryStore.contains(rdd(0, 4)), "rdd_0_4 was in store")
    assert(store.memoryStore.contains(rdd(0, 2)), "rdd_0_2 was not in store")
    assert(store.memoryStore.contains(rdd(0, 3)), "rdd_0_3 was not in store")
  }

  test("tachyon storage") {
    // TODO Make the spark.test.tachyon.enable true after using tachyon 0.5.0 testing jar.
    val tachyonUnitTestEnabled = conf.getBoolean("spark.test.tachyon.enable", false)
    if (tachyonUnitTestEnabled) {
      store = makeBlockManager(1200)
      val a1 = new Array[Byte](400)
      val a2 = new Array[Byte](400)
      val a3 = new Array[Byte](400)
      store.putSingle("a1", a1, StorageLevel.OFF_HEAP)
      store.putSingle("a2", a2, StorageLevel.OFF_HEAP)
      store.putSingle("a3", a3, StorageLevel.OFF_HEAP)
      assert(store.getSingle("a3").isDefined, "a3 was in store")
      assert(store.getSingle("a2").isDefined, "a2 was in store")
      assert(store.getSingle("a1").isDefined, "a1 was in store")
    } else {
      info("tachyon storage test disabled.")
    }
  }

  test("on-disk storage") {
    store = makeBlockManager(1200)
    val a1 = new Array[Byte](400)
    val a2 = new Array[Byte](400)
    val a3 = new Array[Byte](400)
    store.putSingle("a1", a1, StorageLevel.DISK_ONLY)
    store.putSingle("a2", a2, StorageLevel.DISK_ONLY)
    store.putSingle("a3", a3, StorageLevel.DISK_ONLY)
    assert(store.getSingle("a2").isDefined, "a2 was in store")
    assert(store.getSingle("a3").isDefined, "a3 was in store")
    assert(store.getSingle("a1").isDefined, "a1 was in store")
  }

  test("disk and memory storage") {
    store = makeBlockManager(12000)
    val a1 = new Array[Byte](4000)
    val a2 = new Array[Byte](4000)
    val a3 = new Array[Byte](4000)
    store.putSingle("a1", a1, StorageLevel.MEMORY_AND_DISK)
    store.putSingle("a2", a2, StorageLevel.MEMORY_AND_DISK)
    store.putSingle("a3", a3, StorageLevel.MEMORY_AND_DISK)
    assert(store.getSingle("a2").isDefined, "a2 was not in store")
    assert(store.getSingle("a3").isDefined, "a3 was not in store")
    assert(store.memoryStore.getValues("a1") == None, "a1 was in memory store")
    assert(store.getSingle("a1").isDefined, "a1 was not in store")
    assert(store.memoryStore.getValues("a1").isDefined, "a1 was not in memory store")
  }

  test("disk and memory storage with getLocalBytes") {
    store = makeBlockManager(12000)
    val a1 = new Array[Byte](4000)
    val a2 = new Array[Byte](4000)
    val a3 = new Array[Byte](4000)
    store.putSingle("a1", a1, StorageLevel.MEMORY_AND_DISK)
    store.putSingle("a2", a2, StorageLevel.MEMORY_AND_DISK)
    store.putSingle("a3", a3, StorageLevel.MEMORY_AND_DISK)
    assert(store.getLocalBytes("a2").isDefined, "a2 was not in store")
    assert(store.getLocalBytes("a3").isDefined, "a3 was not in store")
    assert(store.memoryStore.getValues("a1") == None, "a1 was in memory store")
    assert(store.getLocalBytes("a1").isDefined, "a1 was not in store")
    assert(store.memoryStore.getValues("a1").isDefined, "a1 was not in memory store")
  }

  test("disk and memory storage with serialization") {
    store = makeBlockManager(12000)
    val a1 = new Array[Byte](4000)
    val a2 = new Array[Byte](4000)
    val a3 = new Array[Byte](4000)
    store.putSingle("a1", a1, StorageLevel.MEMORY_AND_DISK_SER)
    store.putSingle("a2", a2, StorageLevel.MEMORY_AND_DISK_SER)
    store.putSingle("a3", a3, StorageLevel.MEMORY_AND_DISK_SER)
    assert(store.getSingle("a2").isDefined, "a2 was not in store")
    assert(store.getSingle("a3").isDefined, "a3 was not in store")
    assert(store.memoryStore.getValues("a1") == None, "a1 was in memory store")
    assert(store.getSingle("a1").isDefined, "a1 was not in store")
    assert(store.memoryStore.getValues("a1").isDefined, "a1 was not in memory store")
  }

  test("disk and memory storage with serialization and getLocalBytes") {
    store = makeBlockManager(12000)
    val a1 = new Array[Byte](4000)
    val a2 = new Array[Byte](4000)
    val a3 = new Array[Byte](4000)
    store.putSingle("a1", a1, StorageLevel.MEMORY_AND_DISK_SER)
    store.putSingle("a2", a2, StorageLevel.MEMORY_AND_DISK_SER)
    store.putSingle("a3", a3, StorageLevel.MEMORY_AND_DISK_SER)
    assert(store.getLocalBytes("a2").isDefined, "a2 was not in store")
    assert(store.getLocalBytes("a3").isDefined, "a3 was not in store")
    assert(store.memoryStore.getValues("a1") == None, "a1 was in memory store")
    assert(store.getLocalBytes("a1").isDefined, "a1 was not in store")
    assert(store.memoryStore.getValues("a1").isDefined, "a1 was not in memory store")
  }

  test("LRU with mixed storage levels") {
    store = makeBlockManager(12000)
    val a1 = new Array[Byte](4000)
    val a2 = new Array[Byte](4000)
    val a3 = new Array[Byte](4000)
    val a4 = new Array[Byte](4000)
    // First store a1 and a2, both in memory, and a3, on disk only
    store.putSingle("a1", a1, StorageLevel.MEMORY_ONLY_SER)
    store.putSingle("a2", a2, StorageLevel.MEMORY_ONLY_SER)
    store.putSingle("a3", a3, StorageLevel.DISK_ONLY)
    // At this point LRU should not kick in because a3 is only on disk
    assert(store.getSingle("a1").isDefined, "a1 was not in store")
    assert(store.getSingle("a2").isDefined, "a2 was not in store")
    assert(store.getSingle("a3").isDefined, "a3 was not in store")
    // Now let's add in a4, which uses both disk and memory; a1 should drop out
    store.putSingle("a4", a4, StorageLevel.MEMORY_AND_DISK_SER)
    assert(store.getSingle("a1") == None, "a1 was in store")
    assert(store.getSingle("a2").isDefined, "a2 was not in store")
    assert(store.getSingle("a3").isDefined, "a3 was not in store")
    assert(store.getSingle("a4").isDefined, "a4 was not in store")
  }

  test("in-memory LRU with streams") {
    store = makeBlockManager(12000)
    val list1 = List(new Array[Byte](2000), new Array[Byte](2000))
    val list2 = List(new Array[Byte](2000), new Array[Byte](2000))
    val list3 = List(new Array[Byte](2000), new Array[Byte](2000))
    store.putIterator("list1", list1.iterator, StorageLevel.MEMORY_ONLY, tellMaster = true)
    store.putIterator("list2", list2.iterator, StorageLevel.MEMORY_ONLY, tellMaster = true)
    store.putIterator("list3", list3.iterator, StorageLevel.MEMORY_ONLY, tellMaster = true)
    assert(store.get("list2").isDefined, "list2 was not in store")
    assert(store.get("list2").get.data.size === 2)
    assert(store.get("list3").isDefined, "list3 was not in store")
    assert(store.get("list3").get.data.size === 2)
    assert(store.get("list1") === None, "list1 was in store")
    assert(store.get("list2").isDefined, "list2 was not in store")
    assert(store.get("list2").get.data.size === 2)
    // At this point list2 was gotten last, so LRU will getSingle rid of list3
    store.putIterator("list1", list1.iterator, StorageLevel.MEMORY_ONLY, tellMaster = true)
    assert(store.get("list1").isDefined, "list1 was not in store")
    assert(store.get("list1").get.data.size === 2)
    assert(store.get("list2").isDefined, "list2 was not in store")
    assert(store.get("list2").get.data.size === 2)
    assert(store.get("list3") === None, "list1 was in store")
  }

  test("LRU with mixed storage levels and streams") {
    store = makeBlockManager(12000)
    val list1 = List(new Array[Byte](2000), new Array[Byte](2000))
    val list2 = List(new Array[Byte](2000), new Array[Byte](2000))
    val list3 = List(new Array[Byte](2000), new Array[Byte](2000))
    val list4 = List(new Array[Byte](2000), new Array[Byte](2000))
    // First store list1 and list2, both in memory, and list3, on disk only
    store.putIterator("list1", list1.iterator, StorageLevel.MEMORY_ONLY_SER, tellMaster = true)
    store.putIterator("list2", list2.iterator, StorageLevel.MEMORY_ONLY_SER, tellMaster = true)
    store.putIterator("list3", list3.iterator, StorageLevel.DISK_ONLY, tellMaster = true)
    val listForSizeEstimate = new ArrayBuffer[Any]
    listForSizeEstimate ++= list1.iterator
    val listSize = SizeEstimator.estimate(listForSizeEstimate)
    // At this point LRU should not kick in because list3 is only on disk
    assert(store.get("list1").isDefined, "list1 was not in store")
    assert(store.get("list1").get.data.size === 2)
    assert(store.get("list2").isDefined, "list2 was not in store")
    assert(store.get("list2").get.data.size === 2)
    assert(store.get("list3").isDefined, "list3 was not in store")
    assert(store.get("list3").get.data.size === 2)
    assert(store.get("list1").isDefined, "list1 was not in store")
    assert(store.get("list1").get.data.size === 2)
    assert(store.get("list2").isDefined, "list2 was not in store")
    assert(store.get("list2").get.data.size === 2)
    assert(store.get("list3").isDefined, "list3 was not in store")
    assert(store.get("list3").get.data.size === 2)
    // Now let's add in list4, which uses both disk and memory; list1 should drop out
    store.putIterator("list4", list4.iterator, StorageLevel.MEMORY_AND_DISK_SER, tellMaster = true)
    assert(store.get("list1") === None, "list1 was in store")
    assert(store.get("list2").isDefined, "list2 was not in store")
    assert(store.get("list2").get.data.size === 2)
    assert(store.get("list3").isDefined, "list3 was not in store")
    assert(store.get("list3").get.data.size === 2)
    assert(store.get("list4").isDefined, "list4 was not in store")
    assert(store.get("list4").get.data.size === 2)
  }

  test("negative byte values in ByteBufferInputStream") {
    val buffer = ByteBuffer.wrap(Array[Int](254, 255, 0, 1, 2).map(_.toByte).toArray)
    val stream = new ByteBufferInputStream(buffer)
    val temp = new Array[Byte](10)
    assert(stream.read() === 254, "unexpected byte read")
    assert(stream.read() === 255, "unexpected byte read")
    assert(stream.read() === 0, "unexpected byte read")
    assert(stream.read(temp, 0, temp.length) === 2, "unexpected number of bytes read")
    assert(stream.read() === -1, "end of stream not signalled")
    assert(stream.read(temp, 0, temp.length) === -1, "end of stream not signalled")
  }

  test("overly large block") {
    store = makeBlockManager(5000)
    store.putSingle("a1", new Array[Byte](10000), StorageLevel.MEMORY_ONLY)
    assert(store.getSingle("a1") === None, "a1 was in store")
    store.putSingle("a2", new Array[Byte](10000), StorageLevel.MEMORY_AND_DISK)
    assert(store.memoryStore.getValues("a2") === None, "a2 was in memory store")
    assert(store.getSingle("a2").isDefined, "a2 was not in store")
  }

  test("block compression") {
    try {
      conf.set("spark.shuffle.compress", "true")
      store = makeBlockManager(20000, "exec1")
      store.putSingle(ShuffleBlockId(0, 0, 0), new Array[Byte](1000), StorageLevel.MEMORY_ONLY_SER)
      assert(store.memoryStore.getSize(ShuffleBlockId(0, 0, 0)) <= 100,
        "shuffle_0_0_0 was not compressed")
      store.stop()
      store = null

      conf.set("spark.shuffle.compress", "false")
      store = makeBlockManager(20000, "exec2")
      store.putSingle(ShuffleBlockId(0, 0, 0), new Array[Byte](10000), StorageLevel.MEMORY_ONLY_SER)
      assert(store.memoryStore.getSize(ShuffleBlockId(0, 0, 0)) >= 10000,
        "shuffle_0_0_0 was compressed")
      store.stop()
      store = null

      conf.set("spark.broadcast.compress", "true")
      store = makeBlockManager(20000, "exec3")
      store.putSingle(BroadcastBlockId(0), new Array[Byte](10000), StorageLevel.MEMORY_ONLY_SER)
      assert(store.memoryStore.getSize(BroadcastBlockId(0)) <= 1000,
        "broadcast_0 was not compressed")
      store.stop()
      store = null

      conf.set("spark.broadcast.compress", "false")
      store = makeBlockManager(20000, "exec4")
      store.putSingle(BroadcastBlockId(0), new Array[Byte](10000), StorageLevel.MEMORY_ONLY_SER)
      assert(store.memoryStore.getSize(BroadcastBlockId(0)) >= 10000, "broadcast_0 was compressed")
      store.stop()
      store = null

      conf.set("spark.rdd.compress", "true")
      store = makeBlockManager(20000, "exec5")
      store.putSingle(rdd(0, 0), new Array[Byte](10000), StorageLevel.MEMORY_ONLY_SER)
      assert(store.memoryStore.getSize(rdd(0, 0)) <= 1000, "rdd_0_0 was not compressed")
      store.stop()
      store = null

      conf.set("spark.rdd.compress", "false")
      store = makeBlockManager(20000, "exec6")
      store.putSingle(rdd(0, 0), new Array[Byte](10000), StorageLevel.MEMORY_ONLY_SER)
      assert(store.memoryStore.getSize(rdd(0, 0)) >= 10000, "rdd_0_0 was compressed")
      store.stop()
      store = null

      // Check that any other block types are also kept uncompressed
      store = makeBlockManager(20000, "exec7")
      store.putSingle("other_block", new Array[Byte](10000), StorageLevel.MEMORY_ONLY)
      assert(store.memoryStore.getSize("other_block") >= 10000, "other_block was compressed")
      store.stop()
      store = null
    } finally {
      System.clearProperty("spark.shuffle.compress")
      System.clearProperty("spark.broadcast.compress")
      System.clearProperty("spark.rdd.compress")
    }
  }

  test("block store put failure") {
    // Use Java serializer so we can create an unserializable error.
    val transfer = new NioBlockTransferService(conf, securityMgr)
    store = new BlockManager(SparkContext.DRIVER_IDENTIFIER, actorSystem, master,
      new JavaSerializer(conf), 1200, conf, mapOutputTracker, shuffleManager, transfer, securityMgr,
      0)

    // The put should fail since a1 is not serializable.
    class UnserializableClass
    val a1 = new UnserializableClass
    intercept[java.io.NotSerializableException] {
      store.putSingle("a1", a1, StorageLevel.DISK_ONLY)
    }

    // Make sure get a1 doesn't hang and returns None.
    failAfter(1 second) {
      assert(store.getSingle("a1") == None, "a1 should not be in store")
    }
  }

  test("reads of memory-mapped and non memory-mapped files are equivalent") {
    val confKey = "spark.storage.memoryMapThreshold"

    // Create a non-trivial (not all zeros) byte array
    var counter = 0.toByte
    def incr = {counter = (counter + 1).toByte; counter;}
    val bytes = Array.fill[Byte](1000)(incr)
    val byteBuffer = ByteBuffer.wrap(bytes)

    val blockId = BlockId("rdd_1_2")

    // This sequence of mocks makes these tests fairly brittle. It would
    // be nice to refactor classes involved in disk storage in a way that
    // allows for easier testing.
    val blockManager = mock(classOf[BlockManager])
    when(blockManager.conf).thenReturn(conf.clone.set(confKey, 0.toString))
    val diskBlockManager = new DiskBlockManager(blockManager, conf)

    val diskStoreMapped = new DiskStore(blockManager, diskBlockManager)
    diskStoreMapped.putBytes(blockId, byteBuffer, StorageLevel.DISK_ONLY)
    val mapped = diskStoreMapped.getBytes(blockId).get

    when(blockManager.conf).thenReturn(conf.clone.set(confKey, (1000 * 1000).toString))
    val diskStoreNotMapped = new DiskStore(blockManager, diskBlockManager)
    diskStoreNotMapped.putBytes(blockId, byteBuffer, StorageLevel.DISK_ONLY)
    val notMapped = diskStoreNotMapped.getBytes(blockId).get

    // Not possible to do isInstanceOf due to visibility of HeapByteBuffer
    assert(notMapped.getClass.getName.endsWith("HeapByteBuffer"),
      "Expected HeapByteBuffer for un-mapped read")
    assert(mapped.isInstanceOf[MappedByteBuffer], "Expected MappedByteBuffer for mapped read")

    def arrayFromByteBuffer(in: ByteBuffer): Array[Byte] = {
      val array = new Array[Byte](in.remaining())
      in.get(array)
      array
    }

    val mappedAsArray = arrayFromByteBuffer(mapped)
    val notMappedAsArray = arrayFromByteBuffer(notMapped)
    assert(Arrays.equals(mappedAsArray, bytes))
    assert(Arrays.equals(notMappedAsArray, bytes))
  }

  test("updated block statuses") {
    store = makeBlockManager(12000)
    val list = List.fill(2)(new Array[Byte](2000))
    val bigList = List.fill(8)(new Array[Byte](2000))

    // 1 updated block (i.e. list1)
    val updatedBlocks1 =
      store.putIterator("list1", list.iterator, StorageLevel.MEMORY_ONLY, tellMaster = true)
    assert(updatedBlocks1.size === 1)
    assert(updatedBlocks1.head._1 === TestBlockId("list1"))
    assert(updatedBlocks1.head._2.storageLevel === StorageLevel.MEMORY_ONLY)

    // 1 updated block (i.e. list2)
    val updatedBlocks2 =
      store.putIterator("list2", list.iterator, StorageLevel.MEMORY_AND_DISK, tellMaster = true)
    assert(updatedBlocks2.size === 1)
    assert(updatedBlocks2.head._1 === TestBlockId("list2"))
    assert(updatedBlocks2.head._2.storageLevel === StorageLevel.MEMORY_ONLY)

    // 2 updated blocks - list1 is kicked out of memory while list3 is added
    val updatedBlocks3 =
      store.putIterator("list3", list.iterator, StorageLevel.MEMORY_ONLY, tellMaster = true)
    assert(updatedBlocks3.size === 2)
    updatedBlocks3.foreach { case (id, status) =>
      id match {
        case TestBlockId("list1") => assert(status.storageLevel === StorageLevel.NONE)
        case TestBlockId("list3") => assert(status.storageLevel === StorageLevel.MEMORY_ONLY)
        case _ => fail("Updated block is neither list1 nor list3")
      }
    }
    assert(store.memoryStore.contains("list3"), "list3 was not in memory store")

    // 2 updated blocks - list2 is kicked out of memory (but put on disk) while list4 is added
    val updatedBlocks4 =
      store.putIterator("list4", list.iterator, StorageLevel.MEMORY_ONLY, tellMaster = true)
    assert(updatedBlocks4.size === 2)
    updatedBlocks4.foreach { case (id, status) =>
      id match {
        case TestBlockId("list2") => assert(status.storageLevel === StorageLevel.DISK_ONLY)
        case TestBlockId("list4") => assert(status.storageLevel === StorageLevel.MEMORY_ONLY)
        case _ => fail("Updated block is neither list2 nor list4")
      }
    }
    assert(store.diskStore.contains("list2"), "list2 was not in disk store")
    assert(store.memoryStore.contains("list4"), "list4 was not in memory store")

    // No updated blocks - list5 is too big to fit in store and nothing is kicked out
    val updatedBlocks5 =
      store.putIterator("list5", bigList.iterator, StorageLevel.MEMORY_ONLY, tellMaster = true)
    assert(updatedBlocks5.size === 0)

    // memory store contains only list3 and list4
    assert(!store.memoryStore.contains("list1"), "list1 was in memory store")
    assert(!store.memoryStore.contains("list2"), "list2 was in memory store")
    assert(store.memoryStore.contains("list3"), "list3 was not in memory store")
    assert(store.memoryStore.contains("list4"), "list4 was not in memory store")
    assert(!store.memoryStore.contains("list5"), "list5 was in memory store")

    // disk store contains only list2
    assert(!store.diskStore.contains("list1"), "list1 was in disk store")
    assert(store.diskStore.contains("list2"), "list2 was not in disk store")
    assert(!store.diskStore.contains("list3"), "list3 was in disk store")
    assert(!store.diskStore.contains("list4"), "list4 was in disk store")
    assert(!store.diskStore.contains("list5"), "list5 was in disk store")
  }

  test("query block statuses") {
    store = makeBlockManager(12000)
    val list = List.fill(2)(new Array[Byte](2000))

    // Tell master. By LRU, only list2 and list3 remains.
    store.putIterator("list1", list.iterator, StorageLevel.MEMORY_ONLY, tellMaster = true)
    store.putIterator("list2", list.iterator, StorageLevel.MEMORY_AND_DISK, tellMaster = true)
    store.putIterator("list3", list.iterator, StorageLevel.MEMORY_ONLY, tellMaster = true)

    // getLocations and getBlockStatus should yield the same locations
    assert(store.master.getLocations("list1").size === 0)
    assert(store.master.getLocations("list2").size === 1)
    assert(store.master.getLocations("list3").size === 1)
    assert(store.master.getBlockStatus("list1", askSlaves = false).size === 0)
    assert(store.master.getBlockStatus("list2", askSlaves = false).size === 1)
    assert(store.master.getBlockStatus("list3", askSlaves = false).size === 1)
    assert(store.master.getBlockStatus("list1", askSlaves = true).size === 0)
    assert(store.master.getBlockStatus("list2", askSlaves = true).size === 1)
    assert(store.master.getBlockStatus("list3", askSlaves = true).size === 1)

    // This time don't tell master and see what happens. By LRU, only list5 and list6 remains.
    store.putIterator("list4", list.iterator, StorageLevel.MEMORY_ONLY, tellMaster = false)
    store.putIterator("list5", list.iterator, StorageLevel.MEMORY_AND_DISK, tellMaster = false)
    store.putIterator("list6", list.iterator, StorageLevel.MEMORY_ONLY, tellMaster = false)

    // getLocations should return nothing because the master is not informed
    // getBlockStatus without asking slaves should have the same result
    // getBlockStatus with asking slaves, however, should return the actual block statuses
    assert(store.master.getLocations("list4").size === 0)
    assert(store.master.getLocations("list5").size === 0)
    assert(store.master.getLocations("list6").size === 0)
    assert(store.master.getBlockStatus("list4", askSlaves = false).size === 0)
    assert(store.master.getBlockStatus("list5", askSlaves = false).size === 0)
    assert(store.master.getBlockStatus("list6", askSlaves = false).size === 0)
    assert(store.master.getBlockStatus("list4", askSlaves = true).size === 0)
    assert(store.master.getBlockStatus("list5", askSlaves = true).size === 1)
    assert(store.master.getBlockStatus("list6", askSlaves = true).size === 1)
  }

  test("get matching blocks") {
    store = makeBlockManager(12000)
    val list = List.fill(2)(new Array[Byte](100))

    // insert some blocks
    store.putIterator("list1", list.iterator, StorageLevel.MEMORY_AND_DISK, tellMaster = true)
    store.putIterator("list2", list.iterator, StorageLevel.MEMORY_AND_DISK, tellMaster = true)
    store.putIterator("list3", list.iterator, StorageLevel.MEMORY_AND_DISK, tellMaster = true)

    // getLocations and getBlockStatus should yield the same locations
    assert(store.master.getMatchingBlockIds(_.toString.contains("list"), askSlaves = false).size === 3)
    assert(store.master.getMatchingBlockIds(_.toString.contains("list1"), askSlaves = false).size === 1)

    // insert some more blocks
    store.putIterator("newlist1", list.iterator, StorageLevel.MEMORY_AND_DISK, tellMaster = true)
    store.putIterator("newlist2", list.iterator, StorageLevel.MEMORY_AND_DISK, tellMaster = false)
    store.putIterator("newlist3", list.iterator, StorageLevel.MEMORY_AND_DISK, tellMaster = false)

    // getLocations and getBlockStatus should yield the same locations
    assert(store.master.getMatchingBlockIds(_.toString.contains("newlist"), askSlaves = false).size === 1)
    assert(store.master.getMatchingBlockIds(_.toString.contains("newlist"), askSlaves = true).size === 3)

    val blockIds = Seq(RDDBlockId(1, 0), RDDBlockId(1, 1), RDDBlockId(2, 0))
    blockIds.foreach { blockId =>
      store.putIterator(blockId, list.iterator, StorageLevel.MEMORY_ONLY, tellMaster = true)
    }
    val matchedBlockIds = store.master.getMatchingBlockIds(_ match {
      case RDDBlockId(1, _) => true
      case _ => false
    }, askSlaves = true)
    assert(matchedBlockIds.toSet === Set(RDDBlockId(1, 0), RDDBlockId(1, 1)))
  }

  test("SPARK-1194 regression: fix the same-RDD rule for cache replacement") {
    store = makeBlockManager(12000)
    store.putSingle(rdd(0, 0), new Array[Byte](4000), StorageLevel.MEMORY_ONLY)
    store.putSingle(rdd(1, 0), new Array[Byte](4000), StorageLevel.MEMORY_ONLY)
    // Access rdd_1_0 to ensure it's not least recently used.
    assert(store.getSingle(rdd(1, 0)).isDefined, "rdd_1_0 was not in store")
    // According to the same-RDD rule, rdd_1_0 should be replaced here.
    store.putSingle(rdd(0, 1), new Array[Byte](4000), StorageLevel.MEMORY_ONLY)
    // rdd_1_0 should have been replaced, even it's not least recently used.
    assert(store.memoryStore.contains(rdd(0, 0)), "rdd_0_0 was not in store")
    assert(store.memoryStore.contains(rdd(0, 1)), "rdd_0_1 was not in store")
    assert(!store.memoryStore.contains(rdd(1, 0)), "rdd_1_0 was in store")
  }

  test("reserve/release unroll memory") {
    store = makeBlockManager(12000)
    val memoryStore = store.memoryStore
    assert(memoryStore.currentUnrollMemory === 0)
    assert(memoryStore.currentUnrollMemoryForThisThread === 0)

    // Reserve
    memoryStore.reserveUnrollMemoryForThisThread(100)
    assert(memoryStore.currentUnrollMemoryForThisThread === 100)
    memoryStore.reserveUnrollMemoryForThisThread(200)
    assert(memoryStore.currentUnrollMemoryForThisThread === 300)
    memoryStore.reserveUnrollMemoryForThisThread(500)
    assert(memoryStore.currentUnrollMemoryForThisThread === 800)
    memoryStore.reserveUnrollMemoryForThisThread(1000000)
    assert(memoryStore.currentUnrollMemoryForThisThread === 800) // not granted
    // Release
    memoryStore.releaseUnrollMemoryForThisThread(100)
    assert(memoryStore.currentUnrollMemoryForThisThread === 700)
    memoryStore.releaseUnrollMemoryForThisThread(100)
    assert(memoryStore.currentUnrollMemoryForThisThread === 600)
    // Reserve again
    memoryStore.reserveUnrollMemoryForThisThread(4400)
    assert(memoryStore.currentUnrollMemoryForThisThread === 5000)
    memoryStore.reserveUnrollMemoryForThisThread(20000)
    assert(memoryStore.currentUnrollMemoryForThisThread === 5000) // not granted
    // Release again
    memoryStore.releaseUnrollMemoryForThisThread(1000)
    assert(memoryStore.currentUnrollMemoryForThisThread === 4000)
    memoryStore.releaseUnrollMemoryForThisThread() // release all
    assert(memoryStore.currentUnrollMemoryForThisThread === 0)
  }

  /**
   * Verify the result of MemoryStore#unrollSafely is as expected.
   */
  private def verifyUnroll(
      expected: Iterator[Any],
      result: Either[Array[Any], Iterator[Any]],
      shouldBeArray: Boolean): Unit = {
    val actual: Iterator[Any] = result match {
      case Left(arr: Array[Any]) =>
        assert(shouldBeArray, "expected iterator from unroll!")
        arr.iterator
      case Right(it: Iterator[Any]) =>
        assert(!shouldBeArray, "expected array from unroll!")
        it
      case _ =>
        fail("unroll returned neither an iterator nor an array...")
    }
    expected.zip(actual).foreach { case (e, a) =>
      assert(e === a, "unroll did not return original values!")
    }
  }

  test("safely unroll blocks") {
    store = makeBlockManager(12000)
    val smallList = List.fill(40)(new Array[Byte](100))
    val bigList = List.fill(40)(new Array[Byte](1000))
    val memoryStore = store.memoryStore
    val droppedBlocks = new ArrayBuffer[(BlockId, BlockStatus)]
    assert(memoryStore.currentUnrollMemoryForThisThread === 0)

    // Unroll with all the space in the world. This should succeed and return an array.
    var unrollResult = memoryStore.unrollSafely("unroll", smallList.iterator, droppedBlocks)
    verifyUnroll(smallList.iterator, unrollResult, shouldBeArray = true)
    assert(memoryStore.currentUnrollMemoryForThisThread === 0)

    // Unroll with not enough space. This should succeed after kicking out someBlock1.
    store.putIterator("someBlock1", smallList.iterator, StorageLevel.MEMORY_ONLY)
    store.putIterator("someBlock2", smallList.iterator, StorageLevel.MEMORY_ONLY)
    unrollResult = memoryStore.unrollSafely("unroll", smallList.iterator, droppedBlocks)
    verifyUnroll(smallList.iterator, unrollResult, shouldBeArray = true)
    assert(memoryStore.currentUnrollMemoryForThisThread === 0)
    assert(droppedBlocks.size === 1)
    assert(droppedBlocks.head._1 === TestBlockId("someBlock1"))
    droppedBlocks.clear()

    // Unroll huge block with not enough space. Even after ensuring free space of 12000 * 0.4 =
    // 4800 bytes, there is still not enough room to unroll this block. This returns an iterator.
    // In the mean time, however, we kicked out someBlock2 before giving up.
    store.putIterator("someBlock3", smallList.iterator, StorageLevel.MEMORY_ONLY)
    unrollResult = memoryStore.unrollSafely("unroll", bigList.iterator, droppedBlocks)
    verifyUnroll(bigList.iterator, unrollResult, shouldBeArray = false)
    assert(memoryStore.currentUnrollMemoryForThisThread > 0) // we returned an iterator
    assert(droppedBlocks.size === 1)
    assert(droppedBlocks.head._1 === TestBlockId("someBlock2"))
    droppedBlocks.clear()
  }

  test("safely unroll blocks through putIterator") {
    store = makeBlockManager(12000)
    val memOnly = StorageLevel.MEMORY_ONLY
    val memoryStore = store.memoryStore
    val smallList = List.fill(40)(new Array[Byte](100))
    val bigList = List.fill(40)(new Array[Byte](1000))
    def smallIterator = smallList.iterator.asInstanceOf[Iterator[Any]]
    def bigIterator = bigList.iterator.asInstanceOf[Iterator[Any]]
    assert(memoryStore.currentUnrollMemoryForThisThread === 0)

    // Unroll with plenty of space. This should succeed and cache both blocks.
    val result1 = memoryStore.putIterator("b1", smallIterator, memOnly, returnValues = true)
    val result2 = memoryStore.putIterator("b2", smallIterator, memOnly, returnValues = true)
    assert(memoryStore.contains("b1"))
    assert(memoryStore.contains("b2"))
    assert(result1.size > 0) // unroll was successful
    assert(result2.size > 0)
    assert(result1.data.isLeft) // unroll did not drop this block to disk
    assert(result2.data.isLeft)
    assert(memoryStore.currentUnrollMemoryForThisThread === 0)

    // Re-put these two blocks so block manager knows about them too. Otherwise, block manager
    // would not know how to drop them from memory later.
    memoryStore.remove("b1")
    memoryStore.remove("b2")
    store.putIterator("b1", smallIterator, memOnly)
    store.putIterator("b2", smallIterator, memOnly)

    // Unroll with not enough space. This should succeed but kick out b1 in the process.
    val result3 = memoryStore.putIterator("b3", smallIterator, memOnly, returnValues = true)
    assert(result3.size > 0)
    assert(result3.data.isLeft)
    assert(!memoryStore.contains("b1"))
    assert(memoryStore.contains("b2"))
    assert(memoryStore.contains("b3"))
    assert(memoryStore.currentUnrollMemoryForThisThread === 0)
    memoryStore.remove("b3")
    store.putIterator("b3", smallIterator, memOnly)

    // Unroll huge block with not enough space. This should fail and kick out b2 in the process.
    val result4 = memoryStore.putIterator("b4", bigIterator, memOnly, returnValues = true)
    assert(result4.size === 0) // unroll was unsuccessful
    assert(result4.data.isLeft)
    assert(!memoryStore.contains("b1"))
    assert(!memoryStore.contains("b2"))
    assert(memoryStore.contains("b3"))
    assert(!memoryStore.contains("b4"))
    assert(memoryStore.currentUnrollMemoryForThisThread > 0) // we returned an iterator
  }

  /**
   * This test is essentially identical to the preceding one, except that it uses MEMORY_AND_DISK.
   */
  test("safely unroll blocks through putIterator (disk)") {
    store = makeBlockManager(12000)
    val memAndDisk = StorageLevel.MEMORY_AND_DISK
    val memoryStore = store.memoryStore
    val diskStore = store.diskStore
    val smallList = List.fill(40)(new Array[Byte](100))
    val bigList = List.fill(40)(new Array[Byte](1000))
    def smallIterator = smallList.iterator.asInstanceOf[Iterator[Any]]
    def bigIterator = bigList.iterator.asInstanceOf[Iterator[Any]]
    assert(memoryStore.currentUnrollMemoryForThisThread === 0)

    store.putIterator("b1", smallIterator, memAndDisk)
    store.putIterator("b2", smallIterator, memAndDisk)

    // Unroll with not enough space. This should succeed but kick out b1 in the process.
    // Memory store should contain b2 and b3, while disk store should contain only b1
    val result3 = memoryStore.putIterator("b3", smallIterator, memAndDisk, returnValues = true)
    assert(result3.size > 0)
    assert(!memoryStore.contains("b1"))
    assert(memoryStore.contains("b2"))
    assert(memoryStore.contains("b3"))
    assert(diskStore.contains("b1"))
    assert(!diskStore.contains("b2"))
    assert(!diskStore.contains("b3"))
    memoryStore.remove("b3")
    store.putIterator("b3", smallIterator, StorageLevel.MEMORY_ONLY)
    assert(memoryStore.currentUnrollMemoryForThisThread === 0)

    // Unroll huge block with not enough space. This should fail and drop the new block to disk
    // directly in addition to kicking out b2 in the process. Memory store should contain only
    // b3, while disk store should contain b1, b2 and b4.
    val result4 = memoryStore.putIterator("b4", bigIterator, memAndDisk, returnValues = true)
    assert(result4.size > 0)
    assert(result4.data.isRight) // unroll returned bytes from disk
    assert(!memoryStore.contains("b1"))
    assert(!memoryStore.contains("b2"))
    assert(memoryStore.contains("b3"))
    assert(!memoryStore.contains("b4"))
    assert(diskStore.contains("b1"))
    assert(diskStore.contains("b2"))
    assert(!diskStore.contains("b3"))
    assert(diskStore.contains("b4"))
    assert(memoryStore.currentUnrollMemoryForThisThread > 0) // we returned an iterator
  }

  test("multiple unrolls by the same thread") {
    store = makeBlockManager(12000)
    val memOnly = StorageLevel.MEMORY_ONLY
    val memoryStore = store.memoryStore
    val smallList = List.fill(40)(new Array[Byte](100))
    def smallIterator = smallList.iterator.asInstanceOf[Iterator[Any]]
    assert(memoryStore.currentUnrollMemoryForThisThread === 0)

    // All unroll memory used is released because unrollSafely returned an array
    memoryStore.putIterator("b1", smallIterator, memOnly, returnValues = true)
    assert(memoryStore.currentUnrollMemoryForThisThread === 0)
    memoryStore.putIterator("b2", smallIterator, memOnly, returnValues = true)
    assert(memoryStore.currentUnrollMemoryForThisThread === 0)

    // Unroll memory is not released because unrollSafely returned an iterator
    // that still depends on the underlying vector used in the process
    memoryStore.putIterator("b3", smallIterator, memOnly, returnValues = true)
    val unrollMemoryAfterB3 = memoryStore.currentUnrollMemoryForThisThread
    assert(unrollMemoryAfterB3 > 0)

    // The unroll memory owned by this thread builds on top of its value after the previous unrolls
    memoryStore.putIterator("b4", smallIterator, memOnly, returnValues = true)
    val unrollMemoryAfterB4 = memoryStore.currentUnrollMemoryForThisThread
    assert(unrollMemoryAfterB4 > unrollMemoryAfterB3)

    // ... but only to a certain extent (until we run out of free space to grant new unroll memory)
    memoryStore.putIterator("b5", smallIterator, memOnly, returnValues = true)
    val unrollMemoryAfterB5 = memoryStore.currentUnrollMemoryForThisThread
    memoryStore.putIterator("b6", smallIterator, memOnly, returnValues = true)
    val unrollMemoryAfterB6 = memoryStore.currentUnrollMemoryForThisThread
    memoryStore.putIterator("b7", smallIterator, memOnly, returnValues = true)
    val unrollMemoryAfterB7 = memoryStore.currentUnrollMemoryForThisThread
    assert(unrollMemoryAfterB5 === unrollMemoryAfterB4)
    assert(unrollMemoryAfterB6 === unrollMemoryAfterB4)
    assert(unrollMemoryAfterB7 === unrollMemoryAfterB4)
  }
}


text:
require_relative 'spec_helper'

load_extension('io')

describe "C-API IO function" do
  before :each do
    @o = CApiIOSpecs.new

    @name = tmp("c_api_rb_io_specs")
    touch @name

    @io = new_io @name, "w:utf-8"
    @io.sync = true
  end

  after :each do
    @io.close unless @io.closed?
    rm_r @name
  end

  describe "rb_io_addstr" do
    it "calls #to_s to convert the object to a String" do
      obj = mock("rb_io_addstr string")
      obj.should_receive(:to_s).and_return("rb_io_addstr data")

      @o.rb_io_addstr(@io, obj)
      File.read(@name).should == "rb_io_addstr data"
    end

    it "writes the String to the IO" do
      @o.rb_io_addstr(@io, "rb_io_addstr data")
      File.read(@name).should == "rb_io_addstr data"
    end

    it "returns the io" do
      @o.rb_io_addstr(@io, "rb_io_addstr data").should eql(@io)
    end
  end

  describe "rb_io_printf" do
    it "calls #to_str to convert the format object to a String" do
      obj = mock("rb_io_printf format")
      obj.should_receive(:to_str).and_return("%s")

      @o.rb_io_printf(@io, [obj, "rb_io_printf"])
      File.read(@name).should == "rb_io_printf"
    end

    it "calls #to_s to convert the object to a String" do
      obj = mock("rb_io_printf string")
      obj.should_receive(:to_s).and_return("rb_io_printf")

      @o.rb_io_printf(@io, ["%s", obj])
      File.read(@name).should == "rb_io_printf"
    end

    it "writes the Strings to the IO" do
      @o.rb_io_printf(@io, ["%s_%s_%s", "rb", "io", "printf"])
      File.read(@name).should == "rb_io_printf"
    end
  end

  describe "rb_io_print" do
    it "calls #to_s to convert the object to a String" do
      obj = mock("rb_io_print string")
      obj.should_receive(:to_s).and_return("rb_io_print")

      @o.rb_io_print(@io, [obj])
      File.read(@name).should == "rb_io_print"
    end

    it "writes the Strings to the IO with no separator" do
      @o.rb_io_print(@io, ["rb_", "io_", "print"])
      File.read(@name).should == "rb_io_print"
    end
  end

  describe "rb_io_puts" do
    it "calls #to_s to convert the object to a String" do
      obj = mock("rb_io_puts string")
      obj.should_receive(:to_s).and_return("rb_io_puts")

      @o.rb_io_puts(@io, [obj])
      File.read(@name).should == "rb_io_puts\n"
    end

    it "writes the Strings to the IO separated by newlines" do
      @o.rb_io_puts(@io, ["rb", "io", "write"])
      File.read(@name).should == "rb\nio\nwrite\n"
    end
  end

  describe "rb_io_write" do
    it "calls #to_s to convert the object to a String" do
      obj = mock("rb_io_write string")
      obj.should_receive(:to_s).and_return("rb_io_write")

      @o.rb_io_write(@io, obj)
      File.read(@name).should == "rb_io_write"
    end

    it "writes the String to the IO" do
      @o.rb_io_write(@io, "rb_io_write")
      File.read(@name).should == "rb_io_write"
    end
  end
end

describe "C-API IO function" do
  before :each do
    @o = CApiIOSpecs.new

    @name = tmp("c_api_io_specs")
    touch @name

    @io = new_io @name, "r:utf-8"
  end

  after :each do
    @io.close unless @io.closed?
    rm_r @name
  end

  describe "rb_io_close" do
    it "closes an IO object" do
      @io.closed?.should be_false
      @o.rb_io_close(@io)
      @io.closed?.should be_true
    end
  end

  describe "rb_io_check_io" do
    it "returns the IO object if it is valid" do
      @o.rb_io_check_io(@io).should == @io
    end

    it "returns nil for non IO objects" do
      @o.rb_io_check_io({}).should be_nil
    end
  end

  describe "rb_io_check_closed" do
    it "does not raise an exception if the IO is not closed" do
      # The MRI function is void, so we use should_not raise_error
      -> { @o.rb_io_check_closed(@io) }.should_not raise_error
    end

    it "raises an error if the IO is closed" do
      @io.close
      -> { @o.rb_io_check_closed(@io) }.should raise_error(IOError)
    end
  end

  describe "rb_io_set_nonblock" do
    platform_is_not :windows do
      it "returns true when nonblock flag is set" do
        require 'io/nonblock'
        @o.rb_io_set_nonblock(@io)
        @io.nonblock?.should be_true
      end
    end
  end

  # NOTE: unlike the name might suggest in MRI this function checks if an
  # object is frozen, *not* if it's tainted.
  describe "rb_io_taint_check" do
    it "does not raise an exception if the IO is not frozen" do
      -> { @o.rb_io_taint_check(@io) }.should_not raise_error
    end

    it "raises an exception if the IO is frozen" do
      @io.freeze

      -> { @o.rb_io_taint_check(@io) }.should raise_error(RuntimeError)
    end
  end

  describe "GetOpenFile" do
    it "allows access to the system fileno" do
      @o.GetOpenFile_fd($stdin).should == 0
      @o.GetOpenFile_fd($stdout).should == 1
      @o.GetOpenFile_fd($stderr).should == 2
      @o.GetOpenFile_fd(@io).should == @io.fileno
    end
  end

  describe "rb_io_binmode" do
    it "returns self" do
      @o.rb_io_binmode(@io).should == @io
    end

    it "sets binmode" do
      @o.rb_io_binmode(@io)
      @io.binmode?.should be_true
    end
  end
end

describe "C-API IO function" do
  before :each do
    @o = CApiIOSpecs.new
    @r_io, @w_io = IO.pipe

    @name = tmp("c_api_io_specs")
    touch @name
    @rw_io = new_io @name, "w+"
  end

  after :each do
    @r_io.close unless @r_io.closed?
    @w_io.close unless @w_io.closed?
    @rw_io.close unless @rw_io.closed?
    rm_r @name
  end

  describe "rb_io_check_readable" do
    it "does not raise an exception if the IO is opened for reading" do
      # The MRI function is void, so we use should_not raise_error
      -> { @o.rb_io_check_readable(@r_io) }.should_not raise_error
    end

    it "does not raise an exception if the IO is opened for read and write" do
      -> { @o.rb_io_check_readable(@rw_io) }.should_not raise_error
    end

    it "raises an IOError if the IO is not opened for reading" do
      -> { @o.rb_io_check_readable(@w_io) }.should raise_error(IOError)
    end

  end

  describe "rb_io_check_writable" do
    it "does not raise an exception if the IO is opened for writing" do
      # The MRI function is void, so we use should_not raise_error
      -> { @o.rb_io_check_writable(@w_io) }.should_not raise_error
    end

    it "does not raise an exception if the IO is opened for read and write" do
      -> { @o.rb_io_check_writable(@rw_io) }.should_not raise_error
    end

    it "raises an IOError if the IO is not opened for reading" do
      -> { @o.rb_io_check_writable(@r_io) }.should raise_error(IOError)
    end
  end

  describe "rb_io_wait_writable" do
    it "returns false if there is no error condition" do
      @o.errno = 0
      @o.rb_io_wait_writable(@w_io).should be_false
    end

    it "raises an IOError if the IO is closed" do
      @w_io.close
      -> { @o.rb_io_wait_writable(@w_io) }.should raise_error(IOError)
    end
  end

  describe "rb_thread_fd_writable" do
    it "waits til an fd is ready for writing" do
      @o.rb_thread_fd_writable(@w_io).should be_nil
    end
  end

  platform_is_not :windows do
    describe "rb_io_wait_readable" do
      it "returns false if there is no error condition" do
        @o.errno = 0
        @o.rb_io_wait_readable(@r_io, false).should be_false
      end

      it "raises and IOError if passed a closed stream" do
        @r_io.close
        -> {
          @o.rb_io_wait_readable(@r_io, false)
        }.should raise_error(IOError)
      end

      it "blocks until the io is readable and returns true" do
        @o.instance_variable_set :@write_data, false
        thr = Thread.new do
          Thread.pass until @o.instance_variable_get(:@write_data)
          @w_io.write "rb_io_wait_readable"
        end

        @o.errno = Errno::EAGAIN.new.errno
        @o.rb_io_wait_readable(@r_io, true).should be_true
        @o.instance_variable_get(:@read_data).should == "rb_io_wait_re"

        thr.join
      end
    end
  end

  describe "rb_thread_wait_fd" do
    it "waits til an fd is ready for reading" do
      start = false
      thr = Thread.new do
        start = true
        sleep 0.05
        @w_io.write "rb_io_wait_readable"
      end

      Thread.pass until start

      @o.rb_thread_wait_fd(@r_io).should be_nil

      thr.join
    end
  end

  describe "rb_wait_for_single_fd" do
    it "waits til an fd is ready for reading" do
      start = false
      thr = Thread.new do
        start = true
        sleep 0.05
        @w_io.write "rb_io_wait_readable"
      end

      Thread.pass until start

      @o.rb_wait_for_single_fd(@r_io, 1, nil, nil).should == 1

      thr.join
    end

    it "polls whether an fd is ready for reading if timeout is 0" do
      @o.rb_wait_for_single_fd(@r_io, 1, 0, 0).should == 0
    end
  end
end

describe "rb_fd_fix_cloexec" do

  before :each do
    @o = CApiIOSpecs.new

    @name = tmp("c_api_rb_io_specs")
    touch @name

    @io = new_io @name, "w:utf-8"
    @io.close_on_exec = false
    @io.sync = true
  end

  after :each do
    @io.close unless @io.closed?
    rm_r @name
  end

  it "sets close_on_exec on the IO" do
    @o.rb_fd_fix_cloexec(@io)
    @io.close_on_exec?.should be_true
  end

end

describe "rb_cloexec_open" do
  before :each do
    @o = CApiIOSpecs.new
    @name = tmp("c_api_rb_io_specs")
    touch @name

    @io = nil
  end

  after :each do
    @io.close unless @io.nil? || @io.closed?
    rm_r @name
  end

  it "sets close_on_exec on the newly-opened IO" do
    @io = @o.rb_cloexec_open(@name, 0, 0)
    @io.close_on_exec?.should be_true
  end
end

describe "rb_io_t modes flags" do
  before :each do
    @o = CApiIOSpecs.new
    @name = tmp("c_api_rb_io_specs")
    touch @name
  end

  after :each do
    rm_r @name
  end

  it "has the sync flag set if the IO object is synced in Ruby" do
    File.open(@name) { |io|
      io.sync = true
      @o.rb_io_mode_sync_flag(io).should == true
    }
  end

  it "has the sync flag unset if the IO object is not synced in Ruby" do
    File.open(@name) { |io|
      io.sync = false
      @o.rb_io_mode_sync_flag(io).should == false
    }
  end
end

redpajama_set_name: RedPajamaGithub


text:
class Repository {

    _makeModel(Model, promise) {
        return promise.then(instance => {
            return instance ? new Model(instance) : null;
        });
    }

    create(Model, criteria) {
        let promise = Model.dbModelType.create(criteria);
        return this._makeModel(Model, promise);
    }

    delete(Model, criteria) {
        let promise = Model.dbModelType.destroy({
            where: criteria
        });
        return this._makeModel(Model, promise);
    }

    findOne(Model, criteria) {
        let promise = Model.dbModelType.findOne({
            where: criteria
        });
        return this._makeModel(Model, promise);
    }
}

module.exports = Repository;


text: Stategic Tic Tac Toe Game replica type thingy


text:
package edu.washington.escience.myria.column.builder;

import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.annotation.Nonnull;

import org.joda.time.DateTime;

import com.almworks.sqlite4java.SQLiteException;
import com.almworks.sqlite4java.SQLiteStatement;

import edu.washington.escience.myria.column.Column;
import edu.washington.escience.myria.column.mutable.MutableColumn;
import edu.washington.escience.myria.storage.ReadableColumn;
import edu.washington.escience.myria.storage.ReplaceableColumn;

/**
 * @param <T> type of the objects in this column.
 */
public abstract class ColumnBuilder<T extends Comparable<?>>
    implements ReadableColumn, WritableColumn, ReplaceableColumn {

  @Override
  public ColumnBuilder<T> appendBoolean(final boolean value) throws BufferOverflowException {
    throw new UnsupportedOperationException(getClass().getName());
  }

  @Override
  public ColumnBuilder<T> appendDateTime(final DateTime value) throws BufferOverflowException {
    throw new UnsupportedOperationException(getClass().getName());
  }

  @Override
  public boolean getBoolean(final int row) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  @Override
  public DateTime getDateTime(final int row) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  @Override
  public double getDouble(final int row) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  @Override
  public float getFloat(final int row) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  @Override
  public int getInt(final int row) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  @Override
  public long getLong(final int row) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  @Override
  public String getString(final int row) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  @Override
  public ByteBuffer getBlob(final int row) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  @Override
  public ColumnBuilder<T> appendBlob(final ByteBuffer value) throws BufferOverflowException {
    throw new UnsupportedOperationException(getClass().getName());
  }

  @Override
  public ColumnBuilder<T> appendDouble(final double value) throws BufferOverflowException {
    throw new UnsupportedOperationException(getClass().getName());
  }

  @Override
  public ColumnBuilder<T> appendFloat(final float value) throws BufferOverflowException {
    throw new UnsupportedOperationException(getClass().getName());
  }

  @Override
  public ColumnBuilder<T> appendInt(final int value) throws BufferOverflowException {
    throw new UnsupportedOperationException(getClass().getName());
  }

  @Override
  public ColumnBuilder<T> appendLong(final long value) throws BufferOverflowException {
    throw new UnsupportedOperationException(getClass().getName());
  }

  @Override
  public ColumnBuilder<T> appendString(final String value) throws BufferOverflowException {
    throw new UnsupportedOperationException(getClass().getName());
  }

  /**
   * Extracts the appropriate value from a JDBC ResultSet object and appends it to this column.
   *
   * @param resultSet contains the results
   * @param jdbcIndex the position of the element to extract. 1-indexed.
   * @return this column builder.
   * @throws SQLException if there are JDBC errors.
   * @throws BufferOverflowException if the column is already full
   */
  public abstract ColumnBuilder<T> appendFromJdbc(ResultSet resultSet, int jdbcIndex)
      throws SQLException, BufferOverflowException;

  /**
   * Extracts the appropriate value from a SQLiteStatement object and appends it to this column.
   *
   * @param statement contains the results
   * @return this column builder.
   * @param index the position of the element to extract. 0-indexed.
   * @throws SQLiteException if there are SQLite errors.
   * @throws BufferOverflowException if the column is already full
   */
  public abstract ColumnBuilder<T> appendFromSQLite(SQLiteStatement statement, int index)
      throws SQLiteException, BufferOverflowException;

  /**
   * @return a column with the contents built.
   */
  public abstract Column<T> build();

  /**
   * @return a mutable column with the contents built.
   */
  public abstract MutableColumn<T> buildMutable();

  /**
   * expand some size.
   *
   * @param size to expand
   * @return this column builder.
   * @throws BufferOverflowException if expanding size exceeds the column capacity
   */
  public abstract ColumnBuilder<T> expand(int size) throws BufferOverflowException;

  /**
   * expand to full size.
   *
   * @return this column builder.
   */
  public abstract ColumnBuilder<T> expandAll();

  /**
   * @return a new builder with the contents initialized by this builder. The two builders share no data.
   */
  public abstract ColumnBuilder<T> forkNewBuilder();

  @Override
  public void replaceBoolean(final boolean value, final int row) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  @Override
  public void replaceDateTime(@Nonnull final DateTime value, final int row) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  @Override
  public void replaceDouble(final double value, final int row) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  @Override
  public void replaceFloat(final float value, final int row) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  @Override
  public void replaceInt(final int value, final int row) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  @Override
  public void replaceLong(final long value, final int row) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  @Override
  public void replaceString(@Nonnull final String value, final int row) {
    throw new UnsupportedOperationException(getClass().getName());
  }

  /**
   * @param type the type of the column to be returned.
   * @return a new empty column of the specified type.
   */
  /*
  public static ColumnBuilder<?> of(final Type type) {
    switch (type) {
      case BOOLEAN_TYPE:
        return new BooleanColumnBuilder();
      case DATETIME_TYPE:
        return new DateTimeColumnBuilder();
      case DOUBLE_TYPE:
        return new DoubleColumnBuilder();
      case FLOAT_TYPE:
        return new FloatColumnBuilder();
      case INT_TYPE:
        return new IntColumnBuilder();
      case LONG_TYPE:
        return new LongColumnBuilder();
      case STRING_TYPE:
        return new StringColumnBuilder();
      default:
        throw new IllegalArgumentException("Type " + type + " is invalid");
    }
  }
  */

  @Override
  public void replaceBlob(@Nonnull final ByteBuffer value, final int row) {
    throw new UnsupportedOperationException(getClass().getName());
  }
}


text:
package org.springframework.boot.configurationprocessor;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.annotation.processing.ProcessingEnvironment;
import javax.tools.Diagnostic;
import javax.tools.FileObject;
import javax.tools.StandardLocation;

import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
import org.springframework.boot.configurationprocessor.metadata.InvalidConfigurationMetadataException;
import org.springframework.boot.configurationprocessor.metadata.JsonMarshaller;

/**
 * A {@code MetadataStore} is responsible for the storage of metadata on the filesystem.
 *
 * @author Andy Wilkinson
 * @since 1.2.2
 */
public class MetadataStore {

	static final String METADATA_PATH = "META-INF/spring-configuration-metadata.json";

	private static final String ADDITIONAL_METADATA_PATH = "META-INF/additional-spring-configuration-metadata.json";

	private static final String RESOURCES_FOLDER = "resources";

	private static final String CLASSES_FOLDER = "classes";

	private final ProcessingEnvironment environment;

	public MetadataStore(ProcessingEnvironment environment) {
		this.environment = environment;
	}

	public ConfigurationMetadata readMetadata() {
		try {
			return readMetadata(getMetadataResource().openInputStream());
		}
		catch (IOException ex) {
			return null;
		}
	}

	public void writeMetadata(ConfigurationMetadata metadata) throws IOException {
		if (!metadata.getItems().isEmpty()) {
			try (OutputStream outputStream = createMetadataResource()
					.openOutputStream()) {
				new JsonMarshaller().write(metadata, outputStream);
			}
		}
	}

	public ConfigurationMetadata readAdditionalMetadata() throws IOException {
		return readMetadata(getAdditionalMetadataStream());
	}

	private ConfigurationMetadata readMetadata(InputStream in) throws IOException {
		try {
			return new JsonMarshaller().read(in);
		}
		catch (IOException ex) {
			return null;
		}
		catch (Exception ex) {
			throw new InvalidConfigurationMetadataException(
					"Invalid additional meta-data in '" + METADATA_PATH + "': "
							+ ex.getMessage(),
					Diagnostic.Kind.ERROR);
		}
		finally {
			in.close();
		}
	}

	private FileObject getMetadataResource() throws IOException {
		FileObject resource = this.environment.getFiler()
				.getResource(StandardLocation.CLASS_OUTPUT, "", METADATA_PATH);
		return resource;
	}

	private FileObject createMetadataResource() throws IOException {
		FileObject resource = this.environment.getFiler()
				.createResource(StandardLocation.CLASS_OUTPUT, "", METADATA_PATH);
		return resource;
	}

	private InputStream getAdditionalMetadataStream() throws IOException {
		// Most build systems will have copied the file to the class output location
		FileObject fileObject = this.environment.getFiler()
				.getResource(StandardLocation.CLASS_OUTPUT, "", ADDITIONAL_METADATA_PATH);
		File file = locateAdditionalMetadataFile(new File(fileObject.toUri()));
		return (file.exists() ? new FileInputStream(file)
				: fileObject.toUri().toURL().openStream());
	}

	File locateAdditionalMetadataFile(File standardLocation) throws IOException {
		if (standardLocation.exists()) {
			return standardLocation;
		}
		return new File(locateGradleResourcesFolder(standardLocation),
				ADDITIONAL_METADATA_PATH);
	}

	private File locateGradleResourcesFolder(File standardAdditionalMetadataLocation)
			throws FileNotFoundException {
		String path = standardAdditionalMetadataLocation.getPath();
		int index = path.lastIndexOf(CLASSES_FOLDER);
		if (index < 0) {
			throw new FileNotFoundException();
		}
		String buildFolderPath = path.substring(0, index);
		File classOutputLocation = standardAdditionalMetadataLocation.getParentFile()
				.getParentFile();
		return new File(buildFolderPath,
				RESOURCES_FOLDER + '/' + classOutputLocation.getName());
	}

}


text:
/**
 * \file SimpleSinusGeneratorFilter.h
 */

#ifndef ATK_MOCK_SIMPLESINUSGENERATORFILTER_H
#define ATK_MOCK_SIMPLESINUSGENERATORFILTER_H

#include <ATK/Core/TypedBaseFilter.h>
#include <ATK/Mock/config.h>

namespace ATK
{
  /// Filter creating a sinus signal (usng std::sin, so not precise enough and fast enough)
  template<class DataType_>
  class ATK_MOCK_EXPORT SimpleSinusGeneratorFilter final : public TypedBaseFilter<DataType_>
  {
  public:
    /// Simplify parent calls
    using Parent = TypedBaseFilter<DataType_>;
    using typename Parent::DataType;
    using Parent::outputs;
    using Parent::output_sampling_rate;

    /// Constructor
    SimpleSinusGeneratorFilter();
    /// Destructor
    ~SimpleSinusGeneratorFilter() override = default;

    /// Sets the amplitude of the sinusoid
    void set_amplitude(DataType_ amplitude);
    /// Sets the frequency of the sinusoid
    void set_frequency(int frequency);

  protected:
    void process_impl(gsl::index size) const final;

  private:
    mutable double state = 0;
    DataType_ amplitude = 1;
    int frequency = 1;
  };
}

#endif


text:
using System;
using System.Collections.Generic;
using System.Linq;

namespace King_s_Gambit
{
    public class Startup
    {
        public static void Main()
        {
            var king = new King(Console.ReadLine());

            var soldiers = new List<Soldier>();
            soldiers.AddRange(Console.ReadLine().Split().Select(rg => new RoyalGuard(rg)));
            soldiers.AddRange(Console.ReadLine().Split().Select(f => new Footman(f)));
            soldiers.ForEach(soldier => king.UnderAttack += soldier.OnUnderAttack);

            while (true)
            {
                var input = Console.ReadLine();

                if (input.Equals("End"))
                {
                    break;
                }

                var commandArgs = input.Split();
                var command = commandArgs[0];

                if (command.Equals("Attack"))
                {
                    king.KingUnderAttack();
                }
                else
                {
                    var soldier = soldiers.Find(s => s.Name.Equals(commandArgs[1]));
                    soldiers.Remove(soldier);
                    king.UnderAttack -= soldier.OnUnderAttack;
                }
            }
        }
    }
}


text:
/** @jsx _jsx */
import { useEffect, useState } from "react";
import ReactModal from "react-modal";
import { css, jsx as _jsx } from "@emotion/core";
import keyboardJS from "keyboardjs";
import "./Keyboard.css";
import { ConsoleKeyCombinations } from "../../screens/Console/Console.keybinds";
import Tippy from "@tippyjs/react";
import { GridConfig } from "../../config";

const ToggleKeyComb = GridConfig.globalKeybinds.toggleKeybindsPage;

export default function KeyBoardHelp() {
	let [showModal, setShowModal] = useState(false);
	const handleCloseModal = () => setShowModal(false);
	useEffect(() => {
		keyboardJS.bind(
			ToggleKeyComb,
			/*
				Pressed is not getting fired on the very first time
				So using released callback which acts the same
				(TODO:P5) see why this issue doesn't occur at other keybinds
			*/
			null,
			() => {
				// Here an important thing to note is that
				// if we use showModal variable and call `setShowModal(!showModal)`
				// the showmodal is always false because it is pass by value
				// Clear explaination https://stackoverflow.com/a/53024497/8608146

				// So we should do it like this: get the current value and toggle it
				setShowModal((state) => !state);
			},
			// prevent duplicates
			true
		);

		keyboardJS.bind("esc", handleCloseModal);

		// close modal on focus and enter
		keyboardJS.bind("enter", () => {
			const dataset = (document.activeElement as HTMLElement).dataset;
			if (dataset.close) handleCloseModal();
		});

		return () => {
			keyboardJS.reset();
		};
	}, []); // empty dependencies ensures this will be only called once

	return (

			<ReactModal isOpen={showModal} contentLabel="Minimal Modal Example">

					{Object.entries(ConsoleKeyCombinations).map(([k, v], _i, _arr) => {
						return (

									{v.keys
										.join(" or ")
										.replace("left", "←")
										.replace("right", "→")}

								{v.desc}
								{v.subdescs !== undefined &&
									v.subdescs.map((subdesc) => (

											{subdesc}

									))}

						);
					})}
					<Tippy content={`${ToggleKeyComb} or esc`}>

					</Tippy>

			</ReactModal>
			CTRL + / keyboard shortcuts

	);
}


text:
ACCEPTED

#### According to
Index Fungorum

#### Published in
Bull. trimest. Soc. mycol. Fr. 69: 290 (1954)

#### Original name
Cercosperma sessilis G. Arnaud

### Remarks
null


text:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class sandbox_platform3 : MonoBehaviour {

	public sandboxcontroller controller;

	// Use this for initialization
	void Start () {

	}

	// Update is called once per frame
	void Update () {
		Vector3 pos = transform.position;
		pos.y = controller.platform3_height;
		transform.position = pos;
	}
}


text:
<?php
if (!$IWEB_SHOP_IN) {
    die('Hacking attempt');
}

require_once($webRoot . "/foundation/cxmloperator.class.php");

$ri_langpackage = new rightlp;
$a_langpackage = new adminlp;

$dbo = new dbex;
$t_admin_group = $tablePreStr . "admin_group";

$gid = get_args('id');

if (get_args('action')) {

    //权限管理
    $right = check_rights("group_privi");
//	if($right){
//		header('location:m.php?app=error');
//	}
    //E.T eidt
    if (!$right) {
        header('location:m.php?app=error');
        exit();
    }
    //E.T eidt
    dbtarget('w', $dbServs);
    $gid = get_args('group_id');
    $sql = "update $t_admin_group set rights=''";
    if (!is_null(get_args('rights'))) $sql = "update $t_admin_group set rights='" . implode(",", get_args('rights')) . "'";
    $sql .= "where id='$gid'";
    if ($dbo->exeUpdate($sql)) {
        $a_permissions_update_suc = $a_langpackage->a_permissions_update_suc;
        echo "";

    }
}

dbtarget('r', $dbServs);
$sql = "select * from $t_admin_group where id='$gid'";
$groups = $dbo->getRow($sql);
$group = $groups['rights'];
$rights = explode(",", $group);
$rights = array_flip($rights);

$dom = new DOMDocument();
$dom->load("resources/resources.xml");
$classes = $dom->getElementsByTagName("group");

$dom_plugin = new DOMDocument();
$dom_plugin->load($webRoot . "plugins/resources.xml");
$classes_plugin = $dom_plugin->getElementsByTagName("group");

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>

<link rel="stylesheet" type="text/css" href="skin/css/admin.css">
<link rel="stylesheet" type="text/css" href="skin/css/main.css">

    <?php include("messagebox.php"); ?>

<?php echo $a_langpackage->a_location; ?>
            >> <?php echo $a_langpackage->a_m_member_oprate; ?> >> <?php echo $ri_langpackage->ri_allot; ?>

<?php echo $ri_langpackage->ri_allot; ?> <?php echo $a_langpackage->a_m_admingroup_set; ?>

                <form action="m.php?app=group_rights" method="post" name="form">
                    <input type="hidden" name="group_id" value="<?php echo $gid; ?>">

                            <?php echo $ri_langpackage->ri_local_user; ?>:<?php echo $groups['group_name']; ?>

                        <?php
                        $group_num = 0;
                        foreach ($classes as $class) {
                            $group_num++;
                            echo "
 " . $class->getAttributeNode('value')->value . " <input type='checkbox' title='group_" . $group_num . "' onclick='select_rights(this)' />" . $ri_langpackage->ri_all_select . "

";
                            $items = $class->getElementsByTagName("resource");
                            for ($i = 0; $i < $items->length; $i++) {
                                $is_checked = "";
                                if (isset($rights[$items->item($i)->getAttributeNode('id')->value])) {
                                    $is_checked = "checked=checked";
                                }
                                ?>

<input type="checkbox" title="<?php echo "group_" . $group_num; ?>"
                                                          name="rights[<?php echo $items->item($i)->getAttributeNode('id')->value; ?>]"
                                                          value="<?php echo $items->item($i)->getAttributeNode('id')->value; ?>"  <?php echo $is_checked; ?> /><?php echo $items->item($i)->getAttributeNode('value')->value; ?>

                            <?php } ?>

                        <?php
                        }
                        foreach ($classes_plugin as $class) {
                            $group_num++;
                            echo "
 " . $class->getAttributeNode('value')->value . " <input type='checkbox' title='group_" . $group_num . "' onclick='select_rights(this)' />" . $ri_langpackage->ri_all_select . "

";
                            $items = $class->getElementsByTagName("resource");
                            for ($i = 0; $i < $items->length; $i++) {
                                $is_checked = "";
                                if (isset($rights[$items->item($i)->getAttributeNode('id')->value])) {
                                    $is_checked = "checked=checked";
                                }
                                ?>

<input type="checkbox" title="<?php echo "group_" . $group_num; ?>"
                                                          name="rights[<?php echo $items->item($i)->getAttributeNode('id')->value; ?>]"
                                                          value="<?php echo $items->item($i)->getAttributeNode('id')->value; ?>"  <?php echo $is_checked; ?> /><?php echo $items->item($i)->getAttributeNode('value')->value; ?>

                            <?php } ?>

                        <?php } ?>

                            <input class="regular-button" type="submit" name="action"
                                                                      value="<?php echo $ri_langpackage->ri_allot; ?>"/>

                </form>


text:
CSS 625 Spring 2015
Course projects of Talha Oz


text:
package ru.job4j3.io;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.function.Consumer;

/**
 * Class EchoServer on socket.
 *
 * @author v.ivanov
 * @version 1
 * @since 21.03.2020
 */
public class EchoServer {

    /** Logger instance.*/
    private static final Logger LOG = LoggerFactory.getLogger(EchoServer.class.getName());

    /**
     * Enum MSG with request part and corresponding answer.
     */
    private enum MSG {
        /** Hello.*/
        HELLO("msg=Hello", "Hello"),

        /** Exit.*/
        EXIT("msg=Exit", "Server finished working");

        /** Message name.*/
        private String part;

        /** Message answer.*/
        private String answer;

        /**
         * Enum MSG constructor.
         * @param part part of request value
         * @param answer value to answer
         */
        MSG(String part, String answer) {
            this.part = part;
            this.answer = answer;
        }
    }

    /**
     * Entry method to run program.
     *
     * @param args args
     * @throws IOException throws exception
     */
    public static void main(String[] args) {
        try (ServerSocket server = new ServerSocket(9000)) {
            boolean isClosed = false;
            Message msg = new Message();
            while (!isClosed) {
                Socket socket = server.accept();
                try (OutputStream out = socket.getOutputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
                    String str;
                    int inc = 0;
                    do {
                        str = in.readLine();
                        System.out.println(str);
                        if (str != null) {
                            String[] arrStr = str.split("\\?");
                            if (++inc == 1 && arrStr.length > 1) {
                                String msgPart = arrStr[1].substring(0, arrStr[1].length() - 8).trim();
                                if (str.startsWith("GET") && msgPart.equals(MSG.EXIT.part)) {
                                    isClosed = true;
                                    msg.accept(msgPart);
                                    server.close();
                                } else {
                                    msg.accept(msgPart);
                                }
                            }
                        }
                    } while (!str.isEmpty());
                    if (!isClosed) {
                        out.write("HTTP/1.1 200 OK\r\n".getBytes());
                        out.write((msg.answer + "\r\n").getBytes());
                    } else {
                        out.write("HTTP/1.1 503 Service Unavailable\r\n".getBytes());
                        out.write((msg.answer + "\r\n").getBytes());
                    }
                }
            }
        } catch (IOException e) {
            LOG.error("IOException error", e);
        }
    }

    /**
     * Class Message.
     */
    private static class Message implements Consumer<String> {

        /** Result answer.*/
        private String answer;

        @Override
        public void accept(String s) {
            if (s.equals(MSG.EXIT.part)) {
                this.answer = MSG.EXIT.answer;
            } else if (s.equals(MSG.HELLO.part)) {
                this.answer = MSG.HELLO.answer;
            } else {
                this.answer = s.replaceAll("msg=", "");
            }
        }
    }
}


text:
<?php

namespace App\MediaWiki\Api\Apis;

/**
 * MediaWiki Prop:Info API
 */
class PropInfoApi extends \App\MediaWiki\Api\Apis\ApiBase
{
	const NAME = 'info';

	const PARAM_PROP = 'inprop';
	const PARAM_CONTINUE = 'incontinue';

	private $parameters = array(
		self::PARAM_PROP => 'displaytitle',
		);

	/**
	 * PropInfoApi
	 *
	 * @param array $parameters an array of parameters to override
	 */
	public function __construct($parameters = array())
	{
		parent::__construct(false);

		$this->parameters = array_merge($this->parameters, $parameters);
	}

	/**
	 * Get the parameter list
	 *
	 * @return array
	 */
	public function parameterList()
	{
		return $this->parameters;
	}

	/**
	 * Get the API query identifier
	 */
	public function apiQueryIdentifier()
	{
		return self::NAME;
	}

	/**
	 * Get the API query name
	 */
	public function apiQueryName()
	{
		return 'prop';
	}
}


text:
  Montdidier est
      une commune située dans le département de Somme en Picardie. Elle totalisait 6 006 habitants en 2008.

À proximité de Montdidier sont situées les communes de
Framicourt située à 3 km, 158 habitants,
Ételfay située à 4 km, 376 habitants,
Courtemanche située à 2 km, 92 habitants,
Royaucourt à 4 km, 215 habitants,
Faverolles située à 3 km, 159 habitants,
Ayencourt située à 2 km, 176 habitants,
 entre autres. De plus, Montdidier est située à seulement 17 km de Roye.

La commune compte de multiples  équipements sportifs, elle propose entre autres un bassin de natation, deux terrains de tennis, un centre d'équitation et quatre boucles de randonnée.

Le nombre de logements, à Montdidier, se décomposait en 2011 en 714 appartements et 2 130 maisons soit
 un marché plutôt équilibré.

À Montdidier le salaire moyen par mois par habitant se situe à approximativement 1 825 €  net. Ceci est au dessous de la moyenne nationale.

La ville dispose s'agissant de l'enseignement aux enfants de deux collèges et un lycée.
Pour les plus jeunes, la commune est équipée de deux maternelles et quatre écoles primaires.
Montdidier est équipée des  installations permettant une vie locale facilitée.
Lors d'un projet immobilier à Montdidier, vous devrez impérativement regarder la valeur des écoles de la communes

À Montdidier, la valeur moyenne à la vente d'un appartement s'évalue à 1 041 € du m² en vente. La valeur moyenne d'une maison à l'achat se situe à 1 602 € du m². À la location la valeur moyenne se situe à 6,98 € du m² mensuel.


text:
package ch.ntb.inf.deep.runtime.mpc555.demo;

import ch.ntb.inf.deep.runtime.mpc555.driver.Robi2;
import ch.ntb.inf.deep.runtime.ppc32.Task;

/**
 * Demo application for the NTB Robi2. Let the Robi following a line on the
 * surface.
 *
 * This class initializes the task as needed and carries the driving control. In
 * order to use it, build a class creating an instance of this class and install
 * it using Task.install
 */
public class Robi2LineTask extends Task {
	public Robi2LineTask() {
		// Setup the demo task
		this.period = 0;

		// Initialize some LEDs
		for (int i = 0; i < 4; i++) {
			Robi2.setPatternLED(i, 1, true);
		}
	}

	private boolean sensors() {
		boolean isSensor = false;
		if (Robi2.getDistSensorValue(2) > 250) {
			isSensor = true;
		}
		for (short i = 4; i < 16; i++) {
			if (Robi2.getDistSensorValue(i) > 250) {
				isSensor = true;
			}
		}
		return isSensor;
	}

	private boolean leftFloorSensor() {
		return (Robi2.getDistSensorValue(0) > 550);
	}

	private boolean rightFloorSensor() {
		return (Robi2.getDistSensorValue(1) > 550);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see ch.ntb.inf.deep.runtime.mpc555.Task#action()
	 */
	public void action() {
		if (sensors()) {
			Robi2.stop();
		} else if (leftFloorSensor() && rightFloorSensor()) {
			Robi2.setRightDriveSpeed(50);
			Robi2.setLeftDriveSpeed(100);
		} else if (!rightFloorSensor() && !leftFloorSensor()) {
			Robi2.setRightDriveSpeed(-50);
			Robi2.setLeftDriveSpeed(-100);
		} else if (!leftFloorSensor() && rightFloorSensor()) {
			Robi2.setRightDriveSpeed(-50);
			Robi2.setLeftDriveSpeed(-100);
		} else {
			Robi2.drive(80);
		}
	}
}