EventAnalysis  1.0.0
DataStore.hpp
Go to the documentation of this file.
1 /*
2  * DataStore.hpp
3  *
4  * Created on: 14 Dec 2017
5  * Author: Nicola Mori
6  */
7 
8 #ifndef DATASTORE_HPP_
9 #define DATASTORE_HPP_
10 
11 template <class DataType>
12 void DataStore::AddObject(const std::string &name, DataType &obj, const observer_ptr<ObjectProducer> &producer) {
13  InsertionResult ir = ObjectMap::AddObject(name, obj);
14  if (ir != InsertionResult::SUCCESS) {
15  THROW_INSERTION_EXC(ir, name);
16  }
17  if (producer && _remObjTracker) {
18  if (!(_remObjTracker->TrackObject(name, producer))) {
19  ObjectMap::RemoveObject(name);
20  throw Insertion::Error("Can't track object \"" + name + "\"");
21  }
22  }
23 }
24 
25 template <class DataType>
26 void DataStore::AddObject(const std::string &name, observer_ptr<DataType> obj,
27  const observer_ptr<ObjectProducer> &producer) {
28  InsertionResult ir = ObjectMap::AddObject(name, obj);
29  if (ir != InsertionResult::SUCCESS) {
30  THROW_INSERTION_EXC(ir, name);
31  }
32  if (producer && _remObjTracker) {
33  if (!(_remObjTracker->TrackObject(name, producer))) {
34  ObjectMap::RemoveObject(name);
35  throw Insertion::Error("Can't track object \"" + name + "\"");
36  }
37  }
38 }
39 
40 template <class DataType>
41 void DataStore::AddObject(const std::string &name, std::shared_ptr<DataType> obj,
42  const observer_ptr<ObjectProducer> &producer) {
43  InsertionResult ir = ObjectMap::AddObject(name, obj);
44  if (ir != InsertionResult::SUCCESS) {
45  THROW_INSERTION_EXC(ir, name);
46  }
47  if (producer && _remObjTracker) {
48  if (!(_remObjTracker->TrackObject(name, producer))) {
49  ObjectMap::RemoveObject(name);
50  throw Insertion::Error("Can't track object \"" + name + "\"");
51  }
52  }
53 }
54 
55 template <class DataType>
56 void DataStore::AddObject(const std::string &name, std::unique_ptr<DataType> obj,
58  InsertionResult ir = ObjectMap::AddObject(name, std::move(obj));
59  if (ir != InsertionResult::SUCCESS) {
60  THROW_INSERTION_EXC(ir, name);
61  }
62 }
63 
64 template <class DataType> observer_ptr<DataType> DataStore::GetObject(const std::string &name) {
65 
66  // Retrieval of an object. The object will be fetched from the store and
67  // from the data providers (if they exist) in this order.
68  // The retrieve result is the best result obtained in the above steps.
69 
70  // 1. Try to retrieve the requested object from existing ones
71  auto obj = GetFromMap<DataType>(name);
72  if (obj.GetRetrievalResult() == RetrievalResult::SUCCESS) {
73  return obj;
74  }
75 
76  // 2. Try to retrieve the requested object from a registered provider
77  ObjectWrapper w;
78  RetrievalResult result = obj.GetRetrievalResult();
79  // Insert the desired type into the wrapper
80  w = (DataType *)(nullptr);
81  // Retrieve from the provider
82  std::string actualName;
84  result = std::max(GetFromProvider(name, w, actualName, provider), result);
85 
86  // 3. Check the type if the object has been found.
87  if (result == RetrievalResult::SUCCESS) {
88  auto objAfterCast = CastAndWrap<DataType>(name, actualName, std::move(w), provider);
89  if (!objAfterCast) {
90  THROW_RETRIEVAL_EXC(objAfterCast.GetRetrievalResult(), name);
91  }
92  return objAfterCast;
93  } else {
94  if (result == RetrievalResult::NOTFOUND) {
95  // The object is not found in data providers, so let's see if it is produced by an algorithm to eventually
96  // return NOTAVAILABLE.
97  if (IsAlgoProducedObject(name)) {
98  throw Retrieval::NotAvailable("Object \"" + name + "\" is not available");
99  }
100  }
101  THROW_RETRIEVAL_EXC(result, name);
102  }
103 }
104 
105 template <class DataType> ObjPtr<DataType> DataStore::GetFromMap(const std::string &name) {
106  return ObjectMap::GetObject<DataType>(name);
107 }
108 
109 template <class DataType>
110 ObjPtr<DataType> DataStore::CastAndWrap(const std::string &name, const std::string &actualName, ObjectWrapper &&wrapper,
111  observer_ptr<ObjectProducer> provider) {
112  ObjPtr<DataType> retObj;
113  retObj._result = RetrievalResult::SUCCESS;
114 
115  // Object found. Now check for type.
116  retObj = wrapper_cast<DataType>(wrapper);
117  if (retObj) {
118  if (actualName == "") {
119  if (ObjectMap::AddObject(name, std::move(wrapper)) == InsertionResult::DUPLICATE) {
120  // This can happen when an object with the same name but the wrong
121  // type is present in the data store, so that it has not been retrieved
122  // from stored objects in DataStore::GetObject due to a type mismatch but
123  // it prevents the object fetched from the provider to be inserted in the
124  // store
125  retObj._result = RetrievalResult::ERROR;
126  }
127  } else {
128  // name is actually an alias
129  ObjectMap::AddObject(actualName, std::move(wrapper)); // This fails if the aliased object is already present,
130  // so don't check the return value.
131  try {
132  SetAlias(actualName, name);
133  } catch (Insertion::Exception &exc) {
134  ObjectMap::RemoveObject(actualName);
135  retObj = nullptr;
136  retObj._result = RetrievalResult::ERROR;
137  }
138  }
139  } else {
140  retObj._result = RetrievalResult::BADCLASS;
141  }
142  // Add the object to the RemovedObjectsManager
143  if (provider && _remObjTracker) {
144  if (!_remObjTracker->TrackObject(name, provider)) {
145  retObj = nullptr;
146  retObj._result = RetrievalResult::ERROR;
147  }
148  }
149 
150  return retObj;
151 }
152 
153 template <class DataType> observer_ptr<DataType> EventDataStore::GetObject(const std::string &name) {
154 
155  // Retrieval of an object. The object will be fetched from the store, from the cache
156  // (if the cache exists) and from the data providers (if they exist) in this order.
157  // The retrieve result is the best result obtained in the above steps.
158 
159  // 1. Try to retrieve the requested object from existing ones
160  auto obj = GetFromMap<DataType>(name);
161  if (obj.GetRetrievalResult() == RetrievalResult::SUCCESS) {
162  return obj;
163  }
164 
165  ObjectWrapper w;
166  RetrievalResult result = obj.GetRetrievalResult();
167 
168  // 2. Try to retrieve the requested object from the cache
169  if (_eventCache) {
170  result = _eventCache->GetCachedValue(name, w);
171  }
172 
173  // 3. Try to retrieve the requested object from a registered provider
174  std::string actualName;
176  if (result != RetrievalResult::SUCCESS) {
177  // Insert the desired type into the wrapper
178  w = (DataType *)(nullptr);
179  // Retrieve from the provider
180  result = std::max(GetFromProvider(name, w, actualName, provider), result);
181  }
182 
183  // 4. Check the type if the object has been found.
184  if (result == RetrievalResult::SUCCESS) {
185  auto objPtr = CastAndWrap<DataType>(name, actualName, std::move(w), provider);
186  if (!objPtr) {
187  THROW_RETRIEVAL_EXC(objPtr.GetRetrievalResult(), name);
188  }
189  return objPtr;
190  } else {
191  if (result == RetrievalResult::NOTFOUND) {
192  // The object is not found in data providers, so let's see if it is produced by an algorithm to eventually
193  // return NOTAVAILABLE.
194  if (IsAlgoProducedObject(name)) {
195  throw Retrieval::NotAvailable("Object \"" + name + "\" is not available");
196  }
197  }
198  THROW_RETRIEVAL_EXC(result, name);
199  }
200 }
201 
202 #endif /* DATASTORE_HPP_ */
Class describing a pointer to an object retrieved from an ObjectMap.
Definition: ObjPtr.h:31
A smart pointer not owning the wrapped object.
Definition: ObserverPtr.h:28
#define THROW_RETRIEVAL_EXC(retRes, objName)
Definition: RetrievalExceptions.h:50
RetrievalResult
Definition: RetrievalResult.h:16
Exception for generic error in object insertion procedure.
Definition: InsertionExceptions.h:57
Generic wrapper class.
Definition: ObjectWrapper.h:28
Base class for insertion exceptions.
Definition: InsertionExceptions.h:21
InsertionResult
Definition: InsertionResult.h:16
void * wrapper_cast(ObjectWrapper &wrapper, const std::type_info &typeInfo)
Cast function to extract wrapped pointer from wrapper in a type-safe way.
Definition: WrapperCast.cpp:12
Exception for known object but not available at the moment.
Definition: RetrievalExceptions.h:27
#define THROW_INSERTION_EXC(insRes, objName)
Definition: InsertionExceptions.h:62
RetrievalResult _result
Definition: ObjPtr.h:114