3 #if !defined(APINOCACHE) && !defined(XXH_DATLAS) 4 #include "import/cache.hpp" 5 #include "import/lru_cache_policy.hpp" 6 #define XXH_NAMESPACE DATLAS 7 #define XXH_IMPLEMENTATION 9 #if defined(__x86_64__) 10 #define XXH_VECTOR XXH_SSE2 11 #elif defined(__aarch64__) 12 #define XXH_VECTOR XXH_NEON 14 #include "import/xxhash.h" 32 namespace deviceatlas {
38 p_read_fn(
void *a,
size_t max,
char *buf) {
39 std::ifstream *s =
static_cast<std::ifstream *
>(a);
45 p_setpos_fn(
void *a, off_t pos) {
46 std::ifstream *s =
static_cast<std::ifstream *
>(a);
47 s->seekg(pos, s->beg);
48 return (s->tellg() == -1 ? DA_SYS : DA_OK);
53 #if !defined(APINOCACHE) 54 using DaCachePair = std::pair<std::map<size_t, PVPair>, std::map<std::string, size_t>>;
55 using DaCache = caches::fixed_sized_cache<XXH64_hash_t, DaCachePair, caches::LRUCachePolicy>;
58 constexpr
const char *
const defaultCl =
"DAPROPS";
59 static da_property_decl_t defaultep[1] = {{
nullptr, da_type_t(0)}};
70 DwExtractFn dwextract;
75 DeviceDwConfig(std::string _path = std::string(), std::string _url = std::string(),
76 std::chrono::time_point<std::chrono::system_clock> *_sc =
nullptr,
77 da_property_decl_t *_ep = defaultep, DwProcFn _dwproc =
nullptr,
78 DwExtractFn _dwextract =
nullptr, DwNotifyFn _dwnotify =
nullptr,
79 DwLogFn _dwlog =
nullptr) :
83 , dwextract(_dwextract)
87 if (path !=
"" && url !=
"") {
90 auto time = std::chrono::system_clock::to_time_t(*_sc);
91 gmtime_r(&time, &inst.info.rtm);
98 inst.info.url = strdup(url.c_str());
99 inst.info.path = strdup(path.c_str());
103 inst.dwproc = *dwproc.target<dw_proc_fn>();
105 inst.dwproc = curldwproc;
107 inst.dwextract = *dwextract.target<dw_extract_fn>();
109 inst.dwextract = dadwextract;
111 inst.dwnotify_n = *dwnotify.target<dw_notify_n_fn>();
113 inst.dwlog = *dwlog.target<dw_log_fn>();
127 DeviceConfig(
const char *_clname = defaultCl,
size_t _cache_size = 0) :
128 clname(_clname), cache_size(_cache_size) {
129 memset(&inst, 0,
sizeof(inst));
144 #if !defined(APINOCACHE) 145 std::shared_ptr<DaCache> ccache;
152 inst.dcfg = cfg.dwcfg.inst;
155 #if !defined(APINOCACHE) 156 if (cfg.cache_size > 0) {
157 if (cfg.cache_size > DA_CACHE_MAX)
158 cfg.cache_size = DA_CACHE_MAX;
159 ccache = std::shared_ptr<DaCache>(
new DaCache(cfg.cache_size, caches::LRUCachePolicy<XXH64_hash_t>()));
166 void loadDataFromFile(
const char *)
override;
167 void loadDataFromMem(
const char *) noexcept(
false);
168 void download() noexcept(
false);
169 time_t getDataGeneration()
const noexcept;
170 std::string getDataGenerationIso8601()
const noexcept;
171 bool dump(
const char *);
172 const da_dwatlas_t atlas() {
return inst; }
182 const HeaderMap ↦
185 #if !defined(APINOCACHE) 188 std::array<da_evidence_t, 64> ev;
206 #if !defined(APINOCACHE) 207 std::shared_ptr<DaCache> ccache;
212 std::map<size_t, PVPair> ires;
213 void pvpfill()
const {
214 if (!empty() && vres.size() == 0) {
216 for (
auto e = begin(); e != end(); ++e, ++i) {
220 e <<
"invalid property at ";
234 #if __cplusplus >= 201703L 253 const PVPair propdict()
const;
256 const PVPair operator * ()
const;
257 const PVPair *operator -> ();
267 size_t size()
const {
return sz; }
268 bool empty()
const {
return size() == 0; }
269 bool contains(
const char *propname) {
270 return find(propname) != this->end();
276 bool contains(
const char *propname,
const T value) {
277 static_assert(std::is_integral<T>::value,
"invalid type");
278 auto p = find(propname);
279 if (p == this->end())
281 return p->second->u.longval == value;
291 #if __cplusplus >= 201703L 295 std::optional<const Value *>
operator[](std::string_view)
const noexcept;
299 inline bool DeliveryContext::contains<const char *>(
const char *propname,
const char *value) {
300 auto p = find(propname);
301 if (p == this->end())
303 return strcmp(p->second->u.strval, value) == 0;
314 void operator()(ci_searchrequest_t *o)
const {
320 void operator()(ci_searchresult_t *o)
const {
332 std::shared_ptr<ci_atlas_t> inst;
336 auto ptr =
new char[ci_atlas_size()];
337 inst = std::shared_ptr<ci_atlas_t>(
new (ptr) ci_atlas_t, [](ci_atlas_t *o) {
delete [] o; });
340 void loadDataFromFile(
const char *)
override;
350 const HeaderMap ↦
351 std::unique_ptr<ci_searchrequest_t, dropreq> ev;
367 std::unique_ptr<ci_searchresult_t, dropres> dv;
368 std::map<size_t, PVPairSet> ires;
369 void pvpfill()
const {
370 if (!empty() && vres.size() == 0) {
372 for (
auto e = begin(); e != end(); ++e, ++i) {
376 e <<
"invalid property at";
395 const PVPairSet propdict()
const;
398 const PVPairSet operator * ()
const;
399 const PVPairSet *operator -> ();
409 size_t size()
const {
return set ? ci.inst.get()->property_count : 0; }
410 bool empty()
const {
return size() == 0; }
411 bool contains(
const char *propname) {
412 return find(propname) != this->end();
414 const std::string realip()
const {
return dv->u.ei; }
418 const valueset
operator[](
const Property &)
const noexcept;
422 const valueset
operator[](
const char *)
const noexcept;
423 #if __cplusplus >= 201703L 427 std::optional<const valueset>
operator[](std::string_view)
const noexcept;
Definition: devatlas.h:313
Definition: devatlas.h:319
DeliveryContext class for triggering the API lookup and acts as a container map itself to hold the pr...
Definition: devatlas.h:363
Abstract type to map a property.
Definition: common_priv.h:43
Definition: devatlas.h:388
Definition: devatlas.h:246
Definition: common_priv.h:33
DeviceConfig class to hold the API configuration regarding cache size and the client cookie name...
Definition: devatlas.h:122
Definition: common_priv.h:173
Definition: common_priv.h:152
DeviceAtlas class represents the API data mapped to memory through reading a JSON file or a memory ma...
Definition: devatlas.h:138
DeliveryContext class for triggering the API lookup and acts as a container map itself to hold the pr...
Definition: devatlas.h:201
const Value * operator[](const Property &) const noexcept
[] operator overrides return nullptr if property index is non-existent
Carrier class represents the API data mapped to memory through reading a binary file.
Definition: devatlas.h:329
Definition: common_priv.h:139
const valueset operator[](const Property &) const noexcept
[] operator overrides return nullptr if property index is non-existent
Optional download data config offers single and scheduled download modes using standard C++ type to d...
Definition: devatlas.h:66
bool contains(const char *propname, const T value)
checks if propname equals to value
Definition: devatlas.h:276
Abstract type representing a property's value.
Definition: common_priv.h:59