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/serialization/nvp.hpp>
27 #include <boost/serialization/vector.hpp>
28 #include <boost/filesystem.hpp>
29 #include <boost/exception.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"
44 std::vector<ThreadIdxCache> ThreadIdxCache::from_xml(
45 const boost::filesystem::path& cache_xml) {
46 std::vector<ThreadIdxCache> cache_vector;
48 if (boost::filesystem::exists(cache_xml) &&
49 boost::filesystem::is_regular_file(cache_xml)) {
50 std::ifstream ifs(cache_xml.file_string().c_str());
52 boost::archive::xml_iarchive ia(ifs);
53 ia >> boost::serialization::make_nvp("ThreadIdxCache", cache_vector);
54 } catch (const boost::exception& /*e*/) {
55 // TODO thread safety.
56 std::cerr << "cannot load " << cache_xml.file_string() << std::endl;
64 void ThreadIdxCache::to_xml(const boost::filesystem::path& cache_xml,
65 const std::vector<ThreadIdxCache>& cache) {
66 std::ofstream ofs(cache_xml.file_string().c_str());
68 boost::archive::xml_oarchive oa(ofs);
69 oa << boost::serialization::make_nvp("ThreadIdxCache", cache);
70 } catch (const boost::exception& /*e*/) {
71 std::cerr << "cannot save " << cache_xml.file_string() << std::endl;
76 std::vector<ThreadIdxCache> ThreadIdxCache::from_directory(
77 const boost::filesystem::path& idx_dir) {
79 const boost::filesystem::path cache_xml = idx_dir / "cache.xml";
80 std::vector<ThreadIdxCache> cache_vector = ThreadIdxCache::from_xml(cache_xml);
82 boost::unordered_map<ThreadID, ThreadIdxCache> caches;
83 BOOST_FOREACH(const ThreadIdxCache& cache, cache_vector) {
84 caches.insert(std::make_pair(cache.id_, cache));
86 idx_dir_scan(idx_dir, caches);
89 typedef std::pair<ThreadID, ThreadIdxCache> PairType;
90 BOOST_FOREACH(const PairType& cache, caches) {
91 cache_vector.push_back(cache.second);
94 to_xml(cache_xml, cache_vector);
99 boost::unordered_set<ThreadIdxCache::ThreadID> ThreadIdxCache::get_exist_ids(
100 const boost::filesystem::path& idx_dir) {
101 using namespace boost::xpressive;
102 const sregex regex = (s1=-repeat<9, 10>(_d)) >> ".xml";
104 boost::unordered_set<ThreadID> exist_ids;
106 const boost::filesystem::directory_iterator it_end;
107 for (boost::filesystem::directory_iterator it(idx_dir); it != it_end; ++it) {
109 const boost::filesystem::path leaf = it->path();
110 if (!boost::filesystem::is_regular_file(leaf)) continue;
112 const std::string filename = leaf.filename();
114 if (!regex_match(filename, what, regex)) continue;
115 const ThreadID id = what[1];
116 exist_ids.insert(id);
121 std::vector<ThreadIdxCache::ThreadID> ThreadIdxCache::get_deleted_ids(
122 const boost::unordered_map<ThreadID, ThreadIdxCache>& caches,
123 const boost::unordered_set<ThreadID>& exist_ids) {
124 typedef std::pair<ThreadID, ThreadIdxCache> PairType;
125 std::vector<ThreadID> deleted_ids;
126 BOOST_FOREACH(const PairType& pair, caches) {
127 const std::string& cached = pair.first;
128 if (exist_ids.find(cached) == exist_ids.end())
129 deleted_ids.push_back(cached);
134 void ThreadIdxCache::remove_deleted_ids(
135 boost::unordered_map<ThreadID, ThreadIdxCache>& caches,
136 const std::vector<ThreadID>& deleted_ids) {
137 BOOST_FOREACH(const ThreadID& id, deleted_ids) {
138 std::cout << "removed " << id << std::endl;
143 void ThreadIdxCache::merge_idx(const boost::filesystem::path& idx_dir,
144 const boost::unordered_set<ThreadID>& exist_ids,
145 boost::unordered_map<ThreadID, ThreadIdxCache>& caches) {
147 BOOST_FOREACH(const ThreadID& id, exist_ids) {
149 const boost::filesystem::path leaf = idx_dir / (id + ".xml");
150 const std::time_t last_modified = boost::filesystem::last_write_time(leaf);
151 boost::unordered_map<ThreadID, ThreadIdxCache>::iterator it = caches.find(id);
152 if (it != caches.end()) {
153 const ThreadIdxCache& cache = it->second;
154 const std::time_t cache_last_modified =
155 boost::lexical_cast<std::time_t>(cache.idx_last_modified_);
156 if (last_modified <= cache_last_modified) continue;
159 std::cout << "modified:" << id << std::endl;
160 ThreadIdx idx = ThreadIdx::from_xml(leaf);
161 if (idx.last_modified_.empty()) continue;
162 ThreadIdxCache& cache = caches[id];
164 cache.idx_last_modified_ = boost::lexical_cast<std::string>(last_modified);
165 cache.line_count_ = idx.line_count_;
166 cache.title_ = idx.title_;
170 void ThreadIdxCache::idx_dir_scan(const boost::filesystem::path& idx_dir,
171 boost::unordered_map<ThreadID, ThreadIdxCache>& caches) {
173 const boost::unordered_set<ThreadID> exist_ids = get_exist_ids(idx_dir);
175 const std::vector<ThreadID> deleted_ids = get_deleted_ids(caches, exist_ids);
177 remove_deleted_ids(caches, deleted_ids);
179 merge_idx(idx_dir, exist_ids, caches);
184 } // namespace dialektos