自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Kestrel.scala中的PersistentQueue

開發(fā) 后端
本文介紹Kestrel中的PersistentQueue,后面還重點(diǎn)介紹了一下Scala中的match和case的用法。

上一篇講到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一樣拋出一個異常。

讓我們重新讀下面這段代碼:

  1. private[kestrel] def queue(name: String): Option[PersistentQueue] = synchronized {  
  2.     ……  
  3.       Some(queues.get(name) getOrElse {  
  4.         // only happens when creating a queue for the first time.  
  5.         val q = if (name contains '+') {  
  6.           val master = name.split('+')(0)  
  7.           fanout_queues.getOrElseUpdate(master, new mutable.HashSet[String]) += name  
  8.           log.info("Fanout queue %s added to %s", name, master)  
  9.           new PersistentQueue(path.getPath, name, queueConfigs.configMap(master))  
  10.         } else {  
  11.           new PersistentQueue(path.getPath, name, queueConfigs.configMap(name))  
  12.         }  
  13.         q.setup  
  14.         queues(name) = q  
  15.         q  
  16.       })  
  17.   ……  
  18.   }  

先不要暈,根據(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方法里面有這么一段代碼:

  1. queue(key) match {  
  2.   case None => false 
  3.   case Some(q) =>  
  4.     ……  
  5.     val result = q.add(item, normalizedExpiry)  
  6.     if (result) totalAdded.incr()  
  7.     result  
  8. }  

之前我們知道 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。

【編輯推薦】

  1. 走讀Kestrel,了解Scala
  2. Kestrel.scala中的QueueCollection
  3. 從Kestrel看Scala的核心程序模塊
  4. Scala實(shí)例教程:Kestrel
  5. Scala編程語言
責(zé)任編輯:yangsai 來源: dingsding.com
相關(guān)推薦

2009-09-28 11:25:17

PersistentQKestrelScala

2009-09-22 09:59:40

QueueCollecScala

2009-09-28 11:37:03

Journal.scaKestrel

2009-09-18 11:44:05

Scala實(shí)例教程Kestrel

2009-09-28 11:42:21

KestrelScala

2009-09-28 10:26:12

Scala代碼實(shí)例Kestrel

2009-09-22 09:42:24

Scala的核心

2009-07-22 07:53:00

Scala擴(kuò)展類

2009-07-08 15:35:18

Case類Scala

2009-07-22 07:45:00

Scala代碼重復(fù)

2023-06-12 15:33:52

Scalafor循環(huán)語句

2009-07-21 17:21:57

Scala定義函數(shù)

2009-07-08 12:43:59

Scala ServlScala語言

2020-10-31 17:33:18

Scala語言函數(shù)

2010-09-14 15:34:41

Scala

2009-07-22 08:57:49

Scalafinal

2009-07-21 11:25:03

ScalaRational類

2009-07-20 18:03:26

Scala程序Singleton對象

2017-03-07 15:13:28

Scala偏函數(shù)函數(shù)

2009-07-21 14:03:00

Scalaif表達(dá)式while循環(huán)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號