1
|
package eu.dnetlib.data.mdstore.model;
|
2
|
|
3
|
import java.time.Duration;
|
4
|
import java.time.LocalDate;
|
5
|
import java.time.ZoneId;
|
6
|
import java.util.Date;
|
7
|
import java.util.List;
|
8
|
import java.util.Map;
|
9
|
|
10
|
import com.google.common.collect.Lists;
|
11
|
import com.google.common.collect.Maps;
|
12
|
import org.springframework.data.annotation.Id;
|
13
|
import org.springframework.data.mongodb.core.index.Indexed;
|
14
|
|
15
|
/**
|
16
|
* Created by claudio on 13/03/2017.
|
17
|
*/
|
18
|
public class TransactionInfo {
|
19
|
|
20
|
@Id
|
21
|
private String mdId;
|
22
|
|
23
|
@Indexed
|
24
|
private String currentId;
|
25
|
|
26
|
private Map<String, ReadLock> readLocks;
|
27
|
private Map<String, Transaction> transactions;
|
28
|
|
29
|
public static TransactionInfo create() {
|
30
|
return new TransactionInfo()
|
31
|
.setReadLocks(Maps.newHashMap())
|
32
|
.setTransactions(Maps.newHashMap());
|
33
|
}
|
34
|
|
35
|
public ReadLock getReadLock(final String id) {
|
36
|
return readLocks.get(id);
|
37
|
}
|
38
|
|
39
|
public List<String> getExpired(final int retainDays) {
|
40
|
final List<String> res = Lists.newArrayList();
|
41
|
readLocks.entrySet().removeIf(e -> {
|
42
|
final ReadLock info = e.getValue();
|
43
|
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
|
private long getExpiringDays(final ReadLock info) {
|
64
|
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
|
public String getMdId() {
|
70
|
return mdId;
|
71
|
}
|
72
|
|
73
|
public TransactionInfo setMdId(final String mdId) {
|
74
|
this.mdId = mdId;
|
75
|
return this;
|
76
|
}
|
77
|
|
78
|
public String getCurrentId() {
|
79
|
return currentId;
|
80
|
}
|
81
|
|
82
|
public TransactionInfo setCurrentId(final String currentId) {
|
83
|
this.currentId = currentId;
|
84
|
return this;
|
85
|
}
|
86
|
|
87
|
public TransactionInfo setReadLocks(final Map<String, ReadLock> readLocks) {
|
88
|
this.readLocks = readLocks;
|
89
|
return this;
|
90
|
}
|
91
|
|
92
|
public TransactionInfo setTransactions(final Map<String, Transaction> transactions) {
|
93
|
this.transactions = transactions;
|
94
|
return this;
|
95
|
}
|
96
|
|
97
|
public Map<String, ReadLock> getReadLocks() {
|
98
|
return readLocks;
|
99
|
}
|
100
|
|
101
|
public Map<String, Transaction> getTransactions() {
|
102
|
return transactions;
|
103
|
}
|
104
|
|
105
|
|
106
|
}
|