]> git.tdb.fi Git - builder.git/blob - source/cache.cpp
Use default member initializers and constructor delegation
[builder.git] / source / cache.cpp
1 #include <msp/core/maputils.h>
2 #include <msp/fs/dir.h>
3 #include <msp/fs/stat.h>
4 #include <msp/fs/utils.h>
5 #include <msp/io/file.h>
6 #include <msp/io/print.h>
7 #include <msp/strings/utils.h>
8 #include "builder.h"
9 #include "cache.h"
10 #include "sourcepackage.h"
11 #include "target.h"
12
13 using namespace std;
14 using namespace Msp;
15
16 namespace {
17
18 unsigned read_count(IO::Base &in)
19 {
20         unsigned char head = in.get();
21         if((head&0xC0)==0xC0)
22         {
23                 unsigned char tail = in.get();
24                 return (head&0x3F)<<8 | tail;
25         }
26         else
27                 return head;
28 }
29
30 string read_string(IO::Base &in)
31 {
32         unsigned len = read_count(in);
33         char buf[0x4000];
34         len = in.read(buf, len);
35         return string(buf, len);
36 }
37
38 void write_count(IO::Base &out, unsigned count)
39 {
40         if(count<0xC0)
41                 out.put(count);
42         else if(count<0x4000)
43         {
44                 out.put(0xC0 | (count>>8));
45                 out.put(count&0xFF);
46         }
47         else
48                 throw invalid_argument("write_count");
49 }
50
51 void write_string(IO::Base &out, const string &str)
52 {
53         write_count(out, str.size());
54         out.write(str);
55 }
56
57 }
58
59
60 Cache::Cache(SourcePackage &p):
61         package(p),
62         filename(package.get_temp_directory()/"../cache")
63 { }
64
65 void Cache::set_value(const Target *tgt, const string &k, const string &v)
66 {
67         Values vl;
68         vl.push_back(v);
69         set_values(tgt, k, vl);
70 }
71
72 void Cache::append_value(const Target *tgt, const string &k, const string &v)
73 {
74         Key key(tgt->get_name(), k);
75         auto i = data.find(key);
76         if(i==data.end())
77                 i = data.insert({ key, Values() }).first;
78         i->second.push_back(v);
79         changed = true;
80         package.get_builder().get_logger().log("cache", "Updated key %s %s+ %s", tgt->get_name(), k, v);
81 }
82
83 void Cache::set_values(const Target *tgt, const string &k, const Values &v)
84 {
85         data[Key(tgt->get_name(), k)] = v;
86         changed = true;
87         package.get_builder().get_logger().log("cache", "Updated key %s %s: %s", tgt->get_name(), k, join(v.begin(), v.end()));
88 }
89
90 const string &Cache::get_value(const Target *tgt, const string &k)
91 {
92         const Values &values = get_values(tgt, k);
93         if(values.empty())
94                 throw logic_error("values.empty()");
95         return values.front();
96 }
97
98 const Cache::Values &Cache::get_values(const Target *tgt, const string &k)
99 {
100         return get_item(data, Key(tgt->get_name(), k));
101 }
102
103 bool Cache::has_key(const Target *tgt, const string &k)
104 {
105         return data.count(Key(tgt->get_name(), k));
106 }
107
108 void Cache::load()
109 {
110         if(FS::Stat st = FS::stat(filename))
111         {
112                 package.get_builder().get_logger().log("files", "Reading %s", filename);
113                 IO::BufferedFile in(filename.str());
114
115                 while(!in.eof())
116                 {
117                         Key key;
118                         key.first = read_string(in);
119                         key.second = read_string(in);
120                         if(key.first.empty() || key.second.empty())
121                                 break;
122                         Values &values = data[key];
123                         for(unsigned count = read_count(in); count; --count)
124                                 values.push_back(read_string(in));
125                         package.get_builder().get_logger().log("cache", "Loaded key %s %s: %s", key.first, key.second, join(values.begin(), values.end()));
126                 }
127
128                 mtime = st.get_modify_time();
129         }
130 }
131
132 void Cache::save() const
133 {
134         if(data.empty() || !changed)
135                 return;
136
137         FS::Path dir = FS::dirname(filename);
138         if(!FS::exists(dir))
139                 FS::mkpath(dir, 0755);
140         package.get_builder().get_logger().log("files", "Writing %s", filename);
141         IO::BufferedFile out(filename.str(), IO::M_WRITE);
142
143         for(const auto &kvp: data)
144         {
145                 write_string(out, kvp.first.first);
146                 write_string(out, kvp.first.second);
147                 write_count(out, kvp.second.size());
148                 for(const string &v: kvp.second)
149                         write_string(out, v);
150         }
151
152         changed = false;
153 }