summaryrefslogtreecommitdiffstats
path: root/tests/hawd/dataset.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/hawd/dataset.cpp')
-rw-r--r--tests/hawd/dataset.cpp193
1 files changed, 193 insertions, 0 deletions
diff --git a/tests/hawd/dataset.cpp b/tests/hawd/dataset.cpp
new file mode 100644
index 0000000..9211430
--- /dev/null
+++ b/tests/hawd/dataset.cpp
@@ -0,0 +1,193 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "dataset.h"
21
22#include <QDateTime>
23#include <QDebug>
24
25#include <iostream>
26
27namespace HAWD
28{
29
30Dataset::Row::Row(const Row &other)
31 : m_key(other.m_key),
32 m_columns(other.m_columns),
33 m_data(other.m_data),
34 m_dataset(other.m_dataset)
35{
36}
37
38Dataset::Row::Row(const Dataset &dataset, qint64 key)
39 : m_key(key),
40 m_columns(dataset.definition().columns()),
41 m_dataset(&dataset)
42{
43 // TODO: pre-populate m_data, or do that on buffer creation?
44 QHashIterator<QString, DataDefinition> it(dataset.definition().columns());
45 while (it.hasNext()) {
46 it.next();
47 m_data.insert(it.key(), QVariant());
48 }
49}
50
51Dataset::Row &Dataset::Row::operator=(const Row &rhs)
52{
53 m_key = rhs.m_key;
54 m_columns = rhs.m_columns;
55 m_data = rhs.m_data;
56 m_dataset = rhs.m_dataset;
57 return *this;
58}
59
60void Dataset::Row::setValue(const QString &column, const QVariant &value)
61{
62 if (!m_columns.contains(column) || !value.canConvert(m_columns[column].type())) {
63 return;
64 }
65
66 m_data[column] = value;
67}
68
69void Dataset::Row::annotate(const QString &note)
70{
71 m_annotation = note;
72}
73
74qint64 Dataset::Row::key() const
75{
76 if (m_key < 1) {
77 const_cast<Dataset::Row *>(this)->m_key = QDateTime::currentMSecsSinceEpoch();
78 }
79
80 return m_key;
81}
82
83void Dataset::Row::fromBinary(QByteArray &data)
84{
85 QVariant value;
86 QString key;
87 QDataStream stream(&data, QIODevice::ReadOnly);
88
89 while (!stream.atEnd()) {
90 stream >> key >> value;
91 setValue(key, value);
92 }
93}
94
95QByteArray Dataset::Row::toBinary() const
96{
97 QByteArray data;
98 QDataStream stream(&data, QIODevice::WriteOnly);
99 QHashIterator<QString, QVariant> it(m_data);
100 while (it.hasNext()) {
101 it.next();
102 stream << it.key() << it.value();
103 }
104
105 return data;
106}
107
108QString Dataset::Row::toString() const
109{
110 if (m_data.isEmpty()) {
111 return QString();
112 }
113
114 QString string;
115 QHashIterator<QString, QVariant> it(m_data);
116 while (it.hasNext()) {
117 it.next();
118 string.append('\t').append(it.value().toString());
119 }
120
121 if (!m_annotation.isEmpty()) {
122 string.append('\t').append(m_annotation);
123 }
124
125 return string;
126}
127
128Dataset::Dataset(const QString &name, const State &state)
129 : m_definition(state.datasetDefinition(name)),
130 m_storage(state.resultsPath(), m_definition.name(), Storage::ReadWrite)
131{
132 //TODO: it should use a different file name if the data columns have changed
133 m_storage.startTransaction();
134}
135
136Dataset::~Dataset()
137{
138 m_storage.commitTransaction();
139}
140
141bool Dataset::isValid()
142{
143 return m_definition.isValid();
144}
145
146const DatasetDefinition &Dataset::definition() const
147{
148 return m_definition;
149}
150
151qint64 Dataset::insertRow(const Row &row)
152{
153 if (row.m_dataset != this) {
154 return 0;
155 }
156
157 qint64 key = row.key();
158 QByteArray data = row.toBinary();
159 m_storage.write((const char *)&key, sizeof(qint64), data.constData(), data.size());
160 return key;
161}
162
163void Dataset::removeRow(const Row &row)
164{
165 //TODO
166}
167
168Dataset::Row Dataset::row(qint64 key)
169{
170 if (key < 1) {
171 return Row(*this);
172 }
173
174 Row row(*this, key);
175 m_storage.read((const char *)&key, sizeof(qint64),
176 [&row](void *ptr, int size) -> bool {
177 QByteArray array((const char*)ptr, size);
178 row.fromBinary(array);
179 return true;
180 },
181 Storage::basicErrorHandler()
182 );
183 return row;
184}
185
186Dataset::Row Dataset::lastRow()
187{
188 //TODO
189 return Row(*this);
190}
191
192} // namespace HAWD
193