sensordatastore.cpp 5.91 KB
Newer Older
1
2
3
4
5
6
7
/*
 * sensordatastore.cpp
 *
 *  Created on: Jul 24, 2013
 *      Author: Axel Auweter
 */

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
 * @mainpage
 * The DCDBLib library is a dynamic runtime library providing
 * functions to initialize and access the DCDB data store. It
 * is being used by the CollectAgent to handle insertion of
 * data and can be used by tools responsible for data analysis.
 *
 * Its main class is the SensorDataStore class which provides
 * functions to connect to the data store, initialize an empty
 * data base and to (TODO) retrieve data.
 *
 * For its internal handling, SensorDataStore relies on the
 * SensorDataStoreImpl class (which hides all private member
 * functions belonging to the SensorDataStore class from the
 * header that is used by programmers who link against this
 * library). Raw database functionality is abstracted into the
 * CassandraBackend class (to easy switching to other
 * key-value style databases in the future).
 *
 * To use the library in your client application, simnply
 * include the sensordatastore.h header file and initialize
 * an object of the SensorDataStore class.
 */

32
33
34
35
36
#include "sensordatastore_internal.h"

string SensorDataStoreImpl::sidConvert(SensorId *sid)
{
    uint64_t ll[2];
37
38
    ll[0] = Endian::hostToBE(sid->raw[0]);
    ll[1] = Endian::hostToBE(sid->raw[1]);
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    return string((char*)ll, 16);
}

bool SensorDataStoreImpl::topicToSid(SensorId* sid, string topic)
{
    uint64_t pos = 0;
    const char* buf = topic.c_str();
    sid->raw[0] = 0;
    sid->raw[1] = 0;
    while (*buf) {
        if (*buf >= '0' && *buf <= '9') {
            sid->raw[pos / 64] |= (((uint64_t)(*buf - '0')) << (60-(pos%64)));
            pos += 4;
        }
        else if (*buf >= 'A' && *buf <= 'F') {
            sid->raw[pos / 64] |= (((uint64_t)(*buf - 'A' + 0xa)) << (60-(pos%64)));
            pos += 4;
        }
        else if (*buf >= 'a' && *buf <= 'f') {
            sid->raw[pos / 64] |= (((uint64_t)(*buf - 'a' + 0xa)) << (60-(pos%64)));
            pos += 4;
        }
        buf++;
    }
    return pos == 128;
}

66
67
void SensorDataStoreImpl::init(string hostname, int port) {

68
    /*
69
70
     * Open the connection to the Cassandra database and
     * create the necessary keyspace and column family.
71
     */
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    try {
        csBackend->connect(hostname, port);

        if (!csBackend->existsKeyspace(KEYSPACE_NAME)) {
            cout << "Creating Keyspace " << KEYSPACE_NAME << "...\n";
            csBackend->createKeyspace(KEYSPACE_NAME, 1);
        }

        csBackend->selectKeyspace(KEYSPACE_NAME);

        if (!(csBackend->currentKeySpace.name.compare(KEYSPACE_NAME) == 0)) {
            cout << "Cannot select keyspace " << KEYSPACE_NAME << "\n";
            exit(EXIT_FAILURE);
        }

        if (!csBackend->existsColumnFamily(CF_SENSORDATA)) {
            cout << "Creating Column Familiy " CF_SENSORDATA "...\n";
Axel Auweter's avatar
Axel Auweter committed
89
90
91
92
            csBackend->createColumnFamily(CF_SENSORDATA,
                "sid blob, ts bigint, value bigint",
                "sid, ts",
                "COMPACT STORAGE");
93
94
        }
    }
95
96
97
98
99
100
101
102
103
104
105
106
107
108
    catch(const TTransportException& te){
        cout << "TP Exception: " << te.what() << "[" << te.getType() << "]\n";
        exit(EXIT_FAILURE);
    }
    catch(const InvalidRequestException& ire){
        cout << "IRE Exception: " << ire.what() << "[" << ire.why << "]\n";
        exit(EXIT_FAILURE);
    }
    catch(const NotFoundException& nfe){
        cout << "NF Exception: " <<  nfe.what() << "\n";
        exit(EXIT_FAILURE);
    }
}

109
110
void SensorDataStoreImpl::insert(SensorId* sid, uint64_t ts, uint64_t value)
{
111
112
113
114
115
  /* Calculate and insert week number */
  uint16_t week = ts / 604800000000;
  sid->dsid.rsvd = week;

  /* Insert into Cassandra */
Axel Auweter's avatar
Axel Auweter committed
116
  csBackend->insert(CF_SENSORDATA, sidConvert(sid), ts, value);
117
118
119
}

SensorDataStoreImpl::SensorDataStoreImpl(CassandraBackend *csb)
120
{
121
  csBackend = csb;
122
123
124
125
126
127
}

SensorDataStoreImpl::~SensorDataStoreImpl()
{
}

128
129
130
131
132
133
134
/**
 * @details
 * The main task of this function is to ensure that the
 * SensorDataStore object owns an instance of SensorDataStoreImpl.
 * Once this is ensured, the actual initialization work is
 * performed by the init function of SensorDataStoreImpl.
 */
135
136
137
138
void SensorDataStore::init(string hostname, int port)
{
    /* Allocate new SensorDataStoreImpl Object if necessary */
    if (!impl) {
139
        impl = new SensorDataStoreImpl(csBackend);
140
141
    }
    
142
    /* Call the Impl class init function */
143
144
145
    impl->init(hostname, port);
}

146
147
148
149
150
151
/**
 * @details
 * Instead of doing the actual work, this function simply
 * forwards to the insert function of the SensorDataStoreImpl
 * class.
 */
152
153
154
155
156
bool SensorDataStore::topicToSid(SensorId* sid, std::string topic)
{
    return impl->topicToSid(sid, topic);
}

157
158
159
160
161
162
/**
 * @details
 * Instead of doing the actual work, this function simply
 * forwards to the insert function of the SensorDataStoreImpl
 * class.
 */
163
164
165
166
167
void SensorDataStore::insert(SensorId* sid, uint64_t ts, uint64_t value)
{
    impl->insert(sid, ts, value);
}

168
169
170
171
172
173
/**
 * @details
 * This constructor for SensorDataStore acts similarly to the
 * standard constructor by calling the standard constructor
 * with "localhost" as the hostname and 9160 as the port number.
 */
174
175
SensorDataStore::SensorDataStore()
{
176
    csBackend = new CassandraBackend();
177
178
179
180
    impl = nullptr;
    init("localhost", 9160);
}

181
182
183
184
185
186
187
188
189
190
191
/**
 * @details
 * This constructor for SensorDataStore objects initializes
 * the object and connects to the database at the given
 * hostname on the given port.
 *
 * For this, the constructor allocates a CassandraBackend
 * object that is then used by SensorDataStoreImpl for doing
 * the raw database accesses.
 */
SensorDataStore::SensorDataStore(std::string hostname, int port)
192
{
193
    csBackend = new CassandraBackend();
194
195
196
197
    impl = nullptr;
    init(hostname, port);
}

198
199
200
201
202
/**
 * @details
 * The SensorDataStore desctructor deallocates the
 * SensorDataStoreImpl and CassandraBackend objects.
 */
203
204
SensorDataStore::~SensorDataStore()
{
205
206
207
208
209
210
  /* Clean up... */
  if (impl)
    delete impl;
  if (csBackend)
    delete csBackend;

211
}