From f7afb9bf9dc5e37272b8a50653f55cc5bebe69c2 Mon Sep 17 00:00:00 2001 From: Rafael Magalhaes Date: Thu, 17 Oct 2024 15:38:11 +0100 Subject: [PATCH] code dup --- .../edc/edr/core/lock/InMemoryEdrLock.java | 16 ---------------- .../edc/edr/index/sql/lock/SqlEdrLock.java | 18 ------------------ .../index/lock/EndpointDataReferenceLock.java | 18 ++++++++++++++++-- 3 files changed, 16 insertions(+), 36 deletions(-) diff --git a/core/edr-core/src/main/java/org/eclipse/tractusx/edc/edr/core/lock/InMemoryEdrLock.java b/core/edr-core/src/main/java/org/eclipse/tractusx/edc/edr/core/lock/InMemoryEdrLock.java index 774833f26..75d6a2ac1 100644 --- a/core/edr-core/src/main/java/org/eclipse/tractusx/edc/edr/core/lock/InMemoryEdrLock.java +++ b/core/edr-core/src/main/java/org/eclipse/tractusx/edc/edr/core/lock/InMemoryEdrLock.java @@ -20,19 +20,16 @@ package org.eclipse.tractusx.edc.edr.core.lock; import org.eclipse.edc.edr.spi.store.EndpointDataReferenceEntryIndex; -import org.eclipse.edc.edr.spi.types.EndpointDataReferenceEntry; import org.eclipse.edc.spi.result.StoreResult; import org.eclipse.edc.spi.types.domain.DataAddress; import org.eclipse.edc.transaction.spi.TransactionContext; import org.eclipse.tractusx.edc.edr.spi.index.lock.EndpointDataReferenceLock; -import java.time.Instant; import java.util.HashSet; import java.util.Set; import java.util.concurrent.locks.ReentrantReadWriteLock; import static java.lang.Thread.sleep; -import static org.eclipse.tractusx.edc.edr.spi.CoreConstants.EDR_PROPERTY_EXPIRES_IN; public class InMemoryEdrLock implements EndpointDataReferenceLock { @@ -84,19 +81,6 @@ public StoreResult acquireLock(String edrId, DataAddress edr) { } } - @Override - public boolean isExpired(DataAddress edr, EndpointDataReferenceEntry metadata) { - var expiresInString = edr.getStringProperty(EDR_PROPERTY_EXPIRES_IN); - if (expiresInString == null) { - return false; - } - var expiresIn = Long.parseLong(expiresInString); - var expiresAt = metadata.getCreatedAt() / 1000L + expiresIn; - var expiresAtInstant = Instant.ofEpochSecond(expiresAt); - - return expiresAtInstant.isBefore(Instant.now()); - } - @Override public void releaseLock(String edrId) { lockedEdrs.remove(edrId); diff --git a/edc-extensions/edr/edr-index-lock-sql/src/main/java/org/eclipse/tractusx/edc/edr/index/sql/lock/SqlEdrLock.java b/edc-extensions/edr/edr-index-lock-sql/src/main/java/org/eclipse/tractusx/edc/edr/index/sql/lock/SqlEdrLock.java index abb90b731..efa044815 100644 --- a/edc-extensions/edr/edr-index-lock-sql/src/main/java/org/eclipse/tractusx/edc/edr/index/sql/lock/SqlEdrLock.java +++ b/edc-extensions/edr/edr-index-lock-sql/src/main/java/org/eclipse/tractusx/edc/edr/index/sql/lock/SqlEdrLock.java @@ -32,9 +32,6 @@ import java.sql.ResultSet; import java.sql.SQLException; -import java.time.Instant; - -import static org.eclipse.tractusx.edc.edr.spi.CoreConstants.EDR_PROPERTY_EXPIRES_IN; public class SqlEdrLock extends AbstractSqlStore implements EndpointDataReferenceLock { private final EdrLockStatements statements; @@ -63,21 +60,6 @@ public StoreResult acquireLock(String edrId, DataAddress edr) { }); } - @Override - public boolean isExpired(DataAddress edr, EndpointDataReferenceEntry metadata) { - var expiresInString = edr.getStringProperty(EDR_PROPERTY_EXPIRES_IN); - if (expiresInString == null) { - return false; - } - - var expiresIn = Long.parseLong(expiresInString); - // createdAt is in millis, expires-in is in seconds - var expiresAt = metadata.getCreatedAt() / 1000L + expiresIn; - var expiresAtInstant = Instant.ofEpochSecond(expiresAt); - - return expiresAtInstant.isBefore(Instant.now()); - } - @Override public void releaseLock(String edrId) { // do nothing since the lock is implicitly released by the row update. diff --git a/spi/edr-spi/src/main/java/org/eclipse/tractusx/edc/edr/spi/index/lock/EndpointDataReferenceLock.java b/spi/edr-spi/src/main/java/org/eclipse/tractusx/edc/edr/spi/index/lock/EndpointDataReferenceLock.java index f75850f85..a4a1bed7b 100644 --- a/spi/edr-spi/src/main/java/org/eclipse/tractusx/edc/edr/spi/index/lock/EndpointDataReferenceLock.java +++ b/spi/edr-spi/src/main/java/org/eclipse/tractusx/edc/edr/spi/index/lock/EndpointDataReferenceLock.java @@ -23,10 +23,24 @@ import org.eclipse.edc.spi.result.StoreResult; import org.eclipse.edc.spi.types.domain.DataAddress; +import java.time.Instant; + +import static org.eclipse.tractusx.edc.edr.spi.CoreConstants.EDR_PROPERTY_EXPIRES_IN; + public interface EndpointDataReferenceLock { StoreResult acquireLock(String edrId, DataAddress edr); - boolean isExpired(DataAddress edr, EndpointDataReferenceEntry edrEntry); - void releaseLock(String edrId); + + default boolean isExpired(DataAddress edr, EndpointDataReferenceEntry edrEntry) { + var expiresInString = edr.getStringProperty(EDR_PROPERTY_EXPIRES_IN); + if (expiresInString == null) { + return false; + } + var expiresIn = Long.parseLong(expiresInString); + var expiresAt = edrEntry.getCreatedAt() / 1000L + expiresIn; + var expiresAtInstant = Instant.ofEpochSecond(expiresAt); + + return expiresAtInstant.isBefore(Instant.now()); + } }