Skip to content
This repository has been archived by the owner on Oct 7, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1 from mindstand/working_overhaul
Browse files Browse the repository at this point in the history
Working overhaul
  • Loading branch information
Eric Solender authored Feb 3, 2020
2 parents e4a4016 + ee58e88 commit 76c4ae4
Show file tree
Hide file tree
Showing 71 changed files with 4,609 additions and 3,318 deletions.
8 changes: 8 additions & 0 deletions bolt_mode/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package bolt_mode

type AccessMode int

const (
ReadMode AccessMode = 0
WriteMode AccessMode = 1
)
110 changes: 29 additions & 81 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,28 @@ type IClient interface {

// opens a internalDriver pool to neo4j
NewDriverPool(size int) (IDriverPool, error)

// opens a v4 internalDriver
NewDriverV4() (IDriverV4, error)

// opens a v4 internalDriver pool
NewDriverPoolV4(size int) (IDriverPoolV4, error)
}

type Client struct {
// config stuff
connStr string
host string
port int
routing bool
pooled bool
maxConnections int
negotiateVersion bool
user string
password string
serverVersion []byte
timeout time.Duration
chunkSize uint16
useTLS bool
certFile string
caCertFile string
keyFile string
tlsNoVerify bool
readOnly bool
supportsV4 bool
createDbIfNotExists bool
connStr string
host string
port int
routing bool
pooled bool
maxConnections int
negotiateVersion bool
user string
password string
serverVersionBytes []byte
serverVersion int
timeout time.Duration
chunkSize uint16
useTLS bool
certFile string
caCertFile string
keyFile string
tlsNoVerify bool
}

func NewClient(opts ...Opt) (IClient, error) {
Expand All @@ -68,12 +60,6 @@ func NewClient(opts ...Opt) (IClient, error) {
client.timeout = time.Second * time.Duration(60)
}

// check version set correctly
if len(client.serverVersion) == 0 {
// set the server version, default to 3
client.serverVersion = make([]byte, 4)
}

// check chunk size
if client.chunkSize == 0 {
// set default chunk size
Expand Down Expand Up @@ -104,11 +90,6 @@ func NewClient(opts ...Opt) (IClient, error) {
return nil, errors.Wrap(errors.ErrConfiguration, "user can not be empty")
}

// todo check if neo4j allows passwordless users
if client.password == "" {
return nil, errors.Wrap(errors.ErrConfiguration, "password can not be empty")
}

client.connStr = fmt.Sprintf("%s://%s:%s@%s:%v", protocol, client.user, client.password, client.host, client.port)

// append tls portion if needed
Expand All @@ -123,14 +104,12 @@ func NewClient(opts ...Opt) (IClient, error) {
}

func (c *Client) NewDriver() (IDriver, error) {
if c.routing {
return nil, errors.New("can not open non pooled driver with routing enabled")
}

driver := &internalDriver{
createIfNotExists: c.createDbIfNotExists,
connectionFactory: &boltConnectionFactory{
timeout: c.timeout,
chunkSize: c.chunkSize,
serverVersion: c.serverVersion,
connStr: c.connStr,
},
client: c,
}

return &Driver{internalDriver: driver}, nil
Expand All @@ -142,42 +121,11 @@ func (c *Client) NewDriverPool(size int) (IDriverPool, error) {
return nil, err
}

return &DriverPool{
internalPool: driverPool,
}, nil
}

func (c *Client) NewDriverV4() (IDriverV4, error) {
if !c.supportsV4 {
return nil, errors.Wrap(errors.ErrInvalidVersion, "attempting to use v4 internalDriver when actual version is [%s]", string(c.serverVersion))
if c.routing {
return newRoutingPool(c, size)
} else {
return &DriverPool{
internalPool: driverPool,
}, nil
}

driver := &internalDriver{
createIfNotExists: c.createDbIfNotExists,
connectionFactory: &boltConnectionFactory{
timeout: c.timeout,
chunkSize: c.chunkSize,
serverVersion: c.serverVersion,
connStr: c.connStr,
},
}

return &DriverV4{
internalDriver: driver,
}, nil
}

func (c *Client) NewDriverPoolV4(size int) (IDriverPoolV4, error) {
if !c.supportsV4 {
return nil, errors.Wrap(errors.ErrInvalidVersion, "attempting to use v4 internalDriver when actual version is [%s]", string(c.serverVersion))
}

driverPool, err := newDriverPool(c.connStr, size)
if err != nil {
return nil, err
}

return &DriverPoolV4{
internalPool: driverPool,
}, nil
}
55 changes: 18 additions & 37 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -1,75 +1,56 @@
package goBolt

import (
ll "github.com/mindstand/go-bolt/log"
"log"
"github.com/mindstand/go-bolt/bolt_mode"
"github.com/mindstand/go-bolt/log"
"testing"
)

func TestClient(t *testing.T) {
ll.SetLevel("trace")
client, err := NewClient(WithBasicAuth("neo4j", "changeme"), WithHostPort("0.0.0.0", 7687))
log.SetLevel("trace")
log.Info("opening client")
client, err := NewClient(WithBasicAuth("neo4j", "TZU6xiVZLbe5L5UmZaU5"), WithHostPort("0.0.0.0", 7687))
if err != nil {
t.Log(err)
t.FailNow()
}

log.Infof("opening driver")
driver, err := client.NewDriver()
if err != nil {
t.Log(err)
t.FailNow()
}

conn, err := driver.Open(ReadWriteMode)
log.Info("opening connection")
conn, err := driver.Open(bolt_mode.WriteMode)
if err != nil {
t.Log(err)
t.FailNow()
}

rows, err := conn.QueryNeo("match (n) return n", nil)
log.Infof("executing query")
rows, err := conn.Query("create (:TestNode{uuid:$id})", map[string]interface{}{
"id": "random_id",
})
if err != nil {
t.Log(err)
t.FailNow()
}

log.Println(rows.All())
log.Infof("showing rows")
all, m, err := rows.All()
log.Tracef("rows: %v, %v, %v", all, m, err)

log.Trace("closing rows")
err = rows.Close()
if err != nil {
t.Log(err)
t.FailNow()
}
}

func TestClientV4(t *testing.T) {
ll.SetLevel("trace")
client, err := NewClient(WithBasicAuth("neo4j", "changeme"), WithHostPort("0.0.0.0", 7687), WithVersion("4"))
if err != nil {
t.Log(err)
t.FailNow()
}

driver, err := client.NewDriverV4()
if err != nil {
t.Log(err)
t.FailNow()
}

conn, err := driver.Open("system", ReadWriteMode)
if err != nil {
t.Log(err)
t.FailNow()
}

rows, err := conn.QueryNeo("match (n) return n", nil)
if err != nil {
t.Log(err)
t.FailNow()
}

log.Println(rows.All())

err = rows.Close()
log.Trace("closing connection")
err = conn.Close()
if err != nil {
t.Log(err)
t.FailNow()
Expand Down
52 changes: 26 additions & 26 deletions compose/docker-compose-3-5-cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ networks:
services:

core1:
image: neo4j:3.5-enterprise
image: neo4j:3.5.14-enterprise
networks:
- lan
ports:
Expand All @@ -31,7 +31,7 @@ services:
- NEO4J_dbms_connector_bolt_listen__address=:7687

core2:
image: neo4j:3.5-enterprise
image: neo4j:3.5.14-enterprise
networks:
- lan
ports:
Expand All @@ -55,7 +55,7 @@ services:
- NEO4J_dbms_connector_bolt_listen__address=:7688

core3:
image: neo4j:3.5-enterprise
image: neo4j:3.5.14-enterprise
networks:
- lan
ports:
Expand All @@ -79,7 +79,7 @@ services:
- NEO4J_dbms_connector_bolt_listen__address=:7689

read1:
image: neo4j:3.5-enterprise
image: neo4j:3.5.14-enterprise
networks:
- lan
ports:
Expand All @@ -101,25 +101,25 @@ services:
- NEO4J_dbms_connector_bolt_listen__address=:7690

# This core demonstrates that additional instances can be added after the initial core is established (ie: this instance is nost listed within "initialDiscoveryMembers")
core4:
image: neo4j:3.5-enterprise
networks:
- lan
ports:
- 7478:7478
- 6481:6481
- 7691:7691
volumes:
- $HOME/neo4j/neo4j-core4/conf:/conf
- $HOME/neo4j/neo4j-core4/data:/data
- $HOME/neo4j/neo4j-core4/logs:/logs
- $HOME/neo4j/neo4j-core1/plugins:/plugins
environment:
- NEO4J_AUTH=neo4j/changeme
- NEO4J_dbms_mode=CORE
- NEO4J_ACCEPT_LICENSE_AGREEMENT=yes
# - NEO4J_causalClustering_expectedCoreClusterSize=3
- NEO4J_causal__clustering_initial__discovery__members=core1:5000,core2:5000,core3:5000
- NEO4J_dbms_connector_http_listen__address=:7478
- NEO4J_dbms_connector_https_listen__address=:6481
- NEO4J_dbms_connector_bolt_listen__address=:7691
# core4:
# image: neo4j:3.5.14-enterprise
# networks:
# - lan
# ports:
# - 7478:7478
# - 6481:6481
# - 7691:7691
# volumes:
# - $HOME/neo4j/neo4j-core4/conf:/conf
# - $HOME/neo4j/neo4j-core4/data:/data
# - $HOME/neo4j/neo4j-core4/logs:/logs
# - $HOME/neo4j/neo4j-core1/plugins:/plugins
# environment:
# - NEO4J_AUTH=neo4j/changeme
# - NEO4J_dbms_mode=CORE
# - NEO4J_ACCEPT_LICENSE_AGREEMENT=yes
# # - NEO4J_causalClustering_expectedCoreClusterSize=3
# - NEO4J_causal__clustering_initial__discovery__members=core1:5000,core2:5000,core3:5000
# - NEO4J_dbms_connector_http_listen__address=:7478
# - NEO4J_dbms_connector_https_listen__address=:6481
# - NEO4J_dbms_connector_bolt_listen__address=:7691
Loading

0 comments on commit 76c4ae4

Please sign in to comment.