/
StorageManager.java
109 lines (86 loc) · 2.61 KB
/
StorageManager.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
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
package com.gap.Wallet.Storage;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
class StorageManager implements IWalletStorage {
// pool for database file drivers
private final static Map<String,StorageDriver> drivers = new HashMap<String,StorageDriver>();
private final static Map<String, Integer> clients = new HashMap<String, Integer>();
// blocking or nonblocking mode
private static boolean blocking = true;
// session variables
private final String storagefile;
private final IWalletStorage driver;
private StorageManager(String storagefile) {
this.storagefile = storagefile;
this.driver = drivers.get(storagefile);
}
public static void configureBlocking(boolean blocking) {
StorageManager.blocking = blocking;
}
public static synchronized StorageManager createSession(String filename) {
assert(filename != null);
// initialize driver for new database file
String storagefile = Paths.get(filename).toFile().getAbsolutePath();
if (!drivers.containsKey(storagefile)) {
drivers.put(storagefile, new StorageDriver(storagefile));
}
// initialize or increment clients counter
if (clients.containsKey(storagefile)) {
clients.put(storagefile, clients.get(storagefile) + 1);
} else {
clients.put(storagefile, 1);
}
return new StorageManager(storagefile);
}
public boolean exists(byte[] key) throws WalletException {
if (blocking && (clients.get(storagefile) > 1)) {
synchronized (driver) {
return driver.exists(key);
}
}
return driver.exists(key);
}
public byte[] get(byte[] key) throws WalletException {;
if (blocking && (clients.get(storagefile) > 1)) {
synchronized (driver) {
return driver.get(key);
}
}
return driver.get(key);
}
public boolean set(byte[] key, byte[] value) throws WalletException {
if (blocking && (clients.get(storagefile) > 1)) {
synchronized (driver) {
return driver.set(key, value);
}
}
return driver.set(key, value);
}
public boolean remove(byte[] key) throws WalletException {
if (blocking && (clients.get(storagefile) > 1)) {
synchronized (driver) {
return driver.remove(key);
}
}
return driver.remove(key);
}
public long count() {
if (blocking && (clients.get(storagefile) > 1)) {
synchronized (driver) {
return driver.count();
}
}
return driver.count();
}
public void close() {
if (clients.containsKey(storagefile)) {
clients.put(storagefile, clients.get(storagefile) - 1);
if (clients.get(storagefile).equals(0)) {
driver.close();
drivers.remove(storagefile);
clients.remove(storagefile);
}
}
}
}