@@ -317,7 +317,37 @@ public static String constructFullPath(OmKeyInfo omKeyInfo,
317317 public static String constructFullPath (String keyName , long initialParentId , String volumeName , String bucketName ,
318318 ReconNamespaceSummaryManager reconNamespaceSummaryManager ,
319319 ReconOMMetadataManager omMetadataManager ) throws IOException {
320- StringBuilder fullPath = new StringBuilder (keyName );
320+ StringBuilder fullPath = constructFullPathPrefix (initialParentId , volumeName , bucketName ,
321+ reconNamespaceSummaryManager , omMetadataManager );
322+ if (fullPath .length () == 0 ) {
323+ return "" ;
324+ }
325+ fullPath .append (keyName );
326+ return fullPath .toString ();
327+ }
328+
329+
330+ /**
331+ * Constructs the prefix path to a key from its key name and parent ID using a bottom-up approach, starting from the
332+ * leaf node.
333+ *
334+ * The method begins with the leaf node (the key itself) and recursively prepends parent directory names, fetched
335+ * via NSSummary objects, until reaching the parent bucket (parentId is -1). It effectively builds the path from
336+ * bottom to top, finally prepending the volume and bucket names to complete the full path. If the directory structure
337+ * is currently being rebuilt (indicated by the rebuildTriggered flag), this method returns an empty string to signify
338+ * that path construction is temporarily unavailable.
339+ *
340+ * @param initialParentId The parent ID of the key
341+ * @param volumeName The name of the volume
342+ * @param bucketName The name of the bucket
343+ * @return A StringBuilder containing the constructed prefix path of the key, or an empty string builder if a rebuild
344+ * is in progress.
345+ * @throws IOException
346+ */
347+ public static StringBuilder constructFullPathPrefix (long initialParentId , String volumeName ,
348+ String bucketName , ReconNamespaceSummaryManager reconNamespaceSummaryManager ,
349+ ReconOMMetadataManager omMetadataManager ) throws IOException {
350+ StringBuilder fullPath = new StringBuilder ();
321351 long parentId = initialParentId ;
322352 boolean isDirectoryPresent = false ;
323353
@@ -326,16 +356,21 @@ public static String constructFullPath(String keyName, long initialParentId, Str
326356 if (nsSummary == null ) {
327357 log .warn ("NSSummary tree is currently being rebuilt or the directory could be in the progress of " +
328358 "deletion, returning empty string for path construction." );
329- return "" ;
359+ fullPath .setLength (0 );
360+ return fullPath ;
330361 }
331362 if (nsSummary .getParentId () == -1 ) {
332363 if (rebuildTriggered .compareAndSet (false , true )) {
333364 triggerRebuild (reconNamespaceSummaryManager , omMetadataManager );
334365 }
335366 log .warn ("NSSummary tree is currently being rebuilt, returning empty string for path construction." );
336- return "" ;
367+ fullPath .setLength (0 );
368+ return fullPath ;
369+ }
370+ // On the last pass, dir-name will be empty and parent will be zero, indicating the loop should end.
371+ if (!nsSummary .getDirName ().isEmpty ()) {
372+ fullPath .insert (0 , nsSummary .getDirName () + OM_KEY_PREFIX );
337373 }
338- fullPath .insert (0 , nsSummary .getDirName () + OM_KEY_PREFIX );
339374
340375 // Move to the parent ID of the current directory
341376 parentId = nsSummary .getParentId ();
@@ -344,10 +379,18 @@ public static String constructFullPath(String keyName, long initialParentId, Str
344379
345380 // Prepend the volume and bucket to the constructed path
346381 fullPath .insert (0 , volumeName + OM_KEY_PREFIX + bucketName + OM_KEY_PREFIX );
382+ // TODO - why is this needed? It seems lke it should handle double slashes in the path name,
383+ // but its not clear how they get there. This normalize call is quite expensive as it
384+ // creates several objects (URI, PATH, back to string). There was a bug fixed above
385+ // where the last parent dirName was empty, which always caused a double // after the
386+ // bucket name, but with that fixed, it seems like this should not be needed. All tests
387+ // pass without it for key listing.
347388 if (isDirectoryPresent ) {
348- return OmUtils .normalizeKey (fullPath .toString (), true );
389+ String path = fullPath .toString ();
390+ fullPath .setLength (0 );
391+ fullPath .append (OmUtils .normalizeKey (path , true ));
349392 }
350- return fullPath . toString () ;
393+ return fullPath ;
351394 }
352395
353396 private static void triggerRebuild (ReconNamespaceSummaryManager reconNamespaceSummaryManager ,
0 commit comments