From 186dd08ec6a35f3ae80eaa88fb3c1c8dc07d916a Mon Sep 17 00:00:00 2001 From: Remade Date: Fri, 1 Nov 2024 14:26:49 +0100 Subject: [PATCH] WIP --- .golangci.yml | 1 + README.md | 14 ++++++++++ db.go | 5 ++-- db_test.go | 3 +- pkg/connection/connection.go | 8 ------ pkg/connection/embedded.go | 49 ++++++++++----------------------- pkg/connection/embedded_test.go | 32 ++++++--------------- 7 files changed, 43 insertions(+), 69 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 95db2d0..5a5d214 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -22,6 +22,7 @@ linters-settings: - ifElseChain - octalLiteral - whyNoLint + - dupSubExpr # https://github.com/go-critic/go-critic/issues/897#issuecomment-568896534 gocyclo: min-complexity: 15 goimports: diff --git a/README.md b/README.md index b3624a6..c915324 100644 --- a/README.md +++ b/README.md @@ -216,6 +216,20 @@ or for a secure connection db, err := surrealdb.New("https://localhost:8000") ``` +### Using SurrealKV and Memory +SurrealKV and Memory also do not support live notifications at this time. This would be updated in the next +release. + +For Surreal KV +```go +db, err := surrealdb.New("surrealkv://path/to/dbfile.kv") +``` + +For Memory +```go +db, err := surrealdb.New("mem://") +db, err := surrealdb.New("memory://") +``` ## Data Models This package facilitates communication between client and the backend service using the Concise diff --git a/db.go b/db.go index cc229b7..ec62e1e 100644 --- a/db.go +++ b/db.go @@ -3,12 +3,13 @@ package surrealdb import ( "context" "fmt" - "github.com/fxamacker/cbor/v2" "log/slog" "net/url" "os" "strings" + "github.com/fxamacker/cbor/v2" + "github.com/surrealdb/surrealdb.go/pkg/connection" "github.com/surrealdb/surrealdb.go/pkg/constants" "github.com/surrealdb/surrealdb.go/pkg/logger" @@ -48,7 +49,7 @@ func New(connectionURL string) (*DB, error) { con = connection.NewHTTPConnection(newParams) } else if scheme == "ws" || scheme == "wss" { con = connection.NewWebSocketConnection(newParams) - } else if scheme == "mem" || scheme == "surrealkv" { + } else if scheme == "memory" || scheme == "mem" || scheme == "surrealkv" { con = connection.NewEmbeddedConnection(newParams) } else { return nil, fmt.Errorf("invalid connection url") diff --git a/db_test.go b/db_test.go index 5617dff..5635c42 100644 --- a/db_test.go +++ b/db_test.go @@ -2,12 +2,13 @@ package surrealdb_test import ( "fmt" - "github.com/surrealdb/surrealdb.go" "os" "sync" "testing" "time" + "github.com/surrealdb/surrealdb.go" + "github.com/stretchr/testify/suite" "github.com/surrealdb/surrealdb.go/pkg/connection" "github.com/surrealdb/surrealdb.go/pkg/models" diff --git a/pkg/connection/connection.go b/pkg/connection/connection.go index 0b5b86e..46387e9 100644 --- a/pkg/connection/connection.go +++ b/pkg/connection/connection.go @@ -125,14 +125,6 @@ func (bc *BaseConnection) getErrorChannel(id string) (chan error, bool) { return ch, ok } -func (bc *BaseConnection) getLiveChannel(id string) (chan Notification, bool) { - bc.notificationChannelsLock.RLock() - defer bc.notificationChannelsLock.RUnlock() - ch, ok := bc.notificationChannels[id] - - return ch, ok -} - func (bc *BaseConnection) preConnectionChecks() error { if bc.baseURL == "" { return constants.ErrNoBaseURL diff --git a/pkg/connection/embedded.go b/pkg/connection/embedded.go index 07d71a8..052a6c5 100644 --- a/pkg/connection/embedded.go +++ b/pkg/connection/embedded.go @@ -9,12 +9,14 @@ import "C" import ( "fmt" + "net/url" + "sync" + "unsafe" + "github.com/fxamacker/cbor/v2" "github.com/surrealdb/surrealdb.go/internal/codec" "github.com/surrealdb/surrealdb.go/internal/rand" "github.com/surrealdb/surrealdb.go/pkg/constants" - "sync" - "unsafe" ) type EmbeddedConnection struct { @@ -52,7 +54,8 @@ func NewEmbeddedConnection(p NewConnectionParams) *EmbeddedConnection { } func (h *EmbeddedConnection) Connect() error { - if err := h.preConnectionChecks(); err != nil { + err := h.preConnectionChecks() + if err != nil { return err } @@ -60,6 +63,13 @@ func (h *EmbeddedConnection) Connect() error { defer C.sr_free_string(cErr) cEndpoint := C.CString(h.baseURL) + u, err := url.ParseRequestURI(h.baseURL) + if err != nil { + return err + } + if u.Scheme == "mem" || u.Scheme == "memory" { + cEndpoint = C.CString("memory") + } defer C.free(unsafe.Pointer(cEndpoint)) var surrealOptions C.sr_option_t @@ -75,40 +85,9 @@ func (h *EmbeddedConnection) Connect() error { } h.surrealStream = cStream - go h.initialize() - return nil } -func (h *EmbeddedConnection) initialize() { - for { - select { - case <-h.closeChan: - return - default: - //var cNotification C.sr_notification_t - //ret := C.sr_stream_next(h.surrealStream, &cNotification) - - //_ = h.surrealStream.next() - //fmt.Println() - //if ret == C.sr_SR_NONE { - // // stream closed - // return - //} else if ret < 0 { - // // Stream err - // return - //} - // - //C.sr_print_notification(&cNotification) - - } - } -} - -func (h *EmbeddedConnection) handleNotification() { - -} - func (h *EmbeddedConnection) Close() error { C.sr_surreal_rpc_free(h.surrealRPC) @@ -145,7 +124,7 @@ func (h *EmbeddedConnection) Send(res interface{}, method string, params ...inte return nil } - resultBytes := cbor.RawMessage(C.GoBytes(unsafe.Pointer(cRes), C.int(resSize))) + resultBytes := cbor.RawMessage(C.GoBytes(unsafe.Pointer(cRes), resSize)) rpcRes, _ := h.marshaler.Marshal(RPCResponse[cbor.RawMessage]{ID: request.ID, Result: &resultBytes}) return h.unmarshaler.Unmarshal(rpcRes, res) diff --git a/pkg/connection/embedded_test.go b/pkg/connection/embedded_test.go index 5b134a0..d0e8423 100644 --- a/pkg/connection/embedded_test.go +++ b/pkg/connection/embedded_test.go @@ -2,6 +2,8 @@ package connection import ( "fmt" + "testing" + "github.com/stretchr/testify/suite" "github.com/surrealdb/surrealdb.go/pkg/models" ) @@ -12,11 +14,11 @@ type EmbeddedConnectionTestSuite struct { name string } -//func TestEmbeddedConnectionTestSuite(t *testing.T) { -// s := new(EmbeddedConnectionTestSuite) -// s.name = "Test_Embedded_Connection" -// suite.Run(t, s) -//} +func TestEmbeddedConnectionTestSuite(t *testing.T) { + s := new(EmbeddedConnectionTestSuite) + s.name = "Test_Embedded_Connection" + suite.Run(t, s) +} // SetupSuite is called before the s starts running func (s *EmbeddedConnectionTestSuite) SetupSuite() { @@ -50,14 +52,6 @@ func (s *EmbeddedConnectionTestSuite) TestSendRequest() { var versionRes RPCResponse[string] err = s.con.Send(&versionRes, "version") s.Require().NoError(err) - - //var signInRes RPCResponse[string] - //err = con.Send(&signInRes, "signin", map[string]string{ - // "user": "root", - // "pass": "root", - //}) - //assert.NoError(t, err) - //fmt.Println(signInRes) } func (s *EmbeddedConnectionTestSuite) TestLiveAndNotification() { @@ -68,7 +62,7 @@ func (s *EmbeddedConnectionTestSuite) TestLiveAndNotification() { err = s.con.Send(&liveRes, "live", "users", false) s.Require().NoError(err, "should not return error on live request") - liveID := (*liveRes.Result).String() + liveID := liveRes.Result.String() defer func() { err = s.con.Send(nil, "kill", liveID) s.Require().NoError(err) @@ -79,13 +73,5 @@ func (s *EmbeddedConnectionTestSuite) TestLiveAndNotification() { fmt.Println(notifications) - //_, e := surrealdb.Create[testUser](s.db, "users", map[string]interface{}{ - // "username": "johnny", - // "password": "123", - //}) - //s.Require().NoError(e) - // - //notification := <-notifications - //s.Require().Equal(connection.CreateAction, notification.Action) - //s.Require().Equal(live, notification.ID) + // Notification reader not ready on C lib }