1 |
46418
|
claudio.at
|
package eu.dnetlib.data.mdstore.model;
|
2 |
46349
|
claudio.at
|
|
3 |
46509
|
claudio.at
|
import java.time.Duration;
|
4 |
|
|
import java.time.LocalDate;
|
5 |
|
|
import java.time.ZoneId;
|
6 |
|
|
import java.util.Date;
|
7 |
46349
|
claudio.at
|
import java.util.List;
|
8 |
46509
|
claudio.at
|
import java.util.Map;
|
9 |
46349
|
claudio.at
|
|
10 |
|
|
import com.google.common.collect.Lists;
|
11 |
46509
|
claudio.at
|
import com.google.common.collect.Maps;
|
12 |
46517
|
claudio.at
|
import org.springframework.data.annotation.Id;
|
13 |
|
|
import org.springframework.data.mongodb.core.index.Indexed;
|
14 |
46349
|
claudio.at
|
|
15 |
|
|
/**
|
16 |
|
|
* Created by claudio on 13/03/2017.
|
17 |
|
|
*/
|
18 |
46517
|
claudio.at
|
public class TransactionInfo {
|
19 |
46349
|
claudio.at
|
|
20 |
46517
|
claudio.at
|
@Id
|
21 |
46349
|
claudio.at
|
private String mdId;
|
22 |
46517
|
claudio.at
|
|
23 |
|
|
@Indexed
|
24 |
46349
|
claudio.at
|
private String currentId;
|
25 |
|
|
|
26 |
46517
|
claudio.at
|
private Map<String, ReadLock> readLocks;
|
27 |
|
|
private Map<String, Transaction> transactions;
|
28 |
46509
|
claudio.at
|
|
29 |
46517
|
claudio.at
|
public static TransactionInfo create() {
|
30 |
|
|
return new TransactionInfo()
|
31 |
|
|
.setReadLocks(Maps.newHashMap())
|
32 |
|
|
.setTransactions(Maps.newHashMap());
|
33 |
46417
|
claudio.at
|
}
|
34 |
|
|
|
35 |
46517
|
claudio.at
|
public ReadLock getReadLock(final String id) {
|
36 |
|
|
return readLocks.get(id);
|
37 |
46509
|
claudio.at
|
}
|
38 |
|
|
|
39 |
|
|
public List<String> getExpired(final int retainDays) {
|
40 |
|
|
final List<String> res = Lists.newArrayList();
|
41 |
46517
|
claudio.at
|
readLocks.entrySet().removeIf(e -> {
|
42 |
|
|
final ReadLock info = e.getValue();
|
43 |
46509
|
claudio.at
|
if (info.getId().equals(getCurrentId())) {
|
44 |
|
|
return false;
|
45 |
|
|
} else {
|
46 |
|
|
if (getExpiringDays(info) >= retainDays) {
|
47 |
|
|
res.add(info.getId());
|
48 |
|
|
return true;
|
49 |
|
|
} else {
|
50 |
|
|
return false;
|
51 |
|
|
}
|
52 |
|
|
}
|
53 |
|
|
});
|
54 |
|
|
return res;
|
55 |
|
|
}
|
56 |
|
|
|
57 |
|
|
/**
|
58 |
|
|
* Gets the expiring days.
|
59 |
|
|
*
|
60 |
|
|
* @param info the value
|
61 |
|
|
* @return the expiring days
|
62 |
|
|
*/
|
63 |
46517
|
claudio.at
|
private long getExpiringDays(final ReadLock info) {
|
64 |
46509
|
claudio.at
|
final Date lastRead = info.getLastUpdateDate();
|
65 |
|
|
final LocalDate readDate = lastRead.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
|
66 |
|
|
return Duration.between(LocalDate.now().atTime(0, 0), readDate.atTime(0, 0)).toDays();
|
67 |
|
|
}
|
68 |
|
|
|
69 |
46349
|
claudio.at
|
public String getMdId() {
|
70 |
|
|
return mdId;
|
71 |
|
|
}
|
72 |
|
|
|
73 |
46517
|
claudio.at
|
public TransactionInfo setMdId(final String mdId) {
|
74 |
46349
|
claudio.at
|
this.mdId = mdId;
|
75 |
46417
|
claudio.at
|
return this;
|
76 |
46349
|
claudio.at
|
}
|
77 |
|
|
|
78 |
|
|
public String getCurrentId() {
|
79 |
|
|
return currentId;
|
80 |
|
|
}
|
81 |
|
|
|
82 |
46517
|
claudio.at
|
public TransactionInfo setCurrentId(final String currentId) {
|
83 |
46349
|
claudio.at
|
this.currentId = currentId;
|
84 |
46417
|
claudio.at
|
return this;
|
85 |
46349
|
claudio.at
|
}
|
86 |
|
|
|
87 |
46517
|
claudio.at
|
public TransactionInfo setReadLocks(final Map<String, ReadLock> readLocks) {
|
88 |
|
|
this.readLocks = readLocks;
|
89 |
46509
|
claudio.at
|
return this;
|
90 |
46349
|
claudio.at
|
}
|
91 |
|
|
|
92 |
46517
|
claudio.at
|
public TransactionInfo setTransactions(final Map<String, Transaction> transactions) {
|
93 |
|
|
this.transactions = transactions;
|
94 |
46417
|
claudio.at
|
return this;
|
95 |
46349
|
claudio.at
|
}
|
96 |
|
|
|
97 |
46517
|
claudio.at
|
public Map<String, ReadLock> getReadLocks() {
|
98 |
|
|
return readLocks;
|
99 |
46349
|
claudio.at
|
}
|
100 |
|
|
|
101 |
46517
|
claudio.at
|
public Map<String, Transaction> getTransactions() {
|
102 |
|
|
return transactions;
|
103 |
46349
|
claudio.at
|
}
|
104 |
|
|
|
105 |
|
|
|
106 |
|
|
}
|