driver

package
v0.0.0-...-af5c341 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 3, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package driver realize data read and write support for different databases(datastore provider)

All driver object must implement IDriver interface to support read/write data for IoT devices

As an excellent IoT data engineer, the IoT data storage structure you design should have the following parts:

Data collection time, data collection location, [] (data object name, data value)

At the same time, in order to comply with the table architecture specifications of the project, we will set the following fields for the stored data:

Table ID,

Node ID (this is the AgentID set by the user (usually you)) It is also your `data collection location`

Data name

Time

Its format is:

(id, node_id, [your dataColumns], ts)

You don't need to consider id, nodeId and ts, we have already designed it for you!
You just need to make sure that the data object you pass in conforms to the deserializable json object.

Index

Constants

This section is empty.

Variables

View Source
var ErrWriteTxnSize = errors.New("write txn size should be 1<= size <=100")

Functions

func SetRepo

func SetRepo(repo repo.ICoreDb)

Types

type ConNode

type ConNode struct {
	NodeName *string // Node name for exact match
	NodeID   *string // Node ID for exact match
}

ConNode defines node query conditions. Pointer fields allow empty values, both nil means no node constraint.

type ConTimeRange

type ConTimeRange struct {
	StartAt *uint64 // Start timestamp in UNIX format (inclusive, closed interval)
	EndAt   *uint64 // End timestamp in UNIX format (inclusive, closed interval)
}

ConTimeRange defines time range query conditions. Uses pointers to implement optional fields (nil means no time constraint).

type ConValRange

type ConValRange struct {
	Key      string   // Target key name for filtering
	StartVal *float64 // Minimum value pointer (inclusive, nil means no lower bound)
	EndVal   *float64 // Maximum value pointer (inclusive, nil means no upper bound)
}

ConValRange defines value range filter conditions. Used to specify range constraints for specific keys. Support int and float data.

type ConditionOption

type ConditionOption func(driver *QueryCondition)

ConditionOption defines function type for functional option pattern Used to flexibly compose query conditions

func WithNode

func WithNode(nodeName, nodeID string) ConditionOption

WithNode creates a node condition configuration Parameters:

  • nodeName: Exact match node name (empty string means not set)
  • nodeID: Exact match node ID (empty string means not set)

Creates node condition only when either parameter is non-empty

func WithTimeRange

func WithTimeRange(startAt, endAt uint64) ConditionOption

WithTimeRange creates a time range condition configuration Parameters:

  • startAt: UNIX timestamp in seconds (0 means not set)
  • endAt: UNIX timestamp in seconds (0 means not set)

Creates time condition only when startAt/endAt are non-zero

func WithValRange

func WithValRange(key string, startVal, endVal float64) ConditionOption

WithValRange creates a value range condition configuration Parameters:

  • key: Required key name for filtering
  • startVal: Minimum value (inclusive, 0 is considered valid)
  • endVal: Maximum value (inclusive, 0 is considered valid)

Appends new condition to existing ValRange slice

type IDriver

type IDriver interface {
	GetDriverName() string
	// SetTableName Set write/reader table name
	SetTableName(tableName string)
	// GetNewDriver Get a new writer/reader driver
	GetNewDriver(tableName string) IDriver
	// CreateColumnsWithType Create columns with type: key is column name, value is column type
	CreateColumnsWithType(map[string]string) error
	// SetBatchWriteTxnSize Set batch write transaction size: it should be 1<= size <=100.(default is 1)
	//  if size = 0,it will return currentSize,nil
	//  if size > 100,or size < 1,it will return 0,and error
	SetBatchWriteTxnSize(size int) (int, error)
	// Migrate auto migrate table
	Migrate(map[string]string) error
	IWriter
	IReader
}

func NewMySQLDriver

func NewMySQLDriver(dsn, tableName string, userID string) (IDriver, func() error, error)

func NewTDEngineDriver

func NewTDEngineDriver(userId, host, username, password, db string,
	port int, table *string, useSchemaless *bool) (IDriver, func() error, error)

type IReader

type IReader interface {
	GetData(conditions ...ConditionOption) map[string][]any
}

type IWriter

type IWriter interface {
	// Insert insert data: key is column name, value is column value
	Insert(map[string]any) error
	// Writer will auto conversion data []byte to json-key-value, and write to table.
	io.Writer
}

type MySql

type MySql struct {
	// contains filtered or unexported fields
}

func (*MySql) CreateColumnsWithType

func (m *MySql) CreateColumnsWithType(data map[string]string) error

func (*MySql) GetData

func (m *MySql) GetData(conditions ...ConditionOption) map[string][]any

func (*MySql) GetDriverName

func (*MySql) GetDriverName() string

func (*MySql) GetNewDriver

func (m *MySql) GetNewDriver(tableName string) IDriver

func (*MySql) Insert

func (m *MySql) Insert(data map[string]any) error

func (*MySql) Migrate

func (m *MySql) Migrate(columns map[string]string) error

func (*MySql) SetBatchWriteTxnSize

func (m *MySql) SetBatchWriteTxnSize(size int) (int, error)

func (*MySql) SetTableName

func (m *MySql) SetTableName(tableName string)

func (*MySql) Write

func (m *MySql) Write(p []byte) (n int, err error)

type QueryCondition

type QueryCondition struct {
	*ConTimeRange               // Embedded time range condition (optional)
	*ConNode                    // Embedded node condition (optional)
	ValRange      []ConValRange // Slice of value range conditions
}

QueryCondition represents a collection of query conditions. Contains optional time range, node conditions, and multiple value range filters.

type TDEngine

type TDEngine struct {
	// contains filtered or unexported fields
}

func (*TDEngine) CreateColumnsWithType

func (t *TDEngine) CreateColumnsWithType(data map[string]string) error

func (*TDEngine) GetData

func (t *TDEngine) GetData(conditions ...ConditionOption) map[string][]any

func (*TDEngine) GetDriverName

func (*TDEngine) GetDriverName() string

func (*TDEngine) GetNewDriver

func (t *TDEngine) GetNewDriver(tableName string) IDriver

func (*TDEngine) Insert

func (t *TDEngine) Insert(m map[string]any) error

func (*TDEngine) Migrate

func (t *TDEngine) Migrate(m map[string]string) error

func (*TDEngine) SetBatchWriteTxnSize

func (t *TDEngine) SetBatchWriteTxnSize(size int) (int, error)

func (*TDEngine) SetTableName

func (t *TDEngine) SetTableName(tableName string)

func (*TDEngine) Write

func (t *TDEngine) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL