Back to index

nordugrid-arc-nox  1.1.0~rc6
Public Member Functions | Public Attributes
storage.ahash.ahash.CentralAHash Class Reference
Inheritance diagram for storage.ahash.ahash.CentralAHash:
Inheritance graph
[legend]

List of all members.

Public Member Functions

def __init__
def get
def change

Public Attributes

 public_request_names
 store

Detailed Description

A centralized implementation of the A-Hash service. 

Definition at line 32 of file ahash.py.


Constructor & Destructor Documentation

def storage.ahash.ahash.CentralAHash.__init__ (   self,
  cfg 
)
The constructor of the CentralAHash class.

CentralAHash(cfg)

cfg must contain the following parameters:
'storeclass', the name of the class which will store the data
'storecfg', an XMLNode with the configuration of the storeclass

Reimplemented in storage.ahash.replicatedahash.ReplicatedAHash.

Definition at line 35 of file ahash.py.

00035 
00036     def __init__(self, cfg):
00037         """ The constructor of the CentralAHash class.
00038 
00039         CentralAHash(cfg)
00040 
00041         cfg must contain the following parameters:
00042         'storeclass', the name of the class which will store the data
00043         'storecfg', an XMLNode with the configuration of the storeclass
00044         """
00045         log.msg(arc.VERBOSE, "CentralAHash constructor called")
00046         
00047         self.public_request_names = []
00048         # import the storeclass and call its constructor with the datadir
00049 
00050         # the name of the class which is capable of storing the object
00051         storeclass = str(cfg.Get('StoreClass'))
00052         # this is a directory for storing object data
00053         storecfg = cfg.Get('StoreCfg')
00054 
00055         try:
00056             if not hasattr(self, "store"):
00057                 cl = import_class_from_string(storeclass)
00058         except:
00059             log.msg(arc.ERROR, 'Error importing', storeclass)
00060             raise Exception, 'A-Hash cannot run without a store.'
00061         if not hasattr(self, "store"):
00062             self.store = cl(storecfg)


Member Function Documentation

def storage.ahash.ahash.CentralAHash.change (   self,
  changes 
)
Change the '(section, property) : value' entries of an object, if the given conditions are met.

change(changes)

'changes' is a dictionary of {'changeID' : 'change'}, where
    'changeID' is a reference ID used in the response list
    'change' is a (ID, changeType, section, property, value, conditions) tuple, where:
'ID' is the ID of the object which we want to change
'changeType' could be 'set', 'unset' or 'delete'
    'set' sets '(section, property)' to 'value'
    'unset' removes '(section, property)', the value does not matter
    'delete' removes the whole objects
'conditions' is a dictionary of {'conditionID' : 'condition'}, where
    'conditionID' is an ID of this condition
    'condition' is a (type, section, property, value) tuple:
        'type' could be 'is', 'isnot', 'isset', 'unset'
            'is': '(section, property)' is set to 'value'
            'isnot': '(section, property)' is not set to 'value'
            'isset': '(section, property)' is set to any value
            'unset': '(section, property)' is not set at all

This method returns a dictionary of {'changeID' : (success, conditionID)},
    where success could be:
- 'set'
- 'unset'
- 'deleted'
- 'failed'
- 'condition not met' (in this case, 'conditionID' gives the ID of the failed condition)
- 'invalid change type'
- 'unknown'

Definition at line 87 of file ahash.py.

00087 
00088     def change(self, changes):
00089         """ Change the '(section, property) : value' entries of an object, if the given conditions are met.
00090 
00091         change(changes)
00092 
00093         'changes' is a dictionary of {'changeID' : 'change'}, where
00094             'changeID' is a reference ID used in the response list
00095             'change' is a (ID, changeType, section, property, value, conditions) tuple, where:
00096                 'ID' is the ID of the object which we want to change
00097                 'changeType' could be 'set', 'unset' or 'delete'
00098                     'set' sets '(section, property)' to 'value'
00099                     'unset' removes '(section, property)', the value does not matter
00100                     'delete' removes the whole objects
00101                 'conditions' is a dictionary of {'conditionID' : 'condition'}, where
00102                     'conditionID' is an ID of this condition
00103                     'condition' is a (type, section, property, value) tuple:
00104                         'type' could be 'is', 'isnot', 'isset', 'unset'
00105                             'is': '(section, property)' is set to 'value'
00106                             'isnot': '(section, property)' is not set to 'value'
00107                             'isset': '(section, property)' is set to any value
00108                             'unset': '(section, property)' is not set at all
00109         
00110         This method returns a dictionary of {'changeID' : (success, conditionID)},
00111             where success could be:
00112                 - 'set'
00113                 - 'unset'
00114                 - 'deleted'
00115                 - 'failed'
00116                 - 'condition not met' (in this case, 'conditionID' gives the ID of the failed condition)
00117                 - 'invalid change type'
00118                 - 'unknown'
00119         """
00120         # prepare the dictionary which will hold the response
00121         response = {}
00122         for (changeID, (ID, changeType, section, property, value, conditionList)) in changes.items():
00123             # for each change in the changes list
00124             # lock the store to avoid inconsistency
00125             while not self.store.lock(blocking = False):
00126                 #print 'A-Hash cannot acquire lock, waiting...'
00127                 time.sleep(0.2)
00128             # prepare the 'success' of this change
00129             success = 'unknown'
00130             # prepare the 'conditionID' for an unmet condition
00131             unmetConditionID = ''
00132             try:
00133                 # get the current content of the object
00134                 obj = self.store.get(ID)
00135                 # now check all the conditions if there is any
00136                 ok = True
00137                 for (conditionID, (conditionType, conditionSection,
00138                         conditionProperty, conditionValue)) in conditionList.items():
00139                     # get the current value of the conditional (section, property), or None if it is not set
00140                     currentValue = obj.get((conditionSection, conditionProperty), None)
00141                     if conditionType == 'is':
00142                         # if the (section, property) is not set to value
00143                         if currentValue != conditionValue:
00144                             ok = False
00145                             unmetConditionID = conditionID
00146                             # jump out of the for statement
00147                             break
00148                     elif conditionType == 'isnot':
00149                         # if the (section, property) is set to value
00150                         if currentValue == conditionValue:
00151                             ok = False
00152                             unmetConditionID = conditionID
00153                             break
00154                     elif conditionType == 'isset':
00155                         # if the (section, property) is not set:
00156                         if currentValue is None:
00157                             ok = False
00158                             unmetConditionID = conditionID
00159                             break
00160                     elif conditionType == 'unset':
00161                         # if the (section, property) is set:
00162                         if currentValue is not None:
00163                             ok = False
00164                             unmetConditionID = conditionID
00165                             break
00166                 # if 'ok' is true then all conditions are met
00167                 if ok:
00168                     if changeType == 'set':
00169                         # sets the new value (overwrites old value of this (section, property))
00170                         obj[(section, property)] = value
00171                         # store the changed object
00172                         self.store.set(ID, obj)
00173                         # set the result of this change
00174                         success = 'set'
00175                     elif changeType == 'unset':
00176                         # removes the entry of (section, property)
00177                         del obj[(section, property)]
00178                         # store the changed object
00179                         self.store.set(ID, obj)
00180                         # set the result of this change
00181                         success = 'unset'
00182                     elif changeType == 'delete':
00183                         # remove the whole object
00184                         self.store.set(ID, None)
00185                         success = 'deleted'
00186                     else:
00187                         # there is no other changeType
00188                         success = 'invalid change type'
00189                 else:
00190                     success = 'condition not met'
00191             except:
00192                 # if there was an exception, set this to failed
00193                 success = 'failed'
00194                 log.msg()
00195             # we are done, release the lock
00196             self.store.unlock()
00197             # append the result of the change to the response list
00198             response[changeID] = (success, unmetConditionID)
00199         return response

def storage.ahash.ahash.CentralAHash.get (   self,
  ids,
  neededMetadata = [] 
)
Gets all data of the given IDs.

get(ids)

'ids' is a list of strings: the IDs of the requested object
Returns a dictionary of { ID : metadata }
    where 'metadata' is a dictionary where the keys are ('section', 'property') tuples

Definition at line 63 of file ahash.py.

00063 
00064     def get(self, ids, neededMetadata = []):
00065         """ Gets all data of the given IDs.
00066 
00067         get(ids)
00068 
00069         'ids' is a list of strings: the IDs of the requested object
00070         Returns a dictionary of { ID : metadata }
00071             where 'metadata' is a dictionary where the keys are ('section', 'property') tuples
00072         """
00073         if neededMetadata: # if the needed metadata is given
00074             # if a property is empty in a (section, property) pair
00075             # it means we need all properties from that section
00076             allpropsections = [section for (section, property) in neededMetadata if property == '']
00077             # gets the metadata for each ID, filters it and creates an {ID : metadata} dictionary
00078             return dict([(
00079                 ID,
00080                 dict([((section, property), value) # for all metadata entry of this object
00081                     for (section, property), value in self.store.get(ID).items()
00082                         # if this (section, property) is needed or if this section needs all the properties
00083                         if (section, property) in neededMetadata or section in allpropsections])
00084             ) for ID in ids])
00085         else: # gets the metadata for each ID, and creates an {ID : metadata} dictionary
00086             return dict([(ID, self.store.get(ID)) for ID in ids])


Member Data Documentation

Reimplemented in storage.ahash.replicatedahash.ReplicatedAHash.

Definition at line 46 of file ahash.py.

Reimplemented in storage.ahash.replicatedahash.ReplicatedAHash.

Definition at line 61 of file ahash.py.


The documentation for this class was generated from the following file: