diff options
author | Aaron Seigo <aseigo@kde.org> | 2014-12-10 08:49:30 +0100 |
---|---|---|
committer | Aaron Seigo <aseigo@kde.org> | 2014-12-11 01:02:32 +0100 |
commit | 49400da50b9095ea8b935135877ed76163552c7f (patch) | |
tree | 175bb787c8b3e98fc926daf1115fea0cd1a073de | |
parent | 52607c5d331bb997acb0c96c6a68ded5e679b071 (diff) | |
download | sink-49400da50b9095ea8b935135877ed76163552c7f.tar.gz sink-49400da50b9095ea8b935135877ed76163552c7f.zip |
add printing features and stub in hashes
-rw-r--r-- | tests/hawd/dataset.cpp | 91 | ||||
-rw-r--r-- | tests/hawd/dataset.h | 13 |
2 files changed, 94 insertions, 10 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 | ||
30 | static const QString s_annotationKey("__annotation__"); | 30 | static const QString s_annotationKey("__annotation__"); |
31 | static const QString s_hashKey("__commithash__"); | ||
31 | 32 | ||
32 | Dataset::Row::Row(const Row &other) | 33 | Dataset::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 | ||
117 | QString Dataset::Row::toString() const | 124 | QString 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 | |||
159 | QString 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 | ||
137 | Dataset::Dataset(const QString &name, const State &state) | 190 | Dataset::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 | ||
150 | bool Dataset::isValid() | 203 | bool 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 | ||
230 | void 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 | |||
177 | Dataset::Row Dataset::row(qint64 key) | 252 | Dataset::Row Dataset::row(qint64 key) |
178 | { | 253 | { |
179 | if (key < 1) { | 254 | if (key < 1) { |
diff --git a/tests/hawd/dataset.h b/tests/hawd/dataset.h index eb18b70..f23d67c 100644 --- a/tests/hawd/dataset.h +++ b/tests/hawd/dataset.h | |||
@@ -36,6 +36,12 @@ public: | |||
36 | class Row | 36 | class Row |
37 | { | 37 | { |
38 | public: | 38 | public: |
39 | enum StandardCols { | ||
40 | Annotation, | ||
41 | CommitHash, | ||
42 | Timestamp, | ||
43 | All = Annotation | CommitHash | Timestamp | ||
44 | }; | ||
39 | Row(const Row &other); | 45 | Row(const Row &other); |
40 | Row &operator=(const Row &rhs); | 46 | Row &operator=(const Row &rhs); |
41 | void setValue(const QString &column, const QVariant &value); | 47 | void setValue(const QString &column, const QVariant &value); |
@@ -43,7 +49,7 @@ public: | |||
43 | void annotate(const QString ¬e); | 49 | void annotate(const QString ¬e); |
44 | qint64 key() const; | 50 | qint64 key() const; |
45 | QByteArray toBinary() const; | 51 | QByteArray toBinary() const; |
46 | QString toString() const; | 52 | QString toString(const QStringList &cols = QStringList(), int standardCols = All, const QString &seperator = "\t") const; |
47 | 53 | ||
48 | private: | 54 | private: |
49 | Row(); | 55 | Row(); |
@@ -54,6 +60,7 @@ public: | |||
54 | QHash<QString, DataDefinition> m_columns; | 60 | QHash<QString, DataDefinition> m_columns; |
55 | QHash<QString, QVariant> m_data; | 61 | QHash<QString, QVariant> m_data; |
56 | QString m_annotation; | 62 | QString m_annotation; |
63 | QString m_hash; | ||
57 | const Dataset *m_dataset; | 64 | const Dataset *m_dataset; |
58 | friend class Dataset; | 65 | friend class Dataset; |
59 | }; | 66 | }; |
@@ -62,11 +69,13 @@ public: | |||
62 | Dataset(const QString &name, const State &state); | 69 | Dataset(const QString &name, const State &state); |
63 | ~Dataset(); | 70 | ~Dataset(); |
64 | 71 | ||
65 | bool isValid(); | 72 | bool isValid() const; |
66 | const DatasetDefinition &definition() const; | 73 | const DatasetDefinition &definition() const; |
74 | QString tableHeaders(const QStringList &cols = QStringList(), int standardCols = Row::All, const QString &seperator = "\t") const; | ||
67 | 75 | ||
68 | qint64 insertRow(const Row &row); | 76 | qint64 insertRow(const Row &row); |
69 | void removeRow(const Row &row); | 77 | void removeRow(const Row &row); |
78 | void eachRow(const std::function<void(const Row &row)> &resultHandler); | ||
70 | Row row(qint64 key = 0); | 79 | Row row(qint64 key = 0); |
71 | Row lastRow(); | 80 | Row lastRow(); |
72 | //TODO: row cursor | 81 | //TODO: row cursor |