maedhroz commented on code in PR #2267: URL: https://github.com/apache/cassandra/pull/2267#discussion_r1185486855
########## test/unit/org/apache/cassandra/io/tries/WalkerTest.java: ########## @@ -0,0 +1,231 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.io.tries; + +import java.io.IOException; +import java.io.PrintStream; + +import org.apache.commons.lang3.StringUtils; +import org.junit.Test; + +import org.apache.cassandra.io.util.DataOutputBuffer; +import org.apache.cassandra.io.util.Rebufferer; +import org.apache.cassandra.io.util.TailOverridingRebufferer; +import org.apache.cassandra.utils.bytecomparable.ByteComparable; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNull; + +@SuppressWarnings("unchecked") +public class WalkerTest extends AbstractTrieTestBase +{ + @Test + public void testWithoutBounds() throws IOException + { + DataOutputBuffer buf = new DataOutputBufferPaged(); + IncrementalTrieWriter<Integer> builder = makeTrie(buf); + long rootPos = builder.complete(); + + Rebufferer source = new ByteBufRebufferer(buf.asNewBuffer()); + + InternalIterator it = new InternalIterator(source, rootPos); + + DataOutputBuffer dumpBuf = new DataOutputBuffer(); + it.dumpTrie(new PrintStream(dumpBuf), (buf1, payloadPos, payloadFlags) -> String.format("%d/%d", payloadPos, payloadFlags)); + logger.info("Trie dump: \n{}", new String(dumpBuf.getData())); + logger.info("Trie toString: {}", it); + + it.goMax(rootPos); + assertEquals(7, it.payloadFlags()); + assertEquals(TrieNode.Types.PAYLOAD_ONLY.ordinal, it.nodeTypeOrdinal()); + assertEquals(1, it.nodeSize()); + assertFalse(it.hasChildren()); + + it.goMin(rootPos); + assertEquals(1, it.payloadFlags()); + assertEquals(TrieNode.Types.PAYLOAD_ONLY.ordinal, it.nodeTypeOrdinal()); + assertEquals(1, it.nodeSize()); + assertFalse(it.hasChildren()); + + assertEquals(-1, it.follow(source("151"))); + assertEquals(2, it.payloadFlags()); + + assertEquals('3', it.follow(source("135"))); + + assertEquals('3', it.followWithGreater(source("135"))); + it.goMin(it.greaterBranch); + assertEquals(2, it.payloadFlags()); + + assertEquals('3', it.followWithLesser(source("135"))); + it.goMax(it.lesserBranch); + assertEquals(1, it.payloadFlags()); + + assertEquals(3, (Object) it.prefix(source("155"), (walker, payloadPosition, payloadFlags) -> payloadFlags)); + assertNull(it.prefix(source("516"), (walker, payloadPosition, payloadFlags) -> payloadFlags)); + assertEquals(5, (Object) it.prefix(source("5151"), (walker, payloadPosition, payloadFlags) -> payloadFlags)); + assertEquals(1, (Object) it.prefix(source("1151"), (walker, payloadPosition, payloadFlags) -> payloadFlags)); + + assertEquals(3, (Object) it.prefixAndNeighbours(source("155"), (walker, payloadPosition, payloadFlags) -> payloadFlags)); + assertNull(it.prefixAndNeighbours(source("516"), (walker, payloadPosition, payloadFlags) -> payloadFlags)); + assertEquals(5, (Object) it.prefixAndNeighbours(source("5151"), (walker, payloadPosition, payloadFlags) -> payloadFlags)); + assertEquals(1, (Object) it.prefixAndNeighbours(source("1151"), (walker, payloadPosition, payloadFlags) -> payloadFlags)); + + assertEquals(3, (Object) it.prefixAndNeighbours(source("1555"), (walker, payloadPosition, payloadFlags) -> payloadFlags)); + it.goMax(it.lesserBranch); + assertEquals(2, it.payloadFlags()); + it.goMin(it.greaterBranch); + assertEquals(4, it.payloadFlags()); + } + + @Test + public void testWithBounds() throws IOException + { + DataOutputBuffer buf = new DataOutputBufferPaged(); + IncrementalTrieWriter<Integer> builder = makeTrie(buf); + long rootPos = builder.complete(); + + Rebufferer source = new ByteBufRebufferer(buf.asNewBuffer()); + + InternalIterator it = new InternalIterator(source, rootPos, source("151"), source("515"), false); + long pos; + assertNotEquals(-1, pos = it.nextPayloadedNode()); + assertEquals(3, TrieNode.at(buf.asNewBuffer(), (int) pos).payloadFlags(buf.asNewBuffer(), (int) pos)); Review Comment: This was a little confusing to me at first. "15" is clearly a prefix, but "151" is an exact match for an entry, right? The semantics of "admitting a prefix" in that case just looks like an "inclusive lower bound". In `testWithBoundsAndAdmitPrefix()`, we would also have a passing test with the left bound "15", which seems more like "admitting a prefix", but we start w/ "151" there as well. Am I thinking about this wrongly? :D -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

