1 /////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2009-2014 Alan Wright. All rights reserved.
3 // Distributable under the terms of either the Apache License (Version 2.0)
4 // or the GNU Lesser General Public License.
5 /////////////////////////////////////////////////////////////////////////////
6 
7 #include "LuceneInc.h"
8 #include <boost/algorithm/string.hpp>
9 #include "IndexFileNames.h"
10 #include "SegmentInfo.h"
11 #include "StringUtils.h"
12 
13 namespace Lucene {
14 
~IndexFileNames()15 IndexFileNames::~IndexFileNames() {
16 }
17 
SEGMENTS()18 const String& IndexFileNames::SEGMENTS() {
19     static String _SEGMENTS(L"segments");
20     return _SEGMENTS;
21 }
22 
SEGMENTS_GEN()23 const String& IndexFileNames::SEGMENTS_GEN() {
24     static String _SEGMENTS_GEN(L"segments.gen");
25     return _SEGMENTS_GEN;
26 }
27 
DELETABLE()28 const String& IndexFileNames::DELETABLE() {
29     static String _DELETABLE(L"deletable");
30     return _DELETABLE;
31 }
32 
NORMS_EXTENSION()33 const String& IndexFileNames::NORMS_EXTENSION() {
34     static String _NORMS_EXTENSION(L"nrm");
35     return _NORMS_EXTENSION;
36 }
37 
FREQ_EXTENSION()38 const String& IndexFileNames::FREQ_EXTENSION() {
39     static String _FREQ_EXTENSION(L"frq");
40     return _FREQ_EXTENSION;
41 }
42 
PROX_EXTENSION()43 const String& IndexFileNames::PROX_EXTENSION() {
44     static String _PROX_EXTENSION(L"prx");
45     return _PROX_EXTENSION;
46 }
47 
TERMS_EXTENSION()48 const String& IndexFileNames::TERMS_EXTENSION() {
49     static String _TERMS_EXTENSION(L"tis");
50     return _TERMS_EXTENSION;
51 }
52 
TERMS_INDEX_EXTENSION()53 const String& IndexFileNames::TERMS_INDEX_EXTENSION() {
54     static String _TERMS_INDEX_EXTENSION(L"tii");
55     return _TERMS_INDEX_EXTENSION;
56 }
57 
FIELDS_INDEX_EXTENSION()58 const String& IndexFileNames::FIELDS_INDEX_EXTENSION() {
59     static String _FIELDS_INDEX_EXTENSION(L"fdx");
60     return _FIELDS_INDEX_EXTENSION;
61 }
62 
FIELDS_EXTENSION()63 const String& IndexFileNames::FIELDS_EXTENSION() {
64     static String _FIELDS_EXTENSION(L"fdt");
65     return _FIELDS_EXTENSION;
66 }
67 
VECTORS_FIELDS_EXTENSION()68 const String& IndexFileNames::VECTORS_FIELDS_EXTENSION() {
69     static String _VECTORS_FIELDS_EXTENSION(L"tvf");
70     return _VECTORS_FIELDS_EXTENSION;
71 }
72 
VECTORS_DOCUMENTS_EXTENSION()73 const String& IndexFileNames::VECTORS_DOCUMENTS_EXTENSION() {
74     static String _VECTORS_DOCUMENTS_EXTENSION(L"tvd");
75     return _VECTORS_DOCUMENTS_EXTENSION;
76 }
77 
VECTORS_INDEX_EXTENSION()78 const String& IndexFileNames::VECTORS_INDEX_EXTENSION() {
79     static String _VECTORS_INDEX_EXTENSION(L"tvx");
80     return _VECTORS_INDEX_EXTENSION;
81 }
82 
COMPOUND_FILE_EXTENSION()83 const String& IndexFileNames::COMPOUND_FILE_EXTENSION() {
84     static String _COMPOUND_FILE_EXTENSION(L"cfs");
85     return _COMPOUND_FILE_EXTENSION;
86 }
87 
COMPOUND_FILE_STORE_EXTENSION()88 const String& IndexFileNames::COMPOUND_FILE_STORE_EXTENSION() {
89     static String _COMPOUND_FILE_STORE_EXTENSION(L"cfx");
90     return _COMPOUND_FILE_STORE_EXTENSION;
91 }
92 
DELETES_EXTENSION()93 const String& IndexFileNames::DELETES_EXTENSION() {
94     static String _DELETES_EXTENSION(L"del");
95     return _DELETES_EXTENSION;
96 }
97 
FIELD_INFOS_EXTENSION()98 const String& IndexFileNames::FIELD_INFOS_EXTENSION() {
99     static String _FIELD_INFOS_EXTENSION(L"fnm");
100     return _FIELD_INFOS_EXTENSION;
101 }
102 
PLAIN_NORMS_EXTENSION()103 const String& IndexFileNames::PLAIN_NORMS_EXTENSION() {
104     static String _PLAIN_NORMS_EXTENSION(L"f");
105     return _PLAIN_NORMS_EXTENSION;
106 }
107 
SEPARATE_NORMS_EXTENSION()108 const String& IndexFileNames::SEPARATE_NORMS_EXTENSION() {
109     static String _SEPARATE_NORMS_EXTENSION(L"s");
110     return _SEPARATE_NORMS_EXTENSION;
111 }
112 
GEN_EXTENSION()113 const String& IndexFileNames::GEN_EXTENSION() {
114     static String _GEN_EXTENSION(L"gen");
115     return _GEN_EXTENSION;
116 }
117 
INDEX_EXTENSIONS()118 const HashSet<String> IndexFileNames::INDEX_EXTENSIONS() {
119     static HashSet<String> _INDEX_EXTENSIONS;
120     if (!_INDEX_EXTENSIONS) {
121         _INDEX_EXTENSIONS = HashSet<String>::newInstance();
122         _INDEX_EXTENSIONS.add(COMPOUND_FILE_EXTENSION());
123         _INDEX_EXTENSIONS.add(FIELD_INFOS_EXTENSION());
124         _INDEX_EXTENSIONS.add(FIELDS_INDEX_EXTENSION());
125         _INDEX_EXTENSIONS.add(FIELDS_EXTENSION());
126         _INDEX_EXTENSIONS.add(TERMS_INDEX_EXTENSION());
127         _INDEX_EXTENSIONS.add(TERMS_EXTENSION());
128         _INDEX_EXTENSIONS.add(FREQ_EXTENSION());
129         _INDEX_EXTENSIONS.add(PROX_EXTENSION());
130         _INDEX_EXTENSIONS.add(DELETES_EXTENSION());
131         _INDEX_EXTENSIONS.add(VECTORS_INDEX_EXTENSION());
132         _INDEX_EXTENSIONS.add(VECTORS_DOCUMENTS_EXTENSION());
133         _INDEX_EXTENSIONS.add(VECTORS_FIELDS_EXTENSION());
134         _INDEX_EXTENSIONS.add(GEN_EXTENSION());
135         _INDEX_EXTENSIONS.add(NORMS_EXTENSION());
136         _INDEX_EXTENSIONS.add(COMPOUND_FILE_STORE_EXTENSION());
137     }
138     return _INDEX_EXTENSIONS;
139 };
140 
INDEX_EXTENSIONS_IN_COMPOUND_FILE()141 const HashSet<String> IndexFileNames::INDEX_EXTENSIONS_IN_COMPOUND_FILE() {
142     static HashSet<String> _INDEX_EXTENSIONS_IN_COMPOUND_FILE;
143     if (!_INDEX_EXTENSIONS_IN_COMPOUND_FILE) {
144         _INDEX_EXTENSIONS_IN_COMPOUND_FILE = HashSet<String>::newInstance();
145         _INDEX_EXTENSIONS_IN_COMPOUND_FILE.add(FIELD_INFOS_EXTENSION());
146         _INDEX_EXTENSIONS_IN_COMPOUND_FILE.add(FIELDS_INDEX_EXTENSION());
147         _INDEX_EXTENSIONS_IN_COMPOUND_FILE.add(FIELDS_EXTENSION());
148         _INDEX_EXTENSIONS_IN_COMPOUND_FILE.add(TERMS_INDEX_EXTENSION());
149         _INDEX_EXTENSIONS_IN_COMPOUND_FILE.add(TERMS_EXTENSION());
150         _INDEX_EXTENSIONS_IN_COMPOUND_FILE.add(FREQ_EXTENSION());
151         _INDEX_EXTENSIONS_IN_COMPOUND_FILE.add(PROX_EXTENSION());
152         _INDEX_EXTENSIONS_IN_COMPOUND_FILE.add(VECTORS_INDEX_EXTENSION());
153         _INDEX_EXTENSIONS_IN_COMPOUND_FILE.add(VECTORS_DOCUMENTS_EXTENSION());
154         _INDEX_EXTENSIONS_IN_COMPOUND_FILE.add(VECTORS_FIELDS_EXTENSION());
155         _INDEX_EXTENSIONS_IN_COMPOUND_FILE.add(NORMS_EXTENSION());
156     }
157     return _INDEX_EXTENSIONS_IN_COMPOUND_FILE;
158 };
159 
STORE_INDEX_EXTENSIONS()160 const HashSet<String> IndexFileNames::STORE_INDEX_EXTENSIONS() {
161     static HashSet<String> _STORE_INDEX_EXTENSIONS;
162     if (!_STORE_INDEX_EXTENSIONS) {
163         _STORE_INDEX_EXTENSIONS = HashSet<String>::newInstance();
164         _STORE_INDEX_EXTENSIONS.add(VECTORS_INDEX_EXTENSION());
165         _STORE_INDEX_EXTENSIONS.add(VECTORS_FIELDS_EXTENSION());
166         _STORE_INDEX_EXTENSIONS.add(VECTORS_DOCUMENTS_EXTENSION());
167         _STORE_INDEX_EXTENSIONS.add(FIELDS_INDEX_EXTENSION());
168         _STORE_INDEX_EXTENSIONS.add(FIELDS_EXTENSION());
169     }
170     return _STORE_INDEX_EXTENSIONS;
171 };
172 
NON_STORE_INDEX_EXTENSIONS()173 const HashSet<String> IndexFileNames::NON_STORE_INDEX_EXTENSIONS() {
174     static HashSet<String> _NON_STORE_INDEX_EXTENSIONS;
175     if (!_NON_STORE_INDEX_EXTENSIONS) {
176         _NON_STORE_INDEX_EXTENSIONS = HashSet<String>::newInstance();
177         _NON_STORE_INDEX_EXTENSIONS.add(FIELD_INFOS_EXTENSION());
178         _NON_STORE_INDEX_EXTENSIONS.add(FREQ_EXTENSION());
179         _NON_STORE_INDEX_EXTENSIONS.add(PROX_EXTENSION());
180         _NON_STORE_INDEX_EXTENSIONS.add(TERMS_EXTENSION());
181         _NON_STORE_INDEX_EXTENSIONS.add(TERMS_INDEX_EXTENSION());
182         _NON_STORE_INDEX_EXTENSIONS.add(NORMS_EXTENSION());
183     }
184     return _NON_STORE_INDEX_EXTENSIONS;
185 };
186 
COMPOUND_EXTENSIONS()187 const HashSet<String> IndexFileNames::COMPOUND_EXTENSIONS() {
188     static HashSet<String> _COMPOUND_EXTENSIONS;
189     if (!_COMPOUND_EXTENSIONS) {
190         _COMPOUND_EXTENSIONS = HashSet<String>::newInstance();
191         _COMPOUND_EXTENSIONS.add(FIELD_INFOS_EXTENSION());
192         _COMPOUND_EXTENSIONS.add(FREQ_EXTENSION());
193         _COMPOUND_EXTENSIONS.add(PROX_EXTENSION());
194         _COMPOUND_EXTENSIONS.add(FIELDS_INDEX_EXTENSION());
195         _COMPOUND_EXTENSIONS.add(FIELDS_EXTENSION());
196         _COMPOUND_EXTENSIONS.add(TERMS_INDEX_EXTENSION());
197         _COMPOUND_EXTENSIONS.add(TERMS_EXTENSION());
198     }
199     return _COMPOUND_EXTENSIONS;
200 };
201 
VECTOR_EXTENSIONS()202 const HashSet<String> IndexFileNames::VECTOR_EXTENSIONS() {
203     static HashSet<String> _VECTOR_EXTENSIONS;
204     if (!_VECTOR_EXTENSIONS) {
205         _VECTOR_EXTENSIONS = HashSet<String>::newInstance();
206         _VECTOR_EXTENSIONS.add(VECTORS_INDEX_EXTENSION());
207         _VECTOR_EXTENSIONS.add(VECTORS_DOCUMENTS_EXTENSION());
208         _VECTOR_EXTENSIONS.add(VECTORS_FIELDS_EXTENSION());
209     }
210     return _VECTOR_EXTENSIONS;
211 };
212 
fileNameFromGeneration(const String & base,const String & extension,int64_t gen)213 String IndexFileNames::fileNameFromGeneration(const String& base, const String& extension, int64_t gen) {
214     if (gen == SegmentInfo::NO) {
215         return L"";
216     } else if (gen == SegmentInfo::WITHOUT_GEN) {
217         return base + extension;
218     } else {
219         return base + L"_" + StringUtils::toString(gen, StringUtils::CHARACTER_MAX_RADIX) + extension;
220     }
221 }
222 
isDocStoreFile(const String & fileName)223 bool IndexFileNames::isDocStoreFile(const String& fileName) {
224     if (boost::ends_with(fileName, COMPOUND_FILE_STORE_EXTENSION())) {
225         return true;
226     }
227     for (HashSet<String>::iterator index = STORE_INDEX_EXTENSIONS().begin(); index != STORE_INDEX_EXTENSIONS().end(); ++index) {
228         if (boost::ends_with(fileName, *index)) {
229             return true;
230         }
231     }
232     return false;
233 }
234 
segmentFileName(const String & segmentName,const String & ext)235 String IndexFileNames::segmentFileName(const String& segmentName, const String& ext) {
236     return segmentName + L"." + ext;
237 }
238 
239 }
240