harperdb

package module
v0.0.0-...-0f825d8 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2020 License: MIT Imports: 7 Imported by: 1

README

HarperDB SDK for Go

Build Status PkgGoDev CodeQL

Requirements

  • = Go 1.13

Installation

Quickstart

client := harperdb.NewClient("http://localhost:9925", "username", "password")

Documentation

Overview

This is a client for the HarperDB database. It mirrors the HTTP API as of version 2.2.0 and makes it very easy to get up and running with HarperDB and your Go application.

For more information see: https://docs.harperdb.io/

Basics

Instantiate a new client:

client := harperdb.NewClient("http://localhost:9925", "username", "password")

Index

Examples

Constants

View Source
const (
	CSV_ACTION_INSERT = "insert"
	CSV_ACTION_UPDATE = "update"
)
View Source
const (
	JobStatusCompleted  = "COMPLETE"
	JobStatusInProgress = "IN_PROGRESS"

	DATE_FORMAT = "2006-01-02"
)
View Source
const (
	LogOrderAsc  = "asc"
	LogOrderDesc = "desc"

	LogSearchTypeAll       = ""
	LogSearchTypeTimestamp = "timestamp"
	LogSearchTypeUsername  = "username"
	LogSearchTypeHashValue = "hash_value"
)
View Source
const (
	OP_ADD_NODE               = "add_node"
	OP_ADD_ROLE               = "add_role"
	OP_ADD_USER               = "add_user"
	OP_ALTER_ROLE             = "alter_role"
	OP_ALTER_USER             = "alter_user"
	OP_CLUSTER_STATUS         = "cluster_status"
	OP_CREATE_ATTRIBUTE       = "create_attribute"
	OP_CREATE_SCHEMA          = "create_schema"
	OP_CREATE_TABLE           = "create_table"
	OP_CSV_DATA_LOAD          = "csv_data_load"
	OP_CSV_FILE_LOAD          = "csv_file_load"
	OP_CSV_URL_LOAD           = "csv_url_load"
	OP_DELETE_FILES_BEFORE    = "delete_files_before"
	OP_DELETE_TRANSACTION_LOG = "delete_transaction_logs_before"
	OP_DESCRIBE_ALL           = "describe_all"
	OP_DESCRIBE_SCHEMA        = "describe_schema"
	OP_DESCRIBE_TABLE         = "describe_table"
	OP_DELETE                 = "delete"
	OP_DROP_ATTRIBUTE         = "drop_attribute"
	OP_DROP_ROLE              = "drop_role"
	OP_DROP_SCHEMA            = "drop_schema"
	OP_DROP_TABLE             = "drop_table"
	OP_DROP_USER              = "drop_user"
	OP_EXPORT_LOCAL           = "export_local"
	OP_EXPORT_TO_S3           = "export_to_s3"
	OP_GET_FINGERPRINT        = "get_fingerprint"
	OP_GET_JOB                = "get_job"
	OP_INSERT                 = "insert"
	OP_LIST_ROLES             = "list_roles"
	OP_LIST_USERS             = "list_users"
	OP_READ_LOG               = "read_log"
	OP_READ_TRANSACTION_LOG   = "read_transaction_log"
	OP_REGISTRATION_INFO      = "registration_info"
	OP_REMOVE_NODE            = "remove_node"
	OP_SEARCH_BY_HASH         = "search_by_hash"
	OP_SEARCH_BY_VALUE        = "search_by_value"
	OP_SEARCH_JOBS            = "search_jobs_by_start_date"
	OP_SET_LICENSE            = "set_license"
	OP_SQL                    = "sql"
	OP_SYSTEM_INFORMATION     = "system_information"
	OP_UPDATE                 = "update"
	OP_UPDATE_NODE            = "update_node"
	OP_USER_INFO              = "user_info"
)
View Source
const (
	SearchBySQL   = "sql"
	SearchByHash  = "search_by_hash"
	SearchByValue = "search_by_value"
)

Variables

View Source
var (
	ErrJobStatusUnknown = errors.New("unknown job status")
	ErrJobNotFound      = errors.New("job not found")
	ErrNoRows           = errors.New("did not return any rows")
	ErrTooManyRows      = errors.New("did return more than one row")
	ErrNotSingleColumn  = errors.New("expected a single column return")
)
View Source
var AllAttributes = FromStringSlice([]string{"*"})

Functions

This section is empty.

Types

type AffectedResponse

type AffectedResponse struct {
	MessageResponse
	SkippedHashes  []interface{} `json:"skipped_hashes"`
	InsertedHashes []interface{} `json:"inserted_hashes"`
	UpdatedHashes  []interface{} `json:"update_hashes"` // (sic) not updated_hashes
	DeletedHashes  []interface{} `json:"deleted_hashes"`
}

type Attribute

type Attribute string

type AttributeList

type AttributeList interface{}

func FromStringSlice

func FromStringSlice(ss []string) AttributeList

type AttributePermissions

type AttributePermissions struct {
	AttributeName string `json:"attribute_name"`
	Read          bool   `json:"read"`
	Insert        bool   `json:"insert"`
	Update        bool   `json:"update"`
}

type CPULoad

type CPULoad struct {
	Load       float64 `json:"load"`
	LoadUser   float64 `json:"load_user"`
	LoadSystem float64 `json:"load_system"`
	LoadNice   float64 `json:"load_nice"`
	LoadIdle   float64 `json:"load_idle"`
	LoadIRQ    float64 `json:"load_irq"`
}

type Client

type Client struct {
	HttpClient *resty.Client
	// contains filtered or unexported fields
}

func NewClient

func NewClient(endpoint string, username string, password string) *Client
Example
// Connecting to a HarperDB instance
c := NewClient("http://localhost:9925", "HDB_ADMIN", "password")

// (optional) set some proxy
c.HttpClient.SetProxy("http://localhost:8888")
Output:

Example (Https)
// Connecting to a HarperDB instance
c := NewClient("https://localhost:31283", "HDB_ADMIN", "password")

// with a self-signed certificate
// will most likely fail so disable security check (https)
c.HttpClient.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
Output:

func (*Client) AddNode

func (c *Client) AddNode(name, host string, port int, subscriptions []Subscription) error

func (*Client) AddRole

func (c *Client) AddRole(role string, perm Permission) (*Role, error)

func (*Client) AddUser

func (c *Client) AddUser(username, password, roleID string, active bool) error

func (*Client) AlterRole

func (c *Client) AlterRole(id string, role string, perm Permission) (*Role, error)

func (*Client) AlterUser

func (c *Client) AlterUser(username, password, roleID string, active bool) error

func (*Client) CSVDataLoad

func (c *Client) CSVDataLoad(schema, table string, update bool, data io.Reader) (string, error)

CSVDataLoad takes a Reader and executes the CSV Load Data operation if "update" is true, it will not insert but update existing records If successfull, returns the Job ID

func (*Client) CSVFileLoad

func (c *Client) CSVFileLoad(schema, table string, update bool, filePath string) (string, error)

CSVFileLoad takes a path of a file which must exist on the server and executes the CSV Load Data operation if "update" is true, it will not insert but update existing records If successfull, returns the Job ID

func (*Client) CSVURLLoad

func (c *Client) CSVURLLoad(schema, table string, update bool, csvURL string) (string, error)

CSVURLLoad takes a public URL and executes the CSV Load Data operation if "update" is true, it will not insert but update existing records If successfull, returns the Job ID

func (*Client) ClusterStatus

func (c *Client) ClusterStatus() (*ClusterStatusResponse, error)

func (*Client) CreateAttribute

func (c *Client) CreateAttribute(schema, table, attribute string) error

func (*Client) CreateSchema

func (c *Client) CreateSchema(schema string) error

CreateSchema creates a new schema. Returns "AlreadyExistsError" if schema already existed.

func (*Client) CreateTable

func (c *Client) CreateTable(schema, table, hashAttribute string) error

func (*Client) Delete

func (c *Client) Delete(schema, table string, hashValues AttributeList) (*AffectedResponse, error)

Delete delete one or more JSON objects from a table. hashValues must be an array of slice

func (*Client) DeleteFilesBefore

func (c *Client) DeleteFilesBefore(schema, table string, date time.Time) error

func (Client) DeleteTransactionLogsBefore

func (c Client) DeleteTransactionLogsBefore(schema, table string, timestamp time.Time) error

func (*Client) DescribeAll

func (c *Client) DescribeAll() (*DescribeAllResponse, error)

func (*Client) DescribeTable

func (c *Client) DescribeTable(schema, table string) (*DescribeTableResponse, error)

func (*Client) DropAttribute

func (c *Client) DropAttribute(schema, table, attribute string) error

func (*Client) DropRole

func (c *Client) DropRole(id string) error

func (*Client) DropSchema

func (c *Client) DropSchema(schema string) error

DropSchema drops a schema. Returns "DoesNotExistError" if schema did not exist.

func (*Client) DropTable

func (c *Client) DropTable(schema, table, hashAttribute string) error

func (*Client) DropUser

func (c *Client) DropUser(username string) error

DropUser deletes a user. Note: this operation is idempotent, it will not throw an error if the user doesn't exist

func (*Client) ExportLocal

func (c *Client) ExportLocal(format, path string, searchOperation SearchOperation) error

func (*Client) ExportToS3

func (c *Client) ExportToS3(format string, s3creds S3Credentials, searchOperation SearchOperation) error

func (*Client) GetFingerprint

func (c *Client) GetFingerprint() (*GetFingerprintResponse, error)

func (*Client) GetJob

func (c *Client) GetJob(jobID string) (*GetJobResponse, error)

func (*Client) Insert

func (c *Client) Insert(schema, table string, records interface{}) (*AffectedResponse, error)

Insert inserts one or more JSON objects into a table Hash value of the inserted JSON record MUST be present.

func (*Client) ListRoles

func (c *Client) ListRoles() ([]Role, error)

TODO Correct JSON modelling needs to be verified

func (*Client) ListUsers

func (c *Client) ListUsers() ([]User, error)

func (*Client) ReadHarperDBLog

func (c *Client) ReadHarperDBLog(limit, start int, from, until time.Time, order string) (*LogResponse, error)

func (Client) ReadTransactionLog

func (c Client) ReadTransactionLog(schema, table, searchType string, searchValues interface{}) ([]TxLogEntry, error)

ReadTransactionLog requests the transaction log for a table. Use LogSearchType* constants to filter the log entries by searchValues, which should be an array/slice of searchType. Leave searchType empty (LogSearchTypeAll) to get all entries.

func (*Client) RegistrationInfo

func (c *Client) RegistrationInfo() (*RegistrationInfoResponse, error)

func (*Client) RemoveNode

func (c *Client) RemoveNode(name string) error

func (*Client) SQLExec

func (c *Client) SQLExec(stmt string, args ...interface{}) (*AffectedResponse, error)

SQLExec executes UPDATE/INSERT/DELETE statements and returns a struct with the affected row hash values. You can use format verbs (%s, %d) in the stmt and an pass the arguments at the end of the function, like in fmt.Printf

func (*Client) SQLGet

func (c *Client) SQLGet(stmt string, args ...interface{}) (interface{}, error)

SQLGet is to query a scalar value from the database. This function is not part of the official HarperDB API. It executes a SQL statement and expects exactly one object with one key. I.e. SELECT CURRENT_TIMESTAMP Will return the following errors: - ErrNoRows - ErrTooManyRows - ErrNotSingleColumn

func (*Client) SQLSelect

func (c *Client) SQLSelect(v interface{}, stmt string, args ...interface{}) error

SQLSelect executes SELECT statements and returns a record set. You can use format verbs (%s, %d) in the stmt and an pass the arguments at the end of the function, like in fmt.Printf

func (*Client) SearchByHash

func (c *Client) SearchByHash(schema, table string, v interface{}, hashValues AttributeList, getAttributes AttributeList) error

SearchByHash fetches records based on the table's hash field (i.e. primary key).

func (*Client) SearchByValue

func (c *Client) SearchByValue(schema, table string, v interface{}, searchAttribute Attribute, searchValue interface{}, getAttributes AttributeList) error

SearchByValue fetches records based on the value of an attribute Wilcards are allowed in `searchValue`

func (*Client) SearchJobsByStartDate

func (c *Client) SearchJobsByStartDate(fromDate, toDate time.Time) ([]GetJobResponse, error)

func (*Client) SetLicense

func (c *Client) SetLicense(key, company string) error

func (*Client) SystemInformation

func (c *Client) SystemInformation() (*SysInfo, error)

func (*Client) Update

func (c *Client) Update(schema, table string, records interface{}) (*AffectedResponse, error)

Update updates one or more JSON objects in a table. Hash value of the inserted JSON record MUST be present.

func (*Client) UpdateNode

func (c *Client) UpdateNode(name, host string, port int, subscriptions []Subscription) error

func (*Client) UserInfo

func (c *Client) UserInfo() (User, error)

UserInfo returns the current user executing this operation

type ClusterStatusResponse

type ClusterStatusResponse struct {
	IsEnabled bool        `json:"is_enabled"`
	NodeName  interface{} `json:"node_name"` // (sic) this is an int if cluster is not enabled
	Status    struct {
		ID                  string       `json:"id"`
		Type                string       `json:"type"`
		OutboundConnections []Connection `json:"outbound_connections"`
		InboundConnections  []Connection `json:"inbound_connections"`
	} `json:"status"`
}

type Connection

type Connection struct {
	ID            string         `json:"id"`
	HostAddress   string         `json:"host_address"`
	HostPort      int            `json:"host_port"`
	State         string         `json:"state"`
	NodeName      string         `json:"node_name"`
	Subscriptions []Subscription `json:"subscriptions"`
}

type DescribeAllResponse

type DescribeAllResponse map[string]map[string]DescribeTableResponse

type DescribeSchemaResponse

type DescribeSchemaResponse struct {
}

type DescribeTableResponse

type DescribeTableResponse struct {
	Record
	HashAttribute string           `json:"hash_attribute"`
	ID            string           `json:"id"`
	Name          string           `json:"name"`
	Residence     string           `json:"residence"` // TODO Not verified
	Schema        string           `json:"schema"`
	Attributes    []TableAttribute `json:"attributes"`
	RecordCount   int              `json:"record_count"`
}

type DiskSize

type DiskSize struct {
	FS    string  `json:"fs"`
	Type  string  `json:"overlay"`
	Size  int64   `json:"size"`
	Used  int64   `json:"used"`
	Use   float64 `json:"use"`
	Mount string  `json:"mount"`
}

type ErrorResponse

type ErrorResponse struct {
	Error string `json:"error"`
}

type GetFingerprintResponse

type GetFingerprintResponse struct {
	Message string `json:"message"`
}

type GetJobResponse

type GetJobResponse struct {
	Record
	MessageResponse
	CreatedDateTime        Timestamp `json:"created_datetime"`
	EndDateTime            Timestamp `json:"end_datetime"`
	StartDateTime          Timestamp `json:"start_datetime"`
	ID                     string    `json:"id"`
	JobBody                string    `json:"job_body"` // TODO Not verified
	Status                 string    `json:"status"`
	Type                   string    `json:"type"`
	User                   string    `json:"user"`
	StartDateTimeConverted time.Time `json:"start_datetime_converted"`
	EndDateTimeConverted   time.Time `json:"end_datetime_converted"`
}

type JobResponse

type JobResponse struct {
	JobID string
}

type LogEntry

type LogEntry struct {
	Level     string    `json:"level"`
	Message   string    `json:"message"`
	Timestamp time.Time `json:"timestamp"`
}

type LogResponse

type LogResponse struct {
	File []LogEntry `json:"file"`
}

type MessageResponse

type MessageResponse struct {
	Message string `json:"message"`
}

type NetworkConnection

type NetworkConnection struct {
	Protocol     string `json:"protocol"`
	LocalAddress string `json:"localaddress"`
	LocalPort    string `json:"localport"`
	PeerAddress  string `json:"peeraddress"`
	PeerPort     string `json:"peerport"`
	State        string `json:"state"`
	PID          int64  `json:"pid"`
	Process      string `json:"node"`
}

type NetworkInterface

type NetworkInterface struct {
	Iface          string `json:"iface"`
	IfaceName      string `json:"ifaceName"`
	IP4            string `json:"ip4"`
	IP6            string `json:"ip6"`
	Mac            string `json:"mac"`
	OperState      string `json:"operstate"`
	Type           string `json:"virtual"`
	Duplex         string `json:"duplex"`
	Speed          int64  `json:"speed"`
	CarrierChanges int64  `json:"carrierChanges"`
}

type NetworkStats

type NetworkStats struct {
	Iface     string `json:"iface"`
	OperState string `json:"operstate"`
	RxBytes   int64  `json:"rx_bytes"`
	RxDropped int64  `json:"rx_dropped"`
	RxErrors  int64  `json:"rx_errors"`
	TxBytes   int64  `json:"tx_bytes"`
	TxDropped int64  `json:"tx_dropped"`
	TxErrors  int64  `json:"tx_errors"`
}

type OperationError

type OperationError struct {
	StatusCode int
	Message    string
}

func (*OperationError) Error

func (e *OperationError) Error() string

func (*OperationError) IsAlreadyExistsError

func (e *OperationError) IsAlreadyExistsError() bool

func (*OperationError) IsDoesNotExistError

func (e *OperationError) IsDoesNotExistError() bool

func (*OperationError) IsNotAuthorizedError

func (e *OperationError) IsNotAuthorizedError() bool

type Permission

type Permission map[string]interface{}

func (Permission) AddSchemaPermission

func (p Permission) AddSchemaPermission(schema string, sp SchemaPermission)

func (Permission) SetClusterUser

func (p Permission) SetClusterUser(su bool)

func (Permission) SetSuperUser

func (p Permission) SetSuperUser(su bool)

type Record

type Record struct {
	CreatedTime Timestamp `json:"__createdtime__"`
	UpdatedTime Timestamp `json:"__updatedtime__"`
}

Record type gives you convenient access to HarperDB's meta data fields which are automatically added to every record. Currently these are "__createdtime__" and "__updatedtime__".

Meta data fields are read-only. Overwriting .CreatedTime and .UpdatedTime will have no effect.

Example
type Player struct {
	Record // embed the Record type in your custom types

	ID   int    `json:"id"`
	Name string `json:"name"`
	Age  int    `json:"age"`
}
Output:

type RegistrationInfoResponse

type RegistrationInfoResponse struct {
	Registered            bool      `json:"registered"`
	Version               string    `json:"version"`
	StorageType           string    `json:"storage_type"`
	RAMAllocation         int       `json:"ram_allocation"`
	LicenseExpirationDate time.Time `json:"license_expiration_date"`
}

type Role

type Role struct {
	Record
	Role       string     `json:"role"`
	ID         string     `json:"id"`
	Permission Permission `json:"permission"`
}

type S3Credentials

type S3Credentials struct {
	AWSAccessKeyID     string `json:"aws_access_key_id"`
	AWSSecretAccessKey string `json:"aws_secret_access_key"`
	Bucket             string `json:"bucket"`
	Key                string `json:"filename"`
}

type SchemaPermission

type SchemaPermission struct {
	Tables map[string]TablePermission
}

func (SchemaPermission) AddTablePermission

func (p SchemaPermission) AddTablePermission(table string, tp TablePermission)

type SearchOperation

type SearchOperation struct {
	Operation string `json:"operation"`
	SQL       string `json:"sql"`
}

type Subscription

type Subscription struct {
	Channel   string `json:"channel"`
	Subscribe bool   `json:"subscribe"`
	Publish   bool   `json:"publish"`
}

type SysInfo

type SysInfo struct {
	System struct {
		Platform    string `json:"platform"`
		Distro      string `json:"distro"`
		Release     string `json:"release"`
		Codename    string `json:"codename"`
		Kernel      string `json:"kernel"`
		Arch        string `json:"arch"`
		Hostname    string `json:"hostname"`
		NodeVersion string `json:"node_version"`
		NPMVersion  string `json:"npm_version"`
	} `json:"system"`
	Time struct {
		Current      Timestamp `json:"current"`
		Uptime       int64     `json:"uptime"`
		Timezone     string    `json:"timezone"`
		TimezoneName string    `json:"timezoneName"`
	} `json:"time"`
	CPU struct {
		Manufacturer  string `json:"manufacturer"`
		Brand         string `json:"brand"`
		Vendor        string `json:"vendor"`
		Speed         string `json:"speed"`
		Cores         int    `json:"cores"`
		PhysicalCores int    `json:"physicalCores"`
		Processors    int    `json:"processors"`
		CPUSpeed      struct {
			Min   float64   `json:"min"`
			Max   float64   `json:"max"`
			Avg   float64   `json:"avg"`
			Cores []float64 `json:"cores"`
		} `json:"cpu_speed"`
		CurrentLoad struct {
			AvgLoag           float64 `json:"avgload"`
			CurrentLoad       float64 `json:"currentload"`
			CurrentLoadUser   float64 `json:"currentload_user"`
			CurrentLoadSystem float64 `json:"currentload_system"`
			CurrentLoadNice   float64 `json:"currentload_nice"`
			CurrentLoadIdle   float64 `json:"currentload_idle"`
			CurrentLoadIRQ    float64 `json:"currentload_irq"`
		} `json:"current_load"`
		CPUs []CPULoad `json:"cpus"`
	} `json:"cpu"`
	Memory struct {
		Total     int64 `json:"total"`
		Free      int64 `json:"free"`
		Used      int64 `json:"used"`
		Active    int64 `json:"active"`
		Available int64 `json:"available"`
		SwapTotal int64 `json:"swaptotal"`
		SwapUsed  int64 `json:"swapused"`
		SwapFree  int64 `json:"swapfree"`
	} `json:"memory"`
	Disk struct {
		IO struct {
			RIO int64 `json:"rIO"`
			WIO int64 `json:"wIO"`
			TIO int64 `json:"tIO"`
		} `json:"io"`
		ReadWrite struct {
			RX int64 `json:"rx"`
			WX int64 `json:"wx"`
			TX int64 `json:"tx"`
			MS int64 `json:"ms"`
		} `json:"read_write"`
		Size []DiskSize `json:"size"`
	} `json:"disk"`
	Network struct {
		DefaultInterface string `json:"default_interface"`
		Latency          struct {
			URL    string `json:"url"`
			Ok     bool   `json:"ok"`
			Status int64  `json:"status"`
			MS     int64  `json:"ms"`
		} `json:"latency"`
		Interfaces  []NetworkInterface  `json:"interfaces"`
		Stats       []NetworkStats      `json:"stats"`
		Connections []NetworkConnection `json:"connections"`
	} `json:"network"`
	HarperDBProcesses struct {
		Core       interface{} `json:"core"`       // TODO Unknown
		Clustering interface{} `json:"clustering"` // TODO Unknown
	} `json:"harperdb_processes"`
	TableSize []TableSize `json:"table_size"`
}

type TableAttribute

type TableAttribute struct {
	Attribute string `json:"attribute"`
}

type TablePermission

type TablePermission struct {
	Read                 bool                   `json:"read"`
	Insert               bool                   `json:"insert"`
	Update               bool                   `json:"update"`
	Delete               bool                   `json:"delete"`
	AttributePermissions []AttributePermissions `json:"attribute_permissions"`
}

type TableSize

type TableSize struct {
	Schema                    string `json:"schema"`
	Table                     string `json:"table"`
	TableSize                 int64  `json:"table_size"`
	RecordCount               int64  `json:"record_count"`
	TransactionLogSize        int64  `json:"transaction_log_size"`
	TransactionLogRecordCount int64  `json:"transaction_log_record_count"`
}

type Timestamp

type Timestamp int64

func (Timestamp) ToTime

func (t Timestamp) ToTime() time.Time

type TxLogEntry

type TxLogEntry struct {
	Operation  string                   `json:"operation"`
	UserName   string                   `json:"user_name"`
	Timestamp  Timestamp                `json:"timestamp"` // this is a float
	HashValues []interface{}            `json:"hash_values"`
	Records    []map[string]interface{} `json:"records"`
}

type User

type User struct {
	Record
	Active   bool   `json:"active"`
	Role     Role   `json:"role"`
	Username string `json:"username"`
}

Jump to

Keyboard shortcuts

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