Repository navigation
Expand file tree
/
Copy pathIssue162Test.java
More file actions
122 lines (89 loc) · 3.18 KB
/
Copy pathIssue162Test.java
File metadata and controls
122 lines (89 loc) · 3.18 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
package org.mapdb;
import org.junit.Test;
import java.io.*;
import java.util.Map;
import static org.junit.Assert.assertTrue;
public class Issue162Test {
public static class MyValue implements Serializable {
private String string;
public MyValue(String string) {
this.string = string;
}
@Override
public String toString() {
return "MyValue{" + "string='" + string + '\'' + '}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof MyValue)) return false;
MyValue myValue = (MyValue) o;
if (!string.equals(myValue.string)) return false;
return true;
}
@Override
public int hashCode() {
return string.hashCode();
}
}
public static class MyValueSerializer implements Serializable, Serializer<MyValue> {
@Override
public void serialize(DataOutput out, MyValue value) throws IOException {
assertTrue(value != null);
System.out.println("Custom serializer called with '" + value + "'");
out.writeUTF(value.string);
}
@Override
public MyValue deserialize(DataInput in, int available) throws IOException {
String s = in.readUTF();
return new MyValue(s);
}
@Override
public int fixedSize() {
return -1;
}
}
private static void printEntries(Map<Long, MyValue> map) {
System.out.println("Reading back data");
for (Map.Entry<Long, MyValue> entry : map.entrySet()) {
System.out.println("Entry id = " + entry.getKey() + ", contents = " + entry.getValue().toString());
}
}
File path = UtilsTest.tempDbFile();
@Test public void testHashMap() {
System.out.println("--- Testing HashMap with custom serializer");
DB db = DBMaker.newFileDB(path).make();
Map<Long, MyValue> map = db.createHashMap("map")
.valueSerializer(new MyValueSerializer())
.make();
db.commit();
System.out.println("Putting and committing data");
map.put(1L, new MyValue("one"));
map.put(2L, new MyValue("two"));
db.commit();
System.out.println("Closing and reopening db");
db.close();
map = null;
db = DBMaker.newFileDB(path).make();
map = db.getHashMap("map");
printEntries(map);
}
@Test public void testBTreeMap() {
System.out.println("--- Testing BTreeMap with custom serializer");
DB db = DBMaker.newFileDB(path).make();
Map<Long, MyValue> map = db.createTreeMap("map")
.valueSerializer(new MyValueSerializer())
.make();
db.commit();
System.out.println("Putting and committing data");
map.put(1L, new MyValue("one"));
map.put(2L, new MyValue("two"));
db.commit();
System.out.println("Closing and reopening db");
db.close();
map = null;
db = DBMaker.newFileDB(path).make();
map = db.getTreeMap("map");
printEntries(map);
}
}