<commit_before>/*
 *  @author <NAME>
 *
 *  Copyright 2011 University of Zurich
 *      
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *         http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package signalcollect.implementations.storage

import scala.collection.mutable.LinkedHashMap
import signalcollect.interfaces._
import scala.collection.mutable.LinkedHashMap
import scala.concurrent.Lock

/**
 *  Caches Vertices in the store according to a least recently used (LRU) policy
 */
class LRUVertexCache(persistentStorageFactory: Storage => VertexStore,
  storage: Storage,
  var capacity: Int = Int.MaxValue,
  inMemoryRatio: Float = 0.5f) extends VertexStore {
  
  protected val CACHING_THRESHOLD = inMemoryRatio
  protected lazy val persistentStore: VertexStore = persistentStorageFactory(storage)
  protected val cache = new LRUMap[Any, Vertex[_, _]](persistentStore, capacity)

  def get(id: Any): Vertex[_, _] = {
    val result = cache.get(id).get
    if (result != None) {
      result
    } else {
      persistentStore.get(id)
    }
  }

  def put(vertex: Vertex[_, _]) = {
    if (cache.contains(vertex.id) || persistentStore.get(vertex.id) != null) {
      false // Vertex already stored
    } else if (cache.size < capacity) {
      vertex.setMessageBus(storage.getMessageBus)
      cache.put(vertex.id, vertex)
      storage.toCollect.add(vertex.id)
      storage.toSignal.add(vertex.id)
      var usedMemory = Runtime.getRuntime().totalMemory.asInstanceOf[Float] - Runtime.getRuntime().freeMemory
      if ((usedMemory / Runtime.getRuntime().maxMemory) > CACHING_THRESHOLD) {
        capacity = cache.size
      }
      true
    } else {
      persistentStore.put(vertex)
    }
  }

  def remove(id: Any) {
    if (cache.contains(id)) {
      cache.remove(id)
      storage.toCollect.remove(id)
      storage.toSignal.remove(id)<commit_msg>Little fix to prevent None.get errors<commit_after>/*
 *  @author <NAME>
 *
 *  Copyright 2011 University of Zurich
 *      
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *         http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package signalcollect.implementations.storage

import scala.collection.mutable.LinkedHashMap
import signalcollect.interfaces._
import scala.collection.mutable.LinkedHashMap
import scala.concurrent.Lock

/**
 *  Caches Vertices in the store according to a least recently used (LRU) policy
 */
class LRUVertexCache(persistentStorageFactory: Storage => VertexStore,
  storage: Storage,
  var capacity: Int = Int.MaxValue,
  inMemoryRatio: Float = 0.5f) extends VertexStore {
  
  protected val CACHING_THRESHOLD = inMemoryRatio
  protected lazy val persistentStore: VertexStore = persistentStorageFactory(storage)
  protected val cache = new LRUMap[Any, Vertex[_, _]](persistentStore, capacity)

  def get(id: Any): Vertex[_, _] = {
    val result = cache.get(id)
    if (result != None) {
      result.get
    } else {
      persistentStore.get(id)
    }
  }

  def put(vertex: Vertex[_, _]) = {
    if (cache.contains(vertex.id) || persistentStore.get(vertex.id) != null) {
      false // Vertex already stored
    } else if (cache.size < capacity) {
      vertex.setMessageBus(storage.getMessageBus)
      cache.put(vertex.id, vertex)
      storage.toCollect.add(vertex.id)
      storage.toSignal.add(vertex.id)
      var usedMemory = Runtime.getRuntime().totalMemory.asInstanceOf[Float] - Runtime.getRuntime().freeMemory
      if ((usedMemory / Runtime.getRuntime().maxMemory) > CACHING_THRESHOLD) {
        capacity = cache.size
      }
      true
    } else {
      persistentStore.put(vertex)
    }
  }

  def remove(id: Any) {
    if (cache.contains(id)) {
      cache.remove(id)
      storage.toCollect.remove(id)
      storage.toSignal.remove(id)
{'fraction_non_alphanumeric': 0.09884713919726729, 'fraction_numerical': 0.004269854824935952, 'mean_word_length': 3.366262814538677, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6745711', 'n_tokens_mistral': 1384, 'n_tokens_neox': 1379, 'n_words': 396}