Skip to content

Commit 6268591

Browse files
authored
Merge pull request #270 from eclipse/oracle-fix
Allow Oracle NoSQL database have multiple entities in the same structure with different ids.
2 parents 5417790 + ec3c349 commit 6268591

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

CHANGELOG.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ and this project adheres to https://semver.org/spec/v2.0.0.html[Semantic Version
2222

2323
- Remove the `UDT` annotation and use `Column` annotation instead.
2424

25+
=== Fixed
26+
27+
- Allow multiple entities at Oracle NoSQL appending the entity name with the id instead of only the id
28+
2529
== [1.1.0] - 2023-02-05
2630

2731
=== Changed

jnosql-oracle-nosql/src/main/java/org/eclipse/jnosql/databases/oracle/communication/DefaultOracleNoSQLDocumentManager.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ public CommunicationEntity insert(CommunicationEntity entity) {
9696
public CommunicationEntity insert(CommunicationEntity entity, Duration ttl) {
9797
Objects.requireNonNull(entity, "entity is required");
9898
Objects.requireNonNull(ttl, "ttl is required");
99+
if(ttl.toHours() <= 0){
100+
throw new UnsupportedOperationException("Oracle NoSQL Database has support to TTL over one hour. " +
101+
"The current ttl: " + ttl);
102+
}
99103
put(entity, TimeToLive.ofHours(ttl.toHours()));
100104
return entity;
101105
}
@@ -133,7 +137,7 @@ public void delete(DeleteQuery query) {
133137
var oracleQuery = selectBuilder.get();
134138
if (oracleQuery.hasIds()) {
135139
for (String id : oracleQuery.ids()) {
136-
var delRequest = new DeleteRequest().setKey(new MapValue().put(ORACLE_ID, id))
140+
var delRequest = new DeleteRequest().setKey(new MapValue().put(ORACLE_ID, generateId(id, query.name())))
137141
.setTableName(table);
138142
serviceHandle.delete(delRequest);
139143
}
@@ -165,7 +169,7 @@ public Stream<CommunicationEntity> select(SelectQuery query) {
165169
List<CommunicationEntity> entities = new ArrayList<>();
166170

167171
if (oracleQuery.hasIds()) {
168-
entities.addAll(getIds(oracleQuery));
172+
entities.addAll(getIds(oracleQuery, query.name()));
169173
}
170174
if (!oracleQuery.hasOnlyIds()) {
171175
LOGGER.finest("Executing Oracle Query: " + oracleQuery.query());
@@ -199,11 +203,11 @@ private static boolean isNotOracleField(Map.Entry<String, FieldValue> entry) {
199203
return !entry.getKey().equals(ENTITY) && !entry.getKey().equals(JSON_FIELD) && !entry.getKey().equals(ORACLE_ID);
200204
}
201205

202-
private List<CommunicationEntity> getIds(OracleQuery oracleQuery) {
206+
private List<CommunicationEntity> getIds(OracleQuery oracleQuery, String table) {
203207
List<CommunicationEntity> entities = new ArrayList<>();
204208
for (String id : oracleQuery.ids()) {
205209
GetRequest getRequest = new GetRequest();
206-
getRequest.setKey(new MapValue().put(ID_FIELD, id));
210+
getRequest.setKey(new MapValue().put(ID_FIELD, generateId(id, table)));
207211
getRequest.setTableName(name());
208212
GetResult getResult = serviceHandle.get(getRequest);
209213
if (getResult != null && getResult.getValue() != null) {
@@ -284,12 +288,14 @@ private List<CommunicationEntity> executeSQL(String sql, List<FieldValue> params
284288

285289
private void put(CommunicationEntity entity, TimeToLive ttl) {
286290
Map<String, Object> entityMap = new HashMap<>(entity.toMap());
287-
entityMap.put(ENTITY, entity.name());
288-
String id = entity.find(ID).map(Element::get)
291+
String name = entity.name();
292+
entityMap.put(ENTITY, name);
293+
294+
String id = generateId(entity.find(ID).map(Element::get)
289295
.map(Object::toString)
290-
.orElseGet(() -> UUID.randomUUID().toString());
296+
.orElseGet(() -> UUID.randomUUID().toString()), name);
291297

292-
MapValue mapValue = new MapValue().put(ORACLE_ID, id).put(ENTITY, entity.name());
298+
MapValue mapValue = new MapValue().put(ORACLE_ID, id).put(ENTITY, name);
293299
MapValue contentVal = mapValue.putFromJson("content", jsonB.toJson(entityMap),
294300
OPTIONS);
295301
PutRequest putRequest = new PutRequest()
@@ -300,4 +306,8 @@ private void put(CommunicationEntity entity, TimeToLive ttl) {
300306
serviceHandle.put(putRequest);
301307
}
302308

309+
private String generateId(String id, String table) {
310+
return table + ":" + id;
311+
}
312+
303313
}

0 commit comments

Comments
 (0)