Exemplo n.º 1
0
 @Test
 public void testPutReturnValue() {
   cache.put(1, "apple");
   assertNotNull(cache.put(1, "banana"));
   Clock.setTime(3000);
   assertNull(cache.put(1, "mango"));
 }
Exemplo n.º 2
0
 @Test
 public void testSize() throws Exception {
   assertEquals(0, cache.size());
   cache.put(1, "apple");
   assertEquals(1, cache.size());
   Clock.setTime(3000);
   assertEquals(0, cache.size());
 }
Exemplo n.º 3
0
  @Test
  public void testExpiry() throws Exception {

    cache.put(1, "apple");
    assertEquals("apple", cache.get(1));
    assertFalse(cache.isEmpty());
    Clock.setTime(3000);
    assertNull(cache.get(1));
    assertTrue(cache.isEmpty());
  }
Exemplo n.º 4
0
  @Test
  public void testRemove() throws Exception {
    assertNull(cache.remove(new Integer(1)));

    cache.put(new Integer(1), "apple");

    assertEquals("apple", cache.remove(new Integer(1)));

    assertNull(cache.get(new Integer(1)));
    assertEquals(0, cache.size());
  }
Exemplo n.º 5
0
 @SuppressWarnings("unchecked")
 public Object getInstance(Line line, Class clas)
     throws InstantiationException, IllegalAccessException {
   Fixture annotation = (Fixture) clas.getAnnotation(Fixture.class);
   Lifespan lifespan = annotation.lifespan();
   Object instance;
   if (lifespan == Lifespan.FULL_SUITE) {
     instance = fullSuiteCache.getInstance(clas);
   } else if (lifespan == Lifespan.FEATURE) {
     Feature feature = line.getScenario().getFeature();
     instance = featureCache.cache(clas, feature);
   } else if (lifespan == Lifespan.SCENARIO) {
     Scenario scenario = line.getScenario();
     instance = scenarioCache.cache(clas, scenario);
   } else {
     LOG.info("create new instance of '{}'", clas);
     instance = clas.newInstance();
   }
   return instance;
 }
Exemplo n.º 6
0
  public byte[] get(Long key, byte[] data) throws IOException {
    byte[] value = cache.get(key);
    if (value != null) {
      return value;
    }
    value = overwriteBackup.get(key);
    if (value != null) {
      return value;
    }
    value = f.readByHash(key, data);

    return value;
  }
Exemplo n.º 7
0
  public void put(Long key, byte[] data) throws IOException {
    Integer index = f.hashToIndex(key);
    if (index != null) {
      return;
    }
    index = f.getIndex();
    f.incrementIndex();
    Long oldHash = f.indexToHash(index);
    byte[] oldData = new byte[(int) size];
    oldData = f.readByIndex(index, oldData);
    if (oldData != null) {
      overwriteBackup.put(oldHash, oldData);
      overwriteQueue.add(oldHash);
    }
    f.write(index, key, data);

    if (!cache.contains(key)) {
      byte[] dataCopy = new byte[data.length];
      System.arraycopy(data, 0, dataCopy, 0, data.length);
      cache.put(key, dataCopy);
    }
  }
Exemplo n.º 8
0
  private CacheElement put(long address) throws IOException {
    /** get a CacheElement from the stack object pool */
    CacheElement c = map.pop();

    /** read the element from the device */
    c.read(address);

    /** and insert the element into the LinkedHashMap */
    map.put(c);

    /**
     * stack "must" contains at least one entry the placeholder ... so let it throw an exception if
     * this is false
     */
    CacheElement e = map.peek();
    // if an element was discarded from the LRU cache
    // now we can free it ... this will send the element
    // to storage if is marked as dirty
    if (!e.isFree()) e.free();

    return c;
  }
Exemplo n.º 9
0
 /**
  * Reads the object from the storage, returns null if the object isn't there
  *
  * @param name name of the store
  * @return object stored under that name
  */
 public Object readObject(String name) {
   name = fixFileName(name);
   Object o = cache.get(name);
   if (o != null) {
     return o;
   }
   DataInputStream d = null;
   try {
     if (!exists(name)) {
       return null;
     }
     d = new DataInputStream(createInputStream(name));
     o = Util.readObject(d);
     d.close();
     cache.put(name, o);
     return o;
   } catch (Exception err) {
     err.printStackTrace();
     Util.getImplementation().cleanup(d);
     return null;
   }
 }
Exemplo n.º 10
0
  public static void main(String args[]) {
    CacheMap<Integer, String> myCache = new CacheMap<Integer, String>(5);
    myCache.put(1, "a");
    myCache.put(2, "b");
    myCache.put(3, "c");
    myCache.put(4, "d");
    myCache.put(5, "e");
    myCache.put(3, "f");
    myCache.put(6, "h");
    System.out.println(myCache.get(1));
    System.out.println(myCache.get(1));
    System.out.println(myCache.get(1));

    for (Map.Entry<Integer, String> entry : myCache.entrySet()) {
      System.out.println(entry.getKey() + ":" + entry.getValue());
    }
  }
Exemplo n.º 11
0
 /**
  * Writes the given object to storage assuming it is an externalizable type or one of the
  * supported types
  *
  * @param name store name
  * @param o object to store
  * @return true for success, false for failue
  */
 public boolean writeObject(String name, Object o) {
   name = fixFileName(name);
   cache.put(name, o);
   DataOutputStream d = null;
   try {
     d = new DataOutputStream(createOutputStream(name));
     Util.writeObject(o, d);
     d.close();
     return true;
   } catch (Exception err) {
     err.printStackTrace();
     Util.getImplementation().deleteStorageFile(name);
     Util.getImplementation().cleanup(d);
     return false;
   }
 }
Exemplo n.º 12
0
  private CacheElement get(long address) throws IOException {
    CacheElement c = map.get(address);
    access++;

    // if the cache contains the element just return it, we have a cache hit
    // this will update the LRU order: the LinkedHashMap will make it the
    // newest
    //
    // the cache element cannot be null so we can avoid to call
    // containsKey();
    if (c != null) hit++;
    // otherwise put a new element inside the cache
    // possibly flushing and discarding the eldest element
    else c = put(address);

    return c;
  }
Exemplo n.º 13
0
  @Test
  public void testPartialExpiry() throws Exception {
    // Add an apple, it will expire at 2000
    // System.out.println(cache.getTimeToLive());
    cache.put(1, "apple");
    Clock.setTime(1500);
    // System.out.println(cache.getTimeToLive());
    // Add an orange, it will expire at 2500
    cache.put(2, "orange");

    assertEquals("apple", cache.get(1));
    assertEquals("orange", cache.get(2));
    assertEquals(2, cache.size());

    // Set time to 2300 and check that only the apple has disappeared
    Clock.setTime(2300);

    assertNull(cache.get(1));
    assertEquals("orange", cache.get(2));
    assertEquals(1, cache.size());
  }
Exemplo n.º 14
0
 public int getCacheSize() {
   return map.getCacheSize();
 }
Exemplo n.º 15
0
 public String flushOrder() {
   return map.flushOrder();
 }
Exemplo n.º 16
0
 public void flush() throws IOException {
   for (CacheElement c : map.values()) {
     c.flush();
   }
 }
Exemplo n.º 17
0
 public void flush(long address) throws IOException {
   CacheElement c = map.get(address);
   if (c != null) c.flush();
 }
Exemplo n.º 18
0
 public byte[] removeOverwriteBackup(Long key) {
   cache.remove(key);
   return overwriteBackup.remove(key);
 }
Exemplo n.º 19
0
 /**
  * Storage is cached for faster access, however this might cause a problem with refreshing objects
  * since they are not cloned. Clearing the cache allows to actually reload from the storage file.
  */
 public void clearCache() {
   cache.clearAllCache();
 }
Exemplo n.º 20
0
 @Override
 protected boolean isSelfExists(String key) {
   return memoryMap.containsKey(key);
 }
Exemplo n.º 21
0
 /**
  * Indicates the caching size, storage can be pretty slow
  *
  * @param size size in elements (not kb!)
  */
 public void setHardCacheSize(int size) {
   cache.setCacheSize(size);
 }
Exemplo n.º 22
0
 @Override
 protected String selfGet(String key) {
   return memoryMap.get(key);
 }
Exemplo n.º 23
0
 @Before
 public void setUp() throws Exception {
   Clock.setTime(1000);
   cache = new CacheMapImpl<>();
   cache.setTimeToLive(TIME_TO_LIVE);
 }
Exemplo n.º 24
0
 /** Deletes all the files in the application storage */
 public void clearStorage() {
   Util.getImplementation().clearStorage();
   cache.clearAllCache();
 }
Exemplo n.º 25
0
 /**
  * Deletes the given file name from the storage
  *
  * @param name the name of the storage file
  */
 public void deleteStorageFile(String name) {
   name = fixFileName(name);
   Util.getImplementation().deleteStorageFile(name);
   cache.delete(name);
 }
Exemplo n.º 26
0
 public int usedEntries() {
   return map.usedEntries();
 }
Exemplo n.º 27
0
 public int freeEntries() {
   return map.freeEntries();
 }
Exemplo n.º 28
0
 @Override
 protected void selfPut(String key, String value, long expiredTime) {
   memoryMap.put(key, value, expiredTime);
 }
Exemplo n.º 29
0
  @Test
  public void testContainsKeyAndContainsValue() {
    assertFalse(cache.containsKey(1));
    assertFalse(cache.containsValue("apple"));
    assertFalse(cache.containsKey(2));
    assertFalse(cache.containsValue("orange"));

    cache.put(1, "apple");
    assertTrue(cache.containsKey(1));
    assertTrue(cache.containsValue("apple"));
    assertFalse(cache.containsKey(2));
    assertFalse(cache.containsValue("orange"));

    Clock.setTime(3000);
    assertFalse(cache.containsKey(1));
    assertFalse(cache.containsValue("apple"));
    assertFalse(cache.containsKey(2));
    assertFalse(cache.containsValue("orange"));
  }
Exemplo n.º 30
0
 @Override
 protected Long selfRemove(String key) {
   return memoryMap.remove(key) != null ? 1L : 0L;
 }