summaryrefslogtreecommitdiffhomepage
path: root/storage.cpp
blob: a7d5a3612504a01818c890e66c329cf3a296f442 (plain)
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
#include "storage.h"

#include "config.h"

#include <chrono>
#include <iostream>
#include <random>

#include <SQLiteCpp/SQLiteCpp.h>

using namespace std::string_literals;

Storage::Storage(const Config& config):
 m_db(config.getDataPath() + "/whiteboard.db3", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE),
 m_maxage(config.getMaxage()),
 m_stmt_create(m_db),
 m_stmt_getNumberOfDocuments(m_db),
 m_stmt_cleanup(m_db),
 m_stmt_exists(m_db),
 m_stmt_getDocument(m_db),
 m_stmt_getRevision(m_db),
 m_stmt_getCursorPos(m_db),
 m_stmt_getRow(m_db),
 m_stmt_setDocument(m_db),
 m_stmt_setDocument_new(m_db),
 m_stmt_setRevision(m_db),
 m_stmt_setCursorPos(m_db),
 m_stmt_setRow(m_db)
{
 m_stmt_create.init("CREATE TABLE IF NOT EXISTS documents (id VARCHAR(16) PRIMARY KEY, value BLOB, rev INTEGER, cursorpos INTEGER, timestamp BIGINT)");
 m_stmt_create.execute();
}

Storage::~Storage()
{
}

uint64_t Storage::getNumberOfDocuments()
{
 m_stmt_getNumberOfDocuments.init("SELECT COUNT(*) FROM documents");
 if (!m_stmt_getNumberOfDocuments.execute())
  throw std::runtime_error("Count not possible");
 
 return m_stmt_getNumberOfDocuments.getColumn<int64_t>(0);
}

namespace {
 uint64_t unixepoch()
 {
  const auto p1 = std::chrono::system_clock::now();
  return std::chrono::duration_cast<std::chrono::seconds>(p1.time_since_epoch()).count();
 }
} // namespace

void Storage::cleanup()
{
 if (m_maxage == 0)
  return;

 m_stmt_cleanup.init("DELETE FROM documents WHERE timestamp + ? < ?");
 m_stmt_cleanup.bind(1, static_cast<int64_t>(m_maxage));
 m_stmt_cleanup.bind(2, static_cast<int64_t>(unixepoch()));
 m_stmt_cleanup.execute();
}

bool Storage::exists(const std::string& id)
{
 m_stmt_exists.init("SELECT id FROM documents WHERE id = ?");
 m_stmt_exists.bind(1, id);

 return m_stmt_exists.execute();
}

std::string Storage::getDocument(const std::string& id)
{
 m_stmt_getDocument.init("SELECT value FROM documents WHERE id = ?");
 m_stmt_getDocument.bind(1, id);

 if (!m_stmt_getDocument.execute())
  throw std::runtime_error("id "s + id + " not found"s);

 return m_stmt_getDocument.getColumn<std::string>(0);
}

int Storage::getRevision(const std::string& id)
{
 m_stmt_getRevision.init("SELECT rev FROM documents WHERE id = ?");
 m_stmt_getRevision.bind(1, id);

 if (!m_stmt_getRevision.execute())
  throw std::runtime_error("id "s + id + " not found"s);

 return m_stmt_getRevision.getColumn<int>(0);
}

int Storage::getCursorPos(const std::string& id)
{
 m_stmt_getCursorPos.init("SELECT cursorpos FROM documents WHERE id = ?");
 m_stmt_getCursorPos.bind(1, id);

 if (!m_stmt_getCursorPos.execute())
  throw std::runtime_error("id "s + id + " not found"s);

 return m_stmt_getCursorPos.getColumn<int>(0);
}

std::tuple<std::string, int, int> Storage::getRow(const std::string& id)
{
 m_stmt_getRow.init("SELECT value, rev, cursorpos FROM documents WHERE id = ?");
 m_stmt_getRow.bind(1, id);

 if (!m_stmt_getRow.execute())
  throw std::runtime_error("id "s + id + " not found"s);

 return {m_stmt_getRow.getColumn<std::string>(0), m_stmt_getRow.getColumn<int>(1), m_stmt_getRow.getColumn<int>(2)};
}
 
void Storage::setDocument(const std::string& id, const std::string& document)
{
 m_stmt_setDocument.init("UPDATE documents SET value = ?, timestamp = ?, rev = rev + 1 WHERE id = ?");
 m_stmt_setDocument.bind(1, document);
 m_stmt_setDocument.bind(2, static_cast<int64_t>(unixepoch()));
 m_stmt_setDocument.bind(3, id);

 if (!m_stmt_setDocument.execute()) {
  m_stmt_setDocument_new.init("INSERT INTO documents (id, value, rev, cursorpos, timestamp) values (?, ?, ?, ?, ?)");
  m_stmt_setDocument_new.bind(1, id);
  m_stmt_setDocument_new.bind(2, document);
  m_stmt_setDocument_new.bind(3, 0);
  m_stmt_setDocument_new.bind(4, 0);
  m_stmt_setDocument_new.bind(5, static_cast<int64_t>(unixepoch()));
  m_stmt_setDocument_new.execute();
 }
}

void Storage::setRevision(const std::string& id, int rev)
{
 m_stmt_setRevision.init("UPDATE documents SET rev = ? WHERE id = ?");
 m_stmt_setRevision.bind(1, rev);
 m_stmt_setRevision.bind(2, id);

 if (!m_stmt_setRevision.execute())
  throw std::runtime_error("Unable to insert row with id "s + id);
}

void Storage::setCursorPos(const std::string& id, int cursorPos)
{
 m_stmt_setCursorPos.init("UPDATE documents SET cursorpos = ? WHERE id = ?");
 m_stmt_setCursorPos.bind(1, cursorPos);
 m_stmt_setCursorPos.bind(2, id);

 if (!m_stmt_setCursorPos.execute())
  throw std::runtime_error("Unable to insert row with id "s + id);
}

void Storage::setRow(const std::string& id, const std::string& document, int rev, int cursorPos)
{
 m_stmt_setRow.init("INSERT OR REPLACE INTO documents (id, value, rev, cursorpos, timestamp) values (?, ?, ?, ?, ?)");
 m_stmt_setRow.bind(1, id);
 m_stmt_setRow.bind(2, document);
 m_stmt_setRow.bind(3, rev);
 m_stmt_setRow.bind(4, cursorPos);
 m_stmt_setRow.bind(5, static_cast<int64_t>(unixepoch()));
 if (!m_stmt_setRow.execute())
  throw std::runtime_error("Unable to insert row with id "s + id);
}

uint32_t Storage::checksum32(const std::string& s)
{
 uint32_t result{0};
 for (unsigned int i = 0; i < s.size(); i++) {
  result = ((result >> 1) | ((result & 1) << 31)) ^ (s[i] & 0xFF);
 }
 return result & 0x7FFFFFFF;
}

std::string Storage::generate_id()
{
 static std::random_device r;
 static std::default_random_engine e1(r());
 static std::uniform_int_distribution<int> uniform_dist(0, 35);

 // limit tries
 for (int j = 0; j < 100000; j++) {
  std::string result;
  for (int i = 0; i < 6; i++) {
   char c{static_cast<char>('0' + uniform_dist(e1))};
   if (c > '9')
    c = c - '9' - 1 + 'a';
   result.push_back(c);
  }

  if (!exists(result))
   return result;
 }

 return "endofcodes";
}