-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
138 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
core/src/main/java/com/scalar/db/util/groupcommit/DefaultGroupCommitKeyManipulator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.scalar.db.util.groupcommit; | ||
|
||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
public class DefaultGroupCommitKeyManipulator | ||
implements GroupCommitKeyManipulator<String, String, String, String, String> { | ||
private static final int PRIMARY_KEY_SIZE = 24; | ||
private static final char DELIMITER = '$'; | ||
private static final int MAX_FULL_KEY_SIZE = 64; | ||
private static final int MAX_CHILD_KEY_SIZE = | ||
MAX_FULL_KEY_SIZE - PRIMARY_KEY_SIZE - 1 /* delimiter */; | ||
private static final char[] CHARS_FOR_PRIMARY_KEY; | ||
private static final int CHARS_FOR_PRIMARY_KEY_SIZE; | ||
|
||
static { | ||
int digitsLen = '9' - '0' + 1; | ||
int upperCasesLen = 'Z' - 'A' + 1; | ||
int lowerCasesLen = 'z' - 'a' + 1; | ||
CHARS_FOR_PRIMARY_KEY = new char[digitsLen + upperCasesLen + lowerCasesLen]; | ||
|
||
int index = 0; | ||
for (char c = '0'; c <= '9'; c++) { | ||
CHARS_FOR_PRIMARY_KEY[index++] = c; | ||
} | ||
for (char c = 'A'; c <= 'Z'; c++) { | ||
CHARS_FOR_PRIMARY_KEY[index++] = c; | ||
} | ||
for (char c = 'a'; c <= 'z'; c++) { | ||
CHARS_FOR_PRIMARY_KEY[index++] = c; | ||
} | ||
|
||
CHARS_FOR_PRIMARY_KEY_SIZE = CHARS_FOR_PRIMARY_KEY.length; | ||
} | ||
|
||
@Override | ||
public String generateParentKey() { | ||
char[] chars = new char[PRIMARY_KEY_SIZE]; | ||
for (int i = 0; i < PRIMARY_KEY_SIZE; i++) { | ||
chars[i] = | ||
CHARS_FOR_PRIMARY_KEY[ThreadLocalRandom.current().nextInt(CHARS_FOR_PRIMARY_KEY_SIZE)]; | ||
} | ||
return new String(chars); | ||
} | ||
|
||
@Override | ||
public String fullKey(String parentKey, String childKey) { | ||
if (parentKey.length() != PRIMARY_KEY_SIZE) { | ||
throw new IllegalArgumentException( | ||
String.format( | ||
"The length of parent key must be %d. ParentKey: %s", PRIMARY_KEY_SIZE, childKey)); | ||
} | ||
if (childKey.length() > MAX_CHILD_KEY_SIZE) { | ||
throw new IllegalArgumentException( | ||
String.format( | ||
"The length of child key must not exceed %d. ChildKey: %s", | ||
MAX_CHILD_KEY_SIZE, childKey)); | ||
} | ||
return parentKey + DELIMITER + childKey; | ||
} | ||
|
||
@Override | ||
public boolean isFullKey(Object obj) { | ||
if (!(obj instanceof String)) { | ||
return false; | ||
} | ||
String key = (String) obj; | ||
return key.length() > PRIMARY_KEY_SIZE && key.charAt(PRIMARY_KEY_SIZE) == DELIMITER; | ||
} | ||
|
||
@Override | ||
public Keys<String, String, String> keysFromFullKey(String fullKey) { | ||
if (!isFullKey(fullKey)) { | ||
throw new IllegalArgumentException("Invalid full key. Key:" + fullKey); | ||
} | ||
|
||
return new Keys<>( | ||
fullKey.substring(0, PRIMARY_KEY_SIZE), | ||
fullKey.substring(PRIMARY_KEY_SIZE + 1 /* delimiter */), | ||
fullKey); | ||
} | ||
|
||
@Override | ||
public String emitFullKeyFromFullKey(String fullKey) { | ||
// Return the string as is since the value is already String. | ||
return fullKey; | ||
} | ||
|
||
@Override | ||
public String emitParentKeyFromParentKey(String parentKey) { | ||
// Return the string as is since the value is already String. | ||
return parentKey; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.