Skip to content

Commit

Permalink
#XD-466 fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
penemue committed Sep 8, 2015
1 parent fd570f0 commit 566cb59
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 58 deletions.
11 changes: 7 additions & 4 deletions environment/src/main/java/jetbrains/exodus/tree/TreeCursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,14 @@ protected boolean retreat() {

@Override
public boolean getLast() {
if (traverser.moveToLast()) {
inited = true;
return true;
// move up to root
while (traverser.canMoveUp()) {
traverser.moveUp();
}
return false;
traverser.init(false);
inited = true;
alreadyIn = !traverser.isNotEmpty() && traverser.hasValue();
return getPrev();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.jetbrains.annotations.Nullable;

public interface TreeTraverser {

void init(boolean left);

boolean isNotEmpty();
Expand All @@ -30,6 +31,8 @@ public interface TreeTraverser {
@NotNull
ByteIterable getValue();

boolean hasValue();

boolean canMoveRight();

boolean canMoveLeft();
Expand Down Expand Up @@ -63,7 +66,4 @@ public interface TreeTraverser {
boolean moveTo(ByteIterable key, @Nullable ByteIterable value);

boolean moveToRange(ByteIterable key, @Nullable ByteIterable value);

boolean moveToLast();

}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,16 @@ public ByteIterable getKey() {
@Override
@NotNull
public ByteIterable getValue() {
return node.getValue();
final ByteIterable result = node.getValue();
if (result == null) {
throw new NullPointerException();
}
return result;
}

@Override
public boolean hasValue() {
return node.hasValue();
}

@Override
Expand Down Expand Up @@ -228,29 +237,6 @@ private boolean doMoveTo(@NotNull ByteIterable key, @Nullable ByteIterable value
return true;
}

@Override
public boolean moveToLast() {
final int oldTop = top;
BasePage node = oldTop == 0 ? currentNode : stack[0].node; // the most bottom node, ignoring lower bound
top = 0;
if (currentNode.size == 0) {
for (int i = 0; i < oldTop; ++i) {
stack[i] = null;
}
this.node = ILeafNode.EMPTY;
return false;
}
currentNode = node;
currentPos = node.size - 1;
while (canMoveDown()) {
moveDownToLast();
}
for (int i = top; i < oldTop; ++i) {
stack[i] = null;
}
return true;
}

@NotNull
@Override
public BTreeBase getTree() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ public ByteIterable getValue() {
return result;
}

@Override
public boolean hasValue() {
return currentValue != null;
}

@Override
public void moveUp() {
--top;
Expand Down Expand Up @@ -329,29 +334,6 @@ public boolean moveToRange(@NotNull ByteIterable key, @Nullable ByteIterable val
return true;
}

@Override
public boolean moveToLast() {
final int oldTop = top;
NodeBase node = oldTop == 0 ? currentNode : stack[0].getParentNode(); // the most bottom node, ignoring lower bound
top = 0;
if (currentNode.getChildrenCount() == 0) {
for (int i = 0; i < oldTop; ++i) {
stack[i] = null;
}
return false;
}
setCurrentNode(node);
currentIterator = node.getChildrenLast();
currentChild = currentIterator.getNode();
while (canMoveDown()) {
moveDownToLast();
}
for (int i = top; i < oldTop; ++i) {
stack[i] = null;
}
return true;
}

@NotNull
@Override
public PatriciaTreeBase getTree() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import jetbrains.exodus.util.Random;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import java.io.IOException;
Expand Down Expand Up @@ -319,17 +318,27 @@ public void testGetPrev2() throws IOException {
c.close();
}

@Ignore
@Test
public void testGetLast_XD_466() throws IOException {
tm = createMutableTree(true, 1);
for (int i = 0; i < 10; ++i) {
final StringKVNode kv = (StringKVNode) kv(Integer.toString(i), Integer.toString(i));
try (ITreeCursor c = tm.openCursor()) {
assertFalse(c.getLast());
}
for (int i = 0; i < 9999; ++i) {
final StringKVNode kv = (StringKVNode) kv(i, Integer.toString(i));
tm.put(kv);
try (ITreeCursor c = tm.openCursor()) {
assertTrue(c.getLast());
assertEquals(kv.getKey(), c.getKey());
assertEquals(kv.getValue(), c.getValue());
if (i > 0) {
assertTrue(c.getPrev());
assertNotEquals(kv.getKey(), c.getKey());
assertNotEquals(kv.getValue(), c.getValue());
}
assertTrue(c.getLast());
assertEquals(kv.getKey(), c.getKey());
assertEquals(kv.getValue(), c.getValue());
}
}
}
Expand Down

0 comments on commit 566cb59

Please sign in to comment.