-
Notifications
You must be signed in to change notification settings - Fork 0
/
YcsbCommon.java
129 lines (112 loc) · 4.53 KB
/
YcsbCommon.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.scalar.db.benchmarks.ycsb;
import com.scalar.db.api.Consistency;
import com.scalar.db.api.Get;
import com.scalar.db.api.Put;
import com.scalar.db.io.Key;
import com.scalar.db.io.TextColumn;
import com.scalar.kelpie.config.Config;
import java.util.Random;
public class YcsbCommon {
static final long DEFAULT_LOAD_CONCURRENCY = 1;
static final long DEFAULT_LOAD_BATCH_SIZE = 1;
static final long DEFAULT_RECORD_COUNT = 1000;
static final long DEFAULT_PAYLOAD_SIZE = 1000;
static final String NAMESPACE = "ycsb";
static final String NAMESPACE_PRIMARY = "ycsb_primary"; // for multi-storage mode
static final String NAMESPACE_SECONDARY = "ycsb_secondary"; // for multi-storage mode
static final String TABLE = "usertable";
static final String YCSB_KEY = "ycsb_key";
static final String PAYLOAD = "payload";
static final String CONFIG_NAME = "ycsb_config";
static final String LOAD_CONCURRENCY = "load_concurrency";
static final String LOAD_BATCH_SIZE = "load_batch_size";
static final String LOAD_OVERWRITE = "load_overwrite";
static final String RECORD_COUNT = "record_count";
static final String PAYLOAD_SIZE = "payload_size";
static final String OPS_PER_TX = "ops_per_tx";
private static final int CHAR_START = 32; // [space]
private static final int CHAR_STOP = 126; // [~]
private static final char[] CHAR_SYMBOLS = new char[1 + CHAR_STOP - CHAR_START];
static {
for (int i = 0; i < CHAR_SYMBOLS.length; i++) {
CHAR_SYMBOLS[i] = (char) (CHAR_START + i);
}
}
private static final int[] FAST_MASKS = {
554189328, // 10000
277094664, // 01000
138547332, // 00100
69273666, // 00010
34636833, // 00001
346368330, // 01010
727373493, // 10101
588826161, // 10001
935194491, // 11011
658099827, // 10011
};
public static Get prepareGet(int key) {
return prepareGet(NAMESPACE, TABLE, key);
}
public static Get prepareGet(String namespace, int key) {
return prepareGet(namespace, TABLE, key);
}
public static Get prepareGet(String namespace, String table, int key) {
return Get.newBuilder()
.namespace(namespace)
.table(table)
.partitionKey(Key.ofInt(YCSB_KEY, key))
.consistency(Consistency.LINEARIZABLE)
.build();
}
public static Put preparePut(int key, String payload) {
return preparePut(NAMESPACE, TABLE, key, payload);
}
public static Put preparePut(String namespace, int key, String payload) {
return preparePut(namespace, TABLE, key, payload);
}
public static Put preparePut(String namespace, String table, int key, String payload) {
return Put.newBuilder()
.namespace(namespace)
.table(table)
.partitionKey(Key.ofInt(YCSB_KEY, key))
.value(TextColumn.of(PAYLOAD, payload))
.consistency(Consistency.LINEARIZABLE)
.build();
}
public static int getLoadConcurrency(Config config) {
return (int) config.getUserLong(CONFIG_NAME, LOAD_CONCURRENCY, DEFAULT_LOAD_CONCURRENCY);
}
public static int getLoadBatchSize(Config config) {
return (int) config.getUserLong(CONFIG_NAME, LOAD_BATCH_SIZE, DEFAULT_LOAD_BATCH_SIZE);
}
public static boolean getLoadOverwrite(Config config) {
return config.getUserBoolean(CONFIG_NAME, LOAD_OVERWRITE, false);
}
public static int getRecordCount(Config config) {
return (int) config.getUserLong(CONFIG_NAME, RECORD_COUNT, DEFAULT_RECORD_COUNT);
}
public static int getPayloadSize(Config config) {
return (int) config.getUserLong(CONFIG_NAME, PAYLOAD_SIZE, DEFAULT_PAYLOAD_SIZE);
}
// This method is taken from benchbase.
// https://github.com/cmu-db/benchbase/blob/bbe8c1db84ec81c6cdec6fbeca27b24b1b4e6612/src/main/java/com/oltpbenchmark/util/TextGenerator.java#L80
public static char[] randomFastChars(Random rng, char[] chars) {
// Ok so now the goal of this is to reduce the number of times that we have to
// invoke a random number. We'll do this by grabbing a single random int
// and then taking different bitmasks
int num_rounds = chars.length / FAST_MASKS.length;
int i = 0;
for (int ctr = 0; ctr < num_rounds; ctr++) {
int rand = rng.nextInt(10000); // CHAR_SYMBOLS.length);
for (int mask : FAST_MASKS) {
chars[i++] = CHAR_SYMBOLS[(rand | mask) % CHAR_SYMBOLS.length];
}
}
// Use the old way for the remaining characters
// I am doing this because I am too lazy to think of something more clever
for (; i < chars.length; i++) {
chars[i] = CHAR_SYMBOLS[rng.nextInt(CHAR_SYMBOLS.length)];
}
return (chars);
}
}