2020package org .apache .iotdb .db .storageengine .dataregion .read .control ;
2121
2222import org .apache .iotdb .commons .utils .TestOnly ;
23+ import org .apache .iotdb .db .storageengine .dataregion .tsfile .TsFileID ;
2324import org .apache .iotdb .db .storageengine .dataregion .tsfile .TsFileResource ;
2425
2526import org .apache .tsfile .common .conf .TSFileConfig ;
@@ -58,25 +59,25 @@ public class FileReaderManager {
5859 * the key of closedFileReaderMap is the file path and the value of closedFileReaderMap is the
5960 * corresponding reader.
6061 */
61- private Map <String , TsFileSequenceReader > closedFileReaderMap ;
62+ private Map <TsFileID , TsFileSequenceReader > closedFileReaderMap ;
6263
6364 /**
6465 * the key of unclosedFileReaderMap is the file path and the value of unclosedFileReaderMap is the
6566 * corresponding reader.
6667 */
67- private Map <String , TsFileSequenceReader > unclosedFileReaderMap ;
68+ private Map <TsFileID , TsFileSequenceReader > unclosedFileReaderMap ;
6869
6970 /**
7071 * the key of closedFileReaderMap is the file path and the value of closedFileReaderMap is the
7172 * file's reference count.
7273 */
73- private Map <String , AtomicInteger > closedReferenceMap ;
74+ private Map <TsFileID , AtomicInteger > closedReferenceMap ;
7475
7576 /**
7677 * the key of unclosedFileReaderMap is the file path and the value of unclosedFileReaderMap is the
7778 * file's reference count.
7879 */
79- private Map <String , AtomicInteger > unclosedReferenceMap ;
80+ private Map <TsFileID , AtomicInteger > unclosedReferenceMap ;
8081
8182 private FileReaderManager () {
8283 closedFileReaderMap = new ConcurrentHashMap <>();
@@ -89,14 +90,14 @@ public static FileReaderManager getInstance() {
8990 return FileReaderManagerHelper .INSTANCE ;
9091 }
9192
92- public synchronized void closeFileAndRemoveReader (String filePath ) throws IOException {
93- closedReferenceMap .remove (filePath );
94- TsFileSequenceReader reader = closedFileReaderMap .remove (filePath );
93+ public synchronized void closeFileAndRemoveReader (TsFileID tsFileID ) throws IOException {
94+ closedReferenceMap .remove (tsFileID );
95+ TsFileSequenceReader reader = closedFileReaderMap .remove (tsFileID );
9596 if (reader != null ) {
9697 reader .close ();
9798 }
98- unclosedReferenceMap .remove (filePath );
99- reader = unclosedFileReaderMap .remove (filePath );
99+ unclosedReferenceMap .remove (tsFileID );
100+ reader = unclosedFileReaderMap .remove (tsFileID );
100101 if (reader != null ) {
101102 reader .close ();
102103 }
@@ -107,35 +108,38 @@ public synchronized void closeFileAndRemoveReader(String filePath) throws IOExce
107108 * exists, just get it from closedFileReaderMap or unclosedFileReaderMap depending on isClosing .
108109 * Otherwise a new reader will be created and cached.
109110 *
110- * @param filePath the path of the file, of which the reader is desired.
111+ * @param filePath the path of the tsfile
112+ * @param tsFileID the id of the tsfile, of which the reader is desired.
111113 * @param isClosed whether the corresponding file still receives insertions or not.
112114 * @return the reader of the file specified by filePath.
113115 * @throws IOException when reader cannot be created.
114116 */
115117 @ SuppressWarnings ("squid:S2095" )
116- public synchronized TsFileSequenceReader get (String filePath , boolean isClosed )
118+ public synchronized TsFileSequenceReader get (String filePath , TsFileID tsFileID , boolean isClosed )
117119 throws IOException {
118- return get (filePath , isClosed , null );
120+ return get (filePath , tsFileID , isClosed , null );
119121 }
120122
121123 /**
122124 * Get the reader of the file(tsfile or unseq tsfile) indicated by filePath. If the reader already
123125 * exists, just get it from closedFileReaderMap or unclosedFileReaderMap depending on isClosing .
124126 * Otherwise a new reader will be created and cached.
125127 *
126- * @param filePath the path of the file, of which the reader is desired.
128+ * @param filePath the path of the tsfile
129+ * @param tsFileID the id of the tsfile, of which the reader is desired.
127130 * @param isClosed whether the corresponding file still receives insertions or not.
128131 * @param ioSizeRecorder can be null
129132 * @return the reader of the file specified by filePath.
130133 * @throws IOException when reader cannot be created.
131134 */
132135 @ SuppressWarnings ("squid:S2095" )
133136 public synchronized TsFileSequenceReader get (
134- String filePath , boolean isClosed , LongConsumer ioSizeRecorder ) throws IOException {
137+ String filePath , TsFileID tsFileID , boolean isClosed , LongConsumer ioSizeRecorder )
138+ throws IOException {
135139
136- Map <String , TsFileSequenceReader > readerMap =
140+ Map <TsFileID , TsFileSequenceReader > readerMap =
137141 !isClosed ? unclosedFileReaderMap : closedFileReaderMap ;
138- if (!readerMap .containsKey (filePath )) {
142+ if (!readerMap .containsKey (tsFileID )) {
139143 int currentOpenedReaderCount = readerMap .size ();
140144 if (currentOpenedReaderCount >= MAX_CACHED_FILE_SIZE
141145 && (currentOpenedReaderCount % PRINT_INTERVAL == 0 )) {
@@ -156,11 +160,11 @@ public synchronized TsFileSequenceReader get(
156160 "The version of TsFile %s is not correct: %s" , filePath , versionNumber ));
157161 }
158162 }
159- readerMap .put (filePath , tsFileReader );
163+ readerMap .put (tsFileID , tsFileReader );
160164 return tsFileReader ;
161165 }
162166
163- return readerMap .get (filePath );
167+ return readerMap .get (tsFileID );
164168 }
165169
166170 /**
@@ -172,11 +176,11 @@ public void increaseFileReaderReference(TsFileResource tsFile, boolean isClosed)
172176 synchronized (this ) {
173177 if (!isClosed ) {
174178 unclosedReferenceMap
175- .computeIfAbsent (tsFile .getTsFilePath (), k -> new AtomicInteger ())
179+ .computeIfAbsent (tsFile .getTsFileID (), k -> new AtomicInteger ())
176180 .getAndIncrement ();
177181 } else {
178182 closedReferenceMap
179- .computeIfAbsent (tsFile .getTsFilePath (), k -> new AtomicInteger ())
183+ .computeIfAbsent (tsFile .getTsFileID (), k -> new AtomicInteger ())
180184 .getAndIncrement ();
181185 }
182186 }
@@ -188,38 +192,39 @@ public void increaseFileReaderReference(TsFileResource tsFile, boolean isClosed)
188192 */
189193 public void decreaseFileReaderReference (TsFileResource tsFile , boolean isClosed ) {
190194 synchronized (this ) {
191- if (!isClosed && unclosedReferenceMap .containsKey (tsFile .getTsFilePath ())) {
192- if (unclosedReferenceMap .get (tsFile .getTsFilePath ()).decrementAndGet () == 0 ) {
193- closeUnUsedReaderAndRemoveRef (tsFile .getTsFilePath (), false );
195+ if (!isClosed && unclosedReferenceMap .containsKey (tsFile .getTsFileID ())) {
196+ if (unclosedReferenceMap .get (tsFile .getTsFileID ()).decrementAndGet () == 0 ) {
197+ closeUnUsedReaderAndRemoveRef (tsFile .getTsFilePath (), tsFile . getTsFileID (), false );
194198 }
195- } else if (closedReferenceMap .containsKey (tsFile .getTsFilePath ())
196- && (closedReferenceMap .get (tsFile .getTsFilePath ()).decrementAndGet () == 0 )) {
197- closeUnUsedReaderAndRemoveRef (tsFile .getTsFilePath (), true );
199+ } else if (closedReferenceMap .containsKey (tsFile .getTsFileID ())
200+ && (closedReferenceMap .get (tsFile .getTsFileID ()).decrementAndGet () == 0 )) {
201+ closeUnUsedReaderAndRemoveRef (tsFile .getTsFilePath (), tsFile . getTsFileID (), true );
198202 }
199203 }
200204 tsFile .readUnlock ();
201205 }
202206
203- private void closeUnUsedReaderAndRemoveRef (String tsFilePath , boolean isClosed ) {
204- Map <String , TsFileSequenceReader > readerMap =
207+ private void closeUnUsedReaderAndRemoveRef (
208+ String tsFilePath , TsFileID tsFileID , boolean isClosed ) {
209+ Map <TsFileID , TsFileSequenceReader > readerMap =
205210 isClosed ? closedFileReaderMap : unclosedFileReaderMap ;
206- Map <String , AtomicInteger > refMap = isClosed ? closedReferenceMap : unclosedReferenceMap ;
211+ Map <TsFileID , AtomicInteger > refMap = isClosed ? closedReferenceMap : unclosedReferenceMap ;
207212 synchronized (this ) {
208213 // check ref num again
209- if (refMap .get (tsFilePath ).get () != 0 ) {
214+ if (refMap .get (tsFileID ).get () != 0 ) {
210215 return ;
211216 }
212217
213- TsFileSequenceReader reader = readerMap .get (tsFilePath );
218+ TsFileSequenceReader reader = readerMap .get (tsFileID );
214219 if (reader != null ) {
215220 try {
216221 reader .close ();
217222 } catch (IOException e ) {
218223 logger .error ("Can not close TsFileSequenceReader {} !" , reader .getFileName (), e );
219224 }
220225 }
221- readerMap .remove (tsFilePath );
222- refMap .remove (tsFilePath );
226+ readerMap .remove (tsFileID );
227+ refMap .remove (tsFileID );
223228 if (resourceLogger .isDebugEnabled ()) {
224229 resourceLogger .debug ("{} TsFileReader is closed because of no reference." , tsFilePath );
225230 }
@@ -233,10 +238,10 @@ private void closeUnUsedReaderAndRemoveRef(String tsFilePath, boolean isClosed)
233238 * @throws IOException if failed to close file handlers, IOException will be thrown
234239 */
235240 public synchronized void closeAndRemoveAllOpenedReaders () throws IOException {
236- Iterator <Map .Entry <String , TsFileSequenceReader >> iterator =
241+ Iterator <Map .Entry <TsFileID , TsFileSequenceReader >> iterator =
237242 closedFileReaderMap .entrySet ().iterator ();
238243 while (iterator .hasNext ()) {
239- Map .Entry <String , TsFileSequenceReader > entry = iterator .next ();
244+ Map .Entry <TsFileID , TsFileSequenceReader > entry = iterator .next ();
240245 entry .getValue ().close ();
241246 if (resourceLogger .isDebugEnabled ()) {
242247 resourceLogger .debug ("{} closedTsFileReader is closed." , entry .getKey ());
@@ -246,7 +251,7 @@ public synchronized void closeAndRemoveAllOpenedReaders() throws IOException {
246251 }
247252 iterator = unclosedFileReaderMap .entrySet ().iterator ();
248253 while (iterator .hasNext ()) {
249- Map .Entry <String , TsFileSequenceReader > entry = iterator .next ();
254+ Map .Entry <TsFileID , TsFileSequenceReader > entry = iterator .next ();
250255 entry .getValue ().close ();
251256 if (resourceLogger .isDebugEnabled ()) {
252257 resourceLogger .debug ("{} unclosedTsFileReader is closed." , entry .getKey ());
@@ -258,17 +263,17 @@ public synchronized void closeAndRemoveAllOpenedReaders() throws IOException {
258263
259264 /** This method is only for unit tests. */
260265 public synchronized boolean contains (TsFileResource tsFile , boolean isClosed ) {
261- return (isClosed && closedFileReaderMap .containsKey (tsFile .getTsFilePath ()))
262- || (!isClosed && unclosedFileReaderMap .containsKey (tsFile .getTsFilePath ()));
266+ return (isClosed && closedFileReaderMap .containsKey (tsFile .getTsFileID ()))
267+ || (!isClosed && unclosedFileReaderMap .containsKey (tsFile .getTsFileID ()));
263268 }
264269
265270 @ TestOnly
266- public Map <String , TsFileSequenceReader > getClosedFileReaderMap () {
271+ public Map <TsFileID , TsFileSequenceReader > getClosedFileReaderMap () {
267272 return closedFileReaderMap ;
268273 }
269274
270275 @ TestOnly
271- public Map <String , TsFileSequenceReader > getUnclosedFileReaderMap () {
276+ public Map <TsFileID , TsFileSequenceReader > getUnclosedFileReaderMap () {
272277 return unclosedFileReaderMap ;
273278 }
274279
0 commit comments