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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.fastsearch;
import com.yahoo.fs4.Packet;
import com.yahoo.fs4.QueryPacket;
import com.yahoo.fs4.QueryResultPacket;
import com.yahoo.search.Query;
import com.yahoo.processing.request.CompoundName;
/**
* The cache control logic for FastSearcher
*
* @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
*/
public class CacheControl {
private static final CompoundName nocachewrite=new CompoundName("nocachewrite");
/** Whether this CacheControl actually should cache hits at all. */
private final boolean activeCache;
/** Direct unsychronized cache access */
private final PacketCache packetCache;
public CacheControl(int sizeMegaBytes, double cacheTimeOutSeconds) {
activeCache = sizeMegaBytes > 0 && cacheTimeOutSeconds > 0.0d;
if (activeCache) {
packetCache = new PacketCache(sizeMegaBytes, 0, cacheTimeOutSeconds);
} else {
packetCache = null;
}
}
/** Returns the capacity of the packet cache in megabytes */
public final int capacity() {
return packetCache.getCapacity();
}
public final boolean useCache(Query query) {
return (activeCache && !query.getNoCache());
}
public final PacketWrapper lookup(CacheKey key, Query query) {
if ((key != null) && useCache(query)) {
long now = System.currentTimeMillis();
synchronized (packetCache) {
return packetCache.get(key, now);
}
}
return null;
}
// updates first phase in multi phase search
void updateCacheEntry(CacheKey key, Query query, QueryResultPacket resultPacket) {
long oldTimestamp;
if (!activeCache) return;
PacketWrapper wrapper = lookup(key, query);
if (wrapper == null) return;
// The timestamp is owned by the QueryResultPacket, this is why this
// update method puts entries into the cache differently from elsewhere
oldTimestamp = wrapper.getTimestamp();
wrapper = (PacketWrapper) wrapper.clone();
wrapper.addResultPacket(resultPacket);
synchronized (packetCache) {
packetCache.put(key, wrapper, oldTimestamp);
}
}
// updates phases after first phase phase in multi phase search
void updateCacheEntry(CacheKey key, Query query, DocsumPacketKey[] packetKeys, Packet[] packets) {
if (!activeCache) return;
PacketWrapper wrapper = lookup(key, query);
if (wrapper== null) return;
wrapper = (PacketWrapper) wrapper.clone();
wrapper.addDocsums(packetKeys, packets);
synchronized (packetCache) {
packetCache.put(key, wrapper, wrapper.getTimestamp());
}
}
void cache(CacheKey key, Query query, DocsumPacketKey[] packetKeys, Packet[] packets) {
if ( ! activeCache) return;
if (query.getNoCache()) return;
if (query.properties().getBoolean(nocachewrite)) return;
PacketWrapper wrapper = lookup(key, query);
if (wrapper == null) {
wrapper = new PacketWrapper(key, packetKeys,packets);
long now = System.currentTimeMillis();
synchronized (packetCache) {
packetCache.put(key, wrapper, now);
}
} else {
wrapper = (PacketWrapper) wrapper.clone();
wrapper.addResultPacket((QueryResultPacket) packets[0]);
wrapper.addDocsums(packetKeys, packets, 1);
synchronized (packetCache) {
packetCache.put(key, wrapper, wrapper.getTimestamp());
}
}
}
/** Test method. */
public void clear() {
if (packetCache != null) {
packetCache.clear();
}
}
}
|