Skip to content

Instantly share code, notes, and snippets.

@undeadcat
Created October 15, 2015 10:15
Show Gist options
  • Save undeadcat/b827c17ca4aef4d5bfb9 to your computer and use it in GitHub Desktop.
Save undeadcat/b827c17ca4aef4d5bfb9 to your computer and use it in GitHub Desktop.
Conflict Resolution
public abstract class ReadCommand extends MonitorableImpl implements ReadQuery
/**
* The time in seconds to use as "now" for this query.
* <p>
* We use the same time as "now" for the whole query to avoid considering different
* values as expired during the query, which would be buggy (would throw of counting amongst other
* things).
*
* @return the time (in seconds) to use as "now".
*/
public int nowInSec()
{
return nowInSec;
}
}
public abstract class Conflicts
{
private Conflicts() {}
public enum Resolution { LEFT_WINS, MERGE, RIGHT_WINS };
public static Resolution resolveRegular(long leftTimestamp,
boolean leftLive,
int leftLocalDeletionTime,
ByteBuffer leftValue,
long rightTimestamp,
boolean rightLive,
int rightLocalDeletionTime,
ByteBuffer rightValue)
{
if (leftTimestamp != rightTimestamp)
return leftTimestamp < rightTimestamp ? Resolution.RIGHT_WINS : Resolution.LEFT_WINS;
if (leftLive != rightLive)
return leftLive ? Resolution.RIGHT_WINS : Resolution.LEFT_WINS;
int c = leftValue.compareTo(rightValue);
if (c < 0)
return Resolution.RIGHT_WINS;
else if (c > 0)
return Resolution.LEFT_WINS;
// Prefer the longest ttl if relevant
return leftLocalDeletionTime < rightLocalDeletionTime ? Resolution.RIGHT_WINS : Resolution.LEFT_WINS;
}
}
public static class FBUtilities{
public static long timestampMicros()
{
// we use microsecond resolution for compatibility with other client libraries, even though
// we can't actually get microsecond precision.
return System.currentTimeMillis() * 1000;
}
public static int nowInSeconds()
{
return (int) (System.currentTimeMillis() / 1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment