Kestrel.scala中的PersistentQueue

上一篇讲到Kestrel.scala中的QueueCollection,下面将介绍PersistentQueue。

继续走读QueueCollection.scala的代码,因为后面封装的大量方法,都是对queues和fanout_queues的操作,根据定义,这两个变量都是mutable.HashMap[String, XXXX]类型的,所以我们先介绍一下mutable.HashMap的几个在Java中陌生的方法:( scala 的 apidoc 在 http://www.scala-lang.org/docu/files/api/index.html 可以查到)

◆apply (key : A) : B

◆Retrieve the value which is associated with the given key. This method throws an exception if there is no mapping from the given key to a value.

◆get (key : A) : Option[B]

◆Check if this map maps key to a value and return the value if it exists.

◆getOrElse [B2 >: B](key : A, default : => B2) : B2

◆Check if this map maps key to a value. Return that value if it exists, otherwise return default.

◆getOrElseUpdate (key : A, default : => B) : B

◆Check if this map maps key to a value. Return that value if it exists, otherwise put default as that key’s value and return it.

我们发现get和apply在Scala中,是完全相同的功能,但是在get返回值里面的Option究竟是什么意思呢?这个问题从刚开始阅读Scala代码的时候就已经困惑我们很久了。其实查询一下Scala的手册,我们不难发现,这是一个对于NULL的改造,因为在Java里面,有些是面向对象的变量,而有些不是,如果需要在Scala的语言内,保证所有对空的判断是一致的,那么就需要做一点什么。所以Scala设计了Option这个抽象类,以及两个子类Some和None。Option的实例,要么是Some类型,要么是None类型。所以把Option[类型]作为参数传递,也就是把这种类型的空值一并处理了,如果不存在,返回的是None[类型],不需要象apply一样抛出一个异常。

让我们重新读下面这段代码:

private[kestrel] def queue(name: String): Option[PersistentQueue] = synchronized {  


    ……  


      Some(queues.get(name) getOrElse {  



        // only happens when creating a queue for the first time.  




        val q = if (name contains '+') {  




          val master = name.split('+')(0)  




          fanout_queues.getOrElseUpdate(master, new mutable.HashSet[String]) += name  




          log.info("Fanout queue %s added to %s", name, master)  




          new PersistentQueue(path.getPath, name, queueConfigs.configMap(master))  




        } else {  




          new PersistentQueue(path.getPath, name, queueConfigs.configMap(name))  



        }  


        q.setup  


        queues(name) = q  


        q  


      })  


  ……  


  }  

先不要晕,根据之前对Option的理解,我们知道这是一个被Option封装了的PersistentQueue类。我们也知道了所有的Scala方法都不需要return,最后一条执行命令的返回值就是这个方法的返回值,所以,在这里,Some(……)就是整个方法的返回值,很高兴,因为方法对返回值的定义是PersistentQueue,所以我们知道Some括号里面的一定也是PersistentQueue。

Some(queues.get(name) ……),很好,因为queues的定义是mutable.HashMap[String, PersistentQueue],所以get返回的就是Option[PersistentQueue]。这个方法貌似已经写完了,后面的到底是在做什么呢?getOrElse,按照定义,就是如果值不存在,那么就做后面{}里面的事情,这样的写法,其实就是对空值的处理。用QueueCollection角度来看,就是当查询queues的时候,这个队列如果不存在,那么就做{}里面的处理,创建一个队列。这里需要注意的是――这个getOrElse不是HashMap的getOrElse,而是Option的getOrElse。

然后读起来就比较顺利了,创建一个q,是PersistentQueue类型的,把它赋值给queues(name)中,加入HashMap表中。最后不要忘记把q作为整个函数的返回,也就是Some()的返回。和get(name)存在的时候一样。

有了queue这个函数作为基础,后面读起来就容易很多了,我们就重点介绍一下match和case的用法,在add方法里面有这么一段代码:

queue(key) match {  



  case None => false 




  case Some(q) =>  



    ……  


    val result = q.add(item, normalizedExpiry)  



    if (result) totalAdded.incr()  



    result  


}  

之前我们知道 queue(key)返回的是Option[PersistentQueue],match就是做匹配,根据不同的匹配来执行不同的操作,None,如果这个queue没有查询到,那么就返回false。Some(q),如果queue返回的是一个Some类型,也就是Option有值的时候的返回,那么这个q就是返回的PersistentQueue类型的那个实例!就像函数的参数一样,可以直接使用。

相关推荐