sanpwc commented on a change in pull request #121:
URL: https://github.com/apache/ignite-3/pull/121#discussion_r635380146
##########
File path:
modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/watch/KeyCriterion.java
##########
@@ -91,72 +93,216 @@ public RangeCriterion(Key from, Key to) {
}
/** {@inheritDoc} */
- @Override public IgniteBiTuple<Key, Key> toRange() {
- return new IgniteBiTuple<>(from, to);
+ @Override public boolean contains(Key key) {
+ return key.compareTo(from) >= 0 && key.compareTo(to) < 0;
+ }
+
+ /**
+ * Calculate range representation for prefix criterion
+ * as {@code (prefixKey, nextKey(prefixKey)) }.
+ *
+ * @param prefixKey prefix.
+ * @return calculated range
+ */
+ public static RangeCriterion fromPrefixKey(Key prefixKey) {
+ return new RangeCriterion(prefixKey, nextKey(prefixKey));
+ }
+
+ /** {@inheritDoc} */
+ @Override protected KeyCriterion union(KeyCriterion keyCriterion,
boolean swapTry) {
+ Key from;
+ Key to;
+
+ if (keyCriterion instanceof ExactCriterion) {
+ from = ((ExactCriterion)keyCriterion).key;
+ to = nextKey(from);
+ }
+ else if (keyCriterion instanceof CollectionCriterion) {
+ from =
Collections.min(((CollectionCriterion)keyCriterion).keys);
+ to =
nextKey(Collections.max(((CollectionCriterion)keyCriterion).keys));
+ }
+ else if (keyCriterion instanceof RangeCriterion) {
+ from = ((RangeCriterion)keyCriterion).from;
+ to = ((RangeCriterion)keyCriterion).to;
+ }
+ else if (!swapTry)
+ return keyCriterion.union(this, true);
+ else throw KeyCriterion.unsupportedUnionException(this,
keyCriterion);
+
+ return new RangeCriterion(
+ Collections.min(Arrays.asList(this.from, from)),
+ Collections.max(Arrays.asList(this.to, to))
+ );
+
+ }
+
+ /**
+ * Calculate the next key for received key.
+ *
+ * @param key input key
+ * @return next key
+ */
+ private static Key nextKey(Key key) {
+ var bytes = Arrays.copyOf(key.bytes(), key.bytes().length);
+
+ if (bytes[bytes.length - 1] != Byte.MAX_VALUE)
+ bytes[bytes.length - 1]++;
+ else
+ bytes = Arrays.copyOf(bytes, bytes.length + 1);
+
+ return new Key(bytes);
}
/** {@inheritDoc} */
- @Override public boolean contains(Key key) {
- return key.compareTo(from) >= 0 && key.compareTo(to) < 0;
+ @Override public boolean equals(Object o) {
+ if (this == o)
+ return true;
+
+ if (o == null || getClass() != o.getClass())
+ return false;
+
+ RangeCriterion criterion = (RangeCriterion)o;
+ return Objects.equals(from, criterion.from) && Objects.equals(to,
criterion.to);
+ }
+
+ /** {@inheritDoc} */
+ @Override public int hashCode() {
+ return Objects.hash(from, to);
+ }
+
+ /**
+ * @return Start of the range.
+ */
+ public Key from() {
+ return from;
+ }
+
+ /**
+ * @return End of the range (exclusive).
+ */
+ public Key to() {
+ return to;
}
}
/**
* Criterion which consists collection of keys.
*/
- static class CollectionCriterion implements KeyCriterion {
+ public static class CollectionCriterion extends KeyCriterion {
/** Collection of keys. */
- private final Collection<Key> keys;
+ private final Set<Key> keys;
/**
* Creates the instance of collection criterion.
*
* @param keys Collection of keys.
*/
public CollectionCriterion(Collection<Key> keys) {
- this.keys = keys;
+ this.keys = new HashSet<>(keys);
}
/** {@inheritDoc} */
- @Override public IgniteBiTuple<Key, Key> toRange() {
- return new IgniteBiTuple<>(Collections.min(keys),
Collections.max(keys));
+ @Override public boolean contains(Key key) {
+ return keys.contains(key);
}
/** {@inheritDoc} */
- @Override public boolean contains(Key key) {
- return keys.contains(key);
+ @Override protected KeyCriterion union(KeyCriterion keyCriterion,
boolean swapTry) {
+ var newKeys = new HashSet<>(keys);
+
+ if (keyCriterion instanceof ExactCriterion) {
+ newKeys.add(((ExactCriterion)keyCriterion).key);
+
+ return new CollectionCriterion(newKeys);
+ }
+ else if (keyCriterion instanceof CollectionCriterion) {
+ newKeys.addAll(((CollectionCriterion)keyCriterion).keys);
+
+ return new CollectionCriterion(newKeys);
+ } else if (!swapTry)
+ return keyCriterion.union(keyCriterion, true);
+ else
+ throw KeyCriterion.unsupportedUnionException(this,
keyCriterion);
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean equals(Object o) {
+ if (this == o)
+ return true;
+
+ if (o == null || getClass() != o.getClass())
+ return false;
+
+ CollectionCriterion criterion = (CollectionCriterion)o;
+ return Objects.equals(keys, criterion.keys);
+ }
+
+ /** {@inheritDoc} */
+ @Override public int hashCode() {
+ return Objects.hash(keys);
+ }
+
+ /**
+ * @return Collection of keys.
+ */
+ public Collection<Key> keys() {
+ return keys;
}
}
/**
- * Criterion which consists of all keys with defined prefix.
+ * Simple criterion which contains exactly one key.
*/
- static class PrefixCriterion implements KeyCriterion {
- /** Prefix of the key. */
- private final Key prefixKey;
+ public static class ExactCriterion extends KeyCriterion {
+ /** The key of criterion. */
+ private final Key key;
/**
- * Creates the instance of prefix key criterion.
+ * Creates the instance of exact criterion.
*
- * @param prefixKey Prefix of the key.
+ * @param key Instance of the reference key.
*/
- public PrefixCriterion(Key prefixKey) {
- this.prefixKey = prefixKey;
+ public ExactCriterion(Key key) {
+ this.key = key;
}
/** {@inheritDoc} */
- @Override public IgniteBiTuple<Key, Key> toRange() {
- var bytes = Arrays.copyOf(prefixKey.bytes(),
prefixKey.bytes().length);
- if (bytes[bytes.length - 1] != Byte.MAX_VALUE)
- bytes[bytes.length - 1]++;
+ @Override public boolean contains(Key key) {
+ return this.key.equals(key);
+ }
+
+ /** {@inheritDoc} */
+ @Override protected KeyCriterion union(KeyCriterion keyCriterion,
boolean swapTry) {
+ if (keyCriterion instanceof ExactCriterion)
+ return new CollectionCriterion(Arrays.asList(key,
((ExactCriterion)keyCriterion).key));
Review comment:
Why should we use CollectionCriterion keys are equal?
--
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.
For queries about this service, please contact Infrastructure at:
[email protected]