private void insert(SortedIndex<TestEntry> rbt, int count) { for (int i = 0; i < count; i++) { rbt.put(new TestEntry(i)); } }
private ArrayList<Long> populate(SortedIndex<TestEntry> index, int itemCount, int maxKey) { Random rnd = new Random(0); val keys = new ArrayList<Long>(); for (int i = 0; i < itemCount; i++) { // Generate a unique key. long key; do { key = rnd.nextInt(maxKey); } while (index.get(key) != null); keys.add(key); index.put(new TestEntry(key)); } return keys; }
@GuardedBy("lock") private ReadIndexEntry addToIndex(ReadIndexEntry entry) { // Insert the new entry and figure out if an old entry was overwritten. ReadIndexEntry oldEntry = this.indexEntries.put(entry); if (entry.isDataEntry()) { if (entry instanceof MergedIndexEntry) { // This entry has already existed in the cache for a while; do not change its generation. this.summary.add(entry.getLength(), entry.getGeneration()); } else { // Update the Stats with the entry's length, and set the entry's generation as well. int generation = this.summary.add(entry.getLength()); entry.setGeneration(generation); } } if (oldEntry != null && oldEntry.isDataEntry()) { // Need to eject the old entry's data from the Cache Stats. this.summary.remove(oldEntry.getLength(), oldEntry.getGeneration()); } return oldEntry; }
/** * Tests the forEach() method. */ @Test public void testForEach() { // Create an index and populate it. val index = createIndex(); val validKeys = populate(index); // Extract the keys using forEach - they should be ordered naturally. val actualKeys = new ArrayList<Long>(); index.forEach(e -> actualKeys.add(e.key())); // Order the inserted keys using the same comparator we used for the index. validKeys.sort(KEY_COMPARATOR); // Verify that modifying the index while looping through it does throw an exception. AssertExtensions.assertThrows( "forEach did not throw when a new item was added during enumeration.", () -> index.forEach(e -> index.put(new TestEntry(index.size()))), ex -> ex instanceof ConcurrentModificationException); AssertExtensions.assertThrows( "forEach did not throw when an item was removed during enumeration.", () -> index.forEach(e -> index.remove(e.key())), ex -> ex instanceof ConcurrentModificationException); AssertExtensions.assertThrows( "forEach did not throw when the index was cleared during enumeration.", () -> index.forEach(e -> index.clear()), ex -> ex instanceof ConcurrentModificationException); }
/** * Tests various operations on already sorted input. */ @Test public void testSortedInput() { val index = createIndex(); for (int key = 0; key < ITEM_COUNT; key++) { index.put(new TestEntry(key)); } //Get + GetCeiling. for (int key = 0; key < ITEM_COUNT; key++) { Assert.assertEquals("Unexpected value from get() for key " + key, key, (long) index.get(key).key()); Assert.assertEquals("Unexpected value from getCeiling() for key " + key, key, (long) index.getCeiling(key).key()); } // Remove + get. for (long key = 0; key < ITEM_COUNT; key++) { long removedKey = index.remove(key).key(); Assert.assertEquals("Unexpected value from remove(). ", key, removedKey); Assert.assertNull("Unexpected value from get() for removed key " + key, index.get(key)); if (key == ITEM_COUNT - 1) { Assert.assertNull("Unexpected value from getCeiling() for removed key " + key, index.getCeiling(key)); } else { Assert.assertEquals("Unexpected value from getCeiling() for removed key " + key, key + 1, (long) index.getCeiling(key).key()); } } }