-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistributedMapDb.java
More file actions
151 lines (138 loc) · 4.08 KB
/
DistributedMapDb.java
File metadata and controls
151 lines (138 loc) · 4.08 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
package storage.mapdb;
import java.util.ArrayList;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import model.Entity;
import model.codec.EncodedEntity;
import model.exceptions.CodecException;
import model.lightchain.Identifier;
import modules.codec.JsonEncoder;
import org.mapdb.DB;
import org.mapdb.DBMaker;
import org.mapdb.HTreeMap;
import org.mapdb.Serializer;
/**
* Distributed databese that store encoded entities.
*/
public class DistributedMapDb implements storage.Distributed {
private final DB db;
private final ReentrantReadWriteLock lock;
private static final String MAP_NAME = "distributed_map";
private final HTreeMap distributedMap;
/**
* Creates DistributedMapDb.
*/
public DistributedMapDb(String filePath) {
this.db = DBMaker.fileDB(filePath).make();
this.lock = new ReentrantReadWriteLock();
distributedMap = this.db.hashMap(MAP_NAME)
.keySerializer(Serializer.BYTE_ARRAY)
.createOrOpen();
}
/**
* Checks existence of entity on the database.
*
* @param entityId Identifier of entity.
* @return true if a entity with that identifier exists, false otherwise.
*/
@Override
public boolean has(Identifier entityId) {
boolean hasBoolean;
try {
lock.readLock().lock();
hasBoolean = distributedMap.containsKey(entityId.getBytes());
} finally {
lock.readLock().unlock();
}
return hasBoolean;
}
/**
* Adds entity to the database.
*
* @param e given entity to be added.
* @return true if entity did not exist on the database, false if entity is already in
* database.
*/
@Override
public boolean add(Entity e) throws CodecException {
JsonEncoder encoder = new JsonEncoder();
boolean addBoolean;
try {
lock.writeLock().lock();
addBoolean = distributedMap.putIfAbsentBoolean(e.id().getBytes(), encoder.encode(e));
} catch (CodecException ex) {
throw new CodecException("could not encode the entity", ex);
} finally {
lock.writeLock().unlock();
}
return addBoolean;
}
/**
* Removes entity with given identifier.
*
* @param e identifier of the entity.
* @return true if entity exists on database and removed successfully, false if entity does not exist on
* database.
*/
@Override
public boolean remove(Entity e) throws CodecException {
JsonEncoder encoder = new JsonEncoder();
boolean removeBoolean;
try {
lock.writeLock().lock();
removeBoolean = distributedMap.remove(e.id().getBytes(), encoder.encode(e));
} catch (CodecException exception) {
throw new CodecException("could not encode entity", exception);
} finally {
lock.writeLock().unlock();
}
return removeBoolean;
}
/**
* Returns the entity with given identifier.
*
* @param entityId identifier of the entity.
* @return the entity itself if exists and null otherwise.
*/
@Override
public Entity get(Identifier entityId) throws CodecException {
Entity decodedEntity;
try {
JsonEncoder encoder = new JsonEncoder();
lock.readLock().lock();
EncodedEntity encodedEntity = (EncodedEntity) distributedMap.get(entityId.getBytes());
if (encodedEntity == null) {
return null;
}
decodedEntity = encoder.decode(encodedEntity);
} catch (CodecException e) {
throw new CodecException("could not found the class", e);
} finally {
lock.readLock().unlock();
}
return decodedEntity;
}
/**
* Returns all entities stored in database.
*
* @return all stored entities in database.
*/
@Override
public ArrayList<Entity> all() throws CodecException {
JsonEncoder encoder = new JsonEncoder();
ArrayList<Entity> allEntities = new ArrayList<>();
for (Object encodedEntity : distributedMap.values()) {
try {
allEntities.add(encoder.decode((EncodedEntity) encodedEntity));
} catch (CodecException e) {
throw new CodecException("could not found the class", e);
}
}
return allEntities;
}
/**
* It closes the database.
*/
public void closeDb() {
db.close();
}
}