CUBRID Engine  latest
load_class_registry.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /*
20  * load_class_registry.cpp - class registry for server loaddb
21  */
22 
23 #include "load_class_registry.hpp"
24 
25 #include <algorithm>
26 
27 namespace cubload
28 {
29 
30  // attribute
31  attribute::attribute (const std::string &name, std::size_t index, or_attribute *repr)
32  : m_name (name)
33  , m_index (index)
34  , m_repr (repr)
35  {
36  //
37  }
38 
39  const char *
41  {
42  return m_name.c_str ();
43  }
44 
45  std::size_t
47  {
48  return m_index;
49  }
50 
51  const or_attribute &
53  {
54  return *m_repr;
55  }
56 
57  const tp_domain &
59  {
60  return *m_repr->domain;
61  }
62 
63  // class_entry
64  class_entry::class_entry (std::string &class_name, OID &class_oid, class_id clsid,
65  std::vector<const attribute *> &attributes)
66  : m_clsid (clsid)
67  , m_class_oid (class_oid)
68  , m_class_name (std::move (class_name))
69  , m_attributes (attributes.size ())
70  , m_is_ignored (false)
71  {
72  std::copy (attributes.begin (), attributes.end (), m_attributes.begin ());
73  }
74 
75  class_entry::class_entry (std::string &class_name, class_id clsid, bool is_ignored)
76  : m_clsid (clsid)
77  , m_class_name (class_name)
78  , m_is_ignored (is_ignored)
79  {
80  ; // Do nothing
81  }
82 
84  {
85  for (const attribute *attr : m_attributes)
86  {
87  delete attr;
88  }
89  m_attributes.clear ();
90  }
91 
92  const OID &
94  {
95  return m_class_oid;
96  }
97 
98  const char *
100  {
101  return m_class_name.c_str ();
102  }
103 
104  const attribute &
105  class_entry::get_attribute (std::size_t index) const
106  {
107  // assert that index is within the range
108  assert (index < m_attributes.size ());
109 
110  return *m_attributes[index];
111  }
112 
113  size_t
115  {
116  return m_attributes.size ();
117  }
118 
119  bool
121  {
122  return m_is_ignored;
123  }
124 
125  // class_registry
127  : m_mutex ()
128  , m_class_by_id ()
129  {
130  //
131  }
132 
134  {
135  for (auto &it : m_class_by_id)
136  {
137  delete it.second;
138  }
139  m_class_by_id.clear ();
140  }
141 
142  void
143  class_registry::register_class (const char *class_name, class_id clsid, OID class_oid,
144  std::vector<const attribute *> &attributes)
145  {
146  std::unique_lock<std::mutex> ulock (m_mutex);
147 
148  const class_entry *c_entry = get_class_entry_without_lock (clsid);
149  if (c_entry != NULL)
150  {
151  // class was registered already
152  return;
153  }
154 
155  std::string c_name (class_name);
156  c_entry = new class_entry (c_name, class_oid, clsid, attributes);
157 
158  m_class_by_id.insert (std::make_pair (clsid, c_entry));
159  }
160 
161  void
163  {
164  std::unique_lock<std::mutex> ulock (m_mutex);
165 
166  assert (cls_entry != NULL);
167  assert (cls_entry->is_ignored ());
168 
169  m_class_by_id.insert (std::make_pair (cls_id, cls_entry));
170  }
171 
172  const class_entry *
174  {
175  std::unique_lock<std::mutex> ulock (m_mutex);
176 
177  return get_class_entry_without_lock (clsid);
178  }
179 
180  void
181  class_registry::get_all_class_entries (std::vector<const class_entry *> &entries) const
182  {
183  std::transform (m_class_by_id.begin (), m_class_by_id.end (), std::back_inserter (entries),
184  std::bind (&class_map::value_type::second, std::placeholders::_1));
185  }
186 
187  const class_entry *
189  {
190  auto found = m_class_by_id.find (clsid);
191  if (found != m_class_by_id.end ())
192  {
193  return found->second;
194  }
195  else
196  {
197  return NULL;
198  }
199  }
200 
201 } // namespace cubload
class_entry()=delete
~class_entry()
const char * get_class_name() const
void register_class(const char *class_name, class_id clsid, OID class_oid, std::vector< const attribute * > &attributes)
const class_entry * get_class_entry(class_id clsid)
const attribute & get_attribute(std::size_t index) const
#define assert(x)
const tp_domain & get_domain() const
OID m_class_oid
const std::string m_name
void register_ignored_class(class_entry *cls_entry, class_id cls_id)
#define NULL
Definition: freelistheap.h:34
std::size_t get_index() const
class_id m_clsid
std::string m_class_name
const char * get_name() const
const std::size_t m_index
size_t get_attributes_size() const
std::vector< const attribute * > m_attributes
const bool m_is_ignored
const class_entry * get_class_entry_without_lock(class_id clsid)
bool is_ignored() const
const or_attribute & get_repr() const
const OID & get_class_oid() const
const or_attribute * m_repr
int class_id
Definition: load_common.hpp:40
void get_all_class_entries(std::vector< const class_entry * > &entries) const