diff options
author | Aaron Seigo <aseigo@kde.org> | 2014-12-09 22:02:35 +0100 |
---|---|---|
committer | Aaron Seigo <aseigo@kde.org> | 2014-12-11 01:01:13 +0100 |
commit | a9fb4f869271f47c3a4507d2bc6b8b3d756ec873 (patch) | |
tree | 3d4c171a0a9ed85dd01008af388cfe63bcda7538 /tests/hawd | |
parent | 02c16b560eb2b1f00c154a945b60478419ecc2eb (diff) | |
download | sink-a9fb4f869271f47c3a4507d2bc6b8b3d756ec873.tar.gz sink-a9fb4f869271f47c3a4507d2bc6b8b3d756ec873.zip |
DataSet, and create a small lib to be used by tests
Diffstat (limited to 'tests/hawd')
-rw-r--r-- | tests/hawd/CMakeLists.txt | 14 | ||||
-rw-r--r-- | tests/hawd/dataset.cpp | 193 | ||||
-rw-r--r-- | tests/hawd/dataset.h | 80 | ||||
-rw-r--r-- | tests/hawd/datasetdefinition.cpp | 46 | ||||
-rw-r--r-- | tests/hawd/datasetdefinition.h | 14 | ||||
-rw-r--r-- | tests/hawd/state.cpp | 24 | ||||
-rw-r--r-- | tests/hawd/state.h | 2 |
7 files changed, 301 insertions, 72 deletions
diff --git a/tests/hawd/CMakeLists.txt b/tests/hawd/CMakeLists.txt index 8c677c0..ebc5ac2 100644 --- a/tests/hawd/CMakeLists.txt +++ b/tests/hawd/CMakeLists.txt | |||
@@ -2,15 +2,25 @@ project(hawd) | |||
2 | 2 | ||
3 | include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) | 3 | include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) |
4 | 4 | ||
5 | set(SRCS | 5 | set(lib_SRCS |
6 | dataset.cpp | ||
6 | datasetdefinition.cpp | 7 | datasetdefinition.cpp |
8 | state.cpp | ||
9 | ) | ||
10 | |||
11 | set(SRCS | ||
7 | main.cpp | 12 | main.cpp |
8 | module.cpp | 13 | module.cpp |
9 | state.cpp | ||
10 | modules/list.cpp | 14 | modules/list.cpp |
11 | ) | 15 | ) |
12 | 16 | ||
17 | add_library(libhawd ${lib_SRCS}) | ||
18 | qt5_use_modules(libhawd Core) | ||
19 | target_link_libraries(libhawd akonadi2common) | ||
20 | install(TARGETS libhawd DESTINATION lib) | ||
21 | |||
13 | add_executable(${PROJECT_NAME} ${SRCS}) | 22 | add_executable(${PROJECT_NAME} ${SRCS}) |
14 | qt5_use_modules(${PROJECT_NAME} Core) | 23 | qt5_use_modules(${PROJECT_NAME} Core) |
24 | target_link_libraries(${PROJECT_NAME} libhawd) | ||
15 | install(TARGETS ${PROJECT_NAME} DESTINATION bin) | 25 | install(TARGETS ${PROJECT_NAME} DESTINATION bin) |
16 | 26 | ||
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 | |||
27 | namespace HAWD | ||
28 | { | ||
29 | |||
30 | Dataset::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 | |||
38 | Dataset::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 | |||
51 | Dataset::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 | |||
60 | void 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 | |||
69 | void Dataset::Row::annotate(const QString ¬e) | ||
70 | { | ||
71 | m_annotation = note; | ||
72 | } | ||
73 | |||
74 | qint64 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 | |||
83 | void 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 | |||
95 | QByteArray 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 | |||
108 | QString 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 | |||
128 | Dataset::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 | |||
136 | Dataset::~Dataset() | ||
137 | { | ||
138 | m_storage.commitTransaction(); | ||
139 | } | ||
140 | |||
141 | bool Dataset::isValid() | ||
142 | { | ||
143 | return m_definition.isValid(); | ||
144 | } | ||
145 | |||
146 | const DatasetDefinition &Dataset::definition() const | ||
147 | { | ||
148 | return m_definition; | ||
149 | } | ||
150 | |||
151 | qint64 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 | |||
163 | void Dataset::removeRow(const Row &row) | ||
164 | { | ||
165 | //TODO | ||
166 | } | ||
167 | |||
168 | Dataset::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 | |||
186 | Dataset::Row Dataset::lastRow() | ||
187 | { | ||
188 | //TODO | ||
189 | return Row(*this); | ||
190 | } | ||
191 | |||
192 | } // namespace HAWD | ||
193 | |||
diff --git a/tests/hawd/dataset.h b/tests/hawd/dataset.h new file mode 100644 index 0000000..eb18b70 --- /dev/null +++ b/tests/hawd/dataset.h | |||
@@ -0,0 +1,80 @@ | |||
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 | #pragma once | ||
21 | |||
22 | #include "datasetdefinition.h" | ||
23 | |||
24 | #include "state.h" | ||
25 | #include "common/storage.h" | ||
26 | |||
27 | #include <QHash> | ||
28 | #include <QVariant> | ||
29 | |||
30 | namespace HAWD | ||
31 | { | ||
32 | |||
33 | class Dataset | ||
34 | { | ||
35 | public: | ||
36 | class Row | ||
37 | { | ||
38 | public: | ||
39 | Row(const Row &other); | ||
40 | Row &operator=(const Row &rhs); | ||
41 | void setValue(const QString &column, const QVariant &value); | ||
42 | QVariant value(const QString &column); | ||
43 | void annotate(const QString ¬e); | ||
44 | qint64 key() const; | ||
45 | QByteArray toBinary() const; | ||
46 | QString toString() const; | ||
47 | |||
48 | private: | ||
49 | Row(); | ||
50 | Row(const Dataset &dataset, qint64 key = 0); | ||
51 | void fromBinary(QByteArray &binary); | ||
52 | |||
53 | qint64 m_key; | ||
54 | QHash<QString, DataDefinition> m_columns; | ||
55 | QHash<QString, QVariant> m_data; | ||
56 | QString m_annotation; | ||
57 | const Dataset *m_dataset; | ||
58 | friend class Dataset; | ||
59 | }; | ||
60 | |||
61 | Dataset(const DatasetDefinition &definition); | ||
62 | Dataset(const QString &name, const State &state); | ||
63 | ~Dataset(); | ||
64 | |||
65 | bool isValid(); | ||
66 | const DatasetDefinition &definition() const; | ||
67 | |||
68 | qint64 insertRow(const Row &row); | ||
69 | void removeRow(const Row &row); | ||
70 | Row row(qint64 key = 0); | ||
71 | Row lastRow(); | ||
72 | //TODO: row cursor | ||
73 | |||
74 | private: | ||
75 | DatasetDefinition m_definition; | ||
76 | Storage m_storage; | ||
77 | }; | ||
78 | |||
79 | } // namespace HAWD | ||
80 | |||
diff --git a/tests/hawd/datasetdefinition.cpp b/tests/hawd/datasetdefinition.cpp index d67c136..2fa96cf 100644 --- a/tests/hawd/datasetdefinition.cpp +++ b/tests/hawd/datasetdefinition.cpp | |||
@@ -54,6 +54,8 @@ DataDefinition::DataDefinition(const QJsonObject &json) | |||
54 | s_types.insert("int", QMetaType::Int); | 54 | s_types.insert("int", QMetaType::Int); |
55 | s_types.insert("uint", QMetaType::UInt); | 55 | s_types.insert("uint", QMetaType::UInt); |
56 | s_types.insert("bool", QMetaType::Bool); | 56 | s_types.insert("bool", QMetaType::Bool); |
57 | s_types.insert("float", QMetaType::Float); | ||
58 | s_types.insert("double", QMetaType::Double); | ||
57 | s_types.insert("char", QMetaType::QChar); | 59 | s_types.insert("char", QMetaType::QChar); |
58 | s_types.insert("string", QMetaType::QString); | 60 | s_types.insert("string", QMetaType::QString); |
59 | s_types.insert("datetime", QMetaType::QDateTime); | 61 | s_types.insert("datetime", QMetaType::QDateTime); |
@@ -95,50 +97,6 @@ int DataDefinition::max() const | |||
95 | return m_max; | 97 | return m_max; |
96 | } | 98 | } |
97 | 99 | ||
98 | DatasetRow::DatasetRow(const QHash<QString, DataDefinition> &columns) | ||
99 | : m_columns(columns) | ||
100 | { | ||
101 | QHashIterator<QString, DataDefinition> it(columns); | ||
102 | while (it.hasNext()) { | ||
103 | it.next(); | ||
104 | m_data.insert(it.key(), QVariant()); | ||
105 | } | ||
106 | } | ||
107 | |||
108 | void DatasetRow::setValue(const QString &column, const QVariant &value) | ||
109 | { | ||
110 | if (!m_columns.contains(column) || !value.canConvert(m_columns[column].type())) { | ||
111 | return; | ||
112 | } | ||
113 | |||
114 | m_data[column] = value; | ||
115 | } | ||
116 | |||
117 | void DatasetRow::annotate(const QString ¬e) | ||
118 | { | ||
119 | m_annotation = note; | ||
120 | } | ||
121 | |||
122 | QString DatasetRow::toString() const | ||
123 | { | ||
124 | if (m_data.isEmpty()) { | ||
125 | return QString(); | ||
126 | } | ||
127 | |||
128 | QString string = QString::number(QDateTime::currentMSecsSinceEpoch()); | ||
129 | QHashIterator<QString, QVariant> it(m_data); | ||
130 | while (it.hasNext()) { | ||
131 | it.next(); | ||
132 | string.append('\t').append(it.value().toString()); | ||
133 | } | ||
134 | |||
135 | if (!m_annotation.isEmpty()) { | ||
136 | string.append('\t').append(m_annotation); | ||
137 | } | ||
138 | |||
139 | return string; | ||
140 | } | ||
141 | |||
142 | DatasetDefinition::DatasetDefinition(const QString &path) | 100 | DatasetDefinition::DatasetDefinition(const QString &path) |
143 | : m_valid(false) | 101 | : m_valid(false) |
144 | { | 102 | { |
diff --git a/tests/hawd/datasetdefinition.h b/tests/hawd/datasetdefinition.h index 0593f39..eb4b780 100644 --- a/tests/hawd/datasetdefinition.h +++ b/tests/hawd/datasetdefinition.h | |||
@@ -47,20 +47,6 @@ private: | |||
47 | int m_max; | 47 | int m_max; |
48 | }; | 48 | }; |
49 | 49 | ||
50 | class DatasetRow | ||
51 | { | ||
52 | public: | ||
53 | DatasetRow(const QHash<QString, DataDefinition> &columns); | ||
54 | void setValue(const QString &column, const QVariant &value); | ||
55 | void annotate(const QString ¬e); | ||
56 | QString toString() const; | ||
57 | |||
58 | private: | ||
59 | QHash<QString, DataDefinition> m_columns; | ||
60 | QHash<QString, QVariant> m_data; | ||
61 | QString m_annotation; | ||
62 | }; | ||
63 | |||
64 | class DatasetDefinition | 50 | class DatasetDefinition |
65 | { | 51 | { |
66 | public: | 52 | public: |
diff --git a/tests/hawd/state.cpp b/tests/hawd/state.cpp index cfc4326..3ee7775 100644 --- a/tests/hawd/state.cpp +++ b/tests/hawd/state.cpp | |||
@@ -31,22 +31,24 @@ static const QString configFileName("hawd.conf"); | |||
31 | namespace HAWD | 31 | namespace HAWD |
32 | { | 32 | { |
33 | 33 | ||
34 | State::State() | 34 | State::State(const QString &_configPath) |
35 | : m_valid(true) | 35 | : m_valid(true) |
36 | { | 36 | { |
37 | QDir dir; | 37 | QString configPath = _configPath; |
38 | QString configPath; | 38 | if (configPath.isEmpty()) { |
39 | QDir dir; | ||
39 | 40 | ||
40 | while (!dir.exists(configFileName) && dir.cdUp()) { } | 41 | while (!dir.exists(configFileName) && dir.cdUp()) { } |
41 | 42 | ||
42 | if (dir.exists(configFileName)) { | 43 | if (dir.exists(configFileName)) { |
43 | configPath = dir.absoluteFilePath(configFileName); | 44 | configPath = dir.absoluteFilePath(configFileName); |
44 | } | 45 | } |
45 | 46 | ||
46 | if (configPath.isEmpty()) { | 47 | if (configPath.isEmpty()) { |
47 | std::cerr << QObject::tr("Could not find hawd configuration. A hawd.conf file must be in the current directory or in a directory above it.").toStdString() << std::endl; | 48 | std::cerr << QObject::tr("Could not find hawd configuration. A hawd.conf file must be in the current directory or in a directory above it.").toStdString() << std::endl; |
48 | m_valid = false; | 49 | m_valid = false; |
49 | return; | 50 | return; |
51 | } | ||
50 | } | 52 | } |
51 | 53 | ||
52 | QFile configFile(configPath); | 54 | QFile configFile(configPath); |
diff --git a/tests/hawd/state.h b/tests/hawd/state.h index 5e4d3c2..72ac528 100644 --- a/tests/hawd/state.h +++ b/tests/hawd/state.h | |||
@@ -30,7 +30,7 @@ namespace HAWD | |||
30 | class State | 30 | class State |
31 | { | 31 | { |
32 | public: | 32 | public: |
33 | State(); | 33 | State(const QString &configPath = QString()); |
34 | 34 | ||
35 | bool isValid() const; | 35 | bool isValid() const; |
36 | QVariant configValue(const QString &key) const; | 36 | QVariant configValue(const QString &key) const; |