OSDN Git Service

Components for Board are implemented.
[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/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>
35 #include <vector>
36 #include <fstream>
37 #include <iostream>
38 #include "thread_idx.hxx"
39
40
41 namespace dialektos {
42
43
44 std::vector<ThreadIdxCache> ThreadIdxCache::from_xml(
45     const boost::filesystem::path& cache_xml) {
46   std::vector<ThreadIdxCache> cache_vector;
47
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());
51     try {
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;
57     }
58     ifs.close();
59   }
60
61   return cache_vector;
62 }
63
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());
67   try {
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;
72   }
73   ofs.close();
74 }
75
76 std::vector<ThreadIdxCache> ThreadIdxCache::from_directory(
77     const boost::filesystem::path& idx_dir) {
78
79   const boost::filesystem::path cache_xml = idx_dir / "cache.xml";
80   std::vector<ThreadIdxCache> cache_vector = ThreadIdxCache::from_xml(cache_xml);
81
82   boost::unordered_map<ThreadID, ThreadIdxCache> caches;
83   BOOST_FOREACH(const ThreadIdxCache& cache, cache_vector) {
84     caches.insert(std::make_pair(cache.id_, cache));
85   }
86   idx_dir_scan(idx_dir, caches);
87
88   cache_vector.clear();
89   typedef std::pair<ThreadID, ThreadIdxCache> PairType;
90   BOOST_FOREACH(const PairType& cache, caches) {
91     cache_vector.push_back(cache.second);
92   }
93
94   to_xml(cache_xml, cache_vector);
95
96   return cache_vector;
97 }
98
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";
103
104   boost::unordered_set<ThreadID> exist_ids;
105
106   const boost::filesystem::directory_iterator it_end;
107   for (boost::filesystem::directory_iterator it(idx_dir); it != it_end; ++it) {
108
109     const boost::filesystem::path leaf = it->path();
110     if (!boost::filesystem::is_regular_file(leaf)) continue;
111
112     const std::string filename = leaf.filename();
113     smatch what;
114     if (!regex_match(filename, what, regex)) continue;
115     const ThreadID id = what[1];
116     exist_ids.insert(id);
117   }
118   return exist_ids;
119 }
120
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);
130   }
131   return deleted_ids;
132 }
133
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;
139     caches.erase(id);
140   }
141 }
142
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) {
146
147   BOOST_FOREACH(const ThreadID& id, exist_ids) {
148
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;
157     }
158
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];
163     cache.id_ = 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_;
167   }
168 }
169
170 void ThreadIdxCache::idx_dir_scan(const boost::filesystem::path& idx_dir,
171     boost::unordered_map<ThreadID, ThreadIdxCache>& caches) {
172
173   const boost::unordered_set<ThreadID> exist_ids = get_exist_ids(idx_dir);
174
175   const std::vector<ThreadID> deleted_ids = get_deleted_ids(caches, exist_ids);
176
177   remove_deleted_ids(caches, deleted_ids);
178
179   merge_idx(idx_dir, exist_ids, caches);
180 }
181
182
183
184 } // namespace dialektos