@@ -48,8 +48,8 @@ public class PipeTsFileResourceManager {
4848 // This is used to hold the assigner pinned tsFiles.
4949 // Also, it is used to provide metadata cache of the tsFile, and is shared by all the pipe's
5050 // tsFiles.
51- private final Map <String , PipeTsFileMemResource > hardlinkOrCopiedFileToTsFileMemResourceMap =
52- new ConcurrentHashMap <>();
51+ private final Map <String , PipeTsFilePublicResource >
52+ hardlinkOrCopiedFileToTsFilePublicResourceMap = new ConcurrentHashMap <>();
5353
5454 // PipeName -> TsFilePath -> PipeTsFileResource
5555 private final Map <String , Map <String , PipeTsFileResource >>
@@ -81,7 +81,7 @@ public File increaseFileReference(
8181 // just increase reference count and return it
8282 segmentLock .lock (file );
8383 try {
84- if (increaseReferenceIfExists (file , isTsFile , pipeName )) {
84+ if (increaseReferenceIfExists (file , pipeName )) {
8585 return file ;
8686 }
8787 } finally {
@@ -93,11 +93,8 @@ public File increaseFileReference(
9393 final File hardlinkOrCopiedFile = getHardlinkOrCopiedFileInPipeDir (file , pipeName );
9494 segmentLock .lock (hardlinkOrCopiedFile );
9595 try {
96- if (increaseReferenceIfExists (hardlinkOrCopiedFile , isTsFile , pipeName )) {
97- return hardlinkOrCopiedFileToPipeTsFileResourceMap
98- .computeIfAbsent (pipeName , pipe -> new ConcurrentHashMap <>())
99- .get (hardlinkOrCopiedFile .getPath ())
100- .getFile ();
96+ if (increaseReferenceIfExists (hardlinkOrCopiedFile , pipeName )) {
97+ return getResourceMap (pipeName ).get (hardlinkOrCopiedFile .getPath ()).getFile ();
10198 }
10299
103100 // If the file is a tsfile, create a hardlink in pipe dir and will return it.
@@ -110,53 +107,55 @@ public File increaseFileReference(
110107 // If the file is not a hardlink or copied file, and there is no related hardlink or copied
111108 // file in pipe dir, create a hardlink or copy it to pipe dir, maintain a reference count for
112109 // the hardlink or copied file, and return the hardlink or copied file.
113- hardlinkOrCopiedFileToPipeTsFileResourceMap
114- .computeIfAbsent (pipeName , pipe -> new ConcurrentHashMap <>())
115- .put (resultFile .getPath (), new PipeTsFileResource (resultFile ));
110+ if (Objects .nonNull (pipeName )) {
111+ hardlinkOrCopiedFileToPipeTsFileResourceMap
112+ .computeIfAbsent (pipeName , k -> new ConcurrentHashMap <>())
113+ .put (resultFile .getPath (), new PipeTsFileResource (resultFile ));
114+ } else {
115+ hardlinkOrCopiedFileToTsFilePublicResourceMap .put (
116+ resultFile .getPath (), new PipeTsFilePublicResource (resultFile ));
117+ }
116118
117- increaseMemReference (resultFile , isTsFile );
119+ increasePublicReference (resultFile , pipeName );
118120
119121 return resultFile ;
120122 } finally {
121123 segmentLock .unlock (hardlinkOrCopiedFile );
122124 }
123125 }
124126
125- private boolean increaseReferenceIfExists (
126- final File file , final boolean isTsFile , final @ Nullable String pipeName ) {
127+ private boolean increaseReferenceIfExists (final File file , final @ Nullable String pipeName ) {
127128 final String path = file .getPath ();
128- final PipeTsFileResource resource =
129- hardlinkOrCopiedFileToPipeTsFileResourceMap
130- .computeIfAbsent (pipeName , pipe -> new ConcurrentHashMap <>())
131- .get (path );
129+ final PipeTsFileResource resource = getResourceMap (pipeName ).get (path );
132130 if (resource != null ) {
133131 resource .increaseReferenceCount ();
134- increaseMemReference (file , isTsFile );
132+ increasePublicReference (file , pipeName );
135133 return true ;
136134 }
137135 return false ;
138136 }
139137
140- private void increaseMemReference (final File file , final boolean isTsFile ) {
141- if (! isTsFile ) {
138+ private void increasePublicReference (final File file , final String pipeName ) {
139+ if (Objects . isNull ( pipeName ) ) {
142140 return ;
143141 }
144142 // Increase the assigner's file to avoid hard-link or memory cache cleaning
145143 // Note that it does not exist for historical files
146- hardlinkOrCopiedFileToTsFileMemResourceMap .compute (
147- getCommonFilePath (file ),
144+ final String path = getCommonFilePath (file );
145+ hardlinkOrCopiedFileToTsFilePublicResourceMap .compute (
146+ path ,
148147 (k , v ) -> {
149148 if (Objects .isNull (v )) {
150- return new PipeTsFileMemResource ( );
149+ return new PipeTsFilePublicResource ( new File ( path ) );
151150 } else {
152151 v .increaseReferenceCount ();
153152 return v ;
154153 }
155154 });
156155 }
157156
158- public static File getHardlinkOrCopiedFileInPipeDir (final File file , final String pipeName )
159- throws IOException {
157+ public static File getHardlinkOrCopiedFileInPipeDir (
158+ final File file , final @ Nullable String pipeName ) throws IOException {
160159 try {
161160 return new File (getPipeTsFileDirPath (file , pipeName ), getRelativeFilePath (file ));
162161 } catch (final Exception e ) {
@@ -169,7 +168,8 @@ public static File getHardlinkOrCopiedFileInPipeDir(final File file, final Strin
169168 }
170169 }
171170
172- private static String getPipeTsFileDirPath (File file , final String pipeName ) throws IOException {
171+ private static String getPipeTsFileDirPath (File file , final @ Nullable String pipeName )
172+ throws IOException {
173173 while (!file .getName ().equals (IoTDBConstant .SEQUENCE_FOLDER_NAME )
174174 && !file .getName ().equals (IoTDBConstant .UNSEQUENCE_FOLDER_NAME )
175175 && !file .getName ().equals (PipeConfig .getInstance ().getPipeHardlinkBaseDirName ())) {
@@ -206,33 +206,31 @@ private static String getRelativeFilePath(File file) {
206206 * @param hardlinkOrCopiedFile the copied or hard-linked file
207207 */
208208 public void decreaseFileReference (
209- final File hardlinkOrCopiedFile , final @ Nonnull String pipeName ) {
209+ final File hardlinkOrCopiedFile , final @ Nullable String pipeName ) {
210210 segmentLock .lock (hardlinkOrCopiedFile );
211211 try {
212212 final String filePath = hardlinkOrCopiedFile .getPath ();
213- final PipeTsFileResource resource =
214- hardlinkOrCopiedFileToPipeTsFileResourceMap
215- .computeIfAbsent (pipeName , pipe -> new ConcurrentHashMap <>())
216- .get (filePath );
213+ final PipeTsFileResource resource = getResourceMap (pipeName ).get (filePath );
217214 if (resource != null && resource .decreaseReferenceCount ()) {
218- hardlinkOrCopiedFileToPipeTsFileResourceMap
219- .computeIfAbsent (pipeName , pipe -> new ConcurrentHashMap <>())
220- .remove (filePath );
215+ getResourceMap (pipeName ).remove (filePath );
221216 }
222217 // Decrease the assigner's file to clear hard-link and memory cache
223218 // Note that it does not exist for historical files
224- decreaseMemReferenceIfExists (hardlinkOrCopiedFile );
219+ decreasePublicReferenceIfExists (hardlinkOrCopiedFile , pipeName );
225220 } finally {
226221 segmentLock .unlock (hardlinkOrCopiedFile );
227222 }
228223 }
229224
230- private void decreaseMemReferenceIfExists (final File file ) {
225+ private void decreasePublicReferenceIfExists (final File file , final @ Nullable String pipeName ) {
226+ if (Objects .isNull (pipeName )) {
227+ return ;
228+ }
231229 // Increase the assigner's file to avoid hard-link or memory cache cleaning
232230 // Note that it does not exist for historical files
233231 final String commonFilePath = getCommonFilePath (file );
234- if (hardlinkOrCopiedFileToTsFileMemResourceMap .containsKey (commonFilePath )
235- && hardlinkOrCopiedFileToTsFileMemResourceMap
232+ if (hardlinkOrCopiedFileToTsFilePublicResourceMap .containsKey (commonFilePath )
233+ && hardlinkOrCopiedFileToTsFilePublicResourceMap
236234 .get (commonFilePath )
237235 .decreaseReferenceCount ()) {
238236 hardlinkOrCopiedFileToPipeTsFileResourceMap .remove (commonFilePath );
@@ -265,7 +263,7 @@ public int getFileReferenceCount(
265263 ? hardlinkOrCopiedFileToPipeTsFileResourceMap
266264 .computeIfAbsent (pipeName , pipe -> new ConcurrentHashMap <>())
267265 .get (hardlinkOrCopiedFile .getPath ())
268- : hardlinkOrCopiedFileToTsFileMemResourceMap .get (
266+ : hardlinkOrCopiedFileToTsFilePublicResourceMap .get (
269267 getCommonFilePath (hardlinkOrCopiedFile ));
270268 return resource != null ? resource .getReferenceCount () : 0 ;
271269 } finally {
@@ -286,8 +284,9 @@ public boolean cacheObjectsIfAbsent(final File hardlinkOrCopiedTsFile) throws IO
286284 || hardlinkOrCopiedTsFile .getParentFile ().getParentFile () == null ) {
287285 return false ;
288286 }
289- final PipeTsFileMemResource resource =
290- hardlinkOrCopiedFileToTsFileMemResourceMap .get (getCommonFilePath (hardlinkOrCopiedTsFile ));
287+ final PipeTsFilePublicResource resource =
288+ hardlinkOrCopiedFileToTsFilePublicResourceMap .get (
289+ getCommonFilePath (hardlinkOrCopiedTsFile ));
291290 return resource != null && resource .cacheObjectsIfAbsent (hardlinkOrCopiedTsFile );
292291 } finally {
293292 segmentLock .unlock (hardlinkOrCopiedTsFile );
@@ -298,8 +297,9 @@ public Map<IDeviceID, List<String>> getDeviceMeasurementsMapFromCache(
298297 final File hardlinkOrCopiedTsFile ) throws IOException {
299298 segmentLock .lock (hardlinkOrCopiedTsFile );
300299 try {
301- final PipeTsFileMemResource resource =
302- hardlinkOrCopiedFileToTsFileMemResourceMap .get (getCommonFilePath (hardlinkOrCopiedTsFile ));
300+ final PipeTsFilePublicResource resource =
301+ hardlinkOrCopiedFileToTsFilePublicResourceMap .get (
302+ getCommonFilePath (hardlinkOrCopiedTsFile ));
303303 return resource == null ? null : resource .tryGetDeviceMeasurementsMap (hardlinkOrCopiedTsFile );
304304 } finally {
305305 segmentLock .unlock (hardlinkOrCopiedTsFile );
@@ -310,8 +310,9 @@ public Map<IDeviceID, Boolean> getDeviceIsAlignedMapFromCache(
310310 final File hardlinkOrCopiedTsFile , final boolean cacheOtherMetadata ) throws IOException {
311311 segmentLock .lock (hardlinkOrCopiedTsFile );
312312 try {
313- final PipeTsFileMemResource resource =
314- hardlinkOrCopiedFileToTsFileMemResourceMap .get (getCommonFilePath (hardlinkOrCopiedTsFile ));
313+ final PipeTsFilePublicResource resource =
314+ hardlinkOrCopiedFileToTsFilePublicResourceMap .get (
315+ getCommonFilePath (hardlinkOrCopiedTsFile ));
315316 return resource == null
316317 ? null
317318 : resource .tryGetDeviceIsAlignedMap (cacheOtherMetadata , hardlinkOrCopiedTsFile );
@@ -324,8 +325,9 @@ public Map<String, TSDataType> getMeasurementDataTypeMapFromCache(
324325 final File hardlinkOrCopiedTsFile ) throws IOException {
325326 segmentLock .lock (hardlinkOrCopiedTsFile );
326327 try {
327- final PipeTsFileMemResource resource =
328- hardlinkOrCopiedFileToTsFileMemResourceMap .get (getCommonFilePath (hardlinkOrCopiedTsFile ));
328+ final PipeTsFilePublicResource resource =
329+ hardlinkOrCopiedFileToTsFilePublicResourceMap .get (
330+ getCommonFilePath (hardlinkOrCopiedTsFile ));
329331 return resource == null
330332 ? null
331333 : resource .tryGetMeasurementDataTypeMap (hardlinkOrCopiedTsFile );
@@ -334,16 +336,23 @@ public Map<String, TSDataType> getMeasurementDataTypeMapFromCache(
334336 }
335337 }
336338
339+ public Map <String , ? extends PipeTsFileResource > getResourceMap (final @ Nullable String pipeName ) {
340+ return Objects .nonNull (pipeName )
341+ ? hardlinkOrCopiedFileToPipeTsFileResourceMap .computeIfAbsent (
342+ pipeName , k -> new ConcurrentHashMap <>())
343+ : hardlinkOrCopiedFileToTsFilePublicResourceMap ;
344+ }
345+
337346 public void pinTsFileResource (
338- final TsFileResource resource , final boolean withMods , final String pipeName )
347+ final TsFileResource resource , final boolean withMods , final @ Nullable String pipeName )
339348 throws IOException {
340349 increaseFileReference (resource .getTsFile (), true , pipeName );
341350 if (withMods && resource .getModFile ().exists ()) {
342351 increaseFileReference (new File (resource .getModFile ().getFilePath ()), false , pipeName );
343352 }
344353 }
345354
346- public void unpinTsFileResource (final TsFileResource resource , final String pipeName )
355+ public void unpinTsFileResource (final TsFileResource resource , final @ Nullable String pipeName )
347356 throws IOException {
348357 final File pinnedFile = getHardlinkOrCopiedFileInPipeDir (resource .getTsFile (), pipeName );
349358 decreaseFileReference (pinnedFile , pipeName );
0 commit comments