xneo4j

package module
v0.0.0-...-3876231 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2023 License: MIT Imports: 14 Imported by: 0

README

xneo4j

Dependencies

  • github.com/Aoi-hosizora/ahlib
  • github.com/Aoi-hosizora/ahlib-mx/xdbutils
  • github.com/neo4j/neo4j-go-driver
  • github.com/sirupsen/logrus

Documents

Types
  • type P map
  • type DriverOption func
  • type PropertyValue struct
  • type PropertyDict map
  • type OrderByOption func
  • type DialHandler func
  • type Pool struct
  • type LoggerOption func
  • type LogrusLogger struct
  • type StdLogger struct
  • type LoggerParam struct
Variables
  • var FormatLoggerFunc func
  • var FieldifyLoggerFunc func
Constants
  • const DefaultDatabase string
Functions
  • func Collect(result neo4j.Result, err error) ([]neo4j.Record, neo4j.ResultSummary, error)
  • func GetInteger(data interface{}) int64
  • func GetFloat(data interface{}) float64
  • func GetString(data interface{}) string
  • func GetBoolean(data interface{}) bool
  • func GetByteArray(data interface{}) []byte
  • func GetList(data interface{}) []interface{}
  • func GetMap(data interface{}) map[string]interface{}
  • func GetNode(data interface{}) neo4j.Node
  • func GetRel(data interface{}) neo4j.Relationship
  • func GetPath(data interface{}) neo4j.Path
  • func GetPoint(data interface{}) neo4j.Point
  • func GetDate(data interface{}) neo4j.Date
  • func GetTime(data interface{}) neo4j.OffsetTime
  • func GetDateTime(data interface{}) time.Time
  • func GetLocalTime(data interface{}) neo4j.LocalTime
  • func GetLocalDateTime(data interface{}) neo4j.LocalDateTime
  • func GetDuration(data interface{}) neo4j.Duration
  • func WithEncrypted(encrypted bool) DriverOption
  • func WithTrustStrategy(e neo4j.TrustStrategy) DriverOption
  • func WithLog(l neo4j.Logging) DriverOption
  • func WithAddressResolver(resolver neo4j.ServerAddressResolver) DriverOption
  • func WithMaxTransactionRetryTime(t time.Duration) DriverOption
  • func WithMaxConnectionPoolSize(size int) DriverOption
  • func WithMaxConnectionLifetime(t time.Duration) DriverOption
  • func WithConnectionAcquisitionTimeout(t time.Duration) DriverOption
  • func WithSocketConnectTimeout(t time.Duration) DriverOption
  • func WithSocketKeepalive(keepalive bool) DriverOption
  • func NewPropertyValue(reverse bool, destinations ...string) *PropertyValue
  • func WithOrderBySourceSeparator(separator string) OrderByOption
  • func WithOrderByTargetSeparator(separator string) OrderByOption
  • func WithOrderBySourceProcessor(processor func(source string) (field string, asc bool)) OrderByOption
  • func WithOrderByTargetProcessor(processor func(destination string, asc bool) (target string)) OrderByOption
  • func GenerateOrderByExpr(querySource string, dict PropertyDict, options ...OrderByOption) string
  • func NewPool(driver neo4j.Driver, dial DialHandler) *Pool
  • func WithLogErr(log bool) LoggerOption
  • func WithLogCypher(log bool) LoggerOption
  • func WithCounterFields(flag bool) LoggerOption
  • func WithSkip(skip int) LoggerOption
  • func WithSlowThreshold(threshold time.Duration) LoggerOption
  • func EnableLogger()
  • func DisableLogger()
  • func NewLogrusLogger(session neo4j.Session, logger *logrus.Logger, options ...LoggerOption) *LogrusLogger
  • func NewStdLogger(session neo4j.Session, logger logrus.StdLogger, options ...LoggerOption) *StdLogger
Methods
  • func (p *PropertyValue) Destinations() []string
  • func (p *PropertyValue) Reverse() bool
  • func (p *Pool) Dial(mode neo4j.AccessMode, bookmarks ...string) (neo4j.Session, error)
  • func (p *Pool) NewSession(config neo4j.SessionConfig) (neo4j.Session, error)
  • func (p *Pool) Session(mode neo4j.AccessMode, bookmarks ...string) (neo4j.Session, error)
  • func (p *Pool) DialReadMode(bookmarks ...string) (neo4j.Session, error)
  • func (p *Pool) DialWriteMode(bookmarks ...string) (neo4j.Session, error)
  • func (p *Pool) Driver() neo4j.Driver
  • func (p *Pool) Target() url.URL
  • func (p *Pool) VerifyConnectivity() error
  • func (p *Pool) Close() error
  • func (l *LogrusLogger) Run(cypher string, params map[string]interface{}, configurers ...func(*neo4j.TransactionConfig)) (neo4j.Result, error)
  • func (l *StdLogger) Run(cypher string, params map[string]interface{}, configurers ...func(*neo4j.TransactionConfig)) (neo4j.Result, error)

Documentation

Index

Constants

View Source
const (
	// NAME represents ahlib-mx/xneo4j package's name.
	NAME = "ahlib-mx/xneo4j"

	// VERSION represents ahlib-mx/xneo4j package's current version.
	VERSION = "1.6.0"
)
View Source
const DefaultDatabase = ""

DefaultDatabase is a marker for using the default database instance.

Variables

View Source
var (
	// FormatLoggerFunc is a custom LoggerParam's format function for LogrusLogger and StdLogger.
	FormatLoggerFunc func(p *LoggerParam) string

	// FieldifyLoggerFunc is a custom LoggerParam's fieldify function for LogrusLogger.
	FieldifyLoggerFunc func(p *LoggerParam) logrus.Fields
)

Functions

func Collect

func Collect(result neo4j.Result, err error) ([]neo4j.Record, neo4j.ResultSummary, error)

Collect loops through the result stream, collects records and returns the neo4j.Record slice with neo4j.ResultSummary.

Example:

cypher := "MATCH p = ()-[r :FRIEND]->(n) RETURN r, n"
records, summary, err := xneo4j.Collect(session.Run(cypher, nil)) // err contains the connection and execution error
for _, record := range records { // records is a slice of neo4j.Record
	// record is returned values, each value can be got by `Get` or `GetByIndex` methods
	rel := xneo4j.GetRel(record.GetByIndex(0))   // neo4j.Relationship
	node := xneo4j.GetNode(record.GetByIndex(1)) // neo4j.Node
}

func DisableLogger

func DisableLogger()

DisableLogger disables LogrusLogger and StdLogger.

func EnableLogger

func EnableLogger()

EnableLogger enables LogrusLogger and StdLogger to do any log.

func GenerateOrderByExpr

func GenerateOrderByExpr(querySource string, dict PropertyDict, options ...OrderByOption) string

GenerateOrderByExpr returns a generated order-by expression by given order-by query source string (such as "name desc, age asc") and PropertyDict, with some OrderByOption-s. The generated expression will be in mysql-sql (such as "xxx ASC") or neo4j-cypher style (such as "xxx.yyy DESC").

Example:

dict := PropertyDict{
	"uid":  NewPropertyValue(false, "p.uid"),
	"name": NewPropertyValue(false, "p.firstname", "p.lastname"),
	"age":  NewPropertyValue(true, "u.birthday"),
}
_ = GenerateOrderByExpr(`uid, age desc`, dict) // => p.uid ASC, u.birthday ASC
_ = GenerateOrderByExpr(`age, username desc`, dict) // => u.birthday DESC, p.firstname DESC, p.lastname DESC

func GetBoolean

func GetBoolean(data interface{}) bool

GetBoolean returns neo4j Boolean value (bool) from given data.

func GetByteArray

func GetByteArray(data interface{}) []byte

GetByteArray returns neo4j ByteArray value ([]byte) from given data.

func GetDate

func GetDate(data interface{}) neo4j.Date

GetDate returns neo4j Date value (neo4j.Date) from given data.

func GetDateTime

func GetDateTime(data interface{}) time.Time

GetDateTime returns neo4j DateTime value (time.Time) from given data.

func GetDuration

func GetDuration(data interface{}) neo4j.Duration

GetDuration returns neo4j Duration value (neo4j.Duration) from given data.

func GetFloat

func GetFloat(data interface{}) float64

GetFloat returns neo4j Float value (float64) from given data.

func GetInteger

func GetInteger(data interface{}) int64

GetInteger returns neo4j Integer value (int64) from given data.

func GetList

func GetList(data interface{}) []interface{}

GetList returns neo4j List value ([]interface{}) from given data.

func GetLocalDateTime

func GetLocalDateTime(data interface{}) neo4j.LocalDateTime

GetLocalDateTime returns neo4j LocalDateTime value (neo4j.LocalDateTime) from given data.

func GetLocalTime

func GetLocalTime(data interface{}) neo4j.LocalTime

GetLocalTime returns neo4j LocalTime value (neo4j.LocalTime) from given data.

func GetMap

func GetMap(data interface{}) map[string]interface{}

GetMap returns neo4j Map value (map[string]interface{}) from given data.

func GetNode

func GetNode(data interface{}) neo4j.Node

GetNode returns neo4j Node value (neo4j.Node) from given data.

func GetPath

func GetPath(data interface{}) neo4j.Path

GetPath returns neo4j Path value (neo4j.Path) from given data.

func GetPoint

func GetPoint(data interface{}) neo4j.Point

GetPoint returns neo4j Point value (neo4j.Point) from given data.

func GetRel

func GetRel(data interface{}) neo4j.Relationship

GetRel returns neo4j Relationship value (neo4j.Relationship) from given data.

func GetString

func GetString(data interface{}) string

GetString returns neo4j String value (string) from given data.

func GetTime

func GetTime(data interface{}) neo4j.OffsetTime

GetTime returns neo4j Time value (neo4j.OffsetTime) from given data.

Types

type DialHandler

type DialHandler func(driver neo4j.Driver, config *neo4j.SessionConfig) (neo4j.Session, error)

DialHandler represents a neo4j.Session dial function, used to get a session from neo4j.Driver.

type DriverOption

type DriverOption func(*neo4j.Config)

DriverOption represents an option type for neo4j.NewDriver's option, can be created by WithXXX functions.

func WithAddressResolver

func WithAddressResolver(resolver neo4j.ServerAddressResolver) DriverOption

WithAddressResolver creates an DriverOption function to specify address resolver for neo4j.Driver, defaults to nil.

func WithConnectionAcquisitionTimeout

func WithConnectionAcquisitionTimeout(t time.Duration) DriverOption

WithConnectionAcquisitionTimeout creates an DriverOption function to specify connection acquisition timeout for neo4j.Driver, defaults to 1min.

func WithEncrypted

func WithEncrypted(encrypted bool) DriverOption

WithEncrypted creates an DriverOption function to specify encrypted flag for neo4j.Driver, defaults to true.

func WithLog

func WithLog(l neo4j.Logging) DriverOption

WithLog creates an DriverOption function to specify log function for neo4j.Driver, defaults to neo4j.NoOpLogger.

func WithMaxConnectionLifetime

func WithMaxConnectionLifetime(t time.Duration) DriverOption

WithMaxConnectionLifetime creates an DriverOption function to specify max connection lifetime for neo4j.Driver, defaults to 1h.

func WithMaxConnectionPoolSize

func WithMaxConnectionPoolSize(size int) DriverOption

WithMaxConnectionPoolSize creates an DriverOption function to specify max connection pool size for neo4j.Driver, defaults to 100.

func WithMaxTransactionRetryTime

func WithMaxTransactionRetryTime(t time.Duration) DriverOption

WithMaxTransactionRetryTime creates an DriverOption function to specify max transaction retry time for neo4j.Driver, defaults to 30s.

func WithSocketConnectTimeout

func WithSocketConnectTimeout(t time.Duration) DriverOption

WithSocketConnectTimeout creates an DriverOption function to specify socket connect timeout for neo4j.Driver, defaults to 5s.

func WithSocketKeepalive

func WithSocketKeepalive(keepalive bool) DriverOption

WithSocketKeepalive creates an DriverOption function to specify socket keepalive flag for neo4j.Driver, defaults to true.

func WithTrustStrategy

func WithTrustStrategy(e neo4j.TrustStrategy) DriverOption

WithTrustStrategy creates an DriverOption function to specify trust strategy for neo4j.Driver, defaults to neo4j.TrustAny(false).

type LoggerOption

type LoggerOption func(*loggerOptions)

LoggerOption represents an option type for LogrusLogger and StdLogger's option, can be created by WithXXX functions.

func WithCounterFields

func WithCounterFields(flag bool) LoggerOption

WithCounterFields creates a LoggerOption to decide whether to do store counter fields to logrus.Fields or not, defaults to false.

func WithLogCypher

func WithLogCypher(log bool) LoggerOption

WithLogCypher creates a LoggerOption to decide whether to do log for cyphers or not, defaults to true.

func WithLogErr

func WithLogErr(log bool) LoggerOption

WithLogErr creates a LoggerOption to decide whether to do log for errors or not, defaults to true.

func WithSkip

func WithSkip(skip int) LoggerOption

WithSkip creates a LoggerOption to specify runtime skip for getting runtime information, defaults to 1.

func WithSlowThreshold

func WithSlowThreshold(threshold time.Duration) LoggerOption

WithSlowThreshold creates a LoggerOption to specify a slow operation's duration used to highlight loggers, defaults to 0ms, means no highlight.

type LoggerParam

type LoggerParam struct {
	Type     string
	Cypher   string
	Duration time.Duration
	Slow     bool
	Source   string
	Counter  neo4j.Counters
	ErrorMsg string
}

LoggerParam stores some logger parameters and is used by LogrusLogger and StdLogger.

type LogrusLogger

type LogrusLogger struct {
	neo4j.Session
	// contains filtered or unexported fields
}

LogrusLogger represents a neo4j.Session as neo4j's logger, used to log neo4j's executing message to logrus.Logger.

func NewLogrusLogger

func NewLogrusLogger(session neo4j.Session, logger *logrus.Logger, options ...LoggerOption) *LogrusLogger

NewLogrusLogger creates a new LogrusLogger using given logrus.Logger and LoggerOption-s.

Example:

driver, err := neo4j.NewDriver(...)
sess, err := driver.Session(neo4j.AccessModeRead)
l := logrus.New()
l.SetFormatter(&logrus.TextFormatter{})
sess = NewLogrusLogger(sess, l)

func (*LogrusLogger) Run

func (l *LogrusLogger) Run(cypher string, params map[string]interface{}, configurers ...func(*neo4j.TransactionConfig)) (neo4j.Result, error)

Run implements neo4j.Session interface, it executes given cypher and params, and logs neo4j's message to logrus.Logger.

type OrderByOption

type OrderByOption = xdbutils_orderby.OrderByOption

OrderByOption represents an option type for GenerateOrderByExpr's option, can be created by WithXXX functions.

func WithOrderBySourceProcessor

func WithOrderBySourceProcessor(processor func(source string) (field string, asc bool)) OrderByOption

WithOrderBySourceProcessor creates an OrderByOption to specify the source processor for extracting field name and ascending flag from given source, defaults to use the "field asc" or "field desc" format (case-insensitive) to extract information.

func WithOrderBySourceSeparator

func WithOrderBySourceSeparator(separator string) OrderByOption

WithOrderBySourceSeparator creates an OrderByOption to specify the source order-by expression fields separator, defaults to ",".

func WithOrderByTargetProcessor

func WithOrderByTargetProcessor(processor func(destination string, asc bool) (target string)) OrderByOption

WithOrderByTargetProcessor creates an OrderByOption to specify the target processor for combining field name and ascending flag to target expression, defaults to generate the target with "destination ASC" or "destination DESC" format.

func WithOrderByTargetSeparator

func WithOrderByTargetSeparator(separator string) OrderByOption

WithOrderByTargetSeparator creates an OrderByOption to specify the target order-by expression fields separator, defaults to ", ".

type P

type P = map[string]interface{}

P is a cypher parameters type, is always same as `map[string]interface{}`.

Example:

session.Run(`MATCH (n {id: $id}) RETURN n`, xneo4j.P{"id": 2})

type Pool

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

Pool represents a neo4j.Session pool, implements neo4j.Driver interface. Actually this is a neo4j.Driver wrapper container with a DialHandler with custom neo4j.Session config.

Some tips:

1. Each neo4j.Driver instance maintains a pool of connections inside, as a result, it is recommended to only use one driver per application.

2. It is considerably cheap to create new neo4j.Session and neo4j.Transaction, as neo4j.Session and neo4j.Transaction do not create new connections as long as there are free connections available in the connection pool.

3. The neo4j.Driver is thread-safe, while the neo4j.Session or the neo4j.Transaction is not thread-safe.

func NewPool

func NewPool(driver neo4j.Driver, dial DialHandler) *Pool

NewPool creates a Pool using given neo4j.Driver and DialHandler, panics when using nil neo4j.Driver. Also see neo4j.NewDriver, neo4j.Config and neo4j.SessionConfig.

Example:

driver, _ := neo4j.NewDriver(uri, neo4j.BasicAuth(username, password, realm), xneo4j.WithMaxConnectionPoolSize(10))
pool := xneo4j.NewPool(driver, func(driver neo4j.Driver, config *neo4j.SessionConfig) (neo4j.Session, error) {
	return driver.Session(config.AccessMode, config.Bookmarks...) // use default database
})

func (*Pool) Close

func (p *Pool) Close() error

Close closes the neo4j.Driver and all underlying connections.

func (*Pool) Dial

func (p *Pool) Dial(mode neo4j.AccessMode, bookmarks ...string) (neo4j.Session, error)

Dial dials and returns a new neo4j.Session from neo4j.Driver's pool.

func (*Pool) DialReadMode

func (p *Pool) DialReadMode(bookmarks ...string) (neo4j.Session, error)

DialReadMode dials and returns a new neo4j.Session with neo4j.AccessModeRead from neo4j.Driver's pool.

func (*Pool) DialWriteMode

func (p *Pool) DialWriteMode(bookmarks ...string) (neo4j.Session, error)

DialWriteMode dials and returns a new neo4j.Session with neo4j.AccessModeWrite from neo4j.Driver's pool.

func (*Pool) Driver

func (p *Pool) Driver() neo4j.Driver

Driver returned the neo4j.Driver from Pool.

func (*Pool) NewSession

func (p *Pool) NewSession(config neo4j.SessionConfig) (neo4j.Session, error)

NewSession dials and returns a new neo4j.Session from neo4j.Driver's pool.

func (*Pool) Session

func (p *Pool) Session(mode neo4j.AccessMode, bookmarks ...string) (neo4j.Session, error)

Session dials and returns a new neo4j.Session from neo4j.Driver's pool.

func (*Pool) Target

func (p *Pool) Target() url.URL

Target returned the url.URL that this driver is bootstrapped.

func (*Pool) VerifyConnectivity

func (p *Pool) VerifyConnectivity() error

VerifyConnectivity verifies the driver can connect to a remote server or cluster by establishing a network connection with the remote.

type PropertyDict

type PropertyDict = xdbutils_orderby.PropertyDict

PropertyDict is used to store PropertyValue-s for data transfer object (dto) to entity's property mapping rule, is used in GenerateOrderByExpr.

type PropertyValue

type PropertyValue = xdbutils_orderby.PropertyValue

PropertyValue represents database single entity's property mapping rule, is used in GenerateOrderByExpr.

func NewPropertyValue

func NewPropertyValue(reverse bool, destinations ...string) *PropertyValue

NewPropertyValue creates a PropertyValue by given reverse and destinations, is used to describe database single entity's property mapping rule.

Here: 1. `destinations` represents mapping property destination list, use `property_name` directly for sql, use `returned_name.property_name` for cypher. 2. `reverse` represents the flag whether you need to revert the order or not.

type StdLogger

type StdLogger struct {
	neo4j.Session
	// contains filtered or unexported fields
}

StdLogger represents a neo4j.Session as neo4j's logger, used to log neo4j's executing message to logrus.StdLogger.

func NewStdLogger

func NewStdLogger(session neo4j.Session, logger logrus.StdLogger, options ...LoggerOption) *StdLogger

NewStdLogger creates a new StdLogger using given log.Logger and LoggerOption-s.

Example:

driver, err := neo4j.NewDriver(...)
sess, err := driver.Session(neo4j.AccessModeRead)
l := log.Default()
sess = NewStdLogger(sess, l)

func (*StdLogger) Run

func (l *StdLogger) Run(cypher string, params map[string]interface{}, configurers ...func(*neo4j.TransactionConfig)) (neo4j.Result, error)

Run implements neo4j.Session interface, it executes given cypher and params, and logs neo4j's message to logrus.StdLogger.

Jump to

Keyboard shortcuts

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