38
38
import org .apache .lucene .store .AlreadyClosedException ;
39
39
import org .apache .lucene .store .BufferedChecksum ;
40
40
import org .apache .lucene .store .ByteArrayDataInput ;
41
+ import org .apache .lucene .store .ByteBufferIndexInput ;
41
42
import org .apache .lucene .store .ChecksumIndexInput ;
42
43
import org .apache .lucene .store .Directory ;
43
44
import org .apache .lucene .store .FilterDirectory ;
44
45
import org .apache .lucene .store .IOContext ;
45
46
import org .apache .lucene .store .IndexInput ;
46
47
import org .apache .lucene .store .IndexOutput ;
47
48
import org .apache .lucene .store .Lock ;
49
+ import org .apache .lucene .store .RandomAccessInput ;
48
50
import org .apache .lucene .store .SimpleFSDirectory ;
49
51
import org .apache .lucene .util .ArrayUtil ;
50
52
import org .apache .lucene .util .BytesRef ;
95
97
import java .util .Iterator ;
96
98
import java .util .List ;
97
99
import java .util .Map ;
100
+ import java .util .Set ;
98
101
import java .util .concurrent .TimeUnit ;
99
102
import java .util .concurrent .atomic .AtomicBoolean ;
100
103
import java .util .concurrent .locks .ReentrantReadWriteLock ;
126
129
* </pre>
127
130
*/
128
131
public class Store extends AbstractIndexShardComponent implements Closeable , RefCounted {
132
+ /**
133
+ * This is an escape hatch for lucenes internal optimization that checks if the IndexInput is an instance of ByteBufferIndexInput
134
+ * and if that's the case doesn't load the term dictionary into ram but loads it off disk iff the fields is not an ID like field.
135
+ * Since this optimization has been added very late in the release processes we add this setting to allow users to opt-out of
136
+ * this by exploiting lucene internals and wrapping the IndexInput in a simple delegate.
137
+ */
138
+ public static final Setting <Boolean > FORCE_RAM_TERM_DICT = Setting .boolSetting ("index.force_memory_term_dictionary" , false ,
139
+ Property .IndexScope );
129
140
static final String CODEC = "store" ;
130
141
static final int VERSION_WRITE_THROWABLE = 2 ; // we write throwable since 2.0
131
142
static final int VERSION_STACK_TRACE = 1 ; // we write the stack trace too since 1.4.0
@@ -160,7 +171,8 @@ public Store(ShardId shardId, IndexSettings indexSettings, Directory directory,
160
171
final TimeValue refreshInterval = indexSettings .getValue (INDEX_STORE_STATS_REFRESH_INTERVAL_SETTING );
161
172
logger .debug ("store stats are refreshed with refresh_interval [{}]" , refreshInterval );
162
173
ByteSizeCachingDirectory sizeCachingDir = new ByteSizeCachingDirectory (directory , refreshInterval );
163
- this .directory = new StoreDirectory (sizeCachingDir , Loggers .getLogger ("index.store.deletes" , shardId ));
174
+ this .directory = new StoreDirectory (sizeCachingDir , Loggers .getLogger ("index.store.deletes" , shardId ),
175
+ indexSettings .getValue (FORCE_RAM_TERM_DICT ));
164
176
this .shardLock = shardLock ;
165
177
this .onClose = onClose ;
166
178
@@ -700,10 +712,12 @@ public int refCount() {
700
712
static final class StoreDirectory extends FilterDirectory {
701
713
702
714
private final Logger deletesLogger ;
715
+ private final boolean forceRamTermDict ;
703
716
704
- StoreDirectory (ByteSizeCachingDirectory delegateDirectory , Logger deletesLogger ) {
717
+ StoreDirectory (ByteSizeCachingDirectory delegateDirectory , Logger deletesLogger , boolean forceRamTermDict ) {
705
718
super (delegateDirectory );
706
719
this .deletesLogger = deletesLogger ;
720
+ this .forceRamTermDict = forceRamTermDict ;
707
721
}
708
722
709
723
/** Estimate the cumulative size of all files in this directory in bytes. */
@@ -730,6 +744,18 @@ private void innerClose() throws IOException {
730
744
super .close ();
731
745
}
732
746
747
+ @ Override
748
+ public IndexInput openInput (String name , IOContext context ) throws IOException {
749
+ IndexInput input = super .openInput (name , context );
750
+ if (name .endsWith (".tip" ) || name .endsWith (".cfs" )) {
751
+ // only do this if we are reading cfs or tip file - all other files don't need this.
752
+ if (forceRamTermDict && input instanceof ByteBufferIndexInput ) {
753
+ return new DeoptimizingIndexInput (input .toString (), input );
754
+ }
755
+ }
756
+ return input ;
757
+ }
758
+
733
759
@ Override
734
760
public String toString () {
735
761
return "store(" + in .toString () + ")" ;
@@ -1604,4 +1630,126 @@ private static IndexWriterConfig newIndexWriterConfig() {
1604
1630
.setMergePolicy (NoMergePolicy .INSTANCE );
1605
1631
}
1606
1632
1633
+ /**
1634
+ * see {@link #FORCE_RAM_TERM_DICT} for details
1635
+ */
1636
+ private static final class DeoptimizingIndexInput extends IndexInput {
1637
+
1638
+ private final IndexInput in ;
1639
+
1640
+ private DeoptimizingIndexInput (String resourceDescription , IndexInput in ) {
1641
+ super (resourceDescription );
1642
+ this .in = in ;
1643
+ }
1644
+
1645
+ @ Override
1646
+ public IndexInput clone () {
1647
+ return new DeoptimizingIndexInput (toString (), in .clone ());
1648
+ }
1649
+
1650
+ @ Override
1651
+ public void close () throws IOException {
1652
+ in .close ();
1653
+ }
1654
+
1655
+ @ Override
1656
+ public long getFilePointer () {
1657
+ return in .getFilePointer ();
1658
+ }
1659
+
1660
+ @ Override
1661
+ public void seek (long pos ) throws IOException {
1662
+ in .seek (pos );
1663
+ }
1664
+
1665
+ @ Override
1666
+ public long length () {
1667
+ return in .length ();
1668
+ }
1669
+
1670
+ @ Override
1671
+ public String toString () {
1672
+ return in .toString ();
1673
+ }
1674
+
1675
+ @ Override
1676
+ public IndexInput slice (String sliceDescription , long offset , long length ) throws IOException {
1677
+ return new DeoptimizingIndexInput (sliceDescription , in .slice (sliceDescription , offset , length ));
1678
+ }
1679
+
1680
+ @ Override
1681
+ public RandomAccessInput randomAccessSlice (long offset , long length ) throws IOException {
1682
+ return in .randomAccessSlice (offset , length );
1683
+ }
1684
+
1685
+ @ Override
1686
+ public byte readByte () throws IOException {
1687
+ return in .readByte ();
1688
+ }
1689
+
1690
+ @ Override
1691
+ public void readBytes (byte [] b , int offset , int len ) throws IOException {
1692
+ in .readBytes (b , offset , len );
1693
+ }
1694
+
1695
+ @ Override
1696
+ public void readBytes (byte [] b , int offset , int len , boolean useBuffer ) throws IOException {
1697
+ in .readBytes (b , offset , len , useBuffer );
1698
+ }
1699
+
1700
+ @ Override
1701
+ public short readShort () throws IOException {
1702
+ return in .readShort ();
1703
+ }
1704
+
1705
+ @ Override
1706
+ public int readInt () throws IOException {
1707
+ return in .readInt ();
1708
+ }
1709
+
1710
+ @ Override
1711
+ public int readVInt () throws IOException {
1712
+ return in .readVInt ();
1713
+ }
1714
+
1715
+ @ Override
1716
+ public int readZInt () throws IOException {
1717
+ return in .readZInt ();
1718
+ }
1719
+
1720
+ @ Override
1721
+ public long readLong () throws IOException {
1722
+ return in .readLong ();
1723
+ }
1724
+
1725
+ @ Override
1726
+ public long readVLong () throws IOException {
1727
+ return in .readVLong ();
1728
+ }
1729
+
1730
+ @ Override
1731
+ public long readZLong () throws IOException {
1732
+ return in .readZLong ();
1733
+ }
1734
+
1735
+ @ Override
1736
+ public String readString () throws IOException {
1737
+ return in .readString ();
1738
+ }
1739
+
1740
+ @ Override
1741
+ public Map <String , String > readMapOfStrings () throws IOException {
1742
+ return in .readMapOfStrings ();
1743
+ }
1744
+
1745
+ @ Override
1746
+ public Set <String > readSetOfStrings () throws IOException {
1747
+ return in .readSetOfStrings ();
1748
+ }
1749
+
1750
+ @ Override
1751
+ public void skipBytes (long numBytes ) throws IOException {
1752
+ in .skipBytes (numBytes );
1753
+ }
1754
+ }
1607
1755
}
0 commit comments