Skip to content

Commit 5019f03

Browse files
committed
Merge branch 'dev/sugam'
2 parents 043a3f7 + f9cc72a commit 5019f03

File tree

6 files changed

+172
-44
lines changed

6 files changed

+172
-44
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
test
2-
go.sum
1+
**.sum

client/client.go

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,56 @@ package client
22

33
import (
44
"fmt"
5-
"github.com/IntelliLog/IntelliLog-GoLang-Driver/connection"
6-
"strings"
5+
"github.com/citra-org/chrono-db-go-driver/connection"
76
)
87

98
type Client struct {
109
conn *connection.Connection
1110
}
1211

13-
func NewClient(uri string) (*Client, error) {
14-
conn, err := connection.NewConnection(uri)
12+
func Connect(uri string) (*Client, string, error) {
13+
conn, dbName, err := connection.NewConnection(uri)
1514
if err != nil {
16-
return nil, err
15+
return nil, "", err
1716
}
18-
return &Client{conn: conn}, nil
17+
return &Client{conn: conn}, dbName, nil
1918
}
2019

2120
func (c *Client) Close() error {
2221
return c.conn.Close()
2322
}
2423

25-
func (c *Client) Create() error {
26-
response, err := c.conn.Execute("c")
27-
if err != nil {
28-
return err
24+
func (c *Client) CreateChrono(chrono string) error {
25+
if response, err := c.conn.Execute("cc " + "heheh"); err != nil || response != "OK" {
26+
return fmt.Errorf("create failed: %v", err)
2927
}
30-
if response != "OK" {
31-
return fmt.Errorf("create failed: %s", response)
28+
return nil
29+
}
30+
31+
func (c *Client) CreateStream(chrono string, stream string) error {
32+
if response, err := c.conn.Execute("cs " + stream); err != nil || response != "OK" {
33+
return fmt.Errorf("create failed: %v", err)
34+
}
35+
return nil
36+
}
37+
func (c *Client) DeleteStream(chrono string, stream string) error {
38+
if response, err := c.conn.Execute("ds " + stream); err != nil || response != "OK" {
39+
return fmt.Errorf("delete failed: %v", err)
3240
}
3341
return nil
3442
}
3543

36-
func (c *Client) Write(data map[string]string) error {
37-
command := "w"
44+
func (c *Client) WriteEvent(chrono string, stream string, data map[string]string) error {
45+
command := "w " + stream + " "
3846
for k, v := range data {
39-
command += fmt.Sprintf(" %s %s", k, v)
40-
}
41-
response, err := c.conn.Execute(command)
42-
if err != nil {
43-
return err
47+
command += k + " " + v + " "
4448
}
45-
if response != "OK" {
46-
return fmt.Errorf("write failed: %s", response)
49+
if response, err := c.conn.Execute(command); err != nil || response != "OK" {
50+
return fmt.Errorf("write failed: %v", err)
4751
}
4852
return nil
4953
}
5054

51-
func (c *Client) Read() (string, error) {
52-
response, err := c.conn.Execute("r")
53-
if err != nil {
54-
return "", err
55-
}
56-
//TODO: fix this to send lines insted of lines as string (check ops/read.rs)
57-
formattedResponse := strings.ReplaceAll(response, "&/n", "\n")
58-
return formattedResponse, nil
55+
func (c *Client) Read(chrono string, stream string) (string, error) {
56+
return c.conn.Execute("r " + stream)
5957
}

connection/connection.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,47 +11,48 @@ type Connection struct {
1111
conn net.Conn
1212
}
1313

14-
func NewConnection(uri string) (*Connection, error) {
14+
func NewConnection(uri string) (*Connection, string, error) {
1515
parts := strings.Split(uri, "://")
16-
if len(parts) != 2 || parts[0] != "itlg" {
17-
return nil, fmt.Errorf("invalid URI scheme")
16+
if len(parts) != 2 || parts[0] != "chrono" {
17+
return nil, "", fmt.Errorf("invalid URI scheme")
1818
}
1919

2020
authAndHost := strings.Split(parts[1], "@")
2121
if len(authAndHost) != 2 {
22-
return nil, fmt.Errorf("invalid URI format")
22+
return nil, "", fmt.Errorf("invalid URI format")
2323
}
2424

2525
auth := strings.Split(authAndHost[0], ":")
2626
if len(auth) != 2 {
27-
return nil, fmt.Errorf("invalid URI format")
27+
return nil, "", fmt.Errorf("invalid URI format")
2828
}
2929
username := auth[0]
3030
password := auth[1]
3131

3232
hostAndDatabase := strings.Split(authAndHost[1], "/")
3333
if len(hostAndDatabase) != 2 {
34-
return nil, fmt.Errorf("invalid URI format")
34+
return nil, "", fmt.Errorf("invalid URI format")
3535
}
3636
host := hostAndDatabase[0]
37+
chrono := hostAndDatabase[1]
3738

3839
conn, err := net.Dial("tcp", host)
3940
if err != nil {
40-
return nil, err
41+
return nil, "", err
4142
}
4243

4344
connection := &Connection{conn: conn}
4445
err = connection.authenticate(username, password)
4546
if err != nil {
4647
conn.Close()
47-
return nil, err
48+
return nil, "", err
4849
}
4950

50-
return connection, nil
51+
return connection, chrono, nil
5152
}
5253

5354
func (c *Connection) authenticate(username, password string) error {
54-
command := fmt.Sprintf("%s %s auth\n", username, password)
55+
command := fmt.Sprintf("auth %s %s", username, password)
5556
_, err := c.conn.Write([]byte(command))
5657
if err != nil {
5758
return err
@@ -79,10 +80,12 @@ func (c *Connection) Execute(command string) (string, error) {
7980
return "", err
8081
}
8182

82-
response, err := bufio.NewReader(c.conn).ReadString('\n')
83+
buffer := make([]byte, 1024)
84+
n, err := c.conn.Read(buffer)
8385
if err != nil {
8486
return "", err
8587
}
8688

87-
return strings.TrimSpace(response), nil
89+
response := string(buffer[:n])
90+
return response, nil
8891
}

go.mod

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
1-
module github.com/IntelliLog/IntelliLog-GoLang-Driver
1+
module github.com/citra-org/chrono-db-go-driver
22

33
go 1.22.4
4+
5+
require github.com/gin-gonic/gin v1.10.0
6+
7+
require (
8+
github.com/bytedance/sonic v1.11.6 // indirect
9+
github.com/bytedance/sonic/loader v0.1.1 // indirect
10+
github.com/cloudwego/base64x v0.1.4 // indirect
11+
github.com/cloudwego/iasm v0.2.0 // indirect
12+
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
13+
github.com/gin-contrib/sse v0.1.0 // indirect
14+
github.com/go-playground/locales v0.14.1 // indirect
15+
github.com/go-playground/universal-translator v0.18.1 // indirect
16+
github.com/go-playground/validator/v10 v10.20.0 // indirect
17+
github.com/goccy/go-json v0.10.2 // indirect
18+
github.com/json-iterator/go v1.1.12 // indirect
19+
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
20+
github.com/leodido/go-urn v1.4.0 // indirect
21+
github.com/mattn/go-isatty v0.0.20 // indirect
22+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
23+
github.com/modern-go/reflect2 v1.0.2 // indirect
24+
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
25+
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
26+
github.com/ugorji/go/codec v1.2.12 // indirect
27+
golang.org/x/arch v0.8.0 // indirect
28+
golang.org/x/crypto v0.23.0 // indirect
29+
golang.org/x/net v0.25.0 // indirect
30+
golang.org/x/sys v0.20.0 // indirect
31+
golang.org/x/text v0.15.0 // indirect
32+
google.golang.org/protobuf v1.34.1 // indirect
33+
gopkg.in/yaml.v3 v3.0.1 // indirect
34+
)

test/api.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
curl http://localhost:3000/cs/<stream>
2+
curl -X POST -d '{"<header>": "<body>","<header>": "<body>"}' -H "Content-Type: application/json" http://localhost:3000/w/<stream>
3+
curl http://localhost:3000/r/<stream>
4+
curl http://localhost:3000/ds/<stream>

test/test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
"github.com/citra-org/chrono-db-go-driver/client"
8+
"github.com/gin-gonic/gin"
9+
)
10+
11+
var dbClient *client.Client
12+
var dbName string
13+
14+
func main() {
15+
uri := "chrono://admin:[email protected]:3141/dev4"
16+
var err error
17+
18+
dbClient, dbName, err = client.Connect(uri)
19+
if err != nil {
20+
fmt.Println("Error connecting to database:", err)
21+
return
22+
}
23+
defer dbClient.Close()
24+
25+
r := gin.Default()
26+
// r.GET("/c", handleCreate)
27+
r.POST("/w/:stream", handleWrite)
28+
r.GET("/r/:stream", handleRead)
29+
r.GET("/cs/:stream", handleCreateStream)
30+
r.GET("/ds/:stream", handleDeleteStream)
31+
32+
fmt.Println("Server listening on port 3000")
33+
err = r.Run(":3000")
34+
if err != nil {
35+
fmt.Println("Error starting server:", err)
36+
}
37+
}
38+
39+
// func handleCreate(c *gin.Context) {
40+
// err := dbClient.CreateChrono(dbName)
41+
// if err != nil {
42+
// c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Error creating record: %s", err)})
43+
// return
44+
// }
45+
// c.String(http.StatusOK, "Create operation successful")
46+
// }
47+
48+
func handleCreateStream(c *gin.Context) {
49+
stream := c.Param("stream")
50+
err := dbClient.CreateStream(dbName, stream)
51+
if err != nil {
52+
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Error creating record: %s", err)})
53+
return
54+
}
55+
c.String(http.StatusOK, "Create operation successful")
56+
}
57+
58+
func handleDeleteStream(c *gin.Context) {
59+
stream := c.Param("stream")
60+
err := dbClient.DeleteStream(dbName, stream)
61+
if err != nil {
62+
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Error deleting record: %s", err)})
63+
return
64+
}
65+
c.String(http.StatusOK, "Delete operation successful")
66+
}
67+
68+
func handleWrite(c *gin.Context) {
69+
stream := c.Param("stream")
70+
71+
var data map[string]string
72+
if err := c.ShouldBindJSON(&data); err != nil {
73+
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Error decoding request body: %s", err)})
74+
return
75+
}
76+
77+
err := dbClient.WriteEvent(dbName, stream, data)
78+
if err != nil {
79+
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Error writing data: %s", err)})
80+
return
81+
}
82+
c.String(http.StatusOK, "Write operation successful")
83+
}
84+
85+
func handleRead(c *gin.Context) {
86+
stream := c.Param("stream")
87+
response, err := dbClient.Read(dbName, stream)
88+
if err != nil {
89+
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Error reading data: %s", err)})
90+
return
91+
}
92+
c.String(http.StatusOK, response)
93+
}

0 commit comments

Comments
 (0)