-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapdbHelper.java
More file actions
80 lines (72 loc) · 2.17 KB
/
MapdbHelper.java
File metadata and controls
80 lines (72 loc) · 2.17 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
import org.mapdb.DB;
import org.mapdb.DBMaker;
import org.mapdb.HTreeMap;
import org.mapdb.Serializer;
import java.util.*;
/**
* Created by sunyf on 2018/3/27.
*/
public class MapdbHelper {
private static String path;
private static MapdbHelper mapdbHelper;
private MapdbHelper(){}
private static void init(String path){
MapdbHelper.path = path;
}
/**
* 存储set
* @param setName
* @param strings
*/
private static void createSet(String setName, Set<String> strings) {
// creo il db
DB db = DBMaker.fileDB(path).make();
// Creo un insieme nel db
HTreeMap.KeySet<String> hashSet = db.hashSet(setName).serializer(Serializer.STRING).createOrOpen();
if(strings != null){
for (String s:strings) {
hashSet.add(s);
}}
db.close();
}
/**
* 读取set集合
* @param setName
* @return
*/
private static Set<String> readSet(String setName) {
DB db = DBMaker.fileDB(path).make();
HashSet<String> objects = new HashSet<>();
HTreeMap.KeySet<String> hashSet = db.hashSet(setName).serializer(Serializer.STRING).createOrOpen();
for (String s: hashSet){
objects.add(s);
}
db.close();
return objects;
}
/**
* 判断是否存在
* @param setName
* @param value
* @return
*/
private static boolean exitSetValue(String setName,String value) {
DB db = DBMaker.fileDB(path).make();
HTreeMap.KeySet<String> hashSet = db.hashSet(setName).serializer(Serializer.STRING).createOrOpen();
boolean contains = hashSet.contains(value);
db.close();
return contains;
}
public static void main(String[] args) {
init("D:\\workspace\\git\\mp-template\\sender\\mapdb\\map.db");
Set<String> objects = new HashSet<>();
objects.add("123");
MapdbHelper.createSet("wowo",objects);
Set<String> hahah = MapdbHelper.readSet("wowo");
for (String s: hahah){
System.out.println(s);
}
boolean wowo = MapdbHelper.exitSetValue("wowo", "123");
System.out.println(wowo);
}
}