OSDN Git Service

Restrain trace messages.
[fukui-no-namari/dialektos.git] / src / thread_idx_cache.cxx
1 /*
2  * Copyright (C) 2009 by Aiwota Programmer
3  * aiwotaprog@tetteke.tk
4  *
5  * This file is part of Dialektos.
6  *
7  * Dialektos is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * Dialektos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Dialektos.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 #include "thread_idx_cache.hxx"
23
24 #include <boost/archive/xml_iarchive.hpp>
25 #include <boost/archive/xml_oarchive.hpp>
26 #include <boost/archive/binary_iarchive.hpp>
27 #include <boost/archive/binary_oarchive.hpp>
28 #include <boost/serialization/vector.hpp>
29 #include <boost/filesystem.hpp>
30 #include <boost/foreach.hpp>
31 #include <boost/xpressive/xpressive.hpp>
32 #include <boost/unordered_map.hpp>
33 #include <boost/unordered_set.hpp>
34 #include <boost/lexical_cast.hpp>
35 #include <vector>
36 #include <fstream>
37 #include <iostream>
38 #include "thread_idx.hxx"
39 #include "misc.hxx"
40
41
42 namespace dialektos {
43
44
45 std::vector<ThreadIdxCache> ThreadIdxCache::from_xml(
46     const boost::filesystem::path& cache_xml) {
47   std::vector<ThreadIdxCache> cache_vector;
48
49   if (boost::filesystem::exists(cache_xml) &&
50       boost::filesystem::is_regular_file(cache_xml)) {
51     std::ifstream ifs(cache_xml.file_string().c_str());
52     try {
53       boost::archive::binary_iarchive ia(ifs);
54       ia >> cache_vector;
55     } catch (const boost::archive::archive_exception& e) {
56       // TODO thread safety.
57       std::cerr << "ThreadIdxCache::from_xml(): " << e.what() << std::endl;
58     }
59     ifs.close();
60   }
61
62   return cache_vector;
63 }
64
65 void ThreadIdxCache::to_xml(const boost::filesystem::path& cache_xml,
66     const std::vector<ThreadIdxCache>& cache) {
67   if (cache.empty()) return;
68
69   if (!misc::create_directories(cache_xml.parent_path())) return;
70   std::ofstream ofs(cache_xml.file_string().c_str());
71   try {
72     boost::archive::binary_oarchive oa(ofs);
73     oa << cache;
74   } catch (const boost::archive::archive_exception& e) {
75     std::cerr << "ThreadIdxCache::to_xml(): " << e.what() << std::endl;
76   }
77   ofs.close();
78 }
79
80 std::vector<ThreadIdxCache> ThreadIdxCache::from_directory(
81     const boost::filesystem::path& idx_dir) {
82
83   const boost::filesystem::path cache_xml = idx_dir / ".cache";
84   std::vector<ThreadIdxCache> cache_vector = ThreadIdxCache::from_xml(cache_xml);
85
86   boost::unordered_map<ThreadID, ThreadIdxCache> caches;
87   BOOST_FOREACH(const ThreadIdxCache& cache, cache_vector) {
88     caches.insert(std::make_pair(cache.id_, cache));
89   }
90   idx_dir_scan(idx_dir, caches);
91
92   cache_vector.clear();
93   typedef std::pair<ThreadID, ThreadIdxCache> PairType;
94   BOOST_FOREACH(const PairType& cache, caches) {
95     cache_vector.push_back(cache.second);
96   }
97
98   to_xml(cache_xml, cache_vector);
99
100   return cache_vector;
101 }
102
103 boost::unordered_set<ThreadIdxCache::ThreadID> ThreadIdxCache::get_exist_ids(
104     const boost::filesystem::path& idx_dir) {
105   using namespace boost::xpressive;
106   const sregex regex = (s1=-repeat<9, 10>(_d)) >> ".xml";
107
108   boost::unordered_set<ThreadID> exist_ids;
109
110   if (!boost::filesystem::exists(idx_dir) ||
111       !boost::filesystem::is_directory(idx_dir))
112     return exist_ids;
113
114   try {
115     const boost::filesystem::directory_iterator it_end;
116     for (boost::filesystem::directory_iterator it(idx_dir); it != it_end; ++it) {
117
118       const boost::filesystem::path leaf = it->path();
119       if (!boost::filesystem::is_regular_file(leaf)) continue;
120
121       const std::string filename = leaf.filename();
122       smatch what;
123       if (!regex_match(filename, what, regex)) continue;
124       const ThreadID id = what[1];
125       exist_ids.insert(id);
126     }
127   } catch(const boost::filesystem::filesystem_error& e) {
128     std::cerr << e.what() << std::endl;
129   }
130   return exist_ids;
131 }
132
133 std::vector<ThreadIdxCache::ThreadID> ThreadIdxCache::get_deleted_ids(
134     const boost::unordered_map<ThreadID, ThreadIdxCache>& caches,
135     const boost::unordered_set<ThreadID>& exist_ids,
136     const std::string& prefix) {
137   typedef std::pair<ThreadID, ThreadIdxCache> PairType;
138   std::vector<ThreadID> deleted_ids;
139   BOOST_FOREACH(const PairType& pair, caches) {
140     const std::string& cached = pair.first;
141     if (prefix != cached.substr(0, 3)) continue;
142     if (exist_ids.find(cached) == exist_ids.end())
143       deleted_ids.push_back(cached);
144   }
145   return deleted_ids;
146 }
147
148 void ThreadIdxCache::remove_deleted_ids(
149     boost::unordered_map<ThreadID, ThreadIdxCache>& caches,
150     const std::vector<ThreadID>& deleted_ids) {
151   BOOST_FOREACH(const ThreadID& id, deleted_ids) {
152     std::cout << "removed " << id << std::endl;
153     caches.erase(id);
154   }
155 }
156
157 void ThreadIdxCache::merge_idx(const boost::filesystem::path& idx_dir,
158     const boost::unordered_set<ThreadID>& exist_ids,
159     boost::unordered_map<ThreadID, ThreadIdxCache>& caches) {
160
161   BOOST_FOREACH(const ThreadID& id, exist_ids) {
162
163     const boost::filesystem::path leaf = idx_dir / (id + ".xml");
164     const std::time_t last_modified = boost::filesystem::last_write_time(leaf);
165     boost::unordered_map<ThreadID, ThreadIdxCache>::iterator it =  caches.find(id);
166     if (it != caches.end()) {
167       const ThreadIdxCache& cache = it->second;
168       const std::time_t cache_last_modified =
169         boost::lexical_cast<std::time_t>(cache.idx_last_modified_);
170       if (last_modified <= cache_last_modified) continue;
171     }
172
173 //    std::cout << "modified:" << id << std::endl;
174     ThreadIdx idx = ThreadIdx::from_xml(leaf);
175     if (idx.last_modified_.empty()) continue;
176     ThreadIdxCache& cache = caches[id];
177     cache.id_ = id;
178     cache.idx_last_modified_ = boost::lexical_cast<std::string>(last_modified);
179     cache.line_count_ = idx.line_count_;
180     cache.title_ = idx.title_;
181   }
182 }
183
184 void ThreadIdxCache::idx_dir_scan(const boost::filesystem::path& idx_dir,
185     boost::unordered_map<ThreadID, ThreadIdxCache>& caches) {
186
187   std::vector<DirectoryTimeStamp> _directories =
188     directory_timestamp_from_xml(idx_dir / "dirs.xml");
189   boost::unordered_map<std::string, std::time_t> directories;
190   BOOST_FOREACH(const DirectoryTimeStamp& dir, _directories) {
191     directories[dir.filename] = dir.last_modified;
192   }
193
194   try {
195     const boost::filesystem::directory_iterator it_end;
196     for (boost::filesystem::directory_iterator it(idx_dir);
197         it != it_end; ++it) {
198
199       const boost::filesystem::path sub_dir = it->path();
200       if (!boost::filesystem::is_directory(sub_dir)) continue;
201
202       const std::string filename = sub_dir.filename();
203       const std::time_t last_modified =
204         boost::filesystem::last_write_time(sub_dir);
205       if (directories.find(filename) != directories.end() &&
206           directories[filename] == last_modified) {
207         continue;
208       }
209
210       directories[filename] = last_modified;
211
212       const boost::unordered_set<ThreadID> exist_ids = get_exist_ids(sub_dir);
213       const std::vector<ThreadID> deleted_ids =
214         get_deleted_ids(caches, exist_ids, sub_dir.filename());
215       remove_deleted_ids(caches, deleted_ids);
216       merge_idx(sub_dir, exist_ids, caches);
217
218     }
219   } catch(const boost::filesystem::filesystem_error& e) {
220     std::cerr << e.what() << std::endl;
221   }
222
223   _directories.clear();
224   typedef boost::unordered_map<std::string, std::time_t>::value_type PairType;
225   BOOST_FOREACH(const PairType& pair, directories) {
226     DirectoryTimeStamp dir;
227     dir.filename = pair.first;
228     dir.last_modified = pair.second;
229     _directories.push_back(dir);
230   }
231   directory_timestamp_to_xml(idx_dir / "dirs.xml", _directories);
232 }
233
234 std::vector<DirectoryTimeStamp> ThreadIdxCache::directory_timestamp_from_xml(
235     const boost::filesystem::path& xml) {
236   std::vector<DirectoryTimeStamp> cache_vector;
237
238   if (boost::filesystem::exists(xml) && boost::filesystem::is_regular_file(xml)) {
239     std::ifstream ifs(xml.file_string().c_str());
240     try {
241       boost::archive::xml_iarchive ia(ifs);
242       ia >> boost::serialization::make_nvp("DirectoryTimeStamp", cache_vector);
243     } catch (const boost::archive::archive_exception& e) {
244       std::cerr << "directory_timestamp_from_xml(): " << e.what() << std::endl;
245     }
246     ifs.close();
247   }
248
249   return cache_vector;
250 }
251
252 void ThreadIdxCache::directory_timestamp_to_xml(
253     const boost::filesystem::path& xml,
254     const std::vector<DirectoryTimeStamp>& cache) {
255   if (cache.empty()) return;
256
257   if (!misc::create_directories(xml.parent_path())) return;
258   std::ofstream ofs(xml.file_string().c_str());
259   try {
260     boost::archive::xml_oarchive oa(ofs);
261     oa << boost::serialization::make_nvp("DirectoryTimeStamp", cache);
262   } catch (const boost::archive::archive_exception& e) {
263     std::cerr << "directory_timestamp_to_xml(): " << e.what() << std::endl;
264   }
265 }
266
267
268 } // namespace dialektos