-
Notifications
You must be signed in to change notification settings - Fork 0
/
local.go
132 lines (116 loc) · 3.23 KB
/
local.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Copyright (c) 2023, Dana Burkart <dana.burkart@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
package fossil
import (
"errors"
"fmt"
"github.com/dburkart/fossil/pkg/database"
"github.com/dburkart/fossil/pkg/proto"
"github.com/dburkart/fossil/pkg/server"
)
type LocalClient struct {
target proto.ConnectionString
db *database.Database
}
func (client *LocalClient) Open(target proto.ConnectionString, _ uint) error {
var err error
client.target = target
client.db, err = database.NewDatabase(target.Address, target.Database)
if err != nil {
return err
}
return nil
}
func (client *LocalClient) Close() error {
return nil
}
func (client *LocalClient) Send(message proto.Message) (proto.Message, error) {
switch message.Command() {
case proto.CommandVersion:
var versionReq proto.VersionRequest
err := proto.Unmarshal(message.Data(), &versionReq)
if err != nil {
return proto.MessageErrorUnmarshaling, nil
}
return server.VersionResponse(versionReq), nil
case proto.CommandList:
var listReq proto.ListRequest
err := proto.Unmarshal(message.Data(), &listReq)
if err != nil {
return proto.MessageErrorUnmarshaling, nil
}
return server.ListResponse(listReq, client.db, nil), nil
case proto.CommandAppend:
var appendReq proto.AppendRequest
err := proto.Unmarshal(message.Data(), &appendReq)
if err != nil {
return proto.MessageErrorUnmarshaling, nil
}
return server.AppendResponse(appendReq, client.db), nil
case proto.CommandQuery:
var queryReq proto.QueryRequest
err := proto.Unmarshal(message.Data(), &queryReq)
if err != nil {
return proto.MessageErrorUnmarshaling, nil
}
return server.QueryResponse(queryReq, client.db), nil
case proto.CommandCreate:
var createReq proto.CreateTopicRequest
err := proto.Unmarshal(message.Data(), &createReq)
if err != nil {
return proto.MessageErrorUnmarshaling, nil
}
return server.CreateResponse(createReq, client.db), nil
case proto.CommandStats:
return proto.NewMessageWithType(
proto.CommandError,
proto.ErrResponse{Code: 404, Err: errors.New("Stats request not supported in local mode")},
), nil
case proto.CommandUse:
return proto.NewMessageWithType(
proto.CommandError,
proto.ErrResponse{Code: 404, Err: errors.New("Use request not supported in local mode")},
), nil
default:
return proto.NewMessageWithType(
proto.CommandError,
proto.ErrResponse{Code: 404, Err: errors.New(fmt.Sprintf("Unknown command: %s", message.Command()))},
), nil
}
}
func (client *LocalClient) Append(topic string, data []byte) error {
appendMsg := proto.NewMessageWithType(proto.CommandAppend,
proto.AppendRequest{
Topic: topic,
Data: data,
})
resp, err := client.Send(appendMsg)
if err != nil {
return err
}
ok := proto.OkResponse{}
err = ok.Unmarshal(resp.Data())
if err != nil {
return err
}
return nil
}
func (client *LocalClient) Query(q string) (database.Entries, error) {
queryMsg := proto.NewMessageWithType(proto.CommandQuery,
proto.QueryRequest{
Query: q,
})
resp, err := client.Send(queryMsg)
if err != nil {
return nil, err
}
queryResponse := proto.QueryResponse{}
err = queryResponse.Unmarshal(resp.Data())
if err != nil {
return nil, err
}
return queryResponse.Results, nil
}