summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames E. Blair <jeblair@redhat.com>2019-02-10 10:04:44 -0800
committerJames E. Blair <jeblair@redhat.com>2019-02-10 10:04:44 -0800
commita5f2e260951edfea837c31bd615ee0cae543b75b (patch)
tree0b2502fc2190e005b8fd0ae005c6ca1832bab245
parent31d726c6e6aa02957e6885a2c733ea0288f60525 (diff)
Add LRU cache
-rw-r--r--zuul-preview/main.cc66
1 files changed, 64 insertions, 2 deletions
diff --git a/zuul-preview/main.cc b/zuul-preview/main.cc
index fb497bf..be74564 100644
--- a/zuul-preview/main.cc
+++ b/zuul-preview/main.cc
@@ -20,6 +20,7 @@
20#include <config.h> 20#include <config.h>
21#include <pthread.h> 21#include <pthread.h>
22#include <cpprest/http_client.h> 22#include <cpprest/http_client.h>
23#include <bits/stdc++.h>
23 24
24using namespace std; 25using namespace std;
25 26
@@ -34,16 +35,73 @@ vector<string> split(const string &in)
34 return parts; 35 return parts;
35} 36}
36 37
38class Cache {
39 list<pair<string, string>> queue;
40 unordered_map<string, list<pair<string, string>>::iterator> map;
41 uint size;
42
43public:
44 string notfound = "";
45
46 Cache(uint s)
47 : queue {}, map {}, size(0)
48 {
49 size = s;
50 }
51
52 string get(string key)
53 {
54 auto location = map.find(key);
55 if (location == map.end())
56 return notfound;
57
58 pair<string, string> val = *(location->second);
59 queue.erase(location->second);
60 queue.push_front(val);
61 cout << "get push " << val.second << endl;
62 return val.second;
63 }
64
65 void put(string key, string value)
66 {
67 auto location = map.find(key);
68 if (location != map.end())
69 return;
70
71 if (queue.size() == size) {
72 pair<string, string> last = queue.back();
73 cout << "put pop " << last.second << endl;
74 queue.pop_back();
75 map.erase(last.first);
76 }
77
78 cout << "put push " << value << endl;
79 queue.push_front(make_pair(key, value));
80 map[key] = queue.begin();
81 }
82};
83
84
37int main(int, char**) 85int main(int, char**)
38{ 86{
39 web::http::client::http_client client("https://zuul.opendev.org"); 87 web::http::client::http_client client("https://zuul.opendev.org");
40 88
41 string hostname; 89 string hostname;
90 Cache cache = Cache(2);
42 while (getline(cin, hostname)) { 91 while (getline(cin, hostname)) {
43 // Expected hostname: 92 // Expected hostname:
44 // site.75031cad206c4014ad7a3387091d15ab.openstack.preview.opendev.org 93 // site.75031cad206c4014ad7a3387091d15ab.openstack.preview.opendev.org
45 // Apache will drop "preview.opendev.org", so our expected input will be: 94 // Apache will drop "preview.opendev.org", so our expected input will be:
46 // site.75031cad206c4014ad7a3387091d15ab.openstack 95 // site.75031cad206c4014ad7a3387091d15ab.openstack
96 // site.7c16d914db5a4c4b91cd9a31d119dd48.openstack
97 // site.688b70499b9a41a08f498ed6e932960c.openstack
98 // site.dbefc23dcc594577a8bfa4db4f9b0a8f.openstack
99
100 string val = cache.get(hostname);
101 if (val != cache.notfound) {
102 cout << val << endl;
103 continue;
104 }
47 105
48 auto parts = split(hostname); 106 auto parts = split(hostname);
49 if (parts.size() < 3) { 107 if (parts.size() < 3) {
@@ -53,9 +111,11 @@ int main(int, char**)
53 auto artifact = parts[0]; 111 auto artifact = parts[0];
54 auto buildid = parts[1]; 112 auto buildid = parts[1];
55 auto tenant = parts[2]; 113 auto tenant = parts[2];
114 /*
56 cout << artifact << endl 115 cout << artifact << endl
57 << buildid << endl 116 << buildid << endl
58 << tenant << endl; 117 << tenant << endl;
118 */
59 119
60 // 75031cad206c4014ad7a3387091d15ab 120 // 75031cad206c4014ad7a3387091d15ab
61 auto uri = web::uri_builder("/api/tenant/" + tenant + "/build"); 121 auto uri = web::uri_builder("/api/tenant/" + tenant + "/build");
@@ -64,11 +124,13 @@ int main(int, char**)
64 web::http::methods::GET, uri.to_string()).get(); 124 web::http::methods::GET, uri.to_string()).get();
65 // body is a web::json::value 125 // body is a web::json::value
66 auto body = response.extract_json().get(); 126 auto body = response.extract_json().get();
67 cout << response.status_code() << endl; 127 //cout << response.status_code() << endl;
68 cout << body.serialize() << endl; 128 //cout << body.serialize() << endl;
69 129
70 // TODO: use artifact 130 // TODO: use artifact
71 // body["log_url"].as_string() returns a const std::string& 131 // body["log_url"].as_string() returns a const std::string&
72 cout << body["log_url"].as_string() << endl; 132 cout << body["log_url"].as_string() << endl;
133
134 cache.put(hostname, body["log_url"].as_string());
73 } 135 }
74} 136}