Получение полного списка снимков

This commit is contained in:
Alexander Zhirov 2025-09-12 17:06:12 +03:00
parent 9ba43b0e38
commit e42aacbed7
Signed by: alexander
GPG key ID: C8D8BE544A27C511
4 changed files with 18 additions and 26 deletions

View file

@ -3,16 +3,11 @@ module cdcdb.cdc.cas;
import cdcdb.db;
import cdcdb.cdc.core;
import std.digest.sha : SHA256, digest;
import std.format : format;
import zstd;
import std.digest.sha : SHA256, digest;
import std.format : format;
import std.exception : enforce;
import std.stdio : writeln;
import std.conv : to;
import std.file : write;
// Content-Addressable Storage (Контентно-адресуемая система хранения)
// CAS-хранилище со снапшотами
@ -54,6 +49,10 @@ public:
size_t newSnapshot(string label, const(ubyte)[] data, string description = string.init)
{
if (data.length == 0) {
throw new Exception("Данные имеют нулевой размер");
}
ubyte[32] sha256 = digest!SHA256(data);
// Если последний снимок файла соответствует текущему состоянию
@ -132,9 +131,9 @@ public:
return idSnapshot;
}
Snapshot[] getSnapshotList(string filePath)
Snapshot[] getSnapshots(string label = "")
{
return _db.getSnapshots(filePath);
return _db.getSnapshots(label);
}
ubyte[] getSnapshotData(const ref Snapshot snapshot)

View file

@ -8,13 +8,3 @@ struct Chunk
size_t size; // размер чанка
ubyte[32] sha256; // hex(SHA-256) содержимого
}
// Метаданные снимка
struct SnapshotInfo
{
size_t id;
string createdUTC; // ISO-8601
string label;
size_t sourceLength;
size_t chunks;
}

View file

@ -4,7 +4,6 @@ import cdcdb.db.types;
import arsd.sqlite;
import std.file : exists;
import std.exception : enforce;
import std.conv : to;
import std.string : join, replace, toLower;
@ -151,9 +150,11 @@ public:
snapshot.status.to!int
);
if (!queryResult.empty())
return queryResult.front()["id"].to!long;
return 0;
if (queryResult.empty()) {
throw new Exception("Ошибка при добавлении нового снимока в базу данных");
}
return queryResult.front()["id"].to!long;
}
void addBlob(Blob blob)
@ -211,7 +212,7 @@ public:
q{
SELECT id, label, sha256, description, created_utc, source_length,
algo_min, algo_normal, algo_max, mask_s, mask_l, status
FROM snapshots WHERE label = ?
FROM snapshots WHERE (length(?) = 0 OR label = ?1);
}, label
);

View file

@ -7,10 +7,12 @@ import std.file : read;
void main()
{
auto cas = new CAS("/tmp/base.db", true);
cas.newSnapshot("/tmp/text", cast(ubyte[]) read("/tmp/text"));
cas.newSnapshot("/tmp/texts", cast(ubyte[]) read("/tmp/text"));
// import std.stdio : writeln;
writeln(cas.getSnapshotList("/tmp/text"));
foreach (snapshot; cas.getSnapshots()) {
writeln(snapshot);
}
// writeln(cas.getVersion);
}