-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWriteBack.java
69 lines (57 loc) · 1.45 KB
/
WriteBack.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.HashSet;
import java.util.LinkedHashSet;
/*
*Implementation by Eric Xing
*Write-Back writing scheme
*/
public class WriteBack implements Writer {
/*
* all values that have been passed through or are currently contained in the
* cache
*/
HashSet<Integer> database = new HashSet<Integer>();
// Cache
Cache cache;
// Recorded params
int database_writes;
int cache_writes;
// Time data
static final long THRESHOLD = 1000;
long last;
// Data that have not been flushed
LinkedHashSet<Integer> waiting;
// Getter for Cache writes
public int getCacheWrites() {
return cache_writes;
}
// Constructor, initialize everything
public WriteBack(Cache cache) {
this.cache = cache;
database_writes = 0;
cache_writes = 0;
last = System.currentTimeMillis();
waiting = new LinkedHashSet<Integer>();
}
public void write(int value) {
if (System.currentTimeMillis() - last > THRESHOLD) {
/*
* Time to flush the data from the waiting HashSet to the database
*/
for (Integer val : waiting) {
database.add(val);
database_writes++;
}
// Update time
waiting = new LinkedHashSet<Integer>();
last = System.currentTimeMillis();
} else {
// Otherwise, just flush to cache
cache.add(value);
cache_writes++;
}
}
// Display Database Writes
public void display() {
System.out.println("Database Writes: " + database_writes);
}
}