5
5
*/
6
6
package org .elasticsearch .index .store ;
7
7
8
- import org .apache .lucene .store .BaseDirectory ;
9
8
import org .apache .lucene .store .BufferedIndexInput ;
10
9
import org .apache .lucene .store .Directory ;
11
10
import org .apache .lucene .store .IOContext ;
12
11
import org .apache .lucene .store .IndexInput ;
13
- import org .apache .lucene .store .IndexOutput ;
14
- import org .apache .lucene .store .SingleInstanceLockFactory ;
15
12
import org .elasticsearch .common .blobstore .BlobContainer ;
16
13
import org .elasticsearch .index .IndexSettings ;
17
14
import org .elasticsearch .index .shard .ShardPath ;
18
15
import org .elasticsearch .index .snapshots .blobstore .BlobStoreIndexShardSnapshot ;
19
- import org .elasticsearch .index .snapshots .blobstore .BlobStoreIndexShardSnapshot .FileInfo ;
20
16
import org .elasticsearch .repositories .IndexId ;
21
17
import org .elasticsearch .repositories .RepositoriesService ;
22
18
import org .elasticsearch .repositories .Repository ;
25
21
import org .elasticsearch .xpack .searchablesnapshots .cache .CacheDirectory ;
26
22
import org .elasticsearch .xpack .searchablesnapshots .cache .CacheService ;
27
23
28
- import java .io .FileNotFoundException ;
29
24
import java .io .IOException ;
30
25
import java .nio .file .Path ;
31
- import java .util .Collection ;
32
- import java .util .Objects ;
33
- import java .util .Set ;
34
26
import java .util .function .LongSupplier ;
35
27
36
28
import static org .elasticsearch .xpack .searchablesnapshots .SearchableSnapshots .SNAPSHOT_CACHE_ENABLED_SETTING ;
50
42
* shard files and what it stored in the snapshot the {@link BlobStoreIndexShardSnapshot} is used to map a physical file name as expected by
51
43
* Lucene with the one (or the ones) corresponding blob(s) in the snapshot.
52
44
*/
53
- public class SearchableSnapshotDirectory extends BaseDirectory {
54
-
55
- private final BlobStoreIndexShardSnapshot snapshot ;
56
- private final BlobContainer blobContainer ;
45
+ public class SearchableSnapshotDirectory extends BaseSearchableSnapshotDirectory {
57
46
58
47
SearchableSnapshotDirectory (final BlobStoreIndexShardSnapshot snapshot , final BlobContainer blobContainer ) {
59
- super (new SingleInstanceLockFactory ());
60
- this .snapshot = Objects .requireNonNull (snapshot );
61
- this .blobContainer = Objects .requireNonNull (blobContainer );
62
- }
63
-
64
- private FileInfo fileInfo (final String name ) throws FileNotFoundException {
65
- return snapshot .indexFiles ().stream ()
66
- .filter (fileInfo -> fileInfo .physicalName ().equals (name ))
67
- .findFirst ()
68
- .orElseThrow (() -> new FileNotFoundException (name ));
69
- }
70
-
71
- @ Override
72
- public String [] listAll () throws IOException {
73
- ensureOpen ();
74
- return snapshot .indexFiles ().stream ()
75
- .map (FileInfo ::physicalName )
76
- .sorted (String ::compareTo )
77
- .toArray (String []::new );
78
- }
79
-
80
- @ Override
81
- public long fileLength (final String name ) throws IOException {
82
- ensureOpen ();
83
- return fileInfo (name ).length ();
48
+ super (blobContainer , snapshot );
84
49
}
85
50
86
51
@ Override
87
52
public IndexInput openInput (final String name , final IOContext context ) throws IOException {
88
53
ensureOpen ();
89
- return new SearchableSnapshotIndexInput (blobContainer , fileInfo (name ), blobContainer .readBlobPreferredLength (),
54
+ return new SearchableSnapshotIndexInput (blobContainer , fileInfo (name ), context , blobContainer .readBlobPreferredLength (),
90
55
BufferedIndexInput .BUFFER_SIZE );
91
56
}
92
57
93
- @ Override
94
- public void close () {
95
- isOpen = false ;
96
- }
97
-
98
58
@ Override
99
59
public String toString () {
100
60
return this .getClass ().getSimpleName () + "@" + snapshot .snapshot () + " lockFactory=" + lockFactory ;
101
61
}
102
62
103
- @ Override
104
- public Set <String > getPendingDeletions () {
105
- throw unsupportedException ();
106
- }
107
-
108
- @ Override
109
- public void sync (Collection <String > names ) {
110
- throw unsupportedException ();
111
- }
112
-
113
- @ Override
114
- public void syncMetaData () {
115
- throw unsupportedException ();
116
- }
117
-
118
- @ Override
119
- public void deleteFile (String name ) {
120
- throw unsupportedException ();
121
- }
122
-
123
- @ Override
124
- public IndexOutput createOutput (String name , IOContext context ) {
125
- throw unsupportedException ();
126
- }
127
-
128
- @ Override
129
- public IndexOutput createTempOutput (String prefix , String suffix , IOContext context ) {
130
- throw unsupportedException ();
131
- }
132
-
133
- @ Override
134
- public void rename (String source , String dest ) {
135
- throw unsupportedException ();
136
- }
137
-
138
- private static UnsupportedOperationException unsupportedException () {
139
- return new UnsupportedOperationException ("Searchable snapshot directory does not support this operation" );
140
- }
141
-
142
63
public static Directory create (RepositoriesService repositories ,
143
64
CacheService cache ,
144
65
IndexSettings indexSettings ,
@@ -158,11 +79,13 @@ public static Directory create(RepositoriesService repositories,
158
79
SNAPSHOT_SNAPSHOT_ID_SETTING .get (indexSettings .getSettings ()));
159
80
final BlobStoreIndexShardSnapshot snapshot = blobStoreRepository .loadShardSnapshot (blobContainer , snapshotId );
160
81
161
- Directory directory = new SearchableSnapshotDirectory ( snapshot , blobContainer ) ;
82
+ final Directory directory ;
162
83
if (SNAPSHOT_CACHE_ENABLED_SETTING .get (indexSettings .getSettings ())) {
163
84
final Path cacheDir = shardPath .getDataPath ().resolve ("snapshots" ).resolve (snapshotId .getUUID ());
164
- directory = new CacheDirectory (directory , cache , cacheDir , snapshotId , indexId , shardPath .getShardId (),
85
+ directory = new CacheDirectory (snapshot , blobContainer , cache , cacheDir , snapshotId , indexId , shardPath .getShardId (),
165
86
currentTimeNanosSupplier );
87
+ } else {
88
+ directory = new SearchableSnapshotDirectory (snapshot , blobContainer );
166
89
}
167
90
return new InMemoryNoOpCommitDirectory (directory );
168
91
}
0 commit comments