-
Notifications
You must be signed in to change notification settings - Fork 2
/
ChunkEJB.java
122 lines (91 loc) · 3.41 KB
/
ChunkEJB.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package no.difi.datahotel.slave.logic;
import static no.difi.datahotel.util.shared.Filesystem.FOLDER_CACHE_CHUNK;
import static no.difi.datahotel.util.shared.Filesystem.FOLDER_SLAVE;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Singleton;
import no.difi.datahotel.util.csv.CSVParser;
import no.difi.datahotel.util.csv.CSVParserFactory;
import no.difi.datahotel.util.csv.CSVWriter;
import no.difi.datahotel.util.model.Metadata;
import no.difi.datahotel.util.shared.Filesystem;
import no.difi.datahotel.util.shared.Timestamp;
@Singleton
public class ChunkEJB {
private Map<String, Long> posts = new HashMap<String, Long>();
private Map<String, Long> pages = new HashMap<String, Long>();
private int size = 100;
public File getFullDataset(Metadata metadata) {
return Filesystem.getFile(FOLDER_SLAVE, metadata.getLocation(), Filesystem.FILE_DATASET);
}
public void update(Metadata metadata) {
Logger logger = metadata.getLogger();
Timestamp ts = new Timestamp(FOLDER_CACHE_CHUNK, metadata.getLocation(), "timestamp");
if (metadata.getUpdated() == ts.getTimestamp()) {
posts.put(metadata.getLocation(), ts.getLong("posts"));
pages.put(metadata.getLocation(), ts.getLong("pages"));
logger.info("Chunk up to date.");
return;
}
logger.info("Building chunk.");
try {
String locationTmp = metadata.getLocation() + "-tmp." + System.currentTimeMillis();
CSVParser parser = CSVParserFactory.getCSVParser(getFullDataset(metadata));
CSVWriter writer = null;
long number = 0, counter = 0;
while (parser.hasNext()) {
counter++;
if (counter % size == 1) {
number++;
String filename = "dataset-" + number + ".csv";
writer = new CSVWriter(Filesystem.getFile(FOLDER_CACHE_CHUNK, locationTmp, filename));
writer.writeHeader(parser.getHeaders());
}
writer.write(parser.getNextLineArray());
if (counter % size == 0) {
writer.close();
writer = null;
}
}
if (writer != null)
writer.close();
File goal = Filesystem.getFolderPath(FOLDER_CACHE_CHUNK, metadata.getLocation());
Filesystem.delete(FOLDER_CACHE_CHUNK, metadata.getLocation());
Filesystem.getFolderPath(FOLDER_CACHE_CHUNK, locationTmp).renameTo(goal);
posts.put(metadata.getLocation(), counter);
pages.put(metadata.getLocation(), number);
ts.setTimestamp(metadata.getUpdated());
ts.set("posts", counter);
ts.set("pages", number);
ts.save();
} catch (Exception e) {
// TODO Start sending exceptions.
logger.log(Level.WARNING, e.getMessage(), e);
}
}
public ArrayList<Map<String, String>> get(Metadata metadata, int number) {
Logger logger = metadata.getLogger();
File source = Filesystem.getFile(FOLDER_CACHE_CHUNK, metadata.getLocation(), "dataset-" + number + ".csv");
ArrayList<Map<String, String>> result = new ArrayList<Map<String, String>>();
try {
CSVParser parser = CSVParserFactory.getCSVParser(source);
while (parser.hasNext())
result.add(parser.getNextLine());
parser.close();
return result;
} catch (Exception e) {
logger.log(Level.WARNING, e.getMessage());
}
return null;
}
public Long getPosts(String location) {
return posts.containsKey(location) ? posts.get(location) : 0;
}
public Long getPages(String location) {
return pages.containsKey(location) ? pages.get(location) : 0;
}
}