21
21
22
22
import org .elasticsearch .cache .memory .ByteBufferCache ;
23
23
import org .elasticsearch .cache .query .parser .QueryParserCache ;
24
- import org .elasticsearch .cache .query .parser .none .NoneQueryParserCache ;
24
+ import org .elasticsearch .cluster .ClusterChangedEvent ;
25
+ import org .elasticsearch .cluster .ClusterService ;
26
+ import org .elasticsearch .cluster .ClusterStateListener ;
25
27
import org .elasticsearch .common .component .AbstractComponent ;
26
28
import org .elasticsearch .common .inject .Inject ;
27
- import org .elasticsearch .common .settings .ImmutableSettings ;
28
29
import org .elasticsearch .common .settings .Settings ;
29
30
30
31
/**
31
32
* @author kimchy (shay.banon)
32
33
*/
33
- public class NodeCache extends AbstractComponent {
34
+ public class NodeCache extends AbstractComponent implements ClusterStateListener {
35
+
36
+ private final ClusterService clusterService ;
34
37
35
38
private final ByteBufferCache byteBufferCache ;
36
39
37
40
private final QueryParserCache queryParserCache ;
38
41
39
- public NodeCache () {
40
- this (ImmutableSettings .Builder .EMPTY_SETTINGS , new ByteBufferCache (ImmutableSettings .Builder .EMPTY_SETTINGS ), new NoneQueryParserCache ());
41
- }
42
-
43
- @ Inject public NodeCache (Settings settings , ByteBufferCache byteBufferCache , QueryParserCache queryParserCache ) {
42
+ @ Inject public NodeCache (Settings settings , ByteBufferCache byteBufferCache , QueryParserCache queryParserCache , ClusterService clusterService ) {
44
43
super (settings );
44
+ this .clusterService = clusterService ;
45
45
this .byteBufferCache = byteBufferCache ;
46
46
this .queryParserCache = queryParserCache ;
47
+ clusterService .add (this );
47
48
}
48
49
49
50
public void close () {
51
+ clusterService .remove (this );
50
52
byteBufferCache .close ();
53
+ queryParserCache .clear ();
51
54
}
52
55
53
56
public ByteBufferCache byteBuffer () {
@@ -57,4 +60,12 @@ public ByteBufferCache byteBuffer() {
57
60
public QueryParserCache queryParser () {
58
61
return queryParserCache ;
59
62
}
63
+
64
+ // listen on cluster change events to invalidate the query parser cache
65
+ @ Override public void clusterChanged (ClusterChangedEvent event ) {
66
+ // TODO we can do better by detecting just mappings changes
67
+ if (event .metaDataChanged ()) {
68
+ queryParserCache .clear ();
69
+ }
70
+ }
60
71
}
0 commit comments