OSDN Git Service

Change idx sub dir's time stamp when saving idx.
[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   typedef std::pair<ThreadID, ThreadIdxCache> PairType;
137   std::vector<ThreadID> deleted_ids;
138   BOOST_FOREACH(const PairType& pair, caches) {
139     const std::string& cached = pair.first;
140     if (exist_ids.find(cached) == exist_ids.end())
141       deleted_ids.push_back(cached);
142   }
143   return deleted_ids;
144 }
145
146 void ThreadIdxCache::remove_deleted_ids(
147     boost::unordered_map<ThreadID, ThreadIdxCache>& caches,
148     const std::vector<ThreadID>& deleted_ids) {
149   BOOST_FOREACH(const ThreadID& id, deleted_ids) {
150     std::cout << "removed " << id << std::endl;
151     caches.erase(id);
152   }
153 }
154
155 void ThreadIdxCache::merge_idx(const boost::filesystem::path& idx_dir,
156     const boost::unordered_set<ThreadID>& exist_ids,
157     boost::unordered_map<ThreadID, ThreadIdxCache>& caches) {
158
159   BOOST_FOREACH(const ThreadID& id, exist_ids) {
160
161     const boost::filesystem::path leaf = idx_dir / (id + ".xml");
162     const std::time_t last_modified = boost::filesystem::last_write_time(leaf);
163     boost::unordered_map<ThreadID, ThreadIdxCache>::iterator it =  caches.find(id);
164     if (it != caches.end()) {
165       const ThreadIdxCache& cache = it->second;
166       const std::time_t cache_last_modified =
167         boost::lexical_cast<std::time_t>(cache.idx_last_modified_);
168       if (last_modified <= cache_last_modified) continue;
169     }
170
171     std::cout << "modified:" << id << std::endl;
172     ThreadIdx idx = ThreadIdx::from_xml(leaf);
173     if (idx.last_modified_.empty()) continue;
174     ThreadIdxCache& cache = caches[id];
175     cache.id_ = id;
176     cache.idx_last_modified_ = boost::lexical_cast<std::string>(last_modified);
177     cache.line_count_ = idx.line_count_;
178     cache.title_ = idx.title_;
179   }
180 }
181
182 void ThreadIdxCache::idx_dir_scan(const boost::filesystem::path& idx_dir,
183     boost::unordered_map<ThreadID, ThreadIdxCache>& caches) {
184
185   std::vector<DirectoryTimeStamp> _directories =
186     directory_timestamp_from_xml(idx_dir / "dirs.xml");
187   boost::unordered_map<std::string, std::time_t> directories;
188   BOOST_FOREACH(const DirectoryTimeStamp& dir, _directories) {
189     directories[dir.filename] = dir.last_modified;
190   }
191
192   try {
193     const boost::filesystem::directory_iterator it_end;
194     for (boost::filesystem::directory_iterator it(idx_dir);
195         it != it_end; ++it) {
196
197       const boost::filesystem::path sub_dir = it->path();
198       if (!boost::filesystem::is_directory(sub_dir)) continue;
199
200       const std::string filename = sub_dir.filename();
201       const std::time_t last_modified =
202         boost::filesystem::last_write_time(sub_dir);
203       if (directories.find(filename) != directories.end() &&
204           directories[filename] == last_modified) {
205         continue;
206       }
207
208       directories[filename] = last_modified;
209
210       const boost::unordered_set<ThreadID> exist_ids = get_exist_ids(sub_dir);
211       // TODO remove deleted ids.
212 //      const std::vector<ThreadID> deleted_ids =
213 //        get_deleted_ids(caches, exist_ids);
214 //      remove_deleted_ids(caches, deleted_ids);
215       merge_idx(sub_dir, exist_ids, caches);
216
217     }
218   } catch(const boost::filesystem::filesystem_error& e) {
219     std::cerr << e.what() << std::endl;
220   }
221
222   _directories.clear();
223   typedef boost::unordered_map<std::string, std::time_t>::value_type PairType;
224   BOOST_FOREACH(const PairType& pair, directories) {
225     DirectoryTimeStamp dir;
226     dir.filename = pair.first;
227     dir.last_modified = pair.second;
228     _directories.push_back(dir);
229   }
230   directory_timestamp_to_xml(idx_dir / "dirs.xml", _directories);
231 }
232
233 std::vector<DirectoryTimeStamp> ThreadIdxCache::directory_timestamp_from_xml(
234     const boost::filesystem::path& xml) {
235   std::vector<DirectoryTimeStamp> cache_vector;
236
237   if (boost::filesystem::exists(xml) && boost::filesystem::is_regular_file(xml)) {
238     std::ifstream ifs(xml.file_string().c_str());
239     try {
240       boost::archive::xml_iarchive ia(ifs);
241       ia >> boost::serialization::make_nvp("DirectoryTimeStamp", cache_vector);
242     } catch (const boost::archive::archive_exception& e) {
243       std::cerr << "directory_timestamp_from_xml(): " << e.what() << std::endl;
244     }
245     ifs.close();
246   }
247
248   return cache_vector;
249 }
250
251 void ThreadIdxCache::directory_timestamp_to_xml(
252     const boost::filesystem::path& xml,
253     const std::vector<DirectoryTimeStamp>& cache) {
254   if (cache.empty()) return;
255
256   if (!misc::create_directories(xml.parent_path())) return;
257   std::ofstream ofs(xml.file_string().c_str());
258   try {
259     boost::archive::xml_oarchive oa(ofs);
260     oa << boost::serialization::make_nvp("DirectoryTimeStamp", cache);
261   } catch (const boost::archive::archive_exception& e) {
262     std::cerr << "directory_timestamp_to_xml(): " << e.what() << std::endl;
263   }
264 }
265
266
267 } // namespace dialektos