summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMonty Taylor <mordred@inaugust.com>2019-02-10 18:47:10 +0000
committerMonty Taylor <mordred@inaugust.com>2019-02-10 19:17:38 +0000
commitc9245f61ec6b08c9f45ae62001aa5ada1eebf463 (patch)
treecca3c905f2b27eda39ef3941479c3bf648e15168
parent80d56bccb39426eded1b4b0a3140e911bebb3ed4 (diff)
Add consts to LRU and use std::optional
c++17 (which our compiler supports) has std::optional<T> that can be used for thigns that return a value or nothing (like this) Use that. add in some consts and pass parameters by const reference. use std::uint32_t instead of uint. Initialize it with the constructor initializer list instead of in the body of the constructor, and use uniform initialization syntax to do so. Initialize the cache instantiation using uniform initialization. Use a few more autos so that we don't have to type const as much.
-rw-r--r--m4/opendev_canonical.m42
-rw-r--r--zuul-preview/main.cc38
2 files changed, 17 insertions, 23 deletions
diff --git a/m4/opendev_canonical.m4 b/m4/opendev_canonical.m4
index 9dccd1e..fe09120 100644
--- a/m4/opendev_canonical.m4
+++ b/m4/opendev_canonical.m4
@@ -20,7 +20,7 @@ AC_DEFUN([OPENDEV_CANONICAL_TARGET],[
20 AX_IS_RELEASE(always) 20 AX_IS_RELEASE(always)
21 AM_SILENT_RULES([yes]) 21 AM_SILENT_RULES([yes])
22 22
23 AX_CXX_COMPILE_STDCXX([14],[],[mandatory]) 23 AX_CXX_COMPILE_STDCXX([17],[],[mandatory])
24 AM_PROG_CC_C_O 24 AM_PROG_CC_C_O
25 AC_PROG_CC_STDC 25 AC_PROG_CC_STDC
26 gl_VISIBILITY 26 gl_VISIBILITY
diff --git a/zuul-preview/main.cc b/zuul-preview/main.cc
index 831b456..18f4381 100644
--- a/zuul-preview/main.cc
+++ b/zuul-preview/main.cc
@@ -38,35 +38,29 @@ vector<string> split(const string &in)
38class Cache { 38class Cache {
39 // A queue of hostname, URL pairs. The head of the queue is always 39 // A queue of hostname, URL pairs. The head of the queue is always
40 // the most recently accessed entry, the tail is the least. 40 // the most recently accessed entry, the tail is the least.
41 list<pair<string, string>> queue; 41 list<pair<const string, const string>> queue;
42 42
43 // A map of hostname -> iterator that points into the queue, for 43 // A map of hostname -> iterator that points into the queue, for
44 // quick lookup. 44 // quick lookup.
45 unordered_map<string, list<pair<string, string>>::iterator> map; 45 unordered_map<string, list<pair<const string, const string>>::iterator> map;
46 46
47 // The maximum size of the cache. 47 // The maximum size of the cache.
48 uint size; 48 const uint32_t size;
49 49
50public: 50public:
51 // A constant returned by get if the entry is not found.
52 string notfound = "";
53
54 Cache(uint s) 51 Cache(uint s)
55 : queue {}, map {}, size(0) 52 : queue {}, map {}, size{s}
56 { 53 { }
57 size = s;
58 }
59 54
60 // Lookup the hostname in the cache and return the URL if present, 55 // Lookup the hostname in the cache and return the URL if present.
61 // notfound otherwise. If the entry is present, it is moved to the 56 // If the entry is present, it is moved to the head of the queue.
62 // head of the queue. 57 optional<const string> get(const string &key)
63 string get(string key)
64 { 58 {
65 auto location = map.find(key); 59 auto location = map.find(key);
66 if (location == map.end()) 60 if (location == map.end())
67 return notfound; 61 return {};
68 62
69 pair<string, string> val = *(location->second); 63 auto val = *(location->second);
70 queue.erase(location->second); 64 queue.erase(location->second);
71 queue.push_front(val); 65 queue.push_front(val);
72 cout << "get push " << val.second << endl; 66 cout << "get push " << val.second << endl;
@@ -75,14 +69,14 @@ public:
75 69
76 // Add an entry to the cache. If the cache is full, drop the least 70 // Add an entry to the cache. If the cache is full, drop the least
77 // recently used entry. 71 // recently used entry.
78 void put(string key, string value) 72 void put(const string &key, const string &value)
79 { 73 {
80 auto location = map.find(key); 74 auto location = map.find(key);
81 if (location != map.end()) 75 if (location != map.end())
82 return; 76 return;
83 77
84 if (queue.size() == size) { 78 if (queue.size() == size) {
85 pair<string, string> last = queue.back(); 79 auto last = queue.back();
86 cout << "put pop " << last.second << endl; 80 cout << "put pop " << last.second << endl;
87 queue.pop_back(); 81 queue.pop_back();
88 map.erase(last.first); 82 map.erase(last.first);
@@ -100,7 +94,7 @@ int main(int, char**)
100 web::http::client::http_client client("https://zuul.opendev.org"); 94 web::http::client::http_client client("https://zuul.opendev.org");
101 95
102 string hostname; 96 string hostname;
103 Cache cache = Cache(2); 97 Cache cache{2};
104 while (getline(cin, hostname)) { 98 while (getline(cin, hostname)) {
105 // Expected hostname: 99 // Expected hostname:
106 // site.75031cad206c4014ad7a3387091d15ab.openstack.preview.opendev.org 100 // site.75031cad206c4014ad7a3387091d15ab.openstack.preview.opendev.org
@@ -110,9 +104,9 @@ int main(int, char**)
110 // site.688b70499b9a41a08f498ed6e932960c.openstack 104 // site.688b70499b9a41a08f498ed6e932960c.openstack
111 // site.dbefc23dcc594577a8bfa4db4f9b0a8f.openstack 105 // site.dbefc23dcc594577a8bfa4db4f9b0a8f.openstack
112 106
113 string val = cache.get(hostname); 107 auto val = cache.get(hostname);
114 if (val != cache.notfound) { 108 if (val.has_value()) {
115 cout << val << endl; 109 cout << val.value() << endl;
116 continue; 110 continue;
117 } 111 }
118 112