-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathCachingParser.java
More file actions
125 lines (105 loc) · 3.29 KB
/
CachingParser.java
File metadata and controls
125 lines (105 loc) · 3.29 KB
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
123
124
125
package ua_parser;
import java.util.Map;
import org.apache.commons.collections4.map.LRUMap;
/**
* When doing webanalytics (with for example PIG) the main pattern is to process
* weblogs in clickstreams. A basic fact about common clickstreams is that in
* general the same browser will do multiple requests in sequence. This has the
* effect that the same useragent will appear in the logfiles and we will see
* the need to parse the same useragent over and over again.
*
* This class introduces a very simple LRU cache to reduce the number of times
* the parsing is actually done. The default cache size is 1000 and can be
* configured as object creation.
*
* Note: The object handles one cache per parsing method - be carefull when using big sizes.
*
* @author Niels Basjes
*
*/
public class CachingParser extends Parser {
private static final String INVALID_CACHE_SIZE_ERROR_MESSAGE = "Invalid cache size provided - Should be greater than 0";
private int cacheSize = 1000;
private Map<String, Client> cacheClient = null;
private Map<String, UserAgent> cacheUserAgent = null;
private Map<String, Device> cacheDevice = null;
private Map<String, OS> cacheOS = null;
// ------------------------------------------
public CachingParser() {
super();
}
public CachingParser(int cacheSize) {
super();
assert cacheSize > 0: INVALID_CACHE_SIZE_ERROR_MESSAGE;
this.cacheSize = cacheSize;
}
// ------------------------------------------
@Override
public Client parse(String agentString) {
if (agentString == null) {
return null;
}
if (cacheClient == null) {
cacheClient = new LRUMap<>(cacheSize);
}
Client client = cacheClient.get(agentString);
if (client != null) {
return client;
}
client = super.parse(agentString);
cacheClient.put(agentString, client);
return client;
}
// ------------------------------------------
@Override
public UserAgent parseUserAgent(String agentString) {
if (agentString == null) {
return null;
}
if (cacheUserAgent == null) {
cacheUserAgent = new LRUMap<>(cacheSize);
}
UserAgent userAgent = cacheUserAgent.get(agentString);
if (userAgent != null) {
return userAgent;
}
userAgent = super.parseUserAgent(agentString);
cacheUserAgent.put(agentString, userAgent);
return userAgent;
}
// ------------------------------------------
@Override
public Device parseDevice(String agentString) {
if (agentString == null) {
return null;
}
if (cacheDevice == null) {
cacheDevice = new LRUMap<>(cacheSize);
}
Device device = cacheDevice.get(agentString);
if (device != null) {
return device;
}
device = super.parseDevice(agentString);
cacheDevice.put(agentString, device);
return device;
}
// ------------------------------------------
@Override
public OS parseOS(String agentString) {
if (agentString == null) {
return null;
}
if (cacheOS == null) {
cacheOS = new LRUMap<>(cacheSize);
}
OS os = cacheOS.get(agentString);
if (os != null) {
return os;
}
os = super.parseOS(agentString);
cacheOS.put(agentString, os);
return os;
}
// ------------------------------------------
}