direct-io.hg

changeset 5487:bf7a872ed421

bitkeeper revision 1.1713.3.9 (42b306d0pFeN318FP89TE8LRrhROMQ)

xsobj.py:
Cleanup interface to DB.
Updated watches/event code.
Signed-off-by: Mike Wray <mike.wray@hp.com>
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Fri Jun 17 17:22:24 2005 +0000 (2005-06-17)
parents 43ae2740ac94
children 06485a209ecc
files tools/python/xen/xend/xenstore/xsobj.py
line diff
     1.1 --- a/tools/python/xen/xend/xenstore/xsobj.py	Fri Jun 17 16:23:55 2005 +0000
     1.2 +++ b/tools/python/xen/xend/xenstore/xsobj.py	Fri Jun 17 17:22:24 2005 +0000
     1.3 @@ -55,15 +55,15 @@ class DBConverter:
     1.4  
     1.5      getConverter = classmethod(getConverter)
     1.6  
     1.7 -    def convertToDB(cls, val, ty=None):
     1.8 -        return cls.getConverter(ty).toDB(val)
     1.9 +    def exportTypeToDB(cls, db, path, val, ty=None):
    1.10 +        return cls.getConverter(ty).exportToDB(db, path, val)
    1.11  
    1.12 -    convertToDB = classmethod(convertToDB)
    1.13 +    exportTypeToDB = classmethod(exportTypeToDB)
    1.14  
    1.15 -    def convertFromDB(cls, data, ty=None):
    1.16 -        return cls.getConverter(ty).fromDB(data.getData())
    1.17 +    def importTypeFromDB(cls, db, path, ty=None):
    1.18 +        return cls.getConverter(ty).importFromDB(db, path)
    1.19  
    1.20 -    convertFromDB = classmethod(convertFromDB)
    1.21 +    importTypeFromDB = classmethod(importTypeFromDB)
    1.22  
    1.23      # Must define in subclass.
    1.24      name = None
    1.25 @@ -76,6 +76,26 @@ class DBConverter:
    1.26              raise ValueError("invalid converter name: '%s'" % self.name)
    1.27          self.converters[self.name] = self
    1.28  
    1.29 +    def exportToDB(self, db, path, val):
    1.30 +        if val is None:
    1.31 +            return
    1.32 +        try:
    1.33 +            data = self.toDB(val)
    1.34 +        except Exception, ex:
    1.35 +            raise
    1.36 +        setattr(db, path, data)
    1.37 +
    1.38 +    def importFromDB(self, db, path):
    1.39 +        data = getAttr(db, path)
    1.40 +        if data is None:
    1.41 +            val = None
    1.42 +        else:
    1.43 +            try:
    1.44 +                val = self.fromDB(data.getData())
    1.45 +            except Exception, ex:
    1.46 +                raise
    1.47 +        return val
    1.48 +        
    1.49      def toDB(self, val):
    1.50          raise NotImplementedError()
    1.51  
    1.52 @@ -185,10 +205,12 @@ class DBVar:
    1.53          self.attr = varpath[-1]
    1.54  
    1.55      def exportToDB(self, db, obj):
    1.56 -        self.setDB(db, self.getObj(obj))
    1.57 +        val = self.getObj(obj)
    1.58 +        DBConverter.exportTypeToDB(db, self.path, val, ty=self.ty)
    1.59  
    1.60      def importFromDB(self, db, obj):
    1.61 -        self.setObj(obj, self.getDB(db))
    1.62 +        val = DBConverter.importTypeFromDB(db, self.path, ty=self.ty)
    1.63 +        self.setObj(obj, val)
    1.64  
    1.65      def getObj(self, obj):
    1.66          o = obj
    1.67 @@ -207,21 +229,6 @@ class DBVar:
    1.68              return
    1.69          setAttr(o, self.attr, val)
    1.70  
    1.71 -    def getDB(self, db):
    1.72 -        data = getAttr(db, self.path)
    1.73 -        if data is None:
    1.74 -            return None
    1.75 -        return DBConverter.convertFromDB(data, ty=self.ty)
    1.76 -
    1.77 -    def setDB(self, db, val):
    1.78 -        # Don't set in db if val is None.
    1.79 -        #print 'DBVar>setDB>', self.path, 'val=', val
    1.80 -        if val is None:
    1.81 -            return
    1.82 -        data = DBConverter.convertToDB(val, ty=self.ty)
    1.83 -        #print 'DBVar>setDB>', self.path, 'data=', data
    1.84 -        setAttr(db, self.path, data)
    1.85 -
    1.86  class DBMap(dict):
    1.87      """A persistent map. Extends dict with persistence.
    1.88      Set and get values using the usual map syntax:
    1.89 @@ -319,6 +326,21 @@ class DBMap(dict):
    1.90              traceback.print_exc()
    1.91              print 'DBMap>releaseDomain>', ex
    1.92              pass # todo: don't ignore
    1.93 +
    1.94 +    def watch(self, fn, path=""):
    1.95 +        return self.__db__.watch(fn, path=path)
    1.96 +
    1.97 +    def unwatch(self, sid):
    1.98 +        return self.__db__.unwatch(sid)
    1.99 +
   1.100 +    def subscribe(self, event, fn):
   1.101 +        return self.__db__.subscribe(event, fn)
   1.102 +
   1.103 +    def unsubscribe(self, sid):
   1.104 +        return self.__db__.unsubscribe(sid)
   1.105 +
   1.106 +    def sendEvent(self, event, val):
   1.107 +        return self.__db__.sendEvent(event, val)
   1.108          
   1.109      def transactionBegin(self):
   1.110          # Begin a transaction.
   1.111 @@ -333,12 +355,6 @@ class DBMap(dict):
   1.112          # We have changed values, what do we do?
   1.113          pass
   1.114  
   1.115 -    def watch(self, fn):
   1.116 -        pass
   1.117 -
   1.118 -    def unwatch(self, watch):
   1.119 -        pass
   1.120 -        
   1.121      def checkName(self, k):
   1.122          if k == "":
   1.123              raise ValueError("invalid key, empty string")
   1.124 @@ -461,6 +477,9 @@ class DBMap(dict):
   1.125              n = n._addChild(x)
   1.126          return n
   1.127  
   1.128 +    def getDB(self):
   1.129 +        return self.__db__
   1.130 +
   1.131      def setDB(self, db):
   1.132          if (db is not None) and not isinstance(db, XenNode):
   1.133              raise ValueError("invalid db")