2222import ch .cyberduck .core .exception .NotfoundException ;
2323import ch .cyberduck .core .features .AttributesAdapter ;
2424import ch .cyberduck .core .features .AttributesFinder ;
25- import ch .cyberduck .core .io .Checksum ;
2625
27- import org .apache .commons . io . FilenameUtils ;
28- import org .apache .commons . lang3 . StringUtils ;
26+ import org .apache .logging . log4j . LogManager ;
27+ import org .apache .logging . log4j . Logger ;
2928import org .irods .irods4j .high_level .catalog .IRODSQuery ;
30- import org .irods .irods4j .high_level .catalog .IRODSQuery .GenQuery1QueryArgs ;
3129import org .irods .irods4j .high_level .connection .IRODSConnection ;
3230import org .irods .irods4j .high_level .vfs .IRODSFilesystem ;
33- import org .irods .irods4j .low_level .api .GenQuery1Columns ;
31+ import org .irods .irods4j .high_level .vfs .LogicalPath ;
32+ import org .irods .irods4j .high_level .vfs .ObjectStatus ;
3433import org .irods .irods4j .low_level .api .IRODSException ;
3534
3635import java .io .IOException ;
3736import java .util .List ;
3837
3938public class IRODSAttributesFinderFeature implements AttributesFinder , AttributesAdapter <List <String >> {
4039
40+ private static final Logger log = LogManager .getLogger (IRODSAttributesFinderFeature .class );
41+
4142 private final IRODSSession session ;
4243
4344 public IRODSAttributesFinderFeature (final IRODSSession session ) {
@@ -47,42 +48,56 @@ public IRODSAttributesFinderFeature(final IRODSSession session) {
4748 @ Override
4849 public PathAttributes find (final Path file , final ListProgressListener listener ) throws BackgroundException {
4950 try {
50- final IRODSConnection conn = session .getClient ();
51+ log .debug ("looking up path attributes." );
52+
5153 final String logicalPath = file .getAbsolute ();
52- if (!IRODSFilesystem .exists (session .getClient ().getRcComm (), logicalPath )) {
53- throw new NotfoundException (file .getAbsolute ());
54- }
54+ final IRODSConnection conn = session .getClient ();
5555
56- GenQuery1QueryArgs input = new GenQuery1QueryArgs ( );
56+ ObjectStatus status = IRODSFilesystem . status ( session . getClient (). getRcComm (), logicalPath );
5757
58- // select DATA_MODIFY_TIME, DATA_CREATE_TIME, DATA_SIZE, DATA_CHECKSUM ...
59- input .addColumnToSelectClause (GenQuery1Columns .COL_D_MODIFY_TIME );
60- input .addColumnToSelectClause (GenQuery1Columns .COL_D_CREATE_TIME );
61- input .addColumnToSelectClause (GenQuery1Columns .COL_DATA_SIZE );
62- input .addColumnToSelectClause (GenQuery1Columns .COL_D_DATA_CHECKSUM );
58+ if (IRODSFilesystem .isDataObject (status )) {
59+ log .debug ("data object exists in iRODS. fetching data using GenQuery2." );
60+ String query = String .format (
61+ "select DATA_CREATE_TIME, DATA_MODIFY_TIME, DATA_SIZE, DATA_CHECKSUM, DATA_REPL_STATUS where COLL_NAME = '%s' and DATA_NAME = '%s' order by DATA_REPL_STATUS desc, DATA_MODIFY_TIME desc" ,
62+ LogicalPath .parentPath (logicalPath ),
63+ LogicalPath .objectName (logicalPath ));
64+ log .debug ("query = [{}]" , query );
65+ List <List <String >> rows = IRODSQuery .executeGenQuery2 (conn .getRcComm (), query );
6366
64- // where COLL_NAME = '<parent_path>' and DATA_NAME = '<filename>'
65- String collNameCondStr = String .format ("= '%s'" , FilenameUtils .getFullPathNoEndSeparator (logicalPath ));
66- String dataNameCondStr = String .format ("= '%s'" , FilenameUtils .getName (logicalPath ));
67- input .addConditionToWhereClause (GenQuery1Columns .COL_COLL_NAME , collNameCondStr );
68- input .addConditionToWhereClause (GenQuery1Columns .COL_DATA_NAME , dataNameCondStr );
67+ PathAttributes attrs = new PathAttributes ();
6968
70- final PathAttributes attrs = new PathAttributes ();
69+ if (!rows .isEmpty ()) {
70+ List <String > row = rows .get (0 );
71+ if ("0" .equals (row .get (4 )) || "1" .equals (row .get (4 ))) {
72+ setAttributes (attrs , row );
73+ }
74+ }
7175
72- IRODSQuery .executeGenQuery1 (conn .getRcComm (), input , row -> {
73- attrs .setModificationDate (Long .parseLong (row .get (0 )) * 1000 ); // seconds to ms
74- attrs .setCreationDate (Long .parseLong (row .get (1 )) * 1000 );
75- attrs .setSize (Long .parseLong (row .get (2 )));
76+ return attrs ;
77+ }
7678
77- String checksum = row .get (3 );
78- if (!StringUtils .isEmpty (checksum )) {
79- attrs .setChecksum (Checksum .parse (checksum ));
79+ if (IRODSFilesystem .isCollection (status )) {
80+ log .debug ("collection exists in iRODS. fetching data using GenQuery2." );
81+ String query = String .format ("select COLL_CREATE_TIME, COLL_MODIFY_TIME where COLL_NAME = '%s'" , logicalPath );
82+ log .debug ("query = [{}]" , query );
83+ List <List <String >> rows = IRODSQuery .executeGenQuery2 (conn .getRcComm (), query );
84+
85+ PathAttributes attrs = new PathAttributes ();
86+
87+ if (!rows .isEmpty ()) {
88+ // Collections do not have the same properties as data objects
89+ // so fill in the gaps to satisfy requirements of setAttributes.
90+ List <String > row = rows .get (0 );
91+ row .add ("0" ); // Data size
92+ row .add ("" ); // Checksum
93+ row .add ("" ); // Replica status
94+ setAttributes (attrs , row );
8095 }
8196
82- return false ;
83- });
97+ return attrs ;
98+ }
8499
85- return attrs ;
100+ throw new NotfoundException ( logicalPath ) ;
86101 }
87102 catch (IOException | IRODSException e ) {
88103 throw new IRODSExceptionMappingService ().map ("Failure to read attributes of {0}" , e , file );
@@ -91,18 +106,18 @@ public PathAttributes find(final Path file, final ListProgressListener listener)
91106
92107 @ Override
93108 public PathAttributes toAttributes (final List <String > row ) {
94- final IRODSConnection conn = session .getClient ();
95- final PathAttributes attributes = new PathAttributes ();
96-
97- attributes .setModificationDate (Long .parseLong (row .get (0 )) * 1000 ); // seconds to ms
98- attributes .setCreationDate (Long .parseLong (row .get (1 )) * 1000 );
99- attributes .setSize (Long .parseLong (row .get (2 )));
100-
101- String checksum = row .get (3 );
102- if (!StringUtils .isEmpty (checksum )) {
103- attributes .setChecksum (Checksum .parse (checksum ));
104- }
109+ PathAttributes attrs = new PathAttributes ();
110+ setAttributes (attrs , row );
111+ return attrs ;
112+ }
105113
106- return attributes ;
114+ private static void setAttributes (final PathAttributes attrs , final List <String > row ) {
115+ log .debug ("path attribute info: created at [{}], modified at [{}], data size = [{}], checksum = [{}]" ,
116+ row .get (0 ), row .get (1 ), row .get (2 ), row .get (3 ));
117+ attrs .setCreationDate (Long .parseLong (row .get (0 )) * 1000 ); // seconds to ms
118+ attrs .setModificationDate (Long .parseLong (row .get (1 )) * 1000 );
119+ attrs .setSize (Long .parseLong (row .get (2 )));
120+ attrs .setChecksum (IRODSChecksumUtils .toChecksum (row .get (3 )));
107121 }
122+
108123}
0 commit comments