Example #1
0
 @Override
 public void putRight(@NotNull final ByteIterable key, @NotNull final ByteIterable value) {
   final ByteIterator it = key.iterator();
   MutableNode node = root;
   MutableNode prev = null;
   byte prevFirstByte = (byte) 0;
   while (true) {
     final NodeBase.MatchResult matchResult = node.matchesKeySequence(it);
     final int matchingLength = matchResult.matchingLength;
     if (matchingLength < 0) {
       if (!matchResult.hasNext) {
         throw new IllegalArgumentException();
       }
       final MutableNode prefix = node.splitKey(-matchingLength - 1, matchResult.keyByte);
       prefix.hangRight(matchResult.nextByte, it).setValue(value);
       if (prev == null) {
         root = new MutableRoot(prefix, root.sourceAddress);
       } else {
         prev.setChild(prevFirstByte, prefix);
       }
       ++size;
       break;
     }
     if (!it.hasNext()) {
       if (node.hasChildren() || node.hasValue()) {
         throw new IllegalArgumentException();
       }
       node.setValue(value);
       ++size;
       break;
     }
     final byte nextByte = it.next();
     final NodeBase child = node.getRightChild(this, nextByte);
     if (child == null) {
       if (node.hasChildren() || node.hasKey() || node.hasValue()) {
         node.hangRight(nextByte, it).setValue(value);
       } else {
         node.setKeySequence(new ArrayByteIterable(nextByte, it));
         node.setValue(value);
       }
       ++size;
       break;
     }
     prev = node;
     prevFirstByte = nextByte;
     final MutableNode mutableChild = child.getMutableCopy(this);
     if (!child.isMutable()) {
       node.setRightChild(nextByte, mutableChild);
     }
     node = mutableChild;
   }
   TreeCursorMutable.notifyCursors(this);
 }