sensordatastore.cpp 3.47 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
65
66
67
    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";
            csBackend->createColumnFamilies();
        }
    }
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    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);
    }
}

82
83
84
85
86
87
void SensorDataStoreImpl::insert(SensorId* sid, uint64_t ts, uint64_t value)
{
  csBackend->insert(sidConvert(sid), ts, value);
}

SensorDataStoreImpl::SensorDataStoreImpl(CassandraBackend *csb)
88
{
89
  csBackend = csb;
90
91
92
93
94
95
96
97
98
99
}

SensorDataStoreImpl::~SensorDataStoreImpl()
{
}

void SensorDataStore::init(string hostname, int port)
{
    /* Allocate new SensorDataStoreImpl Object if necessary */
    if (!impl) {
100
        impl = new SensorDataStoreImpl(csBackend);
101
102
    }
    
103
    /* Call the Imlp class init function */
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
    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()
{
119
    csBackend = new CassandraBackend();
120
121
122
123
124
125
    impl = nullptr;
    init("localhost", 9160);
}

SensorDataStore::SensorDataStore(string hostname, int port)
{
126
    csBackend = new CassandraBackend();
127
128
129
130
131
132
    impl = nullptr;
    init(hostname, port);
}

SensorDataStore::~SensorDataStore()
{
133
134
135
136
137
138
  /* Clean up... */
  if (impl)
    delete impl;
  if (csBackend)
    delete csBackend;

139
}