当前位置: 首页>>代码示例>>Scala>>正文


Scala mutable类代码示例

本文整理汇总了Scala中scala.collection.mutable的典型用法代码示例。如果您正苦于以下问题:Scala mutable类的具体用法?Scala mutable怎么用?Scala mutable使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了mutable类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: CreateSaltedTable

//设置package包名称以及导入依赖的类
package com.cloudera.sa.apptrans.setup.hbase

import java.io.File

import org.apache.commons.lang.StringUtils
import org.apache.hadoop.hbase.{HBaseConfiguration, HColumnDescriptor, HTableDescriptor, TableName}
import org.apache.hadoop.hbase.client.ConnectionFactory
import org.apache.hadoop.hbase.io.compress.Compression
import org.apache.hadoop.hbase.regionserver.{BloomType, ConstantSizeRegionSplitPolicy}
import org.apache.hadoop.hbase.util.Bytes

import scala.collection.mutable


object CreateSaltedTable {
  def main(args:Array[String]): Unit = {

    if (args.length == 0) {
      println("<tableName> <columnFamily> <regionCount> <numOfSalts> <hbaseConfigFolder>")
    }
    val tableName = args(0)
    val columnFamilyName = args(1)
    val regionCount = args(2).toInt
    val numOfSalts = args(3).toInt
    val hbaseConfigFolder = args(4)

    val conf = HBaseConfiguration.create()

    conf.addResource(new File(hbaseConfigFolder + "hbase-site.xml").toURI.toURL)

    val connection = ConnectionFactory.createConnection(conf)

    val admin = connection.getAdmin

    val tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName))

    val columnDescriptor = new HColumnDescriptor(columnFamilyName)

    columnDescriptor.setCompressionType(Compression.Algorithm.SNAPPY)
    columnDescriptor.setBlocksize(64 * 1024)
    columnDescriptor.setBloomFilterType(BloomType.ROW)

    tableDescriptor.addFamily(columnDescriptor)

    tableDescriptor.setMaxFileSize(Long.MaxValue)
    tableDescriptor.setRegionSplitPolicyClassName(classOf[ConstantSizeRegionSplitPolicy].getName)

    val splitKeys = new mutable.MutableList[Array[Byte]]
    for (i <- 0 to regionCount) {
      val regionSplitStr = StringUtils.leftPad((i*(numOfSalts/regionCount)).toString, 4, "0")
      splitKeys += Bytes.toBytes(regionSplitStr)
    }
    admin.createTable(tableDescriptor, splitKeys.toArray)
  }
} 
开发者ID:tmalaska,项目名称:AppTrans,代码行数:56,代码来源:CreateSaltedTable.scala

示例2: MetadataExtractor

//设置package包名称以及导入依赖的类
package com.jasonfeist.spark.tika

import org.apache.spark.input.PortableDataStream
import org.apache.tika.io.TikaInputStream
import org.apache.tika.metadata.Metadata
import org.apache.tika.parser.{AutoDetectParser, ParseContext}
import org.apache.tika.sax.BodyContentHandler

import scala.collection.mutable


class MetadataExtractor extends Serializable {

  def extract(
               file: (String, PortableDataStream)
             ) : (BodyContentHandler, Metadata, mutable.Map[String, String]) = {

    val tis = TikaInputStream.get(file._2.open())
    val parser = new AutoDetectParser()
    val handler = new BodyContentHandler(-1)
    val metadata = new Metadata()
    parser.parse(tis, handler, metadata, new ParseContext())
    val lowerCaseToCaseSensitive = mutable.Map[String, String]()
    for (name <- metadata.names()) {
      lowerCaseToCaseSensitive += (name.toLowerCase -> name)
    }
    (handler, metadata, lowerCaseToCaseSensitive)
  }

} 
开发者ID:jasonfeist,项目名称:tika-spark-datasource,代码行数:31,代码来源:MetadataExtractor.scala

示例3: KeyByAndMergeSyntax

//设置package包名称以及导入依赖的类
package sbtwhitesource

import scala.collection.GenTraversableOnce
import scala.collection.mutable

object `package` {
  implicit class KeyByAndMergeSyntax[A](val _xs: GenTraversableOnce[A]) extends AnyVal {
    
    def keyByAndMerge[K](keyFn: A => K, merge: (A, A) => Option[A]): Map[K, A] = {
      val m = mutable.Map.empty[K, A]
      for (elem <- _xs) {
        val key = keyFn(elem)
        val value = m get key match {
          case None    => elem
          case Some(a) =>
            merge(a, elem) getOrElse
              (sys error s"Multiple elements for the same key $key:\n\t$a\n\t$elem")
        }
        m(key) = value
      }
      m.toMap
    }
  }
} 
开发者ID:typesafehub,项目名称:sbt-whitesource,代码行数:25,代码来源:package.scala

示例4: TestEnv

//设置package包名称以及导入依赖的类
package tech.artemisia.core

import java.util.concurrent.locks.{ReentrantLock, Lock}
import scala.collection.mutable



object TestEnv extends Env {

  override val osUtil: OSUtil = new TestOsUtil

  class TestOsUtil extends OSUtil {

    private[this] val systemVariableMap: mutable.Map[String,String] = mutable.Map("foo" -> "bar")
    private[this] val systemPropertiesMap: mutable.Map[String,String] = mutable.Map("user.home" -> System.getProperty("user.home"))
    private[this] val lock :Lock = new ReentrantLock()

    override def getSystemVariable(variable: String): Option[String] = {
      systemVariableMap get variable
    }

    def getSystemProperties(variable: String): Option[String] = {
      systemPropertiesMap get variable
    }

    def withSysVar(map: scala.collection.Map[String,String])(body : => Unit): Unit = {
      try {
        lock.lock()
        systemVariableMap.clear()
        systemVariableMap ++= map.toSeq
        body
      }
      catch {
        case e: Throwable => throw e
      }
      finally {
        lock.unlock()
      }
    }
  }

} 
开发者ID:gitter-badger,项目名称:artemisia,代码行数:43,代码来源:TestEnv.scala

示例5: maintainSum

//设置package包名称以及导入依赖的类
package hu.bme.mit.ire.nodes.unary

import hu.bme.mit.ire.datatypes._
import hu.bme.mit.ire.messages.ChangeSet
import hu.bme.mit.ire.util.GenericMath

import scala.collection.immutable.VectorBuilder
import scala.collection.mutable

trait SumLike {
  val sums = new mutable.HashMap[Tuple, Any].withDefault(d => 0)

  def maintainSum(changeSet: ChangeSet, aggregationKeys: Vector[Int], sumKey: Int): ChangeSet = {
    val oldValues = new mutable.HashMap[Tuple, Any]

    for (tuple <- changeSet.positive;
         key = aggregationKeys.map(tuple(_))) {
      if (!oldValues.contains(key)) {
        oldValues(key) = sums(key)
      }
      sums(key) = GenericMath.plus(sums(key), tuple(sumKey))
    }

    for (tuple <- changeSet.negative;
         key = aggregationKeys.map(tuple(_))) {
      if (!oldValues.contains(key)) {
        oldValues(key) = sums(key)
      }
      sums(key) = GenericMath.minus(sums(key), tuple(sumKey))
    }

    val positive = new VectorBuilder[Tuple]
    val negative = new VectorBuilder[Tuple]
    for ((key, oldValue) <- oldValues) {
      if (oldValue != 0) {
        negative += key :+ oldValues(key)
      }
      if (sums(key) != 0) {
        positive += key :+ sums(key)
      }
    }

    return ChangeSet(positive = positive.result(), negative = negative.result())
  }
} 
开发者ID:FTSRG,项目名称:ire,代码行数:46,代码来源:SumLike.scala

示例6: HelloSpec

//设置package包名称以及导入依赖的类
import org.scalatest._

import scala.collection.mutable

class HelloSpec extends FlatSpec with Matchers {
  "Hello" should "have tests" in {
    true should === (true)
  }

  "A Stack" should "pop values in last-in-first-out order" in {
    val stack = new mutable.Stack[Int]

    stack.push(1)
    stack.push(2)
    stack.pop should be (2)
    stack.pop should be (1)
  }

  it should "throw NoSuchElementException if an empty stack is popped " in {
    val emptyStack = new mutable.Stack[Int]
    a [NoSuchElementException] should be thrownBy {
      emptyStack.pop()
    }
  }
} 
开发者ID:greatdreams,项目名称:simple-scala-applciation-template,代码行数:26,代码来源:HelloSpec.scala

示例7: PlayRenderer

//设置package包名称以及导入依赖的类
package eu.tznvy.jancy.transpiler.rendering

import eu.tznvy.jancy.core.Play
import eu.tznvy.jancy.transpiler.helpers.ArraysHelper

import scala.collection.JavaConverters._
import scala.collection.mutable


object PlayRenderer extends Renderer[Play] {

  override def render(play: Play): String =
    YamlContext.get.dump(buildModel(play).asJava)

  override def renderAll(plays: Array[Play]): String = {
    val model = plays.map(buildModel(_).asJava).toArray
    YamlContext.get.dump(model)
  }

  private def buildModel(play: Play): mutable.LinkedHashMap[String, Any] = {
    val orderedPairs = "name" -> play.getName :: List[(String, Any)](
        "hosts" -> ArraysHelper.flattenAnArray(play.getHosts),
        "roles" -> play.getRoles,
        "tasks" -> play.getTasks.map(new TasklikeRenderer().buildModel(_).asJava),
        "handlers" -> play.getHandlers.map(new TasklikeRenderer().buildModel(_).asJava))
      .filter((p) => !ArraysHelper.isAnEmptyArray(p._2))
      .sortBy(_._1)

    mutable.LinkedHashMap[String, Any](orderedPairs: _*)
  }
} 
开发者ID:brthanmathwoag,项目名称:jancy,代码行数:32,代码来源:PlayRenderer.scala

示例8: EnvironmentCacheEntry

//设置package包名称以及导入依赖的类
package com.galacticfog.gestalt.lambda.impl

import java.util.concurrent.TimeoutException

import org.apache.mesos.Protos
import org.joda.time.DateTime
import scala.collection.mutable
import scala.concurrent.{Await, Future}
import scala.concurrent.duration._

case class EnvironmentCacheEntry( lambdaId : String, env : Protos.Environment, queuedTime : DateTime = DateTime.now )
class EnvironmentCache {

  val cache : mutable.Map[String, EnvironmentCacheEntry] = mutable.Map[String, EnvironmentCacheEntry]()
  val EXPIRATION_SECONDS = sys.env.getOrElse( "ENV_CACHE_EXPIRATION_SECONDS", "900" ).toInt

  def getEnvironment( lambdaId : String, env : Future[Map[String,String]] ) : Protos.Environment = {

    val cacheEntry = cache.get( lambdaId )

    if( !cacheEntry.isDefined || cacheEntry.get.queuedTime.plusSeconds( EXPIRATION_SECONDS ).isBeforeNow ) {
      //wait for the future
      try {
        val result = Await.result( env, 5 seconds )
        val builder = Protos.Environment.newBuilder
        result.foreach{ entry =>
          builder.addVariables( Protos.Environment.Variable.newBuilder
            .setName( entry._1 )
            .setValue( entry._2 )
          )
        }

        val newEnv =  builder.build
        cache( lambdaId ) = new EnvironmentCacheEntry( lambdaId, newEnv )
        newEnv
      }
      catch {
        case ex : TimeoutException => {
          println( "TIMEOUT" )
          Protos.Environment.newBuilder.build
        }
      }
    }
    else {
      cache( lambdaId ).env
    }
  }
} 
开发者ID:GalacticFog,项目名称:gestalt-lambda,代码行数:49,代码来源:EnvironmentCache.scala

示例9: MysqlDbDriver

//设置package包名称以及导入依赖的类
package com.criteo.qwebmon.drivers

import javax.sql.DataSource

import com.criteo.qwebmon.{RunningQuery, DbDriver}

import scala.collection.mutable

class MysqlDbDriver(val name: String, dataSource: DataSource) extends DbDriver {

  private val runningQueriesSql = "show processlist"

  override def runningQueries: Seq[RunningQuery] = {
    val runningQueries: Seq[RunningQuery] = JdbcHelpers.executeQuery(dataSource, runningQueriesSql) { rs =>
      val acc = mutable.ListBuffer.empty[RunningQuery]
      while (rs.next()) {
        Option(rs.getString(8)).foreach { query =>
          acc += RunningQuery(
            user = rs.getString(2),
            runSeconds = rs.getInt(6),
            query = query,
            hostname = rs.getString(3)
          )
        }
      }
      acc
    }
    runningQueries
  }

} 
开发者ID:jqcoffey,项目名称:qwebmon,代码行数:32,代码来源:MysqlDbDriver.scala

示例10: VerticaDbDriver

//设置package包名称以及导入依赖的类
package com.criteo.qwebmon.drivers

import javax.sql.DataSource

import com.criteo.qwebmon.{RunningQuery, DbDriver}

import scala.collection.mutable

class VerticaDbDriver(val name: String, dataSource: DataSource) extends DbDriver {

  private val runningQueriesSql =
    """
      |SELECT
      |  user_name as user_name,
      |  DATEDIFF('second', statement_start, clock_timestamp()) as exec_time,
      |  current_statement as current_statement,
      |  client_hostname as client_hostname
      |FROM
      |  sessions
      |WHERE
      |  transaction_id <> -1 and
      |  statement_id is not null
      |ORDER BY
      |  exec_time desc
    """.stripMargin

  override def runningQueries: Seq[RunningQuery] = {
    val runningQueries: Seq[RunningQuery] = JdbcHelpers.executeQuery(dataSource, runningQueriesSql) { rs =>
      val acc = mutable.ListBuffer.empty[RunningQuery]
      while (rs.next()) {
        acc += RunningQuery(
          user = rs.getString(1),
          runSeconds = rs.getInt(2),
          query = rs.getString(3),
          hostname = rs.getString(4)
        )
      }
      acc
    }
    runningQueries
  }

} 
开发者ID:jqcoffey,项目名称:qwebmon,代码行数:44,代码来源:VerticaDbDriver.scala

示例11: Application

//设置package包名称以及导入依赖的类
package controllers

import java.io.{IOException, InputStream}

import models.TheWord
import play.api._
import play.api.mvc._

import scala.collection.mutable

object Application extends Controller {

  implicit val app = Play.current

  def index = Action {
    val real = getThatText("sample1.txt")
    val result = doTheJobFun {
      mutable.Seq(real: _*)
    }
    Ok(views.html.index(real.mkString("\n"), result))

  }

  def test = Action {
    Ok(getThatText("sample1.txt").mkString("\n"))
  }

  def load(filePath: String): InputStream = {
    Play.resourceAsStream("public/inputs/" concat filePath).getOrElse(throw new IOException("file not found: " + filePath))
  }

  def getThatText(fileName: String) = {
    //    val source = scala.io.Source.fromFile(app.getFile(fileName))("UTF-8")
    val source = scala.io.Source.fromInputStream(load(fileName))("UTF-8")
    try source.getLines().toList
    catch {
      case e: Exception => e.printStackTrace(); null
    }
    finally source.close()
  }

  val doTheJobFun = (text: mutable.Seq[String]) => {
    text.flatMap(_.split("[.?!:]"))
      .map(_.split("\\s+").find(_.nonEmpty).getOrElse(""))
      .filter(_.matches("[a-zA-Z].*"))
      .filter(!_.equals(""))
      .map { p => TheWord(p, p.reverse) }
      .toList
  }

} 
开发者ID:muhrifqii,项目名称:learn-to-play-scala,代码行数:52,代码来源:Application.scala

示例12: OSMParser

//设置package包名称以及导入依赖的类
package tosidewalk

import scala.collection.mutable
import scala.xml.XML


class OSMParser {

  def parse = {
    val xml = XML.loadFile("data/shadyside.osm")

    // Parse all the streets from the xml file
    val streets = for {
      way <- xml \ "way"
      tags = way \\ "tag"
      streetness <- tags.map(t => (t \ "@k").toString -> (t \ "@v").toString).filter {
        case (k, v) =>
          (k == "highway") &&
            (v == "trunk" || v == "primary" || v == "secondary" || v == "tertiary" || v == "residential")
      }
    } yield way

    // Find the intersections where *three* or more streets share a node
    val nodeToWays = new mutable.HashMap[String, mutable.ListBuffer[String]]()
    for {
      street <- streets
      node <- street \\ "nd"
    } {
      val wayId = (street \ "@id").toString
      val nodeId = (node \ "@ref").toString
      if (nodeToWays.contains(wayId)) {
        nodeToWays(wayId) += nodeId
      } else {
        nodeToWays(wayId) = mutable.ListBuffer(nodeId)
      }
    }

    val intersectionNodeIds = nodeToWays.filter(_._2.length > 2).keys

    // Split the streets at intersections
    for {
      street <- streets
    } {
      val nodes = (street \\ "nd").map(_ \ "@ref").map(_.toString)
      println(nodes)
    }
  }

} 
开发者ID:kotarohara,项目名称:ToSidewalk,代码行数:50,代码来源:OSMParser.scala

示例13: FriendsListCache

//设置package包名称以及导入依赖的类
package com.init6.db

import scala.collection.mutable


private[db] class FriendsListCache(dbFriendsList: Seq[DbFriend]) {

  private val cache = (mutable.HashMap.newBuilder ++= dbFriendsList.groupBy(_.user_id)).result()

  def get(id: Long) = cache.get(id)

  def insert(user_id: Long, friend_position: Int, friend_id: Long, friend_name: String): DbFriend = {
    val dbFriend = DbFriend(user_id = user_id, friend_position = friend_position, friend_id = friend_id, friend_name = friend_name)

    cache += cache
      .get(user_id)
      .fold(user_id -> Seq(dbFriend))(seq => user_id -> (seq :+ dbFriend))
    DAO.saveInsertedFriend(dbFriend)

    dbFriend
  }

  def update(user_id: Long, friend_position: Int, friend_id: Long, friend_name: String): DbFriend = {
    val dbFriend = DbFriend(user_id = user_id, friend_position = friend_position, friend_id = friend_id, friend_name = friend_name)

    cache
      .get(user_id)
      .foreach(seq => user_id -> (seq :+ dbFriend))
    DAO.saveUpdatedFriend(dbFriend)

    dbFriend
  }

  def delete(user_id: Long, friend_position: Int): DbFriend = {
    val dbFriend = cache(user_id)(friend_position - 1)

    cache
      .get(user_id)
      .foreach(seq => {
        cache += user_id -> ((0 until friend_position - 1).map(seq) ++
        (friend_position until seq.length).map(i => {
          val newDbFriend = seq(i).copy(friend_position = i)
          DAO.saveUpdatedFriend(newDbFriend)
          newDbFriend
        }))
        DAO.saveDeletedFriend(dbFriend)
      })

    dbFriend
  }
} 
开发者ID:fjaros,项目名称:init6,代码行数:52,代码来源:FriendsListCache.scala

示例14: TopInfoSeq

//设置package包名称以及导入依赖的类
package com.init6.utils

import akka.actor.ActorRef
import com.init6.users.TopInfo

import scala.collection.mutable


object TopInfoSeq {
  val DEFAULT_LIMIT = 25

  def apply(limit: Int = DEFAULT_LIMIT) = new TopInfoSeq(limit)
}

// Should be implemented using a MinMaxPriorityQueue (PriorityDeque)
// Doesn't seem scala offers any such thing so let's cheese it
sealed class TopInfoSeq(val limit: Int) {

  private val alreadyLogged = mutable.HashSet.empty[ActorRef]
  private var _l = mutable.PriorityQueue.empty[TopInfo](Ordering.by[TopInfo, Long](_.connectionInfo.connectedTime).reverse)
  private var lastConnectionTime: Long = 0

  def +=(elem: TopInfo): Unit = {
    if (!alreadyLogged.contains(elem.connectionInfo.actor)) {
      if (limit > _l.size) {
        _l += elem
        alreadyLogged += elem.connectionInfo.actor

        if (elem.connectionInfo.connectedTime > lastConnectionTime) {
          lastConnectionTime = elem.connectionInfo.connectedTime
        }

      } else if (elem.connectionInfo.connectedTime < lastConnectionTime) {
        _l += elem
        alreadyLogged += elem.connectionInfo.actor

        // gross as fuck
        val oldQ = _l.dequeueAll
        oldQ.dropRight(1).foreach(_l += _)
      }
    }
  }

  // also gross
  def values: Seq[TopInfo] = _l.clone().dequeueAll
} 
开发者ID:fjaros,项目名称:init6,代码行数:47,代码来源:TopInfoSeq.scala

示例15: CaseInsensitiveFiniteHashSet

//设置package包名称以及导入依赖的类
package com.init6.utils

import scala.collection.mutable


object CaseInsensitiveFiniteHashSet {
  def apply(limit: Int) = new CaseInsensitiveFiniteHashSet(limit)
}

sealed class CaseInsensitiveFiniteHashSet(limit: Int) extends mutable.LinkedHashSet[String] {

  override def contains(elem: String): Boolean = super.contains(elem.toLowerCase)
  override def remove(elem: String): Boolean = super.remove(elem.toLowerCase)

  override def add(elem: String): Boolean = {
    if (size == limit) {
      super.remove(head)
    }
    super.add(elem.toLowerCase)
  }
} 
开发者ID:fjaros,项目名称:init6,代码行数:22,代码来源:CaseInsensitiveFiniteHashSet.scala


注:本文中的scala.collection.mutable类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。