Добавлено количество снимков в объект StorageFile

This commit is contained in:
Alexander Zhirov 2025-11-21 14:57:53 +03:00
parent 3eaae341aa
commit 83769410c3
Signed by: alexander
GPG key ID: C8D8BE544A27C511
3 changed files with 68 additions and 15 deletions

View file

@ -14,6 +14,7 @@ import cdcdb.lib;
struct DBFile { struct DBFile {
Identifier id; Identifier id;
string path; string path;
long countSnapshots;
@trusted pure nothrow @nogc @property bool empty() const { @trusted pure nothrow @nogc @property bool empty() const {
return id.empty(); return id.empty();
@ -224,7 +225,13 @@ public:
DBFile[] getFiles() { DBFile[] getFiles() {
auto queryResult = sql( auto queryResult = sql(
q{ q{
SELECT id, name FROM files SELECT
f.id,
f.name,
count(s.file) count_snapshots
FROM files f
JOIN snapshots s ON s.file = f.id
GROUP BY f.id
} }
); );
@ -236,6 +243,7 @@ public:
file.id = row["id"].as!Blob(Blob.init); file.id = row["id"].as!Blob(Blob.init);
file.path = row["name"].as!string; file.path = row["name"].as!string;
file.countSnapshots = row["count_snapshots"].as!long;
files ~= file; files ~= file;
} }
@ -246,7 +254,13 @@ public:
DBFile getFile(string path) { DBFile getFile(string path) {
auto queryResult = sql( auto queryResult = sql(
q{ q{
SELECT id FROM files WHERE name = ?1 SELECT
f.id,
count(s.file) count_snapshots
FROM files f
JOIN snapshots s ON s.file = f.id
WHERE f.name = ?1
GROUP BY f.id
}, path }, path
); );
@ -257,6 +271,7 @@ public:
file.id = data["id"].as!Blob(Blob.init); file.id = data["id"].as!Blob(Blob.init);
file.path = path; file.path = path;
file.countSnapshots = data["count_snapshots"].as!long;
} }
return file; return file;
@ -265,9 +280,14 @@ public:
DBFile getFile(Identifier id) { DBFile getFile(Identifier id) {
auto queryResult = sql( auto queryResult = sql(
q{ q{
SELECT id, name SELECT
FROM files f.id,
WHERE id = ?1 f.name,
count(s.file) count_snapshots
FROM files f
JOIN snapshots s ON s.file = f.id
WHERE f.id = ?1
GROUP BY f.id
}, id[] }, id[]
); );
@ -278,6 +298,7 @@ public:
file.id = id; file.id = id;
file.path = data["name"].as!string; file.path = data["name"].as!string;
file.countSnapshots = data["count_snapshots"].as!long;
} }
return file; return file;
@ -313,7 +334,14 @@ public:
DBFile[] findFile(string pattern) { DBFile[] findFile(string pattern) {
auto queryResult = sql( auto queryResult = sql(
q{ q{
SELECT id, name FROM files WHERE name LIKE ?1 SELECT
f.id,
f.name,
count(s.file) count_snapshots
FROM files f
JOIN snapshots s ON s.file = f.id
WHERE f.name LIKE ?1
GROUP BY f.id
}, '%' ~ pattern ~ '%' }, '%' ~ pattern ~ '%'
); );
@ -325,6 +353,7 @@ public:
file.id = row["id"].as!Blob(Blob.init); file.id = row["id"].as!Blob(Blob.init);
file.path = row["name"].as!string; file.path = row["name"].as!string;
file.countSnapshots = row["count_snapshots"].as!long;
files ~= file; files ~= file;
} }
@ -338,14 +367,19 @@ public:
DBFile[] findFile(Identifier id) { DBFile[] findFile(Identifier id) {
auto queryResult = sql( auto queryResult = sql(
q{ q{
SELECT id, name SELECT
FROM files f.id,
f.name,
count(s.file) count_snapshots
FROM files f
JOIN snapshots s ON s.file = f.id
WHERE length(?1) BETWEEN 1 AND 16 WHERE length(?1) BETWEEN 1 AND 16
AND CASE AND CASE
WHEN substr(hex(?1), 2 * length(?1), 1) = '0' WHEN substr(hex(?1), 2 * length(?1), 1) = '0'
THEN instr(hex(id), substr(hex(?1), 1, 2 * length(?1) - 1)) > 0 THEN instr(hex(f.id), substr(hex(?1), 1, 2 * length(?1) - 1)) > 0
ELSE substr(id, 1, length(?1)) = ?1 ELSE substr(f.id, 1, length(?1)) = ?1
END END
GROUP BY f.id
}, id[] }, id[]
); );
@ -357,6 +391,7 @@ public:
file.id = row["id"].as!Blob(Blob.init); file.id = row["id"].as!Blob(Blob.init);
file.path = row["name"].as!string; file.path = row["name"].as!string;
file.countSnapshots = row["count_snapshots"].as!long;
files ~= file; files ~= file;
} }
@ -650,6 +685,7 @@ public:
s.id, s.id,
f.id file_id, f.id file_id,
f.name file_name, f.name file_name,
count(sc.file) count_snapshots,
s.sha256, s.sha256,
s.description, s.description,
s.created_utc, s.created_utc,
@ -671,6 +707,8 @@ public:
JOIN users u ON u.uid = s.uid JOIN users u ON u.uid = s.uid
JOIN users r ON r.uid = s.ruid JOIN users r ON r.uid = s.ruid
JOIN files f ON f.id = s.file AND (length(?) = 0 OR f.name = ?1) JOIN files f ON f.id = s.file AND (length(?) = 0 OR f.name = ?1)
JOIN snapshots sc ON f.id = sc.file
GROUP BY s.id
ORDER BY s.created_utc ORDER BY s.created_utc
}, file }, file
); );
@ -684,7 +722,8 @@ public:
snapshot.id = row["id"].as!Blob(Blob.init); snapshot.id = row["id"].as!Blob(Blob.init);
snapshot.file = DBFile( snapshot.file = DBFile(
Identifier(row["file_id"].as!Blob(Blob.init)), Identifier(row["file_id"].as!Blob(Blob.init)),
row["file_name"].as!string row["file_name"].as!string,
row["count_snapshots"].as!long
); );
snapshot.sha256 = row["sha256"].as!Blob(Blob.init); snapshot.sha256 = row["sha256"].as!Blob(Blob.init);
snapshot.description = row["description"].as!string(""); // может быть NULL snapshot.description = row["description"].as!string(""); // может быть NULL
@ -719,6 +758,7 @@ public:
s.id, s.id,
f.id file_id, f.id file_id,
f.name file_name, f.name file_name,
count(sc.file) count_snapshots,
s.sha256, s.sha256,
s.description, s.description,
s.created_utc, s.created_utc,
@ -740,7 +780,9 @@ public:
JOIN users u ON u.uid = s.uid JOIN users u ON u.uid = s.uid
JOIN users r ON r.uid = s.ruid JOIN users r ON r.uid = s.ruid
JOIN files f ON f.id = s.file JOIN files f ON f.id = s.file
JOIN snapshots sc ON f.id = sc.file
WHERE f.id = ?1 WHERE f.id = ?1
GROUP BY s.id
ORDER BY s.created_utc ORDER BY s.created_utc
}, id[] }, id[]
); );
@ -754,7 +796,8 @@ public:
snapshot.id = row["id"].as!Blob(Blob.init); snapshot.id = row["id"].as!Blob(Blob.init);
snapshot.file = DBFile( snapshot.file = DBFile(
Identifier(row["file_id"].as!Blob(Blob.init)), Identifier(row["file_id"].as!Blob(Blob.init)),
row["file_name"].as!string row["file_name"].as!string,
row["count_snapshots"].as!long
); );
snapshot.sha256 = row["sha256"].as!Blob(Blob.init); snapshot.sha256 = row["sha256"].as!Blob(Blob.init);
snapshot.description = row["description"].as!string(""); snapshot.description = row["description"].as!string("");
@ -788,6 +831,7 @@ public:
s.id, s.id,
f.id file_id, f.id file_id,
f.name file_name, f.name file_name,
count(sc.file) count_snapshots,
s.sha256, s.sha256,
s.description, s.description,
s.created_utc, s.created_utc,
@ -809,7 +853,9 @@ public:
JOIN users u ON u.uid = s.uid JOIN users u ON u.uid = s.uid
JOIN users r ON r.uid = s.ruid JOIN users r ON r.uid = s.ruid
JOIN files f ON f.id = s.file JOIN files f ON f.id = s.file
JOIN snapshots sc ON f.id = sc.file
WHERE s.id = ?1 WHERE s.id = ?1
GROUP BY s.id
ORDER BY s.created_utc ORDER BY s.created_utc
}, id[] }, id[]
); );
@ -822,7 +868,8 @@ public:
snapshot.id = data["id"].as!Blob(Blob.init); snapshot.id = data["id"].as!Blob(Blob.init);
snapshot.file = DBFile( snapshot.file = DBFile(
Identifier(data["file_id"].as!Blob(Blob.init)), Identifier(data["file_id"].as!Blob(Blob.init)),
data["file_name"].as!string data["file_name"].as!string,
data["count_snapshots"].as!long
); );
snapshot.sha256 = data["sha256"].as!Blob(Blob.init); snapshot.sha256 = data["sha256"].as!Blob(Blob.init);
snapshot.description = data["description"].as!string(""); snapshot.description = data["description"].as!string("");
@ -857,6 +904,7 @@ public:
s.id, s.id,
f.id file_id, f.id file_id,
f.name file_name, f.name file_name,
count(sc.file) count_snapshots,
s.sha256, s.sha256,
s.description, s.description,
s.created_utc, s.created_utc,
@ -878,12 +926,15 @@ public:
JOIN users u ON u.uid = s.uid JOIN users u ON u.uid = s.uid
JOIN users r ON r.uid = s.ruid JOIN users r ON r.uid = s.ruid
JOIN files f ON f.id = s.file JOIN files f ON f.id = s.file
JOIN snapshots sc ON f.id = sc.file
WHERE length(?1) BETWEEN 1 AND 16 WHERE length(?1) BETWEEN 1 AND 16
AND CASE AND CASE
WHEN substr(hex(?1), 2 * length(?1), 1) = '0' WHEN substr(hex(?1), 2 * length(?1), 1) = '0'
THEN instr(hex(s.id), substr(hex(?1), 1, 2 * length(?1) - 1)) > 0 THEN instr(hex(s.id), substr(hex(?1), 1, 2 * length(?1) - 1)) > 0
ELSE substr(s.id, 1, length(?1)) = ?1 ELSE substr(s.id, 1, length(?1)) = ?1
END END
GROUP BY s.id
ORDER BY s.created_utc
}, id[] }, id[]
); );
@ -896,7 +947,8 @@ public:
snapshot.id = row["id"].as!Blob(Blob.init); snapshot.id = row["id"].as!Blob(Blob.init);
snapshot.file = DBFile( snapshot.file = DBFile(
Identifier(row["file_id"].as!Blob(Blob.init)), Identifier(row["file_id"].as!Blob(Blob.init)),
row["file_name"].as!string row["file_name"].as!string,
row["count_snapshots"].as!long
); );
snapshot.sha256 = row["sha256"].as!Blob(Blob.init); snapshot.sha256 = row["sha256"].as!Blob(Blob.init);
snapshot.description = row["description"].as!string(""); snapshot.description = row["description"].as!string("");

View file

@ -14,6 +14,7 @@ public:
@property ref Identifier id() return { return _dbfile.id; } @property ref Identifier id() return { return _dbfile.id; }
@property string name() const nothrow @safe { return _dbfile.path; } @property string name() const nothrow @safe { return _dbfile.path; }
@property long countSnapshots() const nothrow @safe { return _dbfile.countSnapshots; }
Snapshot[] snapshots() { Snapshot[] snapshots() {
Snapshot[] snapshots; Snapshot[] snapshots;

View file

@ -1,3 +1,3 @@
module cdcdb.version_; module cdcdb.version_;
enum cdcdbVersion = "0.2.0"; enum cdcdbVersion = "0.2.1";