Esempio n. 1
0
File: sqla.py Progetto: 01-/beaker
    def __init__(self, namespace, bind, table, data_dir=None, lock_dir=None,
                 **kwargs):
        """Create a namespace manager for use with a database table via
        SQLAlchemy.

        ``bind``
            SQLAlchemy ``Engine`` or ``Connection`` object

        ``table``
            SQLAlchemy ``Table`` object in which to store namespace data.
            This should usually be something created by ``make_cache_table``.
        """
        OpenResourceNamespaceManager.__init__(self, namespace)

        if lock_dir:
            self.lock_dir = lock_dir
        elif data_dir:
            self.lock_dir = data_dir + "/container_db_lock"
        if self.lock_dir:
            verify_directory(self.lock_dir)

        self.bind = self.__class__.binds.get(str(bind.url), lambda: bind)
        self.table = self.__class__.tables.get('%s:%s' % (bind.url, table.name),
                                               lambda: table)
        self.hash = {}
        self._is_new = False
        self.loaded = False
Esempio n. 2
0
    def __init__(self,
                 namespace,
                 bind,
                 table,
                 data_dir=None,
                 lock_dir=None,
                 **kwargs):
        """Create a namespace manager for use with a database table via
        SQLAlchemy.

        ``bind``
            SQLAlchemy ``Engine`` or ``Connection`` object

        ``table``
            SQLAlchemy ``Table`` object in which to store namespace data.
            This should usually be something created by ``make_cache_table``.
        """
        OpenResourceNamespaceManager.__init__(self, namespace)

        if lock_dir:
            self.lock_dir = lock_dir
        elif data_dir:
            self.lock_dir = data_dir + "/container_db_lock"
        if self.lock_dir:
            verify_directory(self.lock_dir)

        self.bind = self.__class__.binds.get(str(bind.url), lambda: bind)
        self.table = self.__class__.tables.get(
            '%s:%s' % (bind.url, table.name), lambda: table)
        self.hash = {}
        self._is_new = False
        self.loaded = False
Esempio n. 3
0
    def __init__(self, namespace, url=None, sa_opts=None, optimistic=False,
                 table_name='beaker_cache', data_dir=None, lock_dir=None,
                 **params):
        """Creates a database namespace manager

        ``url``
            SQLAlchemy compliant db url
        ``sa_opts``
            A dictionary of SQLAlchemy keyword options to initialize the engine
            with.
        ``optimistic``
            Use optimistic session locking, note that this will result in an
            additional select when updating a cache value to compare version
            numbers.
        ``table_name``
            The table name to use in the database for the cache.
        """
        OpenResourceNamespaceManager.__init__(self, namespace)

        if sa_opts is None:
            sa_opts = params

        if lock_dir:
            self.lock_dir = lock_dir
        elif data_dir:
            self.lock_dir = data_dir + "/container_db_lock"
        if self.lock_dir:
            verify_directory(self.lock_dir)

        # Check to see if the table's been created before
        url = url or sa_opts['sa.url']
        table_key = url + table_name
        def make_cache():
            # Check to see if we have a connection pool open already
            meta_key = url + table_name
            def make_meta():
                # SQLAlchemy pops the url, this ensures it sticks around
                # later
                sa_opts['sa.url'] = url
                engine = sa.engine_from_config(sa_opts, 'sa.')
                meta = sa.MetaData()
                meta.bind = engine
                return meta
            meta = DatabaseNamespaceManager.metadatas.get(meta_key, make_meta)
            # Create the table object and cache it now
            cache = sa.Table(table_name, meta,
                             sa.Column('id', types.Integer, primary_key=True),
                             sa.Column('namespace', types.String(255), nullable=False),
                             sa.Column('accessed', types.DateTime, nullable=False),
                             sa.Column('created', types.DateTime, nullable=False),
                             sa.Column('data', types.PickleType, nullable=False),
                             sa.UniqueConstraint('namespace')
            )
            cache.create(checkfirst=True)
            return cache
        self.hash = {}
        self._is_new = False
        self.loaded = False
        self.cache = DatabaseNamespaceManager.tables.get(table_key, make_cache)
Esempio n. 4
0
    def __init__(self, namespace, table_name, region=None, hash_key='id', **params):

        OpenResourceNamespaceManager.__init__(self, namespace)

        if table_name is None:
            raise MissingCacheParameter('DynamoDB table name required.')

        self._hash_key = hash_key
        self._table = ddb.table.Table(table_name, connection=ddb.connect_to_region(region))
        self._flags = None
        self._item = None
Esempio n. 5
0
    def __init__(self, namespace, table_name, region=None, hash_key='id', **params):

        OpenResourceNamespaceManager.__init__(self, namespace)

        if table_name is None:
            raise MissingCacheParameter('DynamoDB table name required.')

        options = verify_rules(
            dict([(k, v) for k, v in params.iteritems()
                  if k in self._supported_options]),
            self._rules)

        self._hash_key = hash_key
        self._table = ddb.table.Table(
            table_name, connection=ddb.connect_to_region(region, **options))
        self._flags = None
        self._item = None
Esempio n. 6
0
    def __init__(self, namespace, table_name="beaker_cache", **params):
        """Creates a datastore namespace manager"""
        OpenResourceNamespaceManager.__init__(self, namespace)

        def make_cache():
            table_dict = dict(created=db.DateTimeProperty(), accessed=db.DateTimeProperty(), data=db.BlobProperty())
            table = type(table_name, (db.Model,), table_dict)
            return table

        self.table_name = table_name
        self.cache = GoogleNamespaceManager.tables.setdefault(table_name, make_cache())
        self.hash = {}
        self._is_new = False
        self.loaded = False
        self.log_debug = logging.DEBUG >= log.getEffectiveLevel()

        # Google wants namespaces to start with letters, change the namespace
        # to start with a letter
        self.namespace = "p%s" % self.namespace
Esempio n. 7
0
    def __init__(self, namespace, table_name='beaker_cache', **params):
        """Creates a datastore namespace manager"""
        OpenResourceNamespaceManager.__init__(self, namespace)

        def make_cache():
            table_dict = dict(created=db.DateTimeProperty(),
                              accessed=db.DateTimeProperty(),
                              data=db.BlobProperty())
            table = type(table_name, (db.Model,), table_dict)
            return table
        self.table_name = table_name
        self.cache = GoogleNamespaceManager.tables.setdefault(table_name, make_cache())
        self.hash = {}
        self._is_new = False
        self.loaded = False
        self.log_debug = logging.DEBUG >= log.getEffectiveLevel()

        # Google wants namespaces to start with letters, change the namespace
        # to start with a letter
        self.namespace = 'p%s' % self.namespace
Esempio n. 8
0
    def __init__(self,
                 namespace,
                 table_name,
                 region=None,
                 hash_key='id',
                 **params):

        OpenResourceNamespaceManager.__init__(self, namespace)

        if table_name is None:
            raise MissingCacheParameter('DynamoDB table name required.')

        options = verify_rules(
            dict([(k, v) for k, v in params.iteritems()
                  if k in self._supported_options]), self._rules)

        self._hash_key = hash_key
        self._table = ddb.table.Table(table_name,
                                      connection=ddb.connect_to_region(
                                          region, **options))
        self._flags = None
        self._item = None