summaryrefslogtreecommitdiffstats
path: root/conductor/conductor/common/music/model/base.py
blob: d393cbeba2256316e823db1df4f299665b98c993 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#
# -------------------------------------------------------------------------
#   Copyright (c) 2015-2017 AT&T Intellectual Property
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
# -------------------------------------------------------------------------
#

"""Music ORM - Model"""

from abc import ABCMeta
from abc import abstractmethod
import uuid

from oslo_config import cfg
from oslo_log import log as logging
import six

from conductor.common.classes import abstractclassmethod
from conductor.common.classes import classproperty
from conductor.common import db_backend
from conductor.common.music.model import search

LOG = logging.getLogger(__name__)

CONF = cfg.CONF


@six.add_metaclass(ABCMeta)
class Base(object):
    """A custom declarative base ORM-style class.

    Provides some Elixir-inspired shortcuts as well.
    """

    # These must be set in the derived class!
    __tablename__ = None
    __keyspace__ = None

    @classproperty
    def query(cls):  # pylint: disable=E0213
        """Return a query object a la sqlalchemy"""
        return search.Query(cls)

    @classmethod
    def __kwargs(cls):
        """Return common keyword args"""
        kwargs = {
            'keyspace': cls.__keyspace__,
            'table': cls.__tablename__,
        }
        return kwargs

    @classmethod
    def table_create(cls):
        """Create table"""
        kwargs = cls.__kwargs()
        kwargs['schema'] = cls.schema()
        db_backend.DB_API.table_create(**kwargs)

        # Create indexes for the table
        del kwargs['schema']
        if cls.indexes():
            for index in cls.indexes():
                kwargs['index'] = index
                db_backend.DB_API.index_create(**kwargs)

    @abstractclassmethod
    def atomic(cls):
        """Use atomic operations"""
        return False

    @abstractclassmethod
    def schema(cls):
        """Return schema"""
        return cls()

    @classmethod
    def indexes(cls):
        """Return Indexes"""
        pass
        # return cls()

    @abstractclassmethod
    def pk_name(cls):
        """Primary key name"""
        return cls()

    @abstractmethod
    def pk_value(self):
        """Primary key value"""
        pass

    @abstractmethod
    def values(self):
        """Values"""
        pass

    def insert(self):
        """Insert row"""
        kwargs = self.__kwargs()
        kwargs['pk_name'] = self.pk_name()
        kwargs['values'] = self.values()
        kwargs['atomic'] = self.atomic()
        pk_name = kwargs['pk_name']

        if pk_name not in kwargs['values']:
            # TODO(jdandrea): Make uuid4() generation a default method in Base.
            the_id = str(uuid.uuid4())
            kwargs['values'][pk_name] = the_id
            kwargs['pk_value'] = the_id
            setattr(self, pk_name, the_id)
        else:
            kwargs['pk_value'] = kwargs['values'][pk_name]
        response = db_backend.DB_API.row_create(**kwargs)
        return response

    def update(self, condition=None):
        """Update row"""
        kwargs = self.__kwargs()
        kwargs['pk_name'] = self.pk_name()
        kwargs['pk_value'] = self.pk_value()
        kwargs['values'] = self.values()

        # In active-active, all update operations should be atomic
        kwargs['atomic'] = True
        kwargs['condition'] = condition
        # FIXME(jdandrea): Do we need this test/pop clause?
        pk_name = kwargs['pk_name']
        if kwargs['table'] != ('order_locks'):
            if pk_name in kwargs['values']:
                kwargs['values'].pop(pk_name)
        return db_backend.DB_API.row_update(**kwargs)

    def delete(self):
        """Delete row"""
        kwargs = self.__kwargs()
        kwargs['pk_name'] = self.pk_name()
        kwargs['pk_value'] = self.pk_value()
        kwargs['atomic'] = self.atomic()
        db_backend.DB_API.row_delete(**kwargs)

    @classmethod
    def filter_by(cls, **kwargs):
        """Filter objects"""
        return cls.query.filter_by(**kwargs)  # pylint: disable=E1101

    def flush(self, *args, **kwargs):
        """Flush changes to storage"""
        # TODO(jdandrea): Implement in music? May be a no-op
        pass

    def as_dict(self):
        """Return object representation as a dictionary"""
        return dict((k, v) for k, v in self.__dict__.items()
                    if not k.startswith('_'))


def create_dynamic_model(keyspace, classname, baseclass):
    """Create a dynamic ORM class with a custom keyspace/class/table.

    Given a keyspace, a camelcase class name, and a base class
    derived from Base, create a dynamic model that adopts a
    table name based on a lower-cased version of the class name,
    then create the table in the keyspace if it doesn't already exist.
    If the baseclass already has __tablename__ or __keyspace__ set, those
    will take precedence. Set those to None to use keyspace/classname here.
    """

    # The comma after baseclass belongs there! Tuple of length 1.
    model = type(
        classname, (baseclass,), {
            '__tablename__': baseclass.__tablename__ or classname.lower(),
            '__keyspace__': baseclass.__keyspace__ or keyspace})
    model.table_create()
    return model