"Fossies" - the Fresh Open Source Software Archive

Member "zaqar-7.0.0/zaqar/storage/sqlalchemy/queues.py" (30 Aug 2018, 4299 Bytes) of package /linux/misc/openstack/zaqar-7.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "queues.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 5.0.0_vs_7.0.0.

    1 # Copyright (c) 2014 Red Hat, Inc.
    2 #
    3 # Licensed under the Apache License, Version 2.0 (the "License"); you may not
    4 # use this file except in compliance with the License.  You may obtain a copy
    5 # of the License at
    6 #
    7 #    http://www.apache.org/licenses/LICENSE-2.0
    8 #
    9 # Unless required by applicable law or agreed to in writing, software
   10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   12 # License for the specific language governing permissions and limitations under
   13 # the License.
   14 
   15 import oslo_db.exception
   16 import sqlalchemy as sa
   17 
   18 from zaqar import storage
   19 from zaqar.storage import errors
   20 from zaqar.storage.sqlalchemy import tables
   21 from zaqar.storage.sqlalchemy import utils
   22 
   23 
   24 class QueueController(storage.Queue):
   25 
   26     def _list(self, project, kfilter={}, marker=None,
   27               limit=storage.DEFAULT_QUEUES_PER_PAGE, detailed=False,
   28               name=None):
   29 
   30         if project is None:
   31             project = ''
   32 
   33         fields = [tables.Queues.c.name]
   34         if detailed:
   35             fields.append(tables.Queues.c.metadata)
   36 
   37         if marker:
   38             sel = sa.sql.select(fields, sa.and_(
   39                                 tables.Queues.c.project == project,
   40                                 tables.Queues.c.name > marker))
   41         else:
   42             sel = sa.sql.select(fields, tables.Queues.c.project == project)
   43 
   44         sel = sel.order_by(sa.asc(tables.Queues.c.name)).limit(limit)
   45         records = self.driver.run(sel)
   46 
   47         marker_name = {}
   48 
   49         def it():
   50             for rec in records:
   51                 marker_name['next'] = rec[0]
   52                 yield ({'name': rec[0]} if not detailed
   53                        else
   54                        {'name': rec[0], 'metadata': utils.json_decode(rec[1])})
   55 
   56         yield it()
   57         yield marker_name and marker_name['next']
   58 
   59     def get_metadata(self, name, project):
   60         if project is None:
   61             project = ''
   62 
   63         sel = sa.sql.select([tables.Queues.c.metadata], sa.and_(
   64             tables.Queues.c.project == project,
   65             tables.Queues.c.name == name
   66         ))
   67 
   68         queue = self.driver.run(sel).fetchone()
   69         if queue is None:
   70             raise errors.QueueDoesNotExist(name, project)
   71 
   72         return utils.json_decode(queue[0])
   73 
   74     def _get(self, name, project=None):
   75         try:
   76             return self.get_metadata(name, project)
   77         except errors.QueueDoesNotExist:
   78             return {}
   79 
   80     def _create(self, name, metadata=None, project=None):
   81         if project is None:
   82             project = ''
   83 
   84         try:
   85             smeta = utils.json_encode(metadata or {})
   86             ins = tables.Queues.insert().values(project=project,
   87                                                 name=name,
   88                                                 metadata=smeta)
   89             res = self.driver.run(ins)
   90         except oslo_db.exception.DBDuplicateEntry:
   91             return False
   92 
   93         return res.rowcount == 1
   94 
   95     def _exists(self, name, project):
   96         if project is None:
   97             project = ''
   98 
   99         sel = sa.sql.select([tables.Queues.c.id], sa.and_(
  100                             tables.Queues.c.project == project,
  101                             tables.Queues.c.name == name
  102                             ))
  103         res = self.driver.run(sel)
  104         r = res.fetchone()
  105         res.close()
  106         return r is not None
  107 
  108     def set_metadata(self, name, metadata, project):
  109         if project is None:
  110             project = ''
  111 
  112         update = (tables.Queues.update().
  113                   where(sa.and_(
  114                       tables.Queues.c.project == project,
  115                       tables.Queues.c.name == name)).
  116                   values(metadata=utils.json_encode(metadata)))
  117 
  118         res = self.driver.run(update)
  119 
  120         try:
  121             if res.rowcount != 1:
  122                 raise errors.QueueDoesNotExist(name, project)
  123         finally:
  124             res.close()
  125 
  126     def _delete(self, name, project):
  127         if project is None:
  128             project = ''
  129 
  130         dlt = tables.Queues.delete().where(sa.and_(
  131             tables.Queues.c.project == project,
  132             tables.Queues.c.name == name))
  133         self.driver.run(dlt)
  134 
  135     def _stats(self, name, project):
  136         pass