@Override public int compareTo(Key o) { return compare(o, CompareMethod.EQUALS).getCompare(); } }
@Override public CompareResult compare(Key otherKey, CompareMethod method) { return new CompareResult(ALWAYS_EQUALS); } }
@Override public boolean equals(Object obj) { return obj instanceof CompositeKey && compare(((CompositeKey) obj).getKeys(), CompareMethod.EQUALS).getCompare() == 0; }
@Override public CompareResult compare(Key o, CompareMethod method) { if(o != null) { if(o instanceof UUIDKey) { UUIDKey otherKey = (UUIDKey) o; return new CompareResult(compare(otherKey)); } else { return new CompareResult(-1); } } else { throw new NullPointerException("Cannot compare null Key"); } }
@Override public IndexSearchResultIteratorCollection search(SearchCondition condition, SearchLimit limit) throws JasDBStorageException { IndexIterator indexIterator = new FullIndexIterator(rootBlock, lockManager, blockPersister); EqualsCondition equalsCondition = EqualsSearchOperation.validateCondition(keyInfo, condition); Key undesiredKey = equalsCondition.getKey(); List<Key> results = new LinkedList<>(); for(Key key : indexIterator) { if(key.compare(undesiredKey, CompareMethod.EQUALS).getCompare() != 0) { results.add(key); } if(limit.isMaxReached(results.size())) { break; } } return new IndexSearchResultIteratorImpl(results, keyInfo.getKeyNameMapper()); } }
private CompareResult compare(Key[] otherKeys, CompareMethod compareMethod) { Key[] keys = getKeys(); //we assume keys are ordered in same order int keyLength = keys.length; int lastCompare = -1; for(int i=0; i<keyLength; i++) { Key key = keys[i]; Key otherKey = otherKeys.length > i ? otherKeys[i] : null; if(key != null && otherKey != null && i<valueMarker) { lastCompare = key.compareTo(otherKey); if(lastCompare != 0) { return new CompareResult(lastCompare); } } else { break; } } if(compareMethod == CompareMethod.BEFORE) { return new CompareResult(-1); } else { return new CompareResult(lastCompare); } }
private V before(TreeNode<Key, V> currentNode, Key key) { int compare = key.compare(currentNode.getKey(), CompareMethod.BEFORE).getCompare(); if(compare == 0) { return currentNode.getValue(); } else if(compare > 0) { if(currentNode.getRight() != null) { TreeNode<Key, V> right = currentNode.getRight(); if(key.compare(right.min().getKey(), CompareMethod.BEFORE).getCompare() < 0) { return currentNode.getValue(); } else { return before(right, key); } } return currentNode.getValue(); } else if(compare < 0) { if(currentNode.getLeft() != null) { TreeNode<Key, V> left = currentNode.getLeft(); TreeNode<Key, V> leftMax = left.max(); if(key.compare(leftMax.getKey(), CompareMethod.BEFORE).getCompare() > 0) { return leftMax.getValue(); } else { return before(left, key); } } return currentNode.getValue(); } else { return null; } }
private CompareResult evaluateKey(Key o) { int result; if(o instanceof LongKey) { LongKey longIndexPointer = (LongKey) o; if(key.length == 0) { result = 0; } else { result = (getKey() < longIndexPointer.getKey()) ? -1 : ((getKey() == longIndexPointer.getKey()) ? 0 : 1); } } else if(o instanceof StringKey) { StringKey stringKey = (StringKey) o; try { result = Long.valueOf(getKey()).compareTo(Long.parseLong(stringKey.getKey())); } catch(NumberFormatException ignored) { result = -1; } } else { result = -1; } return new CompareResult(result); }
@Override public CompareResult compare(Key o, CompareMethod method) { if(o != null) { int result = -1; if(o instanceof StringKey) { result = compare(unicodeBytes, ((StringKey) o).getUnicodeBytes()); } else if(o instanceof LongKey) { LongKey longKey = (LongKey) o; try { LongKey localKey = new LongKey(Long.parseLong(getKey())); result = localKey.compareTo(longKey); } catch(NumberFormatException e) { result = -1; } } else if(o instanceof UUIDKey) { UUIDKey uuidKey = (UUIDKey) o; byte[] uuidBytes = KeyUtil.getUnicodeBytes(new UUID(uuidKey.getMostSignificant(), uuidKey.getLeastSignificant()).toString()); result = compare(unicodeBytes, uuidBytes); } return new CompareResult(result); } else { throw new RuntimeJasDBException("Cannot compare null Key"); } }