Improve segmented journal actor metrics
[controller.git] / atomix-storage / src / test / java / io / atomix / storage / journal / index / SparseJournalIndexTest.java
1 /*
2  * Copyright 2018-2022 Open Networking Foundation and others.  All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package io.atomix.storage.journal.index;
17
18 import org.junit.Test;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNull;
22
23 /**
24  * Sparse journal index test.
25  */
26 public class SparseJournalIndexTest {
27   @Test
28   public void testSparseJournalIndex() throws Exception {
29     JournalIndex index = new SparseJournalIndex(.2);
30     assertNull(index.lookup(1));
31     index.index(1, 2);
32     assertNull(index.lookup(1));
33     index.index(2, 4);
34     index.index(3, 6);
35     index.index(4, 8);
36     index.index(5, 10);
37     assertEquals(new Position(5, 10), index.lookup(5));
38     index.index(6, 12);
39     index.index(7, 14);
40     index.index(8, 16);
41     assertEquals(new Position(5, 10), index.lookup(8));
42     index.index(9, 18);
43     index.index(10, 20);
44     assertEquals(new Position(10, 20), index.lookup(10));
45     index.truncate(8);
46     assertEquals(new Position(5, 10), index.lookup(8));
47     assertEquals(new Position(5, 10), index.lookup(10));
48     index.truncate(4);
49     assertNull(index.lookup(4));
50     assertNull(index.lookup(8));
51
52     index = new SparseJournalIndex(.2);
53     assertNull(index.lookup(100));
54     index.index(101, 2);
55     assertNull(index.lookup(1));
56     index.index(102, 4);
57     index.index(103, 6);
58     index.index(104, 8);
59     index.index(105, 10);
60     assertEquals(new Position(105, 10), index.lookup(105));
61     index.index(106, 12);
62     index.index(107, 14);
63     index.index(108, 16);
64     assertEquals(new Position(105, 10), index.lookup(108));
65     index.index(109, 18);
66     index.index(110, 20);
67     assertEquals(new Position(110, 20), index.lookup(110));
68     index.truncate(108);
69     assertEquals(new Position(105, 10), index.lookup(108));
70     assertEquals(new Position(105, 10), index.lookup(110));
71     index.truncate(104);
72     assertNull(index.lookup(104));
73     assertNull(index.lookup(108));
74   }
75 }