-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathMapDBClient.java
More file actions
132 lines (104 loc) · 3.59 KB
/
Copy pathMapDBClient.java
File metadata and controls
132 lines (104 loc) · 3.59 KB
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
130
131
132
package site.ycsb;
import org.mapdb.*;
import org.mapdb.DB;
import java.util.*;
/**
* MapDB client for YCSB framework.
*
*/
public class MapDBClient extends site.ycsb.DB {
private DB mapDB;
private HTreeMap<String, TreeMap<String, HashMap<String, byte[]>>> db;
@Override
public void init() throws DBException {
this.mapDB = DBMaker
.fileDB("file.db")
.closeOnJvmShutdown()
.make();
db = mapDB.hashMap("db")
.keySerializer(Serializer.STRING)
.valueSerializer(Serializer.JAVA)
.createOrOpen();
}
public void cleanup() throws DBException {
db.clear();
db.close();
mapDB.close();
}
@Override
public Status read(String table, String key, Set<String> fields, Map<String, ByteIterator> result) {
Map<String, HashMap<String, byte[]>> collection = db.getOrDefault(table, new TreeMap<>());
Map<String, byte[]> map = collection.get(key);
if (map == null) {
return Status.NOT_FOUND;
}
for (String field : fields) {
result.put(field, new ByteArrayByteIterator(map.get(field)));
}
return Status.OK;
}
@Override
public Status scan(String table,
String startkey,
int recordcount,
Set<String> fields,
Vector<HashMap<String, ByteIterator>> result) {
TreeMap<String, HashMap<String, byte[]>> collection = db.getOrDefault(table, new TreeMap<>());
SortedMap<String, HashMap<String, byte[]>> map = collection.tailMap(startkey);
int counter = 0;
for (Map.Entry<String, HashMap<String, byte[]>> entry : map.entrySet()) {
HashMap<String, ByteIterator> filteredMap = new HashMap<>();
if (fields == null) {
for (Map.Entry<String, byte[]> entry1 : entry.getValue().entrySet()) {
filteredMap.put(entry1.getKey(), new ByteArrayByteIterator(entry1.getValue()));
}
} else {
for (Map.Entry<String, byte[]> entry1 : entry.getValue().entrySet()) {
if (fields.contains(entry1.getKey())) {
filteredMap.put(entry1.getKey(), new ByteArrayByteIterator(entry1.getValue()));
}
}
}
result.add(filteredMap);
counter += 1;
if (counter == recordcount) {
break;
}
}
return Status.OK;
}
@Override
public Status update(String table, String key, Map<String, ByteIterator> values) {
TreeMap<String, HashMap<String, byte[]>> collection = db.getOrDefault(table, new TreeMap<>());
HashMap<String, byte[]> map = collection.get(key);
if (map == null) {
return Status.NOT_FOUND;
}
for (Map.Entry<String, ByteIterator> entry : values.entrySet()) {
map.put(entry.getKey(), entry.getValue().toArray());
}
collection.put(key, map);
db.put(table, collection);
return Status.OK;
}
@Override
public Status insert(String table, String key, Map<String, ByteIterator> values) {
TreeMap<String, HashMap<String, byte[]>> collection = db.getOrDefault(table, new TreeMap<>());
HashMap<String, byte[]> map = collection.getOrDefault(key, new HashMap<>());
for (Map.Entry<String, ByteIterator> entry : values.entrySet()) {
map.put(entry.getKey(), entry.getValue().toArray());
}
collection.put(key, map);
db.put(table, collection);
return Status.OK;
}
@Override
public Status delete(String table, String key) {
Map<String, HashMap<String, byte[]>> collection = db.getOrDefault(table, new TreeMap<>());
Map<String, byte[]> removed = collection.remove(key);
if (removed == null) {
return Status.NOT_FOUND;
}
return Status.OK;
}
}