annotate OrthancServer/Plugins/Samples/DelayedDeletion/PendingDeletionsDatabase.cpp @ 5083:75e949689c08 attach-custom-data

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