|
| 1 | +/* |
| 2 | + * Copyright DataStax, Inc. |
| 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 | + |
| 17 | +package org.apache.cassandra.index.sai.utils; |
| 18 | + |
| 19 | +import java.util.function.Consumer; |
| 20 | + |
| 21 | +import org.junit.BeforeClass; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import org.apache.cassandra.Util; |
| 25 | +import org.apache.cassandra.config.DatabaseDescriptor; |
| 26 | +import org.apache.cassandra.db.Clustering; |
| 27 | +import org.apache.cassandra.db.DecoratedKey; |
| 28 | +import org.apache.cassandra.db.DeletionTime; |
| 29 | +import org.apache.cassandra.db.LivenessInfo; |
| 30 | +import org.apache.cassandra.db.RegularAndStaticColumns; |
| 31 | +import org.apache.cassandra.db.marshal.Int32Type; |
| 32 | +import org.apache.cassandra.db.rows.BTreeRow; |
| 33 | +import org.apache.cassandra.db.rows.BufferCell; |
| 34 | +import org.apache.cassandra.db.rows.EncodingStats; |
| 35 | +import org.apache.cassandra.db.rows.Row; |
| 36 | +import org.apache.cassandra.db.rows.RowIterator; |
| 37 | +import org.apache.cassandra.db.rows.UnfilteredRowIterator; |
| 38 | +import org.apache.cassandra.dht.Murmur3Partitioner; |
| 39 | +import org.apache.cassandra.schema.ColumnMetadata; |
| 40 | +import org.apache.cassandra.schema.TableMetadata; |
| 41 | + |
| 42 | +import static org.junit.Assert.assertEquals; |
| 43 | +import static org.junit.Assert.assertNotEquals; |
| 44 | +import static org.junit.Assert.assertNotNull; |
| 45 | +import static org.junit.Assert.assertNull; |
| 46 | +import static org.junit.Assert.assertSame; |
| 47 | +import static org.mockito.Mockito.mock; |
| 48 | +import static org.mockito.Mockito.when; |
| 49 | + |
| 50 | +public class PartitionInfoTest |
| 51 | +{ |
| 52 | + private static DecoratedKey partitionKey; |
| 53 | + private static Row staticRow; |
| 54 | + private static RegularAndStaticColumns columns; |
| 55 | + private static DeletionTime partitionDeletion; |
| 56 | + private static EncodingStats encodingStats; |
| 57 | + |
| 58 | + @BeforeClass |
| 59 | + public static void setup() |
| 60 | + { |
| 61 | + DatabaseDescriptor.setPartitionerUnsafe(new Murmur3Partitioner()); |
| 62 | + TableMetadata tableMetadata = TableMetadata.builder("test", "test") |
| 63 | + .partitioner(Murmur3Partitioner.instance) |
| 64 | + .addPartitionKeyColumn("pk", Int32Type.instance) |
| 65 | + .addRegularColumn("regular_col", Int32Type.instance) |
| 66 | + .addStaticColumn("static_col", Int32Type.instance) |
| 67 | + .build(); |
| 68 | + |
| 69 | + partitionKey = Util.dk("test_key"); |
| 70 | + staticRow = BTreeRow.emptyRow(Clustering.STATIC_CLUSTERING); |
| 71 | + columns = tableMetadata.regularAndStaticColumns(); |
| 72 | + partitionDeletion = new DeletionTime(1000L, 10); |
| 73 | + encodingStats = new EncodingStats(500L, LivenessInfo.NO_EXPIRATION_TIME, 0); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testCreateFromUnfilteredIterator() |
| 78 | + { |
| 79 | + UnfilteredRowIterator unfilteredIterator = unfilteredIterator(); |
| 80 | + PartitionInfo partitionInfo = PartitionInfo.create(unfilteredIterator); |
| 81 | + |
| 82 | + assertNotNull(partitionInfo); |
| 83 | + assertSame(partitionKey, partitionInfo.key); |
| 84 | + assertSame(staticRow, partitionInfo.staticRow); |
| 85 | + assertSame(columns, partitionInfo.columns); |
| 86 | + assertSame(partitionDeletion, partitionInfo.partitionDeletion); |
| 87 | + assertSame(encodingStats, partitionInfo.encodingStats); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testCreateFromRowIterator() |
| 92 | + { |
| 93 | + RowIterator rowIterator = rowIterator(); |
| 94 | + PartitionInfo partitionInfo = PartitionInfo.create(rowIterator); |
| 95 | + |
| 96 | + assertNotNull(partitionInfo); |
| 97 | + assertSame(partitionKey, partitionInfo.key); |
| 98 | + assertSame(staticRow, partitionInfo.staticRow); |
| 99 | + assertSame(columns, partitionInfo.columns); |
| 100 | + assertNull(partitionInfo.partitionDeletion); |
| 101 | + assertNull(partitionInfo.encodingStats); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void testEqualsAndHashCode() |
| 106 | + { |
| 107 | + PartitionInfo info1 = PartitionInfo.create(unfilteredIterator()); |
| 108 | + PartitionInfo info2 = PartitionInfo.create(unfilteredIterator()); |
| 109 | + |
| 110 | + assertEquals(info1, info2); |
| 111 | + assertEquals(info1.hashCode(), info2.hashCode()); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testEqualsWithDifferentKeys() |
| 116 | + { |
| 117 | + testEqualsWithDifferentAttribute(iterator -> { |
| 118 | + DecoratedKey differentKey = Util.dk("different_key"); |
| 119 | + when(iterator.partitionKey()).thenReturn(differentKey); |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testEqualsWithDifferentStaticRow() |
| 125 | + { |
| 126 | + testEqualsWithDifferentAttribute(iterator -> { |
| 127 | + BufferCell cell = BufferCell.live(columns.statics.getSimple(0), 0, Int32Type.instance.decompose(0)); |
| 128 | + Row differentStaticRow = BTreeRow.singleCellRow(Clustering.STATIC_CLUSTERING, cell); |
| 129 | + when(iterator.staticRow()).thenReturn(differentStaticRow); |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void testEqualsWithDifferentColumns() |
| 135 | + { |
| 136 | + testEqualsWithDifferentAttribute(iterator -> { |
| 137 | + ColumnMetadata differentColumn = ColumnMetadata.regularColumn("test", "test", "different_col", Int32Type.instance); |
| 138 | + RegularAndStaticColumns differentColumns = RegularAndStaticColumns.of(differentColumn); |
| 139 | + when(iterator.columns()).thenReturn(differentColumns); |
| 140 | + }); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void testEqualsWithDifferentDeletionTime() |
| 145 | + { |
| 146 | + testEqualsWithDifferentAttribute(iterator -> { |
| 147 | + DeletionTime differentDeletion = new DeletionTime(1001L, 11); |
| 148 | + when(iterator.partitionLevelDeletion()).thenReturn(differentDeletion); |
| 149 | + }); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void testEqualsWithDifferentEncodingStats() |
| 154 | + { |
| 155 | + testEqualsWithDifferentAttribute(iterator -> { |
| 156 | + EncodingStats differentStats = new EncodingStats(501L, LivenessInfo.NO_EXPIRATION_TIME, 0); |
| 157 | + when(iterator.stats()).thenReturn(differentStats); |
| 158 | + }); |
| 159 | + } |
| 160 | + |
| 161 | + private static void testEqualsWithDifferentAttribute(Consumer<UnfilteredRowIterator> attributeChanger) |
| 162 | + { |
| 163 | + UnfilteredRowIterator iterator1 = unfilteredIterator(); |
| 164 | + UnfilteredRowIterator iterator2 = unfilteredIterator(); |
| 165 | + |
| 166 | + attributeChanger.accept(iterator2); |
| 167 | + |
| 168 | + assertNotEquals(PartitionInfo.create(iterator1), |
| 169 | + PartitionInfo.create(iterator2)); |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + public void testEqualsWithNullValues() |
| 174 | + { |
| 175 | + PartitionInfo info1 = PartitionInfo.create(rowIterator()); |
| 176 | + PartitionInfo info2 = PartitionInfo.create(rowIterator()); |
| 177 | + |
| 178 | + assertEquals(info1, info2); |
| 179 | + assertEquals(info1.hashCode(), info2.hashCode()); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + public void testEqualsSameInstance() |
| 184 | + { |
| 185 | + UnfilteredRowIterator iterator = unfilteredIterator(); |
| 186 | + PartitionInfo partitionInfo = PartitionInfo.create(iterator); |
| 187 | + assertEquals(partitionInfo, partitionInfo); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + public void testEqualsWithNull() |
| 192 | + { |
| 193 | + PartitionInfo partitionInfo = PartitionInfo.create(rowIterator()); |
| 194 | + assertNotEquals(null, partitionInfo); |
| 195 | + } |
| 196 | + |
| 197 | + @Test |
| 198 | + public void testEqualsWithDifferentClass() |
| 199 | + { |
| 200 | + PartitionInfo partitionInfo = PartitionInfo.create(rowIterator()); |
| 201 | + assertNotEquals("not a PartitionInfo", partitionInfo); |
| 202 | + } |
| 203 | + |
| 204 | + private static UnfilteredRowIterator unfilteredIterator() |
| 205 | + { |
| 206 | + UnfilteredRowIterator iterator = mock(UnfilteredRowIterator.class); |
| 207 | + when(iterator.partitionKey()).thenReturn(partitionKey); |
| 208 | + when(iterator.staticRow()).thenReturn(staticRow); |
| 209 | + when(iterator.columns()).thenReturn(columns); |
| 210 | + when(iterator.partitionLevelDeletion()).thenReturn(partitionDeletion); |
| 211 | + when(iterator.stats()).thenReturn(encodingStats); |
| 212 | + return iterator; |
| 213 | + } |
| 214 | + |
| 215 | + private static RowIterator rowIterator() |
| 216 | + { |
| 217 | + RowIterator iterator = mock(RowIterator.class); |
| 218 | + when(iterator.partitionKey()).thenReturn(partitionKey); |
| 219 | + when(iterator.staticRow()).thenReturn(staticRow); |
| 220 | + when(iterator.columns()).thenReturn(columns); |
| 221 | + return iterator; |
| 222 | + } |
| 223 | +} |
0 commit comments