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