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.cpp91
1 files changed, 83 insertions, 8 deletions
diff --git a/tests/hawd/dataset.cpp b/tests/hawd/dataset.cpp
index f6a0e4c..6fc85ce 100644
--- a/tests/hawd/dataset.cpp
+++ b/tests/hawd/dataset.cpp
@@ -28,6 +28,7 @@ namespace HAWD
28{ 28{
29 29
30static const QString s_annotationKey("__annotation__"); 30static const QString s_annotationKey("__annotation__");
31static const QString s_hashKey("__commithash__");
31 32
32Dataset::Row::Row(const Row &other) 33Dataset::Row::Row(const Row &other)
33 : m_key(other.m_key), 34 : m_key(other.m_key),
@@ -92,6 +93,8 @@ void Dataset::Row::fromBinary(QByteArray &data)
92 stream >> key >> value; 93 stream >> key >> value;
93 if (key == s_annotationKey) { 94 if (key == s_annotationKey) {
94 m_annotation = value.toString(); 95 m_annotation = value.toString();
96 } else if (key == s_hashKey) {
97 m_hash = value.toString();
95 } else { 98 } else {
96 setValue(key, value); 99 setValue(key, value);
97 } 100 }
@@ -108,35 +111,85 @@ QByteArray Dataset::Row::toBinary() const
108 stream << it.key() << it.value(); 111 stream << it.key() << it.value();
109 } 112 }
110 113
114 if (!m_hash.isEmpty()) {
115 stream << s_hashKey << m_hash;
116 }
117
111 if (!m_annotation.isEmpty()) { 118 if (!m_annotation.isEmpty()) {
112 stream << s_annotationKey << m_annotation; 119 stream << s_annotationKey << m_annotation;
113 } 120 }
114 return data; 121 return data;
115} 122}
116 123
117QString Dataset::Row::toString() const 124QString Dataset::tableHeaders(const QStringList &cols, int standardCols, const QString &seperator) const
125{
126 if (!isValid()) {
127 return QString();
128 }
129
130 QStringList strings;
131
132 if (standardCols & Row::Timestamp) {
133 strings << QObject::tr("Timestamp");
134 }
135
136 if (standardCols & Row::CommitHash) {
137 strings << QObject::tr("Commit");
138 }
139
140 QHashIterator<QString, DataDefinition> it(m_definition.columns());
141 while (it.hasNext()) {
142 it.next();
143 QString header = it.key();
144 if (cols.isEmpty() || cols.contains(header)) {
145 if (!it.value().unit().isEmpty()) {
146 header.append(" (").append(it.value().unit()).append(")");
147 }
148 strings << header;
149 }
150 }
151
152 if (standardCols & Row::Annotation) {
153 strings << QObject::tr("Annotation");
154 }
155
156 return strings.join(seperator);
157}
158
159QString Dataset::Row::toString(const QStringList &cols, int standardCols, const QString &seperator) const
118{ 160{
119 if (m_data.isEmpty()) { 161 if (m_data.isEmpty()) {
120 return QString(); 162 return QString();
121 } 163 }
122 164
123 QString string; 165 QStringList strings;
166
167 if (standardCols & Timestamp) {
168 strings << QString::number(m_key);
169 }
170
171 if (standardCols & CommitHash) {
172 strings << m_hash;
173 }
174
124 QHashIterator<QString, QVariant> it(m_data); 175 QHashIterator<QString, QVariant> it(m_data);
125 while (it.hasNext()) { 176 while (it.hasNext()) {
126 it.next(); 177 it.next();
127 string.append('\t').append(it.value().toString()); 178 if (cols.isEmpty() || cols.contains(it.key())) {
179 strings << it.value().toString();
180 }
128 } 181 }
129 182
130 if (!m_annotation.isEmpty()) { 183 if (standardCols & Annotation) {
131 string.append('\t').append(m_annotation); 184 strings << m_annotation;
132 } 185 }
133 186
134 return string; 187 return strings.join(seperator);
135} 188}
136 189
137Dataset::Dataset(const QString &name, const State &state) 190Dataset::Dataset(const QString &name, const State &state)
138 : m_definition(state.datasetDefinition(name)), 191 : m_definition(state.datasetDefinition(name)),
139 m_storage(state.resultsPath(), m_definition.name(), Storage::ReadWrite) 192 m_storage(state.resultsPath(), name, Storage::ReadWrite)
140{ 193{
141 //TODO: it should use a different file name if the data columns have changed 194 //TODO: it should use a different file name if the data columns have changed
142 m_storage.startTransaction(); 195 m_storage.startTransaction();
@@ -147,7 +200,7 @@ Dataset::~Dataset()
147 m_storage.commitTransaction(); 200 m_storage.commitTransaction();
148} 201}
149 202
150bool Dataset::isValid() 203bool Dataset::isValid() const
151{ 204{
152 return m_definition.isValid(); 205 return m_definition.isValid();
153} 206}
@@ -174,6 +227,28 @@ void Dataset::removeRow(const Row &row)
174 //TODO 227 //TODO
175} 228}
176 229
230void Dataset::eachRow(const std::function<void(const Row &row)> &resultHandler)
231{
232 if (!isValid()) {
233 return;
234 }
235
236 Row row(*this);
237 m_storage.readAll(
238 [&](void *key, int keySize, void *data, int dataSize) -> bool {
239 if (keySize != sizeof(qint64)) {
240 return true;
241 }
242
243 QByteArray array((const char*)data, dataSize);
244 row.fromBinary(array);
245 row.m_key = *(qint64 *)key;
246 resultHandler(row);
247 return true;
248 },
249 Storage::basicErrorHandler());
250}
251
177Dataset::Row Dataset::row(qint64 key) 252Dataset::Row Dataset::row(qint64 key)
178{ 253{
179 if (key < 1) { 254 if (key < 1) {