Kestrel.scala中的PersistentQueue
上一篇講到Kestrel.scala中的QueueCollection,下面將介紹PersistentQueue。
繼續(xù)走讀QueueCollection.scala的代碼,因?yàn)楹竺娣庋b的大量方法,都是對queues和fanout_queues的操作,根據(jù)定義,這兩個變量都是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.
#t#我們發(fā)現(xiàn)get和apply在Scala中,是完全相同的功能,但是在get返回值里面的Option究竟是什么意思呢?這個問題從剛開始閱讀Scala代碼的時候就已經(jīng)困惑我們很久了。其實(shí)查詢一下Scala的手冊,我們不難發(fā)現(xiàn),這是一個對于NULL的改造,因?yàn)樵贘ava里面,有些是面向?qū)ο蟮淖兞浚行┎皇?,如果需要在Scala的語言內(nèi),保證所有對空的判斷是一致的,那么就需要做一點(diǎn)什么。所以Scala設(shè)計了Option這個抽象類,以及兩個子類Some和None。Option的實(shí)例,要么是Some類型,要么是None類型。所以把Option[類型]作為參數(shù)傳遞,也就是把這種類型的空值一并處理了,如果不存在,返回的是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
- })
- ……
- }
先不要暈,根據(jù)之前對Option的理解,我們知道這是一個被Option封裝了的PersistentQueue類。我們也知道了所有的Scala方法都不需要return,***一條執(zhí)行命令的返回值就是這個方法的返回值,所以,在這里,Some(……)就是整個方法的返回值,很高興,因?yàn)榉椒▽Ψ祷刂档亩x是PersistentQueue,所以我們知道Some括號里面的一定也是PersistentQueue。
Some(queues.get(name) ……),很好,因?yàn)閝ueues的定義是mutable.HashMap[String, PersistentQueue],所以get返回的就是Option[PersistentQueue]。這個方法貌似已經(jīng)寫完了,后面的到底是在做什么呢?getOrElse,按照定義,就是如果值不存在,那么就做后面{}里面的事情,這樣的寫法,其實(shí)就是對空值的處理。用QueueCollection角度來看,就是當(dāng)查詢queues的時候,這個隊(duì)列如果不存在,那么就做{}里面的處理,創(chuàng)建一個隊(duì)列。這里需要注意的是——這個getOrElse不是HashMap的getOrElse,而是Option的getOrElse。
然后讀起來就比較順利了,創(chuàng)建一個q,是PersistentQueue類型的,把它賦值給queues(name)中,加入HashMap表中。***不要忘記把q作為整個函數(shù)的返回,也就是Some()的返回。和get(name)存在的時候一樣。
有了queue這個函數(shù)作為基礎(chǔ),后面讀起來就容易很多了,我們就重點(diǎn)介紹一下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就是做匹配,根據(jù)不同的匹配來執(zhí)行不同的操作,None,如果這個queue沒有查詢到,那么就返回false。Some(q),如果queue返回的是一個Some類型,也就是Option有值的時候的返回,那么這個q就是返回的PersistentQueue類型的那個實(shí)例!就像函數(shù)的參數(shù)一樣,可以直接使用。
很驚奇吧,剛接觸Scala的時候,我?guī)缀鯚o法相信case可以這樣做判斷。后來我們發(fā)現(xiàn),之所以能夠做這種判斷,是因?yàn)樗械腟cala都是被類封裝的,并且基于Scala的基類,實(shí)現(xiàn)了一個所謂的case class和case object的抽象類,并且實(shí)現(xiàn)了基于類的統(tǒng)一的==操作符。這一連串的改變造就了異常強(qiáng)大的Scala的case語法。
至于match…case還能怎么用,參考這個鏈接 http://programming-scala.labs.oreilly.com/ch03.html#PatternMatching。
【編輯推薦】