2 * Copyright (C) 2009 by Aiwota Programmer
3 * aiwotaprog@tetteke.tk
5 * This file is part of Dialektos.
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.
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.
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/>.
22 #include "thread_idx_cache.hxx"
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>
38 #include "thread_idx.hxx"
45 std::vector<ThreadIdxCache> ThreadIdxCache::from_xml(
46 const boost::filesystem::path& cache_xml) {
47 std::vector<ThreadIdxCache> cache_vector;
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());
53 boost::archive::binary_iarchive ia(ifs);
55 } catch (const boost::archive::archive_exception& e) {
56 // TODO thread safety.
57 std::cerr << "ThreadIdxCache::from_xml(): " << e.what() << std::endl;
65 void ThreadIdxCache::to_xml(const boost::filesystem::path& cache_xml,
66 const std::vector<ThreadIdxCache>& cache) {
67 if (cache.empty()) return;
69 if (!misc::create_directories(cache_xml.parent_path())) return;
70 std::ofstream ofs(cache_xml.file_string().c_str());
72 boost::archive::binary_oarchive oa(ofs);
74 } catch (const boost::archive::archive_exception& e) {
75 std::cerr << "ThreadIdxCache::to_xml(): " << e.what() << std::endl;
80 std::vector<ThreadIdxCache> ThreadIdxCache::from_directory(
81 const boost::filesystem::path& idx_dir) {
83 const boost::filesystem::path cache_xml = idx_dir / ".cache";
84 std::vector<ThreadIdxCache> cache_vector = ThreadIdxCache::from_xml(cache_xml);
86 boost::unordered_map<ThreadID, ThreadIdxCache> caches;
87 BOOST_FOREACH(const ThreadIdxCache& cache, cache_vector) {
88 caches.insert(std::make_pair(cache.id_, cache));
90 idx_dir_scan(idx_dir, caches);
93 typedef std::pair<ThreadID, ThreadIdxCache> PairType;
94 BOOST_FOREACH(const PairType& cache, caches) {
95 cache_vector.push_back(cache.second);
98 to_xml(cache_xml, cache_vector);
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";
108 boost::unordered_set<ThreadID> exist_ids;
110 if (!boost::filesystem::exists(idx_dir) ||
111 !boost::filesystem::is_directory(idx_dir))
115 const boost::filesystem::directory_iterator it_end;
116 for (boost::filesystem::directory_iterator it(idx_dir); it != it_end; ++it) {
118 const boost::filesystem::path leaf = it->path();
119 if (!boost::filesystem::is_regular_file(leaf)) continue;
121 const std::string filename = leaf.filename();
123 if (!regex_match(filename, what, regex)) continue;
124 const ThreadID id = what[1];
125 exist_ids.insert(id);
127 } catch(const boost::filesystem::filesystem_error& e) {
128 std::cerr << e.what() << std::endl;
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);
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;
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) {
161 BOOST_FOREACH(const ThreadID& id, exist_ids) {
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;
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];
178 cache.idx_last_modified_ = boost::lexical_cast<std::string>(last_modified);
179 cache.line_count_ = idx.line_count_;
180 cache.title_ = idx.title_;
184 void ThreadIdxCache::idx_dir_scan(const boost::filesystem::path& idx_dir,
185 boost::unordered_map<ThreadID, ThreadIdxCache>& caches) {
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;
195 const boost::filesystem::directory_iterator it_end;
196 for (boost::filesystem::directory_iterator it(idx_dir);
197 it != it_end; ++it) {
199 const boost::filesystem::path sub_dir = it->path();
200 if (!boost::filesystem::is_directory(sub_dir)) continue;
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) {
210 directories[filename] = last_modified;
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);
219 } catch(const boost::filesystem::filesystem_error& e) {
220 std::cerr << e.what() << std::endl;
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);
231 directory_timestamp_to_xml(idx_dir / "dirs.xml", _directories);
234 std::vector<DirectoryTimeStamp> ThreadIdxCache::directory_timestamp_from_xml(
235 const boost::filesystem::path& xml) {
236 std::vector<DirectoryTimeStamp> cache_vector;
238 if (boost::filesystem::exists(xml) && boost::filesystem::is_regular_file(xml)) {
239 std::ifstream ifs(xml.file_string().c_str());
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;
252 void ThreadIdxCache::directory_timestamp_to_xml(
253 const boost::filesystem::path& xml,
254 const std::vector<DirectoryTimeStamp>& cache) {
255 if (cache.empty()) return;
257 if (!misc::create_directories(xml.parent_path())) return;
258 std::ofstream ofs(xml.file_string().c_str());
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;
268 } // namespace dialektos