sensordatastore.cpp 3.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
/*
 * sensordatastore.cpp
 *
 *  Created on: Jul 24, 2013
 *      Author: Axel Auweter
 */

#include "sensordatastore_internal.h"

string SensorDataStoreImpl::sidConvert(SensorId *sid)
{
    uint64_t ll[2];
13
14
    ll[0] = Endian::hostToBE(sid->raw[0]);
    ll[1] = Endian::hostToBE(sid->raw[1]);
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    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;
}

42
43
void SensorDataStoreImpl::init(string hostname, int port) {

44
    /*
45
46
     * Open the connection to the Cassandra database and
     * create the necessary keyspace and column family.
47
     */
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    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
65
66
67
68
            csBackend->createColumnFamily(CF_SENSORDATA,
                "sid blob, ts bigint, value bigint",
                "sid, ts",
                "COMPACT STORAGE");
69
70
        }
    }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    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);
    }
}

85
86
void SensorDataStoreImpl::insert(SensorId* sid, uint64_t ts, uint64_t value)
{
Axel Auweter's avatar
Axel Auweter committed
87
  csBackend->insert(CF_SENSORDATA, sidConvert(sid), ts, value);
88
89
90
}

SensorDataStoreImpl::SensorDataStoreImpl(CassandraBackend *csb)
91
{
92
  csBackend = csb;
93
94
95
96
97
98
99
100
101
102
}

SensorDataStoreImpl::~SensorDataStoreImpl()
{
}

void SensorDataStore::init(string hostname, int port)
{
    /* Allocate new SensorDataStoreImpl Object if necessary */
    if (!impl) {
103
        impl = new SensorDataStoreImpl(csBackend);
104
105
    }
    
106
    /* Call the Imlp class init function */
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    impl->init(hostname, port);
}

bool SensorDataStore::topicToSid(SensorId* sid, std::string topic)
{
    return impl->topicToSid(sid, topic);
}

void SensorDataStore::insert(SensorId* sid, uint64_t ts, uint64_t value)
{
    impl->insert(sid, ts, value);
}

SensorDataStore::SensorDataStore()
{
122
    csBackend = new CassandraBackend();
123
124
125
126
127
128
    impl = nullptr;
    init("localhost", 9160);
}

SensorDataStore::SensorDataStore(string hostname, int port)
{
129
    csBackend = new CassandraBackend();
130
131
132
133
134
135
    impl = nullptr;
    init(hostname, port);
}

SensorDataStore::~SensorDataStore()
{
136
137
138
139
140
141
  /* Clean up... */
  if (impl)
    delete impl;
  if (csBackend)
    delete csBackend;

142
}