1 /** @file
2 
3   Inlines base64 images from the ATS cache
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 #pragma once
25 
26 #include <cassert>
27 #include <string>
28 
29 #include "ts.h"
30 #include "util.h"
31 
32 namespace ats
33 {
34 namespace cache
35 {
36   struct Key {
~Keyats::cache::Key37     ~Key()
38     {
39       assert(key_ != nullptr);
40       TSCacheKeyDestroy(key_);
41     }
42 
Keyats::cache::Key43     Key() : key_(TSCacheKeyCreate()) { assert(key_ != nullptr); }
44     Key(const Key &) = delete;
45     Key &operator=(const Key &) = delete;
46 
Keyats::cache::Key47     explicit Key(const std::string &s) : key_(TSCacheKeyCreate())
48     {
49       assert(key_ != nullptr);
50       CHECK(TSCacheKeyDigestSet(key_, s.c_str(), s.size()));
51     }
52 
53     TSCacheKey
keyats::cache::Key54     key() const
55     {
56       return key_;
57     }
58 
59     TSCacheKey key_;
60   };
61 
62   template <class T> struct Read {
63     typedef Read<T> Self;
64 
65     T t_;
66 
Readats::cache::Read67     template <class... A> Read(A &&... a) : t_(std::forward<A>(a)...) {}
68     static int
handleats::cache::Read69     handle(TSCont c, TSEvent e, void *d)
70     {
71       Self *const self = static_cast<Self *const>(TSContDataGet(c));
72       assert(self != nullptr);
73       switch (e) {
74       case TS_EVENT_CACHE_OPEN_READ:
75         assert(d != nullptr);
76         self->t_.hit(static_cast<TSVConn>(d));
77         break;
78       case TS_EVENT_CACHE_OPEN_READ_FAILED:
79         self->t_.miss();
80         break;
81       default:
82         assert(false); // UNREACHABLE.
83         break;
84       }
85       delete self;
86       TSContDataSet(c, nullptr);
87       TSContDestroy(c);
88       return TS_SUCCESS;
89     }
90   };
91 
92   template <class T, class... A>
93   void
fetch(const std::string & k,A &&...a)94   fetch(const std::string &k, A &&... a)
95   {
96     const Key key(k);
97     const TSCont continuation = TSContCreate(Read<T>::handle, TSMutexCreate());
98     assert(continuation != nullptr);
99     TSContDataSet(continuation, new Read<T>(std::forward<A>(a)...));
100     TSCacheRead(continuation, key.key());
101   }
102 
103   struct Write {
104     const std::string content_;
105     io::IO *out_;
106     TSVConn vconnection_;
107 
~Writeats::cache::Write108     ~Write()
109     {
110       if (out_ != nullptr) {
111         delete out_;
112       }
113     }
114 
Writeats::cache::Write115     Write(std::string &&s) : content_(std::move(s)), out_(nullptr), vconnection_(nullptr) {}
116     static int handle(TSCont, TSEvent, void *);
117   };
118 
119   void write(const std::string &, std::string &&);
120 
121 } // namespace cache
122 } // namespace ats
123