• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JohnSnowLabs / spark-nlp / 10675586955

03 Sep 2024 02:30AM UTC coverage: 61.821% (-0.06%) from 61.884%
10675586955

Pull #14379

github

web-flow
Merge 1f222af49 into 9285df8c6
Pull Request #14379: SPARKNLP Introducing LLAMA 3

0 of 27 new or added lines in 3 files covered. (0.0%)

15 existing lines in 11 files now uncovered.

8982 of 14529 relevant lines covered (61.82%)

0.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.59
/src/main/scala/com/johnsnowlabs/nlp/util/LruMap.scala
1
/*
2
 * Copyright 2017-2022 John Snow Labs
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *    http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package com.johnsnowlabs.nlp.util
18

19
import scala.collection.mutable
20

21
@specialized
22
class LruMap[TKey, TValue](maxCacheSize: Int) {
23
  private val cache = mutable.Map.empty[TKey, TValue]
1✔
24
  private val lru = mutable.PriorityQueue.empty[KeyPriority](KeyPriorityOrdering)
1✔
25

26
  private var priorityCounter = 0
1✔
27
  private var size = 0
1✔
28

29
  private def deleteOne(): Unit = {
30
    val oldest = lru.dequeue().key
1✔
31
    cache.remove(oldest)
1✔
32
  }
33

34
  def clear(): Unit = {
35
    cache.clear()
1✔
36
    lru.clear()
1✔
37
    priorityCounter = 0
1✔
38
    size = 0
1✔
39
  }
40

41
  def getSize: Int = {
42
    size
1✔
43
  }
44

45
  def foreach: (((TKey, TValue)) => Any) => Unit = cache.foreach
×
46

47
  def update(key: TKey, value: => TValue): TValue = synchronized {
1✔
48
    val isNewKey = !cache.contains(key)
1✔
49
    if (isNewKey && getSize >= maxCacheSize)
1✔
50
      deleteOne()
1✔
UNCOV
51
    else if (isNewKey)
×
52
      size += 1
1✔
53

54
    val content = value
55
    cache(key) = content
1✔
56
    priorityCounter += 1
1✔
57
    lru.enqueue(KeyPriority(key, priorityCounter))
1✔
58
    content
59
  }
60

61
  def getOrElseUpdate(key: TKey, valueCreator: => TValue): TValue = {
62
    val oldValue = cache.get(key)
1✔
63
    if (oldValue.isDefined) {
1✔
64
      oldValue.get
1✔
65
    } else {
66
      update(key, valueCreator)
1✔
67
    }
68
  }
69

70
  def get(key: TKey): Option[TValue] = {
71
    cache.get(key)
1✔
72
  }
73

74
  case class KeyPriority(key: TKey, priority: Int)
75

76
  object KeyPriorityOrdering extends Ordering[KeyPriority] {
77
    override def compare(x: KeyPriority, y: KeyPriority): Int = y.priority.compareTo(x.priority)
1✔
78
  }
79
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc