This repository has been archived by the owner on Jul 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExampleMain.java
64 lines (57 loc) · 2.25 KB
/
ExampleMain.java
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
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.Transaction;
import java.io.File;
public class ExampleMain {
public static void main(String[] args) {
// write your code here
Database db = null;
Environment env = null;
try {
final File jarFile = new File(ClassLoader.getSystemClassLoader().getResource(".").getPath());
final File envDir = new File(jarFile.getPath() + "/data/je");
envDir.mkdirs();
final EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setTransactional(true);
envConfig.setAllowCreate(true);
env = new Environment(envDir, envConfig);
final DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setTransactional(true);
dbConfig.setAllowCreate(true);
dbConfig.setSortedDuplicates(true);
db = env.openDatabase(null, "exampledb", dbConfig);
} catch(Exception e) {
System.out.println(e);
}
if (db == null) {
return;
}
System.out.println("Created db" + db.getDatabaseName());
Transaction transaction = env.beginTransaction(null, null);
DatabaseEntry key = new DatabaseEntry("key2".getBytes());
DatabaseEntry data = new DatabaseEntry("data1".getBytes());
String result = null;
OperationStatus res = db.put(transaction, key, data);
if (res != OperationStatus.SUCCESS) {
result = "Error: " + res.toString();
} else {
result = key + "/" + data;
}
transaction.commit();
System.out.println("JE " + "did put of: " + result);
transaction = env.beginTransaction(null, null);
data = new DatabaseEntry();
res = db.get(transaction, key, data, null);
if (res != OperationStatus.SUCCESS) {
result = "Error: " + res.toString();
} else {
result = key + "/" + data;
}
transaction.commit();
System.out.println("JE " + "did get of: " + result);
}
}