github.com/cyverse/go-irodsclient@v0.13.2/fs/fs_metadata.go (about)

     1  package fs
     2  
     3  import (
     4  	irods_fs "github.com/cyverse/go-irodsclient/irods/fs"
     5  	"github.com/cyverse/go-irodsclient/irods/types"
     6  	"github.com/cyverse/go-irodsclient/irods/util"
     7  )
     8  
     9  // SearchByMeta searches all file system entries with given metadata
    10  func (fs *FileSystem) SearchByMeta(metaname string, metavalue string) ([]*Entry, error) {
    11  	return fs.searchEntriesByMeta(metaname, metavalue)
    12  }
    13  
    14  // ListMetadata lists metadata for the given path
    15  func (fs *FileSystem) ListMetadata(path string) ([]*types.IRODSMeta, error) {
    16  	// check cache first
    17  	cachedEntry := fs.cache.GetMetadataCache(path)
    18  	if cachedEntry != nil {
    19  		return cachedEntry, nil
    20  	}
    21  
    22  	irodsCorrectPath := util.GetCorrectIRODSPath(path)
    23  
    24  	// otherwise, retrieve it and add it to cache
    25  	conn, err := fs.metaSession.AcquireConnection()
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  	defer fs.metaSession.ReturnConnection(conn)
    30  
    31  	var metadataobjects []*types.IRODSMeta
    32  
    33  	if fs.ExistsDir(irodsCorrectPath) {
    34  		metadataobjects, err = irods_fs.ListCollectionMeta(conn, irodsCorrectPath)
    35  		if err != nil {
    36  			return nil, err
    37  		}
    38  	} else {
    39  		collectionEntry, err := fs.getCollection(util.GetIRODSPathDirname(path))
    40  		if err != nil {
    41  			return nil, err
    42  		}
    43  
    44  		collection := fs.getCollectionFromEntry(collectionEntry)
    45  
    46  		metadataobjects, err = irods_fs.ListDataObjectMeta(conn, collection, util.GetIRODSPathFileName(irodsCorrectPath))
    47  		if err != nil {
    48  			return nil, err
    49  		}
    50  	}
    51  
    52  	// cache it
    53  	fs.cache.AddMetadataCache(irodsCorrectPath, metadataobjects)
    54  
    55  	return metadataobjects, nil
    56  }
    57  
    58  // AddMetadata adds a metadata for the path
    59  func (fs *FileSystem) AddMetadata(irodsPath string, attName string, attValue string, attUnits string) error {
    60  	irodsCorrectPath := util.GetCorrectIRODSPath(irodsPath)
    61  
    62  	metadata := &types.IRODSMeta{
    63  		Name:  attName,
    64  		Value: attValue,
    65  		Units: attUnits,
    66  	}
    67  
    68  	conn, err := fs.metaSession.AcquireConnection()
    69  	if err != nil {
    70  		return err
    71  	}
    72  	defer fs.metaSession.ReturnConnection(conn)
    73  
    74  	if fs.ExistsDir(irodsCorrectPath) {
    75  		err = irods_fs.AddCollectionMeta(conn, irodsCorrectPath, metadata)
    76  		if err != nil {
    77  			return err
    78  		}
    79  	} else {
    80  		err = irods_fs.AddDataObjectMeta(conn, irodsCorrectPath, metadata)
    81  		if err != nil {
    82  			return err
    83  		}
    84  	}
    85  
    86  	fs.cache.RemoveMetadataCache(irodsCorrectPath)
    87  	return nil
    88  }
    89  
    90  // DeleteMetadata deletes a metadata for the path
    91  func (fs *FileSystem) DeleteMetadata(irodsPath string, attName string, attValue string, attUnits string) error {
    92  	irodsCorrectPath := util.GetCorrectIRODSPath(irodsPath)
    93  
    94  	metadata := &types.IRODSMeta{
    95  		Name:  attName,
    96  		Value: attValue,
    97  		Units: attUnits,
    98  	}
    99  
   100  	conn, err := fs.metaSession.AcquireConnection()
   101  	if err != nil {
   102  		return err
   103  	}
   104  	defer fs.metaSession.ReturnConnection(conn)
   105  
   106  	if fs.ExistsDir(irodsCorrectPath) {
   107  		err = irods_fs.DeleteCollectionMeta(conn, irodsCorrectPath, metadata)
   108  		if err != nil {
   109  			return err
   110  		}
   111  	} else {
   112  		err = irods_fs.DeleteDataObjectMeta(conn, irodsCorrectPath, metadata)
   113  		if err != nil {
   114  			return err
   115  		}
   116  	}
   117  
   118  	fs.cache.RemoveMetadataCache(irodsCorrectPath)
   119  	return nil
   120  }
   121  
   122  // AddUserMetadata adds a user metadata
   123  func (fs *FileSystem) AddUserMetadata(user string, avuid int64, attName, attValue, attUnits string) error {
   124  	metadata := &types.IRODSMeta{
   125  		AVUID: avuid,
   126  		Name:  attName,
   127  		Value: attValue,
   128  		Units: attUnits,
   129  	}
   130  
   131  	conn, err := fs.metaSession.AcquireConnection()
   132  	if err != nil {
   133  		return err
   134  	}
   135  	defer fs.metaSession.ReturnConnection(conn)
   136  
   137  	err = irods_fs.AddUserMeta(conn, user, metadata)
   138  	if err != nil {
   139  		return err
   140  	}
   141  
   142  	return nil
   143  }
   144  
   145  // DeleteUserMetadata deletes a user metadata
   146  func (fs *FileSystem) DeleteUserMetadata(user string, avuid int64, attName, attValue, attUnits string) error {
   147  	metadata := &types.IRODSMeta{
   148  		AVUID: avuid,
   149  		Name:  attName,
   150  		Value: attValue,
   151  		Units: attUnits,
   152  	}
   153  
   154  	conn, err := fs.metaSession.AcquireConnection()
   155  	if err != nil {
   156  		return err
   157  	}
   158  	defer fs.metaSession.ReturnConnection(conn)
   159  
   160  	err = irods_fs.DeleteUserMeta(conn, user, metadata)
   161  	if err != nil {
   162  		return err
   163  	}
   164  
   165  	return nil
   166  }
   167  
   168  // ListUserMetadata lists all user metadata
   169  func (fs *FileSystem) ListUserMetadata(user string) ([]*types.IRODSMeta, error) {
   170  	conn, err := fs.metaSession.AcquireConnection()
   171  	if err != nil {
   172  		return nil, err
   173  	}
   174  	defer fs.metaSession.ReturnConnection(conn)
   175  
   176  	metadataobjects, err := irods_fs.ListUserMeta(conn, user)
   177  	if err != nil {
   178  		return nil, err
   179  	}
   180  
   181  	return metadataobjects, nil
   182  }
   183  
   184  // searchEntriesByMeta searches entries by meta
   185  func (fs *FileSystem) searchEntriesByMeta(metaName string, metaValue string) ([]*Entry, error) {
   186  	conn, err := fs.metaSession.AcquireConnection()
   187  	if err != nil {
   188  		return nil, err
   189  	}
   190  	defer fs.metaSession.ReturnConnection(conn)
   191  
   192  	collections, err := irods_fs.SearchCollectionsByMeta(conn, metaName, metaValue)
   193  	if err != nil {
   194  		return nil, err
   195  	}
   196  
   197  	entries := []*Entry{}
   198  
   199  	for _, coll := range collections {
   200  		entry := fs.getEntryFromCollection(coll)
   201  		entries = append(entries, entry)
   202  
   203  		// cache it
   204  		fs.cache.RemoveNegativeEntryCache(entry.Path)
   205  		fs.cache.AddEntryCache(entry)
   206  	}
   207  
   208  	dataobjects, err := irods_fs.SearchDataObjectsMasterReplicaByMeta(conn, metaName, metaValue)
   209  	if err != nil {
   210  		return nil, err
   211  	}
   212  
   213  	for _, dataobject := range dataobjects {
   214  		if len(dataobject.Replicas) == 0 {
   215  			continue
   216  		}
   217  
   218  		entry := fs.getEntryFromDataObject(dataobject)
   219  		entries = append(entries, entry)
   220  
   221  		// cache it
   222  		fs.cache.RemoveNegativeEntryCache(entry.Path)
   223  		fs.cache.AddEntryCache(entry)
   224  	}
   225  
   226  	return entries, nil
   227  }