aweisberg commented on code in PR #54:
URL: https://github.com/apache/cassandra-accord/pull/54#discussion_r1282428109
##########
accord-core/src/main/java/accord/primitives/AbstractKeys.java:
##########
@@ -229,42 +236,64 @@ public final long foldl(IndexedFoldToLong<? super K>
fold, long param, long init
public final FullKeyRoute toRoute(RoutingKey homeKey)
{
if (isEmpty())
- return new FullKeyRoute(homeKey, new RoutingKey[] { homeKey });
+ return new FullKeyRoute(homeKey, false, new RoutingKey[] { homeKey
});
- RoutingKey[] result = toRoutingKeysArray(homeKey);
- int pos = Arrays.binarySearch(result, homeKey);
- return new FullKeyRoute(result[pos], result);
+ return toRoutingKeysArray(homeKey, (routingKeys, homeKeyIndex,
isParticipatingHomeKey) -> new FullKeyRoute(routingKeys[homeKeyIndex],
isParticipatingHomeKey, routingKeys));
}
protected RoutingKey[] toRoutingKeysArray(RoutingKey withKey)
{
- RoutingKey[] result;
- int resultCount;
- int insertPos = Arrays.binarySearch(keys, withKey);
- if (insertPos < 0)
- insertPos = -1 - insertPos;
+ return toRoutingKeysArray(withKey, (routingKeys, homeKeyIndex,
isParticipatingHomeKey) -> routingKeys);
+ }
- if (insertPos < keys.length &&
keys[insertPos].toUnseekable().equals(withKey))
+ interface ToRoutingKeysFactory<T>
+ {
+ T apply(RoutingKey[] keys, int insertPos, boolean includesKey);
+ }
+
+ @SuppressWarnings("SuspiciousSystemArraycopy")
+ protected <T> T toRoutingKeysArray(RoutingKey withKey,
ToRoutingKeysFactory<T> toRoutingKeysFactory)
+ {
+ if (keys.getClass() == RoutingKey[].class)
{
- result = new RoutingKey[keys.length];
- resultCount = copyToRoutingKeys(keys, 0, result, 0, keys.length);
+ int insertPos = Arrays.binarySearch(keys, withKey);
+ if (insertPos >= 0)
+ {
+ Invariants.checkState(keys[insertPos].equals(withKey));
+ return toRoutingKeysFactory.apply((RoutingKey[])keys,
insertPos, true);
+ }
+
+ insertPos = -1 - insertPos;
+ RoutingKey[] result = new RoutingKey[1 + keys.length];
+ System.arraycopy(keys, 0, result, 0, insertPos);
+ result[insertPos] = withKey;
+ System.arraycopy(keys, insertPos, result, insertPos + 1,
keys.length - insertPos);
+ return toRoutingKeysFactory.apply(result, insertPos, false);
}
else
{
- result = new RoutingKey[1 + keys.length];
- resultCount = copyToRoutingKeys(keys, 0, result, 0, insertPos);
- if (resultCount == 0 || !withKey.equals(result[resultCount - 1]))
- result[resultCount++] = withKey;
- resultCount += copyToRoutingKeys(keys, insertPos, result,
resultCount, keys.length - insertPos);
+ // TODO review this is a pretty terrible work around for the fact
that you can't binarySearch these
Review Comment:
I can remove the TODO, Benedict already took a look at it. I'll start using
that syntax, thanks.
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]