Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
remade committed Nov 1, 2024
1 parent 336fa36 commit 186dd08
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 69 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 0 additions & 8 deletions pkg/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 14 additions & 35 deletions pkg/connection/embedded.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -52,14 +54,22 @@ func NewEmbeddedConnection(p NewConnectionParams) *EmbeddedConnection {
}

func (h *EmbeddedConnection) Connect() error {
if err := h.preConnectionChecks(); err != nil {
err := h.preConnectionChecks()
if err != nil {
return err
}

var cErr C.sr_string_t
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
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
32 changes: 9 additions & 23 deletions pkg/connection/embedded_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package connection

import (
"fmt"
"testing"

"github.com/stretchr/testify/suite"
"github.com/surrealdb/surrealdb.go/pkg/models"
)
Expand All @@ -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() {
Expand Down Expand Up @@ -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() {
Expand All @@ -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)
Expand All @@ -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
}

0 comments on commit 186dd08

Please sign in to comment.