Mercurial > prosody-modules
comparison mod_storage_xmlarchive/mod_storage_xmlarchive.lua @ 2632:995d4d9f5d89
mod_storage_xmlarchive: Break out XML file reading into a function
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 21 Mar 2017 10:56:56 +0100 |
parents | 324a6a3b730b |
children | 1330ed88ecd8 |
comparison
equal
deleted
inserted
replaced
2631:2bfa7d476092 | 2632:995d4d9f5d89 |
---|---|
1 -- mod_storage_xmlarchive | 1 -- mod_storage_xmlarchive |
2 -- Copyright (C) 2015-2016 Kim Alvefur | 2 -- Copyright (C) 2015-2017 Kim Alvefur |
3 -- | 3 -- |
4 -- This file is MIT/X11 licensed. | 4 -- This file is MIT/X11 licensed. |
5 -- | 5 -- |
6 -- luacheck: ignore unused self | 6 -- luacheck: ignore unused self |
7 | 7 |
118 if dates[i] == d then | 118 if dates[i] == d then |
119 last_day = i; break; | 119 last_day = i; break; |
120 end | 120 end |
121 end | 121 end |
122 end | 122 end |
123 local items, xmlfile; | 123 local items; |
124 local first_item, last_item; | 124 local first_item, last_item; |
125 if rev then | 125 if rev then |
126 start_day, step, last_day = last_day, -step, start_day; | 126 start_day, step, last_day = last_day, -step, start_day; |
127 if query.before then | 127 if query.before then |
128 local before_day, before_item, items_ = self:_get_idx(username, query.before, dates); | 128 local before_day, before_item, items_ = self:_get_idx(username, query.before, dates); |
149 start_day = after_day; | 149 start_day = after_day; |
150 items = items_; | 150 items = items_; |
151 end | 151 end |
152 end | 152 end |
153 | 153 |
154 local date_open, xmlfile; | |
155 local function read_xml(date, offset, length) | |
156 if xmlfile and date ~= date_open then | |
157 xmlfile:close(); | |
158 xmlfile = nil; | |
159 end | |
160 if not xmlfile then | |
161 date_open = date; | |
162 local filename = dm.getpath(username .. "@" .. date, module.host, self.store, "xml"); | |
163 local ferr; | |
164 xmlfile, ferr = io.open(filename); | |
165 if not xmlfile then | |
166 module:log("error", "Error: %s", ferr); | |
167 return nil, ferr; | |
168 end | |
169 end | |
170 local pos, err = xmlfile:seek("set", offset); | |
171 if pos ~= offset then | |
172 return nil, err or "seek-failed"; | |
173 end | |
174 return xmlfile:read(length); | |
175 end | |
176 | |
154 return function () | 177 return function () |
155 if limit and count >= limit then if xmlfile then xmlfile:close() end return; end | 178 if limit and count >= limit then if xmlfile then xmlfile:close() end return; end |
156 local filename; | |
157 | |
158 for d = start_day, last_day, step do | 179 for d = start_day, last_day, step do |
159 if not items then | 180 if not items then |
160 module:log("debug", "Loading items from %s", dates[d]); | 181 module:log("debug", "Loading items from %s", dates[d]); |
161 start_day = d; | 182 start_day = d; |
162 items = dm.list_load(username .. "@" .. dates[d], module.host, self.store) or empty; | 183 items = dm.list_load(username .. "@" .. dates[d], module.host, self.store) or empty; |
180 end | 201 end |
181 if type(i_when) ~= "number" then | 202 if type(i_when) ~= "number" then |
182 module:log("warn", "data[%q][%d].when is invalid", dates[d], i); | 203 module:log("warn", "data[%q][%d].when is invalid", dates[d], i); |
183 break; | 204 break; |
184 end | 205 end |
185 if not xmlfile then | |
186 local ferr; | |
187 filename = dm.getpath(username .. "@" .. dates[d], module.host, self.store, "xml"); | |
188 xmlfile, ferr = io.open(filename); | |
189 if not xmlfile then | |
190 module:log("error", "Error: %s", ferr); | |
191 return; | |
192 end | |
193 end | |
194 if (not q_with or i_with == q_with) | 206 if (not q_with or i_with == q_with) |
195 and (not q_start or i_when >= q_start) | 207 and (not q_start or i_when >= q_start) |
196 and (not q_end or i_when <= q_end) then | 208 and (not q_end or i_when <= q_end) then |
197 count = count + 1; | 209 count = count + 1; |
198 first_item = i + step; | 210 first_item = i + step; |
199 | 211 |
200 xmlfile:seek("set", item.offset); | 212 local data = read_xml(dates[d], item.offset, item.length); |
201 local data = xmlfile:read(item.length); | 213 if not data then return end |
202 local ok, err = stream:feed(data); | 214 local ok, err = stream:feed(data); |
203 if not ok then | 215 if not ok then |
204 module:log("warn", "Parse error in %s at %d+%d: %s", filename, item.offset, item.length, err); | 216 module:log("warn", "Parse error in %s@%s/%s/%q[%d]: %s", username, module.host, self.store, i, err); |
205 reset_stream(); | 217 reset_stream(); |
206 end | 218 end |
207 if result then | 219 if result then |
208 local stanza = result; | 220 local stanza = result; |
209 result = nil; | 221 result = nil; |