-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBDevice.java
More file actions
188 lines (170 loc) · 5.45 KB
/
Copy pathDBDevice.java
File metadata and controls
188 lines (170 loc) · 5.45 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import com.sleepycat.je.Cursor;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.LockMode;
import com.sleepycat.je.OperationStatus;
// models a hardware device by Berkeley DB
public class DBDevice {
Database db;
Cursor c, c_saved;
@FunctionalInterface
interface DeviceLambda {
boolean apply(String key, String data) throws MyError;
}
public DBDevice(Database _db) {
this.db = _db;
c = null;
c_saved = null;
}
public void openCursor() {
if (c != null)
c.close();
c = db.openCursor(null, null);
c_saved = null;
}
public void closeCursor() {
if (c != null)
c.close();
c = null;
// write data to disk
db.sync();
}
public void openDupCursor() {
if (c != null) {
c_saved = c;
c = c_saved.dup(false);
}
}
public void closeDupCursor() {
if (c_saved != null) {
c.close();
c = c_saved;
c_saved = null;
}
}
// most low-level functions
public void put(String key, String value) {
try {
DatabaseEntry keyEntry = new DatabaseEntry(key.getBytes("UTF-8"));
DatabaseEntry dataEntry = new DatabaseEntry(value.getBytes("UTF-8"));
c.putNoDupData(keyEntry, dataEntry);
DBProcessor.debug("put (" + key + ", " + value + ")");
} catch (Exception e) {
e.printStackTrace();
}
}
public void skip() {
// move cursor to the last position
DatabaseEntry keyEntry = new DatabaseEntry();
DatabaseEntry dataEntry = new DatabaseEntry();
c.getLast(keyEntry, dataEntry, LockMode.DEFAULT);
}
public ArrayList<String> get(String key, DeviceLambda l) throws MyError {
ArrayList<String> result = new ArrayList<String>();
try {
DatabaseEntry keyEntry = new DatabaseEntry(key.getBytes("UTF-8"));
DatabaseEntry dataEntry = new DatabaseEntry();
if (c.getSearchKey(keyEntry, dataEntry, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
do {
String dataString = new String(dataEntry.getData(), "UTF-8");
DBProcessor.debug("get (" + key + ", " + dataString + ")");
if (l == null || l.apply(key, dataString))
result.add(dataString);
} while (c.getNextDup(keyEntry, dataEntry, LockMode.DEFAULT) == OperationStatus.SUCCESS);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}
public ArrayList<String> getSub(String key, DeviceLambda l) throws MyError {
ArrayList<String> result = new ArrayList<String>();
try {
DatabaseEntry keyEntry = new DatabaseEntry(key.getBytes("UTF-8"));
DatabaseEntry dataEntry = new DatabaseEntry();
if (c.getSearchKeyRange(keyEntry, dataEntry, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
do {
String keyString = new String(keyEntry.getData(), "UTF-8");
String dataString = new String(dataEntry.getData(), "UTF-8");
if (keyString.startsWith(key)) {
DBProcessor.debug("get (" + keyString + ", " + dataString + ")");
if (l == null || l.apply(keyString, dataString))
result.add(dataString);
} else
break;
} while (c.getNext(keyEntry, dataEntry, LockMode.DEFAULT) == OperationStatus.SUCCESS);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}
/*
public String getOne(String key) throws MyError {
try {
DatabaseEntry keyEntry = new DatabaseEntry(key.getBytes("UTF-8"));
DatabaseEntry dataEntry = new DatabaseEntry();
if (c.getSearchKey(keyEntry, dataEntry, LockMode.DEFAULT) != OperationStatus.SUCCESS)
return null;
return new String(dataEntry.getData(), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
*/
public void delete(String key, DeviceLambda l) throws MyError {
try {
DatabaseEntry keyEntry = new DatabaseEntry(key.getBytes("UTF-8"));
DatabaseEntry dataEntry = new DatabaseEntry();
if (c.getSearchKey(keyEntry, dataEntry, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
do {
String dataString = new String(dataEntry.getData(), "UTF-8");
if (l == null || l.apply(key, dataString))
{
DBProcessor.debug("delete (" + key + ", " + dataString + ")");
c.delete();
}
} while (c.getNextDup(keyEntry, dataEntry, LockMode.DEFAULT) == OperationStatus.SUCCESS);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public void deleteSub(String key, DeviceLambda l) throws MyError {
try {
DatabaseEntry keyEntry = new DatabaseEntry(key.getBytes("UTF-8"));
DatabaseEntry dataEntry = new DatabaseEntry();
if (c.getSearchKeyRange(keyEntry, dataEntry, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
do {
String keyString = new String(keyEntry.getData(), "UTF-8");
String dataString = new String(dataEntry.getData(), "UTF-8");
if (keyString.startsWith(key)) {
if (l == null || l.apply(keyString, dataString))
{
DBProcessor.debug("delete (" + keyString + ", " + dataString + ")");
c.delete();
}
} else
break;
} while (c.getNext(keyEntry, dataEntry, LockMode.DEFAULT) == OperationStatus.SUCCESS);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
/*
public void deleteOne(String key) throws MyError {
try {
DatabaseEntry keyEntry = new DatabaseEntry(key.getBytes("UTF-8"));
DatabaseEntry dataEntry = new DatabaseEntry();
if (c.getSearchKey(keyEntry, dataEntry, LockMode.DEFAULT) == OperationStatus.SUCCESS)
c.delete();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
*/
}