comparison OrthancServer/Plugins/Samples/DelayedDeletion/PendingDeletionsDatabase.cpp @ 5024:c2ebc47f4f18 delayed-deletion

wip: adding DelayedDeletion plugin
author Alain Mazy <am@osimis.io>
date Mon, 20 Jun 2022 16:53:21 +0200
parents
children 99751c5a7cfe
comparison
equal deleted inserted replaced
5021:559b35d18ef7 5024:c2ebc47f4f18
1 #include "PendingDeletionsDatabase.h"
2
3 #include "../../../../OrthancFramework/Sources/SQLite/Statement.h"
4 #include "../../../../OrthancFramework/Sources/SQLite/Transaction.h"
5
6 void PendingDeletionsDatabase::Setup()
7 {
8 // Performance tuning of SQLite with PRAGMAs
9 // http://www.sqlite.org/pragma.html
10 db_.Execute("PRAGMA SYNCHRONOUS=NORMAL;");
11 db_.Execute("PRAGMA JOURNAL_MODE=WAL;");
12 db_.Execute("PRAGMA LOCKING_MODE=EXCLUSIVE;");
13 db_.Execute("PRAGMA WAL_AUTOCHECKPOINT=1000;");
14
15 {
16 Orthanc::SQLite::Transaction t(db_);
17 t.Begin();
18
19 if (!db_.DoesTableExist("Pending"))
20 {
21 db_.Execute("CREATE TABLE Pending(uuid TEXT, type INTEGER)");
22 }
23
24 t.Commit();
25 }
26 }
27
28
29 PendingDeletionsDatabase::PendingDeletionsDatabase(const std::string& path)
30 {
31 db_.Open(path);
32 Setup();
33 }
34
35
36 void PendingDeletionsDatabase::Enqueue(const std::string& uuid,
37 Orthanc::FileContentType type)
38 {
39 boost::mutex::scoped_lock lock(mutex_);
40
41 Orthanc::SQLite::Transaction t(db_);
42 t.Begin();
43
44 {
45 Orthanc::SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Pending VALUES(?, ?)");
46 s.BindString(0, uuid);
47 s.BindInt(1, type);
48 s.Run();
49 }
50
51 t.Commit();
52 }
53
54
55 bool PendingDeletionsDatabase::Dequeue(std::string& uuid,
56 Orthanc::FileContentType& type)
57 {
58 bool ok = false;
59
60 boost::mutex::scoped_lock lock(mutex_);
61
62 Orthanc::SQLite::Transaction t(db_);
63 t.Begin();
64
65 {
66 Orthanc::SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT uuid, type FROM Pending LIMIT 1");
67
68 if (s.Step())
69 {
70 uuid = s.ColumnString(0);
71 type = static_cast<Orthanc::FileContentType>(s.ColumnInt(1));
72
73 Orthanc::SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM Pending WHERE uuid=?");
74 s.BindString(0, uuid);
75 s.Run();
76
77 ok = true;
78 }
79 }
80
81 t.Commit();
82
83 return ok;
84 }
85
86
87 unsigned int PendingDeletionsDatabase::GetSize()
88 {
89 boost::mutex::scoped_lock lock(mutex_);
90
91 unsigned int value = 0;
92
93 Orthanc::SQLite::Transaction t(db_);
94 t.Begin();
95
96 {
97 Orthanc::SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT COUNT(*) FROM Pending");
98
99 if (s.Step())
100 {
101 int tmp = s.ColumnInt(0);
102 if (tmp > 0)
103 {
104 value = static_cast<unsigned int>(tmp);
105 }
106 }
107 }
108
109 t.Commit();
110
111 return value;
112 }