forked from ua-parser/uap-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCachingParser.java
More file actions
131 lines (111 loc) · 3.28 KB
/
CachingParser.java
File metadata and controls
131 lines (111 loc) · 3.28 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
126
127
128
129
130
131
package ua_parser;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import org.apache.commons.collections.map.LRUMap;
import ua_parser.Client;
import ua_parser.Device;
import ua_parser.OS;
import ua_parser.Parser;
import ua_parser.UserAgent;
/**
* 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.
*
* @author Niels Basjes
*
*/
public class CachingParser extends Parser {
// TODO: Make configurable
private static final int CACHE_SIZE = 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() throws IOException {
super();
}
public CachingParser(InputStream regexYaml) {
super(regexYaml);
}
// ------------------------------------------
@SuppressWarnings("unchecked")
@Override
public Client parse(String agentString) {
if (agentString == null) {
return null;
}
if (cacheClient == null) {
cacheClient = new LRUMap(CACHE_SIZE);
}
Client client = cacheClient.get(agentString);
if (client != null) {
return client;
}
client = super.parse(agentString);
cacheClient.put(agentString, client);
return client;
}
// ------------------------------------------
@SuppressWarnings("unchecked")
@Override
public UserAgent parseUserAgent(String agentString) {
if (agentString == null) {
return null;
}
if (cacheUserAgent == null) {
cacheUserAgent = new LRUMap(CACHE_SIZE);
}
UserAgent userAgent = cacheUserAgent.get(agentString);
if (userAgent != null) {
return userAgent;
}
userAgent = super.parseUserAgent(agentString);
cacheUserAgent.put(agentString, userAgent);
return userAgent;
}
// ------------------------------------------
@SuppressWarnings("unchecked")
@Override
public Device parseDevice(String agentString) {
if (agentString == null) {
return null;
}
if (cacheDevice == null) {
cacheDevice = new LRUMap(CACHE_SIZE);
}
Device device = cacheDevice.get(agentString);
if (device != null) {
return device;
}
device = super.parseDevice(agentString);
cacheDevice.put(agentString, device);
return device;
}
// ------------------------------------------
@SuppressWarnings("unchecked")
@Override
public OS parseOS(String agentString) {
if (agentString == null) {
return null;
}
if (cacheOS == null) {
cacheOS = new LRUMap(CACHE_SIZE);
}
OS os = cacheOS.get(agentString);
if (os != null) {
return os;
}
os = super.parseOS(agentString);
cacheOS.put(agentString, os);
return os;
}
// ------------------------------------------
}