public int compareTo(CacheEntry rhs) {
      // Order first by attribute Id
      if (value.getAttrId() < rhs.getValue().getAttrId()) return -1;
      if (value.getAttrId() > rhs.getValue().getAttrId()) return 1;

      // then by class name
      int strcmp = value.getClass().getName().compareTo(rhs.getValue().getClass().getName());
      if (strcmp != 0) return strcmp;

      // then by value
      return value.compareAttribute(rhs.getValue());
    }
Esempio n. 2
0
 protected static void assertMapEntries(
     Map<Object, Object> expected, InMemoryBasedCacheSupport<Object, Object> cache) {
   assertEquals("Length not currect", expected.size(), cache.getEntries().size());
   for (final Object key : expected.keySet()) {
     final CacheEntry<?, ?> entry = cache.getEntries().get(key);
     assertNotNull("Entry for key '" + key + "' is null!", entry);
     assertEquals("key '" + key + "'", expected.get(key), entry.getValue().getValue());
   }
 }
  // Javadoc inherited.
  public Object perform(MethodActionEvent event) throws Throwable {

    CacheEntry entry = (CacheEntry) event.getArgument(CacheEntry.class);
    if (hasEntry) {
      Assert.assertSame(expectedKey, entry.getKey());
      Assert.assertSame(expectedValue, entry.getValue());
    } else {
      Assert.assertNull(entry);
    }

    return null;
  }
  /*
   * get an entry if not expired else return null
   * Average case complexity = O(1)
   * 		- balanced scenario - indexed CHM bucket contained 1 entry or none
   * Worst case complexity = O(n)
   * 		- all keys had same hashcode but were not equal - same CHM bucket in a linked list
   */
  public V get(K key) {
    try {
      // await on CDL if collector is running - progress on CDL when 0 - collector counts down
      this.getIndexedStoreListElement(key).getLockMechanism().getLatch().await();

      // collector not running - register on phaser
      this.getIndexedStoreListElement(key).getLockMechanism().getPhaser().register();

      if (this.getIndexedStoreListElement(key).getMap().containsKey(key)) {
        CacheEntry<K, V> existingEntry = this.getIndexedStoreListElement(key).getMap().get(key);

        /*
         * synch with puts also since gets can optionally remove entries from map if expired
         * we do not want gets and puts to concurrently run otherwise get can possibly remove a
         * fresh non expired entry
         */
        synchronized (existingEntry) {
          if (existingEntry.isExpired()) {
            this.getIndexedStoreListElement(key).getMap().remove(key);
            return null;
          }
          return existingEntry.getValue();
        }

      } else {
        return null;
      }

    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
      return null;
    } finally {
      /*
       * reduce the number of parties which collector has to wait on
       * once all parties except collector are dereg collector progresses
       */

      this.getIndexedStoreListElement(key).getLockMechanism().getPhaser().arriveAndDeregister();
    }
  }