Repository navigation
Expand file tree
/
Copy pathIssue150Test.java
More file actions
122 lines (93 loc) · 3.03 KB
/
Copy pathIssue150Test.java
File metadata and controls
122 lines (93 loc) · 3.03 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.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.Serializable;
import java.util.Map;
public class Issue150Test {
@Test
public void test() {
// TxMaker txMaker = DBMaker.newFileDB(new File("/tmp/mapdb.test"))
// .closeOnJvmShutdown().asyncWriteDisable().makeTxMaker();
TxMaker txMaker = DBMaker.newMemoryDB().closeOnJvmShutdown()
.makeTxMaker();
DB db = txMaker.makeTx();
EntityA x = new EntityA();
x.setId(126l);
x.setName("nameXXX");
Serializer<EntityA> valueSerializer = new CustomSerializer();
Map<Long, EntityA> map = db.createHashMap("entitya").valueSerializer(valueSerializer).make();
map.put(x.getId(), x);
db.commit();
EntityA y = (EntityA) txMaker.makeTx().getHashMap("entitya")
.get(x.getId());
System.out.println(x.equals(y));
txMaker.close();
}
private static final class CustomSerializer implements
Serializer<Issue150Test.EntityA>, Serializable {
@Override
public void serialize(DataOutput out, EntityA value) throws IOException {
out.writeLong(value.getId());
out.writeUTF(value.getName());
}
@Override
public EntityA deserialize(DataInput in, int available)
throws IOException {
EntityA a = new EntityA();
a.setId(in.readLong());
a.setName(in.readUTF());
return a;
}
@Override
public int fixedSize() {
return -1;
}
}
public static class EntityA implements Serializable {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
EntityA other = (EntityA) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
}