Repository navigation
Expand file tree
/
Copy pathCustom_Value.java
More file actions
134 lines (94 loc) · 3.47 KB
/
Copy pathCustom_Value.java
File metadata and controls
134 lines (94 loc) · 3.47 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 examples;
import org.mapdb.DB;
import org.mapdb.DBMaker;
import org.mapdb.Serializer;
import java.io.*;
import java.util.Map;
/*
* Demonstrates HashMaps with non-standard types of objects as key or value.
*/
public class Custom_Value {
/**
* MapDB uses custom serialization which stores class metadata at single place.
* Thanks to it is 10x more efficient than standard Java serialization.
*
* Using custom values in MapDB has three conditions:
*
* 1) classes should be immutable. There is instance cache, background serialization etc
* Modifing your classes after they were inserted into MapDB may leed to unexpected things.
*
* 2) You should implement `Serializable` marker interface. MapDB tries to stay compatible
* with standard Java serialization.
*
* 3) Even your values should implement equalsTo method for CAS (compare-and-swap) operations.
*
*/
public static class Person implements Serializable{
final String name;
final String city;
public Person(String n, String c){
super();
this.name = n;
this.city = c;
}
public String getName() {
return name;
}
public String getCity() {
return city;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
if (city != null ? !city.equals(person.city) : person.city != null) return false;
if (name != null ? !name.equals(person.name) : person.name != null) return false;
return true;
}
}
public static void main(String[] args) throws IOException {
// Open db in temp directory
File f = File.createTempFile("mapdb","temp");
DB db = DBMaker.newFileDB(f)
.make();
// Open or create table
Map<String,Person> dbMap = db.getTreeMap("personAndCity");
// Add data
Person bilbo = new Person("Bilbo","The Shire");
Person sauron = new Person("Sauron","Mordor");
Person radagast = new Person("Radagast","Crazy Farm");
dbMap.put("west",bilbo);
dbMap.put("south",sauron);
dbMap.put("mid",radagast);
// Commit and close
db.commit();
db.close();
//
// Second option for using cystom values is to use your own serializer.
// This usually leads to better performance as MapDB does not have to
// analyze the class structure.
//
class CustomSerializer implements Serializer<Person>, Serializable{
@Override
public void serialize(DataOutput out, Person value) throws IOException {
out.writeUTF(value.getName());
out.writeUTF(value.getCity());
}
@Override
public Person deserialize(DataInput in, int available) throws IOException {
return new Person(in.readUTF(), in.readUTF());
}
@Override
public int fixedSize() {
return -1;
}
}
Serializer<Person> serializer = new CustomSerializer();
DB db2 = DBMaker.newTempFileDB().make();
Map<String,Person> map2 = db2.createHashMap("map").valueSerializer(serializer).make();
map2.put("North", new Person("Yet another dwarf","Somewhere"));
db2.commit();
db2.close();
}
}