Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is PersistentQueue in Kestrel.scala

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is PersistentQueue in Kestrel.scala". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is PersistentQueue in Kestrel.scala".

Continue to read the code of QueueCollection.scala, because a large number of methods encapsulated later are operations on queues and fanout_queues. By definition, these two variables are of type mutable.HashMap [String, XXXX], so let's first introduce several unfamiliar methods of mutable.HashMap in Java: (scala's apidoc can be found in 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.

We found that get and apply have exactly the same function in Scala, but what exactly does Option mean in the return value of get? This question has puzzled us for a long time since we first started reading the Scala code. In fact, looking up the manual of Scala, it is not difficult to find that this is a modification of NULL, because in Java, some are object-oriented variables and some are not, if you need to ensure that all empty judgments are consistent in the Scala language, then you need to do something. So Scala designed the abstract class Option, as well as two subclasses Some and None. An instance of Option, either of type Some or of type None. So pass Option [type] as a parameter, that is, deal with the null value of this type, if it does not exist, return None [type], there is no need to throw an exception like apply.

Let's reread the following code:

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}). }

Don't get dizzy, based on our previous understanding of Option, we know that this is a PersistentQueue class encapsulated by Option. We also know that all Scala methods do not need return,***. The return value of the execute command is the return value of this method, so here, Some (...) It's the return value of the whole method, and I'm glad, because the method defines the return value as PersistentQueue, so we know that the Some parentheses must also be PersistentQueue.

Some (queues.get (name) …) Good, because queues is defined as mutable.HashMap [String, PersistentQueue], so get returns Option [PersistentQueue]. This method seems to have been written, what on earth are you doing? GetOrElse, by definition, means that if the value does not exist, then do the following {}. This way of writing is actually dealing with null values. From a QueueCollection point of view, when querying queues, if the queue does not exist, then do the processing in {} to create a queue. One thing to note here is that this getOrElse is not the getOrElse of HashMap, but the getOrElse of Option.

Then it's easier to read. Create a Q of type PersistentQueue, assign it to queues (name), and add it to the HashMap table. Don't forget to use Q as the return of the entire function, that is, the return of Some (). It's the same as when get (name) existed.

With the function queue as the basis, it is much easier to read later. Let's focus on the usage of match and case. There is a piece of code in the add method:

Queue (key) match {case None = > false case Some (Q) = >. Val result = q.add (item, normalizedExpiry) if (result) totalAdded.incr () result}

We knew before that queue (key) returns Option [PersistentQueue], and match is to match and perform different operations according to different matches, None. If the queue is not found, then false is returned. Some (Q), if queue returns an Some type, that is, if Option has a value, then this Q is the instance of the returned PersistentQueue type! Just like the parameters of a function, you can use it directly.

Surprisingly, when I first came into contact with Scala, I could hardly believe that case could make such a judgment. We later found that this judgment can be made because all Scala are encapsulated by classes, and based on the base class of Scala, we implement a so-called abstract class of case class and case object, and implement a unified class-based = = operator. This series of changes led to the extremely powerful case syntax of Scala.

Thank you for your reading, the above is the content of "what is PersistentQueue in Kestrel.scala". After the study of this article, I believe you have a deeper understanding of what the PersistentQueue in Kestrel.scala is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report