Skip to content

Commit

Permalink
address some review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
AviAvni committed Apr 7, 2024
1 parent c49e266 commit 1a1b53e
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 92 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
)

func main() {
db, _ := falkordb.FalkorDBNew("0.0.0.0:6379", &falkordb.ConnectionOption{})
db, _ := falkordb.FalkorDBNew(&falkordb.ConnectionOption{Addr: "0.0.0.0:6379"})

graph := db.SelectGraph("social")

Expand Down
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
var graph *Graph

func createGraph() {
db, _ := FalkorDBNew("0.0.0.0:6379", &ConnectionOption{})
db, _ := FalkorDBNew(&ConnectionOption{Addr: "0.0.0.0:6379"})
graph = db.SelectGraph("social")
graph.Delete()

Expand Down
2 changes: 1 addition & 1 deletion example_graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func ExampleSelectGraph() {
db, _ := falkordb.FalkorDBNew("0.0.0.0:6379", &falkordb.ConnectionOption{})
db, _ := falkordb.FalkorDBNew(&falkordb.ConnectionOption{Addr: "0.0.0.0:6379"})

graph := db.SelectGraph("social")

Expand Down
3 changes: 2 additions & 1 deletion examples/falkordb_tls_client/falkordb_tls_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ func main() {
// This should be used only for testing.
clientTLSConfig.InsecureSkipVerify = true

db, _ := falkordb.FalkorDBNew(*host, &falkordb.ConnectionOption{
db, _ := falkordb.FalkorDBNew(&falkordb.ConnectionOption{
Addr: *host,
Password: *password,
TLSConfig: clientTLSConfig,
})
Expand Down
13 changes: 11 additions & 2 deletions falkordb.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func isSentinel(conn *redis.Client) bool {
return info["server"]["redis_mode"] == "sentinel"
}

func FalkorDBNew(address string, options *ConnectionOption) (*FalkorDB, error) {
// FalkorDB Class for interacting with a FalkorDB server.
func FalkorDBNew(options *ConnectionOption) (*FalkorDB, error) {
db := redis.NewClient(options)

if isSentinel(db) {
Expand All @@ -34,12 +35,13 @@ func FalkorDBNew(address string, options *ConnectionOption) (*FalkorDB, error) {
masterName := masters.([]interface{})[0].(map[string]interface{})["name"].(string)
db = redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: masterName,
SentinelAddrs: []string{address},
SentinelAddrs: []string{options.Addr},
})
}
return &FalkorDB{Conn: db}, nil
}

// Creates a new FalkorDB instance from a URL.
func FromURL(url string) (*FalkorDB, error) {
options, err := redis.ParseURL(url)
if err != nil {
Expand All @@ -63,18 +65,25 @@ func FromURL(url string) (*FalkorDB, error) {
return &FalkorDB{Conn: db}, nil
}

// Selects a graph by creating a new Graph instance.
func (db *FalkorDB) SelectGraph(graphName string) *Graph {
return graphNew(graphName, db.Conn)
}

// List all graph names.
// See: https://docs.falkordb.com/commands/graph.list.html
func (db *FalkorDB) ListGraphs() ([]string, error) {
return db.Conn.Do(ctx, "GRAPH.LIST").StringSlice()
}

// Retrieve a DB level configuration.
// For a list of available configurations see: https://docs.falkordb.com/configuration.html#falkordb-configuration-parameters
func (db *FalkorDB) ConfigGet(key string) string {
return db.Conn.Do(ctx, "GRAPH.CONFIG", "GET", key).String()
}

// Update a DB level configuration.
// For a list of available configurations see: https://docs.falkordb.com/configuration.html#falkordb-configuration-parameters
func (db *FalkorDB) ConfigSet(key, value string) error {
return db.Conn.Do(ctx, "GRAPH.CONFIG", "SET", key).Err()
}
28 changes: 9 additions & 19 deletions graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,16 @@ func (options *QueryOptions) GetTimeout() int {
return options.timeout
}

// Query executes a query against the graph.
func (g *Graph) Query(q string, params map[string]interface{}, options *QueryOptions) (*QueryResult, error) {
func (g *Graph) query(command string, q string, params map[string]interface{}, options *QueryOptions) (*QueryResult, error) {
if params != nil {
q = BuildParamsHeader(params) + q
}
var r interface{}
var err error
if options != nil && options.timeout >= 0 {
r, err = g.Conn.Do(ctx, "GRAPH.QUERY", g.Id, q, "--compact", "timeout", options.timeout).Result()
r, err = g.Conn.Do(ctx, command, g.Id, q, "--compact", "timeout", options.timeout).Result()
} else {
r, err = g.Conn.Do(ctx, "GRAPH.QUERY", g.Id, q, "--compact").Result()
r, err = g.Conn.Do(ctx, command, g.Id, q, "--compact").Result()
}
if err != nil {
return nil, err
Expand All @@ -80,23 +79,14 @@ func (g *Graph) Query(q string, params map[string]interface{}, options *QueryOpt
return QueryResultNew(g, r)
}

// Query executes a query against the graph.
func (g *Graph) Query(q string, params map[string]interface{}, options *QueryOptions) (*QueryResult, error) {
return g.query("GRAPH.QUERY", q, params, options)
}

// ROQuery executes a read only query against the graph.
func (g *Graph) ROQuery(q string, params map[string]interface{}, options *QueryOptions) (*QueryResult, error) {
if params != nil {
q = BuildParamsHeader(params) + q
}
var r interface{}
var err error
if options != nil && options.timeout >= 0 {
r, err = g.Conn.Do(ctx, "GRAPH.RO_QUERY", g.Id, q, "--compact", "timeout", options.timeout).Result()
} else {
r, err = g.Conn.Do(ctx, "GRAPH.RO_QUERY", g.Id, q, "--compact").Result()
}
if err != nil {
return nil, err
}

return QueryResultNew(g, r)
return g.query("GRAPH.RO_QUERY", q, params, options)
}

// Procedures
Expand Down
67 changes: 42 additions & 25 deletions graph_schema.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package falkordb

import "errors"

type GraphSchema struct {
graph *Graph
version int
Expand All @@ -24,72 +26,87 @@ func (gs *GraphSchema) clear() {
gs.properties = []string{}
}

func (gs *GraphSchema) refresh_labels() {
qr, _ := gs.graph.CallProcedure("db.labels", nil)
func (gs *GraphSchema) refresh_labels() error {
qr, err := gs.graph.CallProcedure("db.labels", nil)
if err != nil {
return err
}

gs.labels = make([]string, len(qr.results))

for idx, r := range qr.results {
gs.labels[idx] = r.GetByIndex(0).(string)
}
return nil
}

func (gs *GraphSchema) refresh_relationships() {
qr, _ := gs.graph.CallProcedure("db.relationshipTypes", nil)
func (gs *GraphSchema) refresh_relationships() error {
qr, err := gs.graph.CallProcedure("db.relationshipTypes", nil)
if err != nil {
return err
}

gs.relationships = make([]string, len(qr.results))

for idx, r := range qr.results {
gs.relationships[idx] = r.GetByIndex(0).(string)
}
return nil
}

func (gs *GraphSchema) refresh_properties() {
qr, _ := gs.graph.CallProcedure("db.propertyKeys", nil)
func (gs *GraphSchema) refresh_properties() error {
qr, err := gs.graph.CallProcedure("db.propertyKeys", nil)
if err != nil {
return err
}

gs.properties = make([]string, len(qr.results))

for idx, r := range qr.results {
gs.properties[idx] = r.GetByIndex(0).(string)
}
return nil
}

func (gs *GraphSchema) getLabel(lblIdx int) string {
func (gs *GraphSchema) getLabel(lblIdx int) (string, error) {
if lblIdx >= len(gs.labels) {
gs.refresh_labels()
// Retry.
err := gs.refresh_labels()
if err != nil {
return "", err
}
if lblIdx >= len(gs.labels) {
// Error!
panic("Unknown label index.")
return "", errors.New("Unknown label index.")

}
}

return gs.labels[lblIdx]
return gs.labels[lblIdx], nil
}

func (gs *GraphSchema) getRelation(relIdx int) string {
func (gs *GraphSchema) getRelation(relIdx int) (string, error) {
if relIdx >= len(gs.relationships) {
gs.refresh_relationships()
// Retry.
err := gs.refresh_relationships()
if err != nil {
return "", err
}
if relIdx >= len(gs.relationships) {
// Error!
panic("Unknown relation type index.")
return "", errors.New("Unknown label index.")
}
}

return gs.relationships[relIdx]
return gs.relationships[relIdx], nil
}

func (gs *GraphSchema) getProperty(propIdx int) string {
func (gs *GraphSchema) getProperty(propIdx int) (string, error) {
if propIdx >= len(gs.properties) {
gs.refresh_properties()

// Retry.
err := gs.refresh_properties()
if err != nil {
return "", err
}
if propIdx >= len(gs.properties) {
// Error!
panic("Unknown property index.")
return "", errors.New("Unknown property index.")
}
}

return gs.properties[propIdx]
return gs.properties[propIdx], nil
}
Loading

0 comments on commit 1a1b53e

Please sign in to comment.