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