Skip to content

Commit c67386f

Browse files
committed
properly invalidate on core closed reader
1 parent af757fd commit c67386f

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Licensed to ElasticSearch and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. ElasticSearch licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.index.fielddata;
21+
22+
import org.elasticsearch.common.io.stream.StreamInput;
23+
import org.elasticsearch.common.io.stream.StreamOutput;
24+
import org.elasticsearch.common.io.stream.Streamable;
25+
import org.elasticsearch.common.unit.ByteSizeValue;
26+
import org.elasticsearch.common.xcontent.ToXContent;
27+
import org.elasticsearch.common.xcontent.XContentBuilder;
28+
import org.elasticsearch.common.xcontent.XContentBuilderString;
29+
30+
import java.io.IOException;
31+
32+
/**
33+
*/
34+
public class FieldDataStats implements Streamable, ToXContent {
35+
36+
long memorySize;
37+
38+
public FieldDataStats() {
39+
40+
}
41+
42+
public FieldDataStats(long memorySize) {
43+
this.memorySize = memorySize;
44+
}
45+
46+
public void add(FieldDataStats stats) {
47+
this.memorySize += stats.memorySize;
48+
}
49+
50+
public long getMemorySizeInBytes() {
51+
return this.memorySize;
52+
}
53+
54+
public ByteSizeValue getMemorySize() {
55+
return new ByteSizeValue(memorySize);
56+
}
57+
58+
@Override
59+
public void readFrom(StreamInput in) throws IOException {
60+
memorySize = in.readVLong();
61+
}
62+
63+
@Override
64+
public void writeTo(StreamOutput out) throws IOException {
65+
out.writeVLong(memorySize);
66+
}
67+
68+
@Override
69+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
70+
builder.startObject(Fields.FIELD_DATA);
71+
builder.field(Fields.MEMORY_SIZE, memorySize);
72+
builder.field(Fields.MEMORY_SIZE_IN_BYTES, getMemorySize().toString());
73+
builder.endObject();
74+
return builder;
75+
}
76+
77+
static final class Fields {
78+
static final XContentBuilderString FIELD_DATA = new XContentBuilderString("field_data");
79+
static final XContentBuilderString MEMORY_SIZE = new XContentBuilderString("memory_size");
80+
static final XContentBuilderString MEMORY_SIZE_IN_BYTES = new XContentBuilderString("memory_size_in_bytes");
81+
}
82+
}

src/main/java/org/elasticsearch/index/fielddata/IndexFieldDataCache.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ public <FD extends AtomicFieldData, IFD extends IndexFieldData<FD>> FD load(fina
5959
return (FD) cache.get(context.reader().getCoreCacheKey(), new Callable<AtomicFieldData>() {
6060
@Override
6161
public AtomicFieldData call() throws Exception {
62+
if (context.reader() instanceof SegmentReader) {
63+
((SegmentReader) context.reader()).addCoreClosedListener(FieldBased.this);
64+
}
6265
return indexFieldData.loadDirect(context);
6366
}
6467
});

src/main/java/org/elasticsearch/index/fielddata/IndexFieldDataService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ public void clearField(String fieldName) {
9696
}
9797
}
9898

99+
public FieldDataStats stats() {
100+
// TODO: compute the memory size here...
101+
return new FieldDataStats();
102+
}
103+
99104
public <IFD extends IndexFieldData> IFD getForField(FieldMapper mapper) {
100105
return getForField(mapper.names(), mapper.fieldDataType2());
101106
}

0 commit comments

Comments
 (0)