29
29
import static org.junit.Assert.assertEquals;
30
30
import static org.junit.Assert.assertTrue;
31
import static org.junit.Assert.fail;
33
import java.util.ArrayList;
32
34
import java.util.HashMap;
33
35
import java.util.HashSet;
36
import java.util.Iterator;
37
import java.util.List;
34
38
import java.util.Map;
35
39
import java.util.Properties;
40
import java.util.Random;
36
41
import java.util.Set;
38
43
import org.junit.Test;
299
306
assertEquals(50000, countKeys(false));
300
307
assertEquals(0, countKeys(true));
301
_persistit.getJournalManager().pruneObsoleteTransactions(Long.MAX_VALUE, true);
308
_persistit.getJournalManager().pruneObsoleteTransactions(true);
302
309
assertTrue(countKeys(false) < 50000);
303
310
CleanupManager cm = _persistit.getCleanupManager();
304
311
assertTrue(cm.getAcceptedCount() > 0);
305
while (cm.getEnqueuedCount() > 0) {
312
long start = System.currentTimeMillis();
313
while (cm.getPerformedCount() < cm.getAcceptedCount()) {
314
if (System.currentTimeMillis() > start + 30000) {
315
fail("Pruning not done in 30 seconds");
308
319
assertEquals(0, countKeys(false));
399
public void testCleanupPageList() throws Exception {
401
* Remove from the left end
404
final List<PageNode> source =testCleanupPageListSource(10);
405
for (int i = 0; i < 4; i++) {
406
source.get(i).invalidate();
408
testCleanupPageListHelper(source);
412
* Remove from the right end
415
final List<PageNode> source =testCleanupPageListSource(10);
416
for (int i = 10; --i >= 7;) {
417
source.get(i).invalidate();
419
testCleanupPageListHelper(source);
423
* Remove from the middle
426
final List<PageNode> source =testCleanupPageListSource(10);
427
for (int i = 2; i < 8; i++) {
428
source.get(i).invalidate();
430
testCleanupPageListHelper(source);
434
* Randomly invalidated PageNodes
437
final int SIZE = 5000;
438
final Random random = new Random(1);
439
final List<PageNode> source = testCleanupPageListSource(SIZE);
441
for (int index = 0; index < SIZE; index++) {
443
source.get(index).invalidate();
445
index += random.nextInt(50);
446
next = random.nextInt(50) + index;
449
testCleanupPageListHelper(source);
453
private List<PageNode> testCleanupPageListSource(final int size) {
454
final List<PageNode> source = new ArrayList<PageNode>(size);
455
for (int index = 0; index < 1000000; index++) {
456
source.add(new PageNode(0, index, index * 10, index));
462
private void testCleanupPageListHelper(final List<PageNode> source) throws Exception {
463
final List<PageNode> cleaned = new ArrayList<PageNode>(source);
464
for (Iterator<PageNode> iterator = cleaned.iterator(); iterator.hasNext();) {
465
if (iterator.next().isInvalid()) {
469
JournalManager jman = new JournalManager(_persistit);
470
jman.unitTestInjectPageList(source);
471
int removed = jman.cleanupPageList();
472
assertTrue(jman.unitTestPageListEquals(cleaned));
473
assertEquals("Removed count is wrong", source.size() - cleaned.size(), removed);
474
assertTrue("Invalidated no page nodes", source.size() > cleaned.size());
388
478
public void copyBackPagesLeavesOneJournal() throws Exception {
389
479
final int BATCH_SIZE = 1000;
390
480
JournalManager jman = _persistit.getJournalManager();
393
for(long curSize = 0; curSize < JournalManager.ROLLOVER_THRESHOLD; ) {
483
for (long curSize = 0; curSize < JournalManager.ROLLOVER_THRESHOLD;) {
394
484
Exchange ex = _persistit.getExchange(UnitTestProperties.VOLUME_NAME, "JournalManagerTest", true);
395
485
Transaction txn = _persistit.getTransaction();
396
486
Accumulator accum = ex.getTree().getAccumulator(Accumulator.Type.SUM, 0);
398
for(int j = 0; j < BATCH_SIZE; ++j) {
488
for (int j = 0; j < BATCH_SIZE; ++j) {
399
489
ex.clear().append(total + j);
400
490
ex.getValue().put(j);