2018-08-26 13:15:48 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018
|
|
|
|
// Mainflux
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
2018-08-06 17:06:55 +02:00
|
|
|
package cassandra
|
|
|
|
|
|
|
|
import (
|
2019-03-15 18:38:07 +01:00
|
|
|
"fmt"
|
2019-04-20 14:09:11 +02:00
|
|
|
|
2018-08-06 17:06:55 +02:00
|
|
|
"github.com/gocql/gocql"
|
|
|
|
"github.com/mainflux/mainflux"
|
|
|
|
"github.com/mainflux/mainflux/readers"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ readers.MessageRepository = (*cassandraRepository)(nil)
|
|
|
|
|
|
|
|
type cassandraRepository struct {
|
|
|
|
session *gocql.Session
|
|
|
|
}
|
|
|
|
|
|
|
|
// New instantiates Cassandra message repository.
|
|
|
|
func New(session *gocql.Session) readers.MessageRepository {
|
|
|
|
return cassandraRepository{session: session}
|
|
|
|
}
|
|
|
|
|
2019-03-15 18:38:07 +01:00
|
|
|
func (cr cassandraRepository) ReadAll(chanID string, offset, limit uint64, query map[string]string) []mainflux.Message {
|
|
|
|
cql, values := buildQuery(chanID, offset, limit, query)
|
2018-08-06 17:06:55 +02:00
|
|
|
|
2019-03-15 18:38:07 +01:00
|
|
|
iter := cr.session.Query(cql, values...).Iter()
|
2018-08-06 17:06:55 +02:00
|
|
|
scanner := iter.Scanner()
|
|
|
|
|
|
|
|
// skip first OFFSET rows
|
|
|
|
for i := uint64(0); i < offset; i++ {
|
|
|
|
if !scanner.Next() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 19:18:51 +01:00
|
|
|
var floatVal, valueSum *float64
|
|
|
|
var strVal, dataVal *string
|
|
|
|
var boolVal *bool
|
|
|
|
|
2018-08-06 17:06:55 +02:00
|
|
|
page := []mainflux.Message{}
|
|
|
|
for scanner.Next() {
|
|
|
|
var msg mainflux.Message
|
2019-03-15 18:38:07 +01:00
|
|
|
scanner.Scan(&msg.Channel, &msg.Subtopic, &msg.Publisher, &msg.Protocol,
|
2018-11-05 19:18:51 +01:00
|
|
|
&msg.Name, &msg.Unit, &floatVal, &strVal, &boolVal,
|
|
|
|
&dataVal, &valueSum, &msg.Time, &msg.UpdateTime, &msg.Link)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case floatVal != nil:
|
2018-12-05 13:09:25 +01:00
|
|
|
msg.Value = &mainflux.Message_FloatValue{FloatValue: *floatVal}
|
2018-11-05 19:18:51 +01:00
|
|
|
case strVal != nil:
|
2018-12-05 13:09:25 +01:00
|
|
|
msg.Value = &mainflux.Message_StringValue{StringValue: *strVal}
|
2018-11-05 19:18:51 +01:00
|
|
|
case boolVal != nil:
|
2018-12-05 13:09:25 +01:00
|
|
|
msg.Value = &mainflux.Message_BoolValue{BoolValue: *boolVal}
|
2018-11-05 19:18:51 +01:00
|
|
|
case dataVal != nil:
|
2018-12-05 13:09:25 +01:00
|
|
|
msg.Value = &mainflux.Message_DataValue{DataValue: *dataVal}
|
2018-11-05 19:18:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if valueSum != nil {
|
|
|
|
msg.ValueSum = &mainflux.SumValue{Value: *valueSum}
|
|
|
|
}
|
|
|
|
|
2018-08-06 17:06:55 +02:00
|
|
|
page = append(page, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := iter.Close(); err != nil {
|
|
|
|
return []mainflux.Message{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return page
|
|
|
|
}
|
2019-03-15 18:38:07 +01:00
|
|
|
|
|
|
|
func buildQuery(chanID string, offset, limit uint64, query map[string]string) (string, []interface{}) {
|
|
|
|
var condSql string
|
|
|
|
var values []interface{}
|
|
|
|
|
|
|
|
cql := `SELECT channel, subtopic, publisher, protocol, name, unit,
|
|
|
|
value, string_value, bool_value, data_value, value_sum, time,
|
|
|
|
update_time, link FROM messages WHERE channel = ? %s LIMIT ?
|
|
|
|
ALLOW FILTERING`
|
|
|
|
|
|
|
|
values = append(values, chanID)
|
|
|
|
|
|
|
|
for name, value := range query {
|
|
|
|
switch name {
|
|
|
|
case
|
|
|
|
"channel",
|
|
|
|
"subtopic",
|
|
|
|
"publisher",
|
|
|
|
"name",
|
|
|
|
"protocol":
|
|
|
|
condSql = fmt.Sprintf(`%s AND %s = ?`, condSql, name)
|
|
|
|
values = append(values, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
values = append(values, offset+limit)
|
|
|
|
return fmt.Sprintf(cql, condSql), values
|
|
|
|
}
|