@Test public void testRemoveWithKeyAndValue() throws Exception { final SharedHashMap<CharSequence, CharSequence> map = new SharedHashMapBuilder() .minSegments(2) .create(getPersistenceFile(), CharSequence.class, CharSequence.class); map.put("key1", "one"); map.put("key2", "two"); assertEquals("one", map.get("key1")); assertEquals("two", map.get("key2")); // a false remove final boolean wasRemoved1 = map.remove("key1", "three"); assertFalse(wasRemoved1); assertEquals(null, map.get("key1"), "one"); assertEquals("two", map.get("key2"), "two"); map.put("key1", "one"); final boolean wasRemoved2 = map.remove("key1", "three"); assertFalse(wasRemoved2); // lets add one more item for luck ! map.put("key3", "three"); assertEquals("three", map.get("key3")); // and just for kicks we'll overwrite what we have map.put("key3", "overwritten"); assertEquals("overwritten", map.get("key3")); }
@Test public void testRemoveWithKeyAndRemoveReturnsNull() throws Exception { final SharedHashMap<CharSequence, CharSequence> map = new SharedHashMapBuilder() .minSegments(2) .removeReturnsNull(true) .create(getPersistenceFile(), CharSequence.class, CharSequence.class); assertFalse(map.containsKey("key3")); map.put("key1", "one"); map.put("key2", "two"); assertEquals(2, map.size()); assertTrue(map.containsKey("key1")); assertTrue(map.containsKey("key2")); assertFalse(map.containsKey("key3")); assertEquals("one", map.get("key1")); assertEquals("two", map.get("key2")); final CharSequence result = map.remove("key1"); assertEquals(null, result); assertEquals(1, map.size()); assertFalse(map.containsKey("key1")); assertEquals(null, map.get("key1")); assertEquals("two", map.get("key2")); assertFalse(map.containsKey("key3")); // lets add one more item for luck ! map.put("key3", "three"); assertEquals("three", map.get("key3")); assertTrue(map.containsKey("key3")); assertEquals(2, map.size()); // and just for kicks we'll overwrite what we have map.put("key3", "overwritten"); assertEquals("overwritten", map.get("key3")); assertTrue(map.containsKey("key3")); assertEquals(2, map.size()); }
@Test public void testSize() throws Exception { final SharedHashMap<CharSequence, CharSequence> map = new SharedHashMapBuilder() .minSegments(1024) .removeReturnsNull(true) .create(getPersistenceFile(), CharSequence.class, CharSequence.class); for (int i = 1; i < 1024; i++) { map.put("key" + i, "value"); assertEquals(i, map.size()); } for (int i = 1023; i >= 1; ) { map.remove("key" + i); i--; assertEquals(i, map.size()); } }
@Test public void testPutAndRemove() throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException { String TMP = System.getProperty("java.io.tmpdir"); File file = new File(TMP + "/shm-remove-test"); file.delete(); file.deleteOnExit(); int entries = 100 * 1000; SharedHashMap<CharSequence, CharSequence> map = new SharedHashMapBuilder() .entries(entries) .minSegments(16) .entrySize(32) .putReturnsNull(true) .removeReturnsNull(true) .create(file, CharSequence.class, CharSequence.class); StringBuilder key = new StringBuilder(); StringBuilder value = new StringBuilder(); StringBuilder value2 = new StringBuilder(); for (int j = 1; j <= 3; j++) { for (int i = 0; i < entries; i++) { key.setLength(0); key.append("user:"******"value:").append(i); // System.out.println(key); assertNull(map.getUsing(key, value)); assertNull(map.put(key, value)); assertNotNull(map.getUsing(key, value2)); assertEquals(value.toString(), value2.toString()); assertNull(map.remove(key)); assertNull(map.getUsing(key, value)); } } map.close(); }