Ejemplo n.º 1
0
 /** Verify the cache can put and delete a KV pair successfully. */
 @Test
 public void singlePutAndDel() {
   KVCache cache = new KVCache(1, 4);
   cache.put("hello", "world");
   assertEquals("world", cache.get("hello"));
   cache.del("hello");
   assertEquals(null, cache.get("hello"));
   System.out.println("Test2 Success");
 }
Ejemplo n.º 2
0
 /** Verify the cache can put and replace a KV pair successfully. */
 @Test
 public void singlePutAndReplace() {
   KVCache cache = new KVCache(1, 4);
   cache.put("hello", "world");
   assertEquals("world", cache.get("hello"));
   cache.put("hello", "friend");
   assertEquals("friend", cache.get("hello"));
   System.out.println("Test1 Success");
 }
Ejemplo n.º 3
0
  /** Verify the xml creation works. */
  @Test
  public void xmlTest1() {
    KVCache cache = new KVCache(1, 4);
    cache.put("1", "one");
    cache.put("2", "two");
    cache.put("3", "three");
    cache.put("4", "four");
    cache.get("1");
    cache.get("2");
    cache.get("4");
    cache.put("5", "five");

    System.out.println(cache.toString());
    System.out.println("Test5 Success");
  }
Ejemplo n.º 4
0
  /** Verify the cache can change reference bit correctly. */
  @Test
  public void referenceTest() {
    KVCache cache = new KVCache(1, 4);

    // Get
    cache.put("hello", "world");
    assertEquals(0, cache.getReference("hello"));
    assertEquals("world", cache.get("hello"));
    assertEquals(1, cache.getReference("hello"));

    // Put and replace
    assertEquals(-1, cache.getReference("hi"));
    cache.put("hi", "friend");
    assertEquals(0, cache.getReference("hi"));
    cache.put("hi", "buddy");
    assertEquals("buddy", cache.get("hi"));
    assertEquals(1, cache.getReference("hi"));
    System.out.println("Test3 Success");
  }
Ejemplo n.º 5
0
  /** Verify the cache implements second chance correctly. */
  @Test
  public void secondChance() {
    KVCache cache = new KVCache(1, 4);
    cache.put("1", "one");
    cache.put("2", "two");
    cache.put("3", "three");
    cache.put("4", "four");
    cache.get("1");
    cache.get("2");
    cache.get("4");
    cache.put("5", "five");

    assertEquals(null, cache.get("3"));

    assertEquals(0, cache.getReference("1"));
    assertEquals(0, cache.getReference("2"));
    assertEquals(0, cache.getReference("5"));
    assertEquals(1, cache.getReference("4"));
    System.out.println("Test4 Success");
  }
Ejemplo n.º 6
0
  /**
   * Performs get request. Checks cache first. Updates cache if not in cache but located in store.
   *
   * @param key String key
   * @return String value associated with key
   * @throws KVException with ERROR_NO_SUCH_KEY if key does not exist in store
   */
  @Override
  public String get(String key) throws KVException {
    if (key.length() > MAX_KEY_SIZE) {
      KVMessage msg = new KVMessage(KVConstants.RESP, ERROR_NO_SUCH_KEY);
      throw new KVException(msg);
    }

    Lock lock = dataCache.getLock(key);
    String ret = null;
    try {
      lock.lock();
      ret = dataCache.get(key);
      if (ret == null) {
        ret = dataStore.get(key);
        if (ret != null) dataCache.put(key, ret);
      }
    } finally {
      lock.unlock();
    }
    return ret;
  }
Ejemplo n.º 7
0
  @Test
  public void testToXML() throws Exception {
    KVCache cache = new KVCache(3, 2);

    // test empty
    assertEquals(
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?><KVCache>\r\n"
            + "<Set id=\"0\">\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "</Set>\r\n"
            + "<Set id=\"1\">\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "</Set>\r\n"
            + "<Set id=\"2\">\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "</Set>\r\n"
            + "</KVCache>\r\n",
        cache.toXML());

    // test single element
    cache.put("duck", "bawk");
    assertEquals(
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?><KVCache>\r\n"
            + "<Set id=\"0\">\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"true\">\r\n"
            + "<Key>duck</Key>\r\n"
            + "<Value>bawk</Value>\r\n"
            + "</CacheEntry>\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "</Set>\r\n"
            + "<Set id=\"1\">\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "</Set>\r\n"
            + "<Set id=\"2\">\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "</Set>\r\n"
            + "</KVCache>\r\n",
        cache.toXML());

    // test element replacement
    cache.put("duck", "quack");
    assertEquals(
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?><KVCache>\r\n"
            + "<Set id=\"0\">\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"true\">\r\n"
            + "<Key>duck</Key>\r\n"
            + "<Value>quack</Value>\r\n"
            + "</CacheEntry>\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "</Set>\r\n"
            + "<Set id=\"1\">\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "</Set>\r\n"
            + "<Set id=\"2\">\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "</Set>\r\n"
            + "</KVCache>\r\n",
        cache.toXML());

    // test isReferenced
    assertEquals("quack", cache.get("duck"));
    assertEquals(
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?><KVCache>\r\n"
            + "<Set id=\"0\">\r\n"
            + "<CacheEntry isReferenced=\"true\" isValid=\"true\">\r\n"
            + "<Key>duck</Key>\r\n"
            + "<Value>quack</Value>\r\n"
            + "</CacheEntry>\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "</Set>\r\n"
            + "<Set id=\"1\">\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "</Set>\r\n"
            + "<Set id=\"2\">\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "</Set>\r\n"
            + "</KVCache>\r\n",
        cache.toXML());

    // test another set and multiples in a set
    cache.put("cow", "moo");
    cache.put("crow", "cawcawcaw");
    assertEquals(
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?><KVCache>\r\n"
            + "<Set id=\"0\">\r\n"
            + "<CacheEntry isReferenced=\"true\" isValid=\"true\">\r\n"
            + "<Key>duck</Key>\r\n"
            + "<Value>quack</Value>\r\n"
            + "</CacheEntry>\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "</Set>\r\n"
            + "<Set id=\"1\">\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "</Set>\r\n"
            + "<Set id=\"2\">\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"true\">\r\n"
            + "<Key>cow</Key>\r\n"
            + "<Value>moo</Value>\r\n"
            + "</CacheEntry>\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"true\">\r\n"
            + "<Key>crow</Key>\r\n"
            + "<Value>cawcawcaw</Value>\r\n"
            + "</CacheEntry>\r\n"
            + "</Set>\r\n"
            + "</KVCache>\r\n",
        cache.toXML());

    // test deletion
    cache.del("duck");
    assertEquals(
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?><KVCache>\r\n"
            + "<Set id=\"0\">\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "</Set>\r\n"
            + "<Set id=\"1\">\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"false\">\r\n"
            + "<Key/>\r\n"
            + "<Value/>\r\n"
            + "</CacheEntry>\r\n"
            + "</Set>\r\n"
            + "<Set id=\"2\">\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"true\">\r\n"
            + "<Key>cow</Key>\r\n"
            + "<Value>moo</Value>\r\n"
            + "</CacheEntry>\r\n"
            + "<CacheEntry isReferenced=\"false\" isValid=\"true\">\r\n"
            + "<Key>crow</Key>\r\n"
            + "<Value>cawcawcaw</Value>\r\n"
            + "</CacheEntry>\r\n"
            + "</Set>\r\n"
            + "</KVCache>\r\n",
        cache.toXML());
  }