@@ -1343,43 +1343,31 @@ relations:
13431343Tracking and manipulating replicas of Data Objects
13441344--------------------------------------------------
13451345
1346- Putting together the techniques we' ve seen so far, it' s not hard to
1347- write functions that achieve useful, common goals. Suppose that for all
1348- data objects containing replicas on a given named resource (the
1349- " source" ) we want those replicas " moved" to a second, or
1350- " destination" resource. We can achieve it with a function such as the
1351- one below. It achieves the move via a replication of the data objects
1352- found to the destination resource , followed by a trimming of each
1353- replica from the source. We assume for our current purposed that all
1354- replicas are " good" , ie have a status of " 1" :
1355-
1356- ```python
1357- from irods.resource import iRODSResource
1358- from irods.collection import iRODSCollection
1359- from irods.data_object import iRODSDataObject
1360- from irods.models import Resource,Collection,DataObject
1361- def repl_and_trim (srcRescName, dstRescName = ' ' , verbose = False ):
1362- objects_trimmed = 0
1363- q = session.query(Resource).filter(Resource.name == srcRescName)
1364- srcResc = iRODSResource( session.resources, q.one())
1365- # loop over data objects found on srcResc
1366- for q_row in session.query(Collection,DataObject) \
1367- .filter(DataObject.resc_id == srcResc.id):
1368- collection = iRODSCollection (session.collections, result = q_row)
1369- data_object = iRODSDataObject (session.data_objects, parent = collection, results = (q_row,))
1370- objects_trimmed + = 1
1371- if verbose :
1372- import pprint
1373- print ( ' --------' , data_object.name, ' --------' )
1374- pprint.pprint( [vars (r) for r in data_object.replicas if
1375- r.resource_name == srcRescName] )
1376- if dstRescName:
1377- objects_trimmed + = 1
1378- data_object.replicate(dstRescName)
1379- for replica_number in [r.number for r in data_object.replicas]:
1380- options = { kw.DATA_REPL_KW : replica_number }
1381- data_object.unlink( ** options )
1382- return objects_trimmed
1346+ Putting together the techniques we' ve seen so far, it' s not hard to write client code to accomplish
1347+ useful, common tasks. Suppose, for instance, that a data object contains replicas on a given resource
1348+ or resource hierarchy (the " source" ), and we want those replicas " moved" to a second resource
1349+ (the " destination" ). This can be done by combining the replicate and trim operations, as in the following
1350+ code excerpt.
1351+
1352+ We' ll assume, for our current purposes, that all pre-existing replicas are good (ie. they have a
1353+ `status` attribute of `' 1' ` ); and that the nodes in question are named `src` and `dest` ,
1354+ with `src` being the root node of a resource hierarchy and `dest` just a simple storage node.
1355+
1356+ Then we can accomplish the replica " move" thus:
1357+
1358+ ```python
1359+ path = ' /path/to/data/object'
1360+ data = session.data_objects.get(' /path/to/data/object' )
1361+
1362+ # Replicate the data object to the destination.
1363+
1364+ data.replicate(** {kw.DEST_RESC_NAME_KW : ' dest' })
1365+
1366+ # Find and trim replicas on the source resource hierarchy.
1367+
1368+ replica_numbers = [r.number for r in d.replicas if r.resc_hier.startswith(' src;' )]
1369+ for number in replica_numbers:
1370+ session.data_objects.trim(path, ** {kw.DATA_REPL_NUM :number, kw.COPIES_KW :1 })
13831371```
13841372
13851373Listing Users and Groups ; calculating Group Membership
0 commit comments