sensordatastore.h 5.26 KB
Newer Older
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
//================================================================================
// Name        : sensordatastore.h
// Author      : Axel Auweter
// Copyright   : Leibniz Supercomputing Centre
// Description : C++ API for inserting and querying DCDB sensor data.
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
// Copyright (C) 2011-2016 Leibniz Supercomputing Centre
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//================================================================================

27

28
29
/**
 * @file
Michael Ott's avatar
Michael Ott committed
30
 * @brief This file contains the public API for the libdcdb library.
31
32
33
34
35
 * It contains the class definition of the library's main class
 * SensorDataStore as well as the helper struct typedefs SensorId,
 * DeviceLocation, and DeviceSensorId.
 */

36
#include <string>
Axel Auweter's avatar
Axel Auweter committed
37
38
#include <list>
#include <cstdint>
39

Axel Auweter's avatar
Axel Auweter committed
40
#include "sensorid.h"
Axel Auweter's avatar
Axel Auweter committed
41
#include "timestamp.h"
42
43
#include "connection.h"

44
45
46
47
#ifndef DCDB_SENSORDATASTORE_H
#define DCDB_SENSORDATASTORE_H

namespace DCDB {
48

Axel Auweter's avatar
Axel Auweter committed
49
50
51
52
53
typedef enum {
  SDS_OK,
  SDS_EMPTYSET
} SDSQueryResult;

54
/* Forward-declaration of the implementation-internal classes */
55
class SensorDataStoreImpl;
56

Axel Auweter's avatar
Axel Auweter committed
57
58
59
60
61
62
63
64
/**
 * @brief SensorDataStoreReading is the class for a single
 * sensor-timestamp-value entry in the database.
 */
class SensorDataStoreReading
{
public:
  SensorId sensorId;
65
  TimeStamp timeStamp;
66
  int64_t value;
67
68
69
70
71
72
73
74

#if 0
  inline bool operator == (const SensorDataStoreReading& rhs) const {
    return (sensorId == rhs.sensorId) &&
        (timeStamp == rhs.timeStamp) &&
        (value == rhs.value);}
#endif

Axel Auweter's avatar
Axel Auweter committed
75
76
};

77
/**
Michael Ott's avatar
Michael Ott committed
78
 * @brief SensorDataStore is the class of the libdcdb library
Axel Auweter's avatar
Axel Auweter committed
79
 * to write and read sensor data.
80
 */
81
82
83
84
85
86
class SensorDataStore
{
private:
    SensorDataStoreImpl* impl;
    
public:
87
88
89
    /**
     * @brief This function inserts a single sensor reading into
     *        the database.
Axel Auweter's avatar
Axel Auweter committed
90
     * @param sid      A SensorId object.
91
92
93
     * @param ts       The timestamp of the sensor reading.
     * @param value    The value of the sensor reading.
     */
94
    void insert(SensorId* sid, uint64_t ts, int64_t value);
95
    
96
    /**
97
98
     * @brief Set the TTL for newly inserted sensor data.
     * @param ttl      The TTL for the sensor data in seconds.
99
     */
100
    void setTTL(uint64_t ttl);
101

Axel Auweter's avatar
Axel Auweter committed
102
    /**
103
     * @brief This function queries a sensor's values in
Axel Auweter's avatar
Axel Auweter committed
104
105
106
107
108
109
     *        the given time range.
     * @param result   The list where the results will be stored.
     * @param sid      The SensorId to query.
     * @param start    Start of the time series.
     * @param end      End of the time series.
     */
110
    void query(std::list<SensorDataStoreReading>& result, SensorId& sid, TimeStamp& start, TimeStamp& end);
Axel Auweter's avatar
Axel Auweter committed
111

112
113
114
115
116
117
118
119
120
121
122
123
    typedef void (*QueryCbFunc)(SensorDataStoreReading& reading, void* userData);
    /**
     * @brief This function queries a sensor's values in
     *        the given time range.
     * @param cbFunc   A function to called for each reading.
     * @param userData Pointer to user data that will be returned in the callback.
     * @param sid      The SensorId to query.
     * @param start    Start of the time series.
     * @param end      End of the time series.
     */
    void queryCB(QueryCbFunc cbFunc, void* userData, SensorId& sid, TimeStamp& start, TimeStamp& end);

124
125
126
127
128
129
130
    /**
     * @brief This function queries the integrated value (val * sec)
     *        of a sensor for the given time range.
     * @param result   The resulting integrated value.
     * @param sid      The SensorId to query.
     * @param start    Start of the time series.
     * @param end      End of the time series.
Axel Auweter's avatar
Axel Auweter committed
131
     * @return         SDS_OK if ok, SDS_EMPTYSET if not at least 2 readings in interval.
132
     */
133
    SDSQueryResult querySum(int64_t& result, SensorId& sid, TimeStamp& start, TimeStamp& end);
134

135
136
137
138
139
140
141
    /**
     * @brief This function truncates all sensor data that is older than
     *        the specified week.
     * @param weekStamp   The 16-bit weekstamp generated from a cut-off date
     */
    void truncBeforeWeek(uint16_t weekStamp);

142
    /**
143
144
145
     * @brief A shortcut constructor for a SensorDataStore object
     *        that allows accessing the data store through a
     *        connection that is already established.
146
     * @param conn     The Connection object of an established
Axel Auweter's avatar
Axel Auweter committed
147
     *                 connection to Cassandra.
148
     */
149
    SensorDataStore(Connection* conn);
150
151
152
153

    /**
     * @brief The standard destructor for a SensorDatStore object.
     */
154
155
156
    virtual ~SensorDataStore();
};

157
} /* End of namespace DCDB */
158

159
#endif /* DCDB_SENSORDATASTORE_H */