forked from zao/foo_wave_seekbar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackingStore.cc
More file actions
346 lines (296 loc) · 9.48 KB
/
BackingStore.cc
File metadata and controls
346 lines (296 loc) · 9.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright Lars Viklund 2008 - 2011.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "PchSeekbar.h"
#include "BackingStore.h"
#include "waveform_sdk/WaveformImpl.h"
#include "Helpers.h"
#include "Pack.h"
#include "waveform_sdk/Optional.h"
namespace wave
{
backing_store::backing_store(pfc::string const& cache_filename)
{
{
sqlite3* p = 0;
sqlite3_open(cache_filename.get_ptr(), &p);
backing_db.reset(p, &sqlite3_close);
}
sqlite3_exec(
backing_db.get(),
"PRAGMA foreign_keys = ON",
0, 0, 0);
sqlite3_exec(
backing_db.get(),
"CREATE TABLE IF NOT EXISTS file ("
"fid INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
"location TEXT NOT NULL,"
"subsong INTEGER NOT NULL,"
"UNIQUE (location, subsong))",
0, 0, 0);
sqlite3_exec(
backing_db.get(),
"CREATE TABLE IF NOT EXISTS wave ("
"fid INTEGER PRIMARY KEY NOT NULL,"
"min BLOB,"
"max BLOB,"
"rms BLOB,"
"FOREIGN KEY (fid) REFERENCES file(fid))",
0, 0, 0);
sqlite3_exec(
backing_db.get(),
"CREATE TABLE IF NOT EXISTS job ("
"jid INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
"location TEXT NOT NULL,"
"subsong INTEGER NOT NULL,"
"user_submitted INTEGER,"
"UNIQUE (location, subsong))",
0, 0, 0);
sqlite3_exec(
backing_db.get(),
"DROP TRIGGER resonance_cascade",
0, 0, 0);
sqlite3_exec(
backing_db.get(),
"CREATE TRIGGER resonance_cascade BEFORE DELETE ON file BEGIN DELETE FROM wave WHERE wave.fid = OLD.fid; END",
0, 0, 0);
sqlite3_exec(
backing_db.get(),
"ALTER TABLE wave ADD channels INT",
0, 0, 0);
sqlite3_exec(
backing_db.get(),
"ALTER TABLE wave ADD compression INT",
0, 0, 0);
}
backing_store::~backing_store()
{
}
bool backing_store::has(playable_location const& file)
{
auto stmt = prepare_statement(
"SELECT 1 "
"FROM file as f, wave AS w "
"WHERE f.location = ? AND f.subsong = ? AND f.fid = w.fid");
sqlite3_bind_text(stmt.get(), 1, file.get_path(), -1, SQLITE_STATIC);
sqlite3_bind_int(stmt.get(), 2, file.get_subsong());
if (SQLITE_ROW == sqlite3_step(stmt.get())) {
return true;
}
return false;
}
void backing_store::remove(playable_location const& file)
{
auto stmt = prepare_statement(
"DELETE FROM file WHERE file.location = ? AND file.subsong = ?");
sqlite3_bind_text(stmt.get(), 1, file.get_path(), -1, SQLITE_STATIC);
sqlite3_bind_int(stmt.get(), 2, file.get_subsong());
sqlite3_step(stmt.get());
}
bool backing_store::get(ref_ptr<waveform>& out, playable_location const& file)
{
out.reset();
wave::optional<int> compression;
{
auto stmt = prepare_statement(
"SELECT w.min, w.max, w.rms, w.channels, w.compression "
"FROM file AS f NATURAL JOIN wave AS w "
"WHERE f.location = ? AND f.subsong = ?");
sqlite3_bind_text(stmt.get(), 1, file.get_path(), -1, SQLITE_STATIC);
sqlite3_bind_int(stmt.get(), 2, file.get_subsong());
if (SQLITE_ROW != sqlite3_step(stmt.get())) {
return false;
}
wave::optional<int> channels;
if (sqlite3_column_type(stmt.get(), 3) != SQLITE_NULL)
channels = sqlite3_column_int(stmt.get(), 3);
if (sqlite3_column_type(stmt.get(), 4) != SQLITE_NULL)
compression = sqlite3_column_int(stmt.get(), 4);
if (compression && *compression > 1)
return false;
unsigned channel_count = channels ? count_bits_set(*channels) : 1;
if (compression && *compression < 0 || channels && *channels < 0 || channel_count > 18) {
remove(file); // corrupt entry
}
ref_ptr<waveform_impl> w(new waveform_impl);
auto clear_and_set = [&stmt, compression, channel_count, &w](pfc::string name, size_t col) -> bool
{
pfc::list_t<waveform_impl::signal> list;
float const* data = (float const*)sqlite3_column_blob(stmt.get(), col);
t_size count = sqlite3_column_bytes(stmt.get(), col);
if (compression)
{
typedef std::back_insert_iterator<std::vector<char>> Iterator;
bool (*unpack_func)(void const*, size_t, Iterator) = 0;
switch (*compression) {
case 0: unpack_func = &pack::z_unpack<Iterator>; break;
case 1: unpack_func = &pack::lzma_unpack<Iterator>; break;
default: return false; // unknown compression scheme
}
std::vector<char> dst;
dst.reserve(2048 * channel_count * sizeof(float));
if (!unpack_func(data, count, std::back_inserter(dst)))
{
return false;
}
if (dst.size() != channel_count * 2048 * sizeof(float))
{
return false;
}
for (unsigned c = 0; c < channel_count; ++c)
{
waveform_impl::signal channel;
float const * fs = (float*)&dst[2048 * c * sizeof(float)];
channel.add_items_fromptr(fs, 2048);
list.add_item(channel);
}
}
else
{
for (unsigned c = 0; c < channel_count; ++c)
{
waveform_impl::signal channel;
channel.add_items_fromptr(data + 2048*c, 2048);
list.add_item(channel);
}
}
w->fields[name] = list;
return true;
};
if (clear_and_set("minimum", 0) &&
clear_and_set("maximum", 1) &&
clear_and_set("rms", 2))
{
w->channel_map = channels ? *channels : audio_chunk::channel_config_mono;
out = w;
}
else
{
remove(file); // it's corrupt, and thus useless
}
}
if(!compression || *compression == 0)
{
put(out, file);
}
return out.is_valid();
}
void backing_store::put(ref_ptr<waveform> const& w, playable_location const& file)
{
auto stmt = prepare_statement(
"INSERT INTO file (location, subsong) "
"VALUES (?, ?)");
sqlite3_bind_text(stmt.get(), 1, file.get_path(), -1, SQLITE_STATIC);
sqlite3_bind_int(stmt.get(), 2, file.get_subsong());
sqlite3_step(stmt.get());
stmt = prepare_statement(
"REPLACE INTO wave (fid, min, max, rms, channels, compression) "
"SELECT f.fid, ?, ?, ?, ?, ? "
"FROM file AS f "
"WHERE f.location = ? AND f.subsong = ?");
# define BIND_LIST(Member, Idx) \
std::vector<char> Member; \
{ \
std::vector<float> src_buf; \
for (size_t c = 0; c < w->get_channel_count(); ++c) \
{ \
pfc::list_t<float> channel; \
w->get_field(#Member, c, list_array_sink<float>(channel)); \
float * p = (float *)channel.get_ptr(); \
std::copy(p, p + channel.get_size(), std::back_inserter(src_buf)); \
} \
pack::lzma_pack(&src_buf[0], src_buf.size() * sizeof(float), std::back_inserter(Member)); \
} \
sqlite3_bind_blob(stmt.get(), Idx, &Member[0], Member.size(), SQLITE_STATIC)
BIND_LIST(minimum, 1);
BIND_LIST(maximum, 2);
BIND_LIST(rms , 3);
# undef BIND_LIST
sqlite3_bind_int(stmt.get(), 4, w->get_channel_map());
sqlite3_bind_int(stmt.get(), 5, 1); // LZMA compression
sqlite3_bind_text(stmt.get(), 6, file.get_path(), -1, SQLITE_STATIC);
sqlite3_bind_int(stmt.get(), 7, file.get_subsong());
while (SQLITE_ROW == sqlite3_step(stmt.get()));
}
void backing_store::get_jobs(std::deque<job>& out)
{
auto stmt = prepare_statement(
"SELECT location, subsong, user_submitted FROM job ORDER BY jid");
out.clear();
while (SQLITE_ROW == sqlite3_step(stmt.get()))
{
char const* loc = (char const*)sqlite3_column_text(stmt.get(), 0);
t_uint32 sub = (t_uint32)sqlite3_column_int(stmt.get(), 1);
bool user = !!sqlite3_column_int(stmt.get(), 2);
out.push_back(make_job(playable_location_impl(loc, sub), user));
}
}
void backing_store::put_jobs(std::deque<job> const& jobs)
{
sqlite3_exec(backing_db.get(), "BEGIN", 0, 0, 0);
sqlite3_exec(backing_db.get(), "DELETE FROM job", 0, 0, 0);
auto stmt = prepare_statement(
"INSERT INTO job (location, subsong, user_submitted) "
"VALUES (?, ?, ?)");
for (auto& j : jobs)
{
sqlite3_bind_text(stmt.get(), 1, j.loc.get_path(), -1, SQLITE_STATIC);
sqlite3_bind_int(stmt.get(), 2, j.loc.get_subsong());
sqlite3_bind_int(stmt.get(), 3, j.user);
sqlite3_step(stmt.get());
sqlite3_reset(stmt.get());
}
sqlite3_exec(backing_db.get(), "COMMIT", 0, 0, 0);
}
void file_exists(sqlite3_context* ctx, int argc, sqlite3_value** argv)
{
char const* loc = (char const*)sqlite3_value_text(argv[0]);
abort_callback_dummy cb;
try {
bool exists = filesystem::g_exists(loc, cb);
if (exists)
{
sqlite3_result_int(ctx, exists);
return;
}
}
catch (exception_io&) {}
sqlite3_result_null(ctx);
}
void backing_store::remove_dead()
{
sqlite3_create_function(
backing_db.get(),
"file_exists", 1, SQLITE_UTF8, 0,
&file_exists, 0, 0);
sqlite3_exec(backing_db.get(), "DELETE FROM file WHERE file_exists(file.location) IS NULL", 0, 0, 0);
console::info("Waveform cache: removed dead entries from the database.");
}
void backing_store::compact()
{
sqlite3_exec(backing_db.get(), "VACUUM", 0, 0, 0);
console::info("Waveform cache: compacted the database.");
}
void backing_store::get_all(pfc::list_t<playable_location_impl>& out)
{
auto stmt = prepare_statement(
"SELECT location, subsong FROM file ORDER BY location, subsong");
out.remove_all();
while (SQLITE_ROW == sqlite3_step(stmt.get()))
{
char const* loc = (char const*)sqlite3_column_text(stmt.get(), 0);
t_uint32 sub = (t_uint32)sqlite3_column_int(stmt.get(), 1);
out.add_item(playable_location_impl(loc, sub));
}
}
std::shared_ptr<sqlite3_stmt> backing_store::prepare_statement(std::string const& query)
{
sqlite3_stmt* p = 0;
sqlite3_prepare_v2(
backing_db.get(),
query.c_str(),
query.size(), &p, 0);
return std::shared_ptr<sqlite3_stmt>(p, &sqlite3_finalize);
}
}