Skip to content

Commit

Permalink
code dup
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelmag110 committed Oct 17, 2024
1 parent 18f5963 commit f7afb9b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -84,19 +81,6 @@ public StoreResult<Boolean> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,21 +60,6 @@ public StoreResult<Boolean> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean> 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());
}
}

0 comments on commit f7afb9b

Please sign in to comment.