-
Notifications
You must be signed in to change notification settings - Fork 0
/
sia.go
189 lines (155 loc) · 4.03 KB
/
sia.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import "fmt"
import (
ds "github.com/ipfs/go-datastore"
modules "gitlab.com/NebulousLabs/Sia/modules"
//api "gitlab.com/NebulousLabs/Sia/node/api"
"bytes"
//dsq "github.com/ipfs/go-datastore/query"
client "gitlab.com/NebulousLabs/Sia/node/api/client"
"os"
"unicode/utf16"
"unicode/utf8"
)
const (
// listMax is the largest amount of objects you can request from S3 in a list
// call.
listMax = 1000
// deleteMax is the largest amount of objects you can delete from S3 in a
// delete objects call.
deleteMax = 1000
defaultWorkers = 100
)
func main() {
//Test function
//fmt.Printf(string(bytes[:]))
//properBytes, _ := DecodeUTF16(bytes)
//fmt.Printf(properBytes)
//fmt.Printf(fmt.Sprintf("%s", bytes))
//test(ds.RandomKey())
siaStore := NewSiaStore(Config{"", "", "", 0, 0})
key := ds.RandomKey()
siaStore.Put(key, []byte("ABC€"))
fmt.Println(key.String())
bytes, err := siaStore.Get(key)
if err == nil {
fmt.Println(err.Error())
} else {
str, _ := DecodeUTF16(bytes)
fmt.Println("data: " + str)
}
}
func DecodeUTF16(b []byte) (string, error) {
if len(b)%2 != 0 {
return "", fmt.Errorf("Must have even length byte slice")
}
u16s := make([]uint16, 1)
ret := &bytes.Buffer{}
b8buf := make([]byte, 4)
lb := len(b)
for i := 0; i < lb; i += 2 {
u16s[0] = uint16(b[i]) + (uint16(b[i+1]) << 8)
r := utf16.Decode(u16s)
n := utf8.EncodeRune(b8buf, r[0])
ret.Write(b8buf[:n])
}
return ret.String(), nil
}
func test(k ds.Key) {
fmt.Printf(k.String())
}
//Memory cache piece of data.
//Gets deletes from memory after 25 seconds or until garbage collection.
//This is meant only speeding up get/put operations.
//CacheEntry should contain all the K/V pairs.
//CacheEntry is the ENTIRE 4MB sia file block. these may contain many small K/V pairs.
//Must be in some sort of accessible format on the caching layer. Slow sync over to sia.
type CacheEntry struct {
timestamp int
payload []byte
key ds.Key
}
type Config struct {
Address string
APIPassword string
Bucket string //Context we are using
Packing int //How many values should be packed into a block/experimental
Workers int
}
type SiaStore struct {
Config
client client.Client
}
//NewSiaStore Config
func NewSiaStore(conf Config) *SiaStore {
if conf.Address == "" {
conf.Address = "localhost:9980"
}
if conf.APIPassword == "" {
conf.APIPassword = os.Getenv("SIA_API_PASSWORD")
}
clientInt := client.Client{
Address: conf.Address,
Password: conf.APIPassword,
UserAgent: "Sia-Agent",
}
return &SiaStore{
conf,
clientInt,
}
}
func (s *SiaStore) Get(k ds.Key) ([]byte, error) {
path, _ := modules.NewSiaPath(k.String())
bytes, err := s.client.RenterStreamGet(path)
if err != nil {
return nil, err
}
return bytes, nil
}
func (s *SiaStore) Put(k ds.Key, value []byte) error {
reader := bytes.NewReader(value)
path, _ := modules.NewSiaPath(k.String())
err := s.client.RenterUploadStreamPost(reader, path, 10, 12, true)
if err == nil {
return err
}
/*_, err := s.client.PutObject(&s3.PutObjectInput{
Bucket: aws.String(s.Bucket),
Key: aws.String(s.s3Path(k.String())),
Body: bytes.NewReader(value),
})
return parseError(err)*/
return nil
}
func (s *SiaStore) Has(k ds.Key) (exists bool, err error) {
_, err = s.GetSize(k)
if err != nil {
return false, err
}
return true, nil
}
func (s *SiaStore) GetSize(k ds.Key) (size int, err error) {
path, _ := modules.NewSiaPath(k.String())
rf, err := s.client.RenterFileGet(path)
sizeint := rf.File.Filesize
if err != nil {
return 0, err
}
return int(sizeint), nil
}
func (s *SiaStore) Delete(k ds.Key) error {
path, _ := modules.NewSiaPath(k.String())
err := s.client.RenterDeletePost(path)
return err
}
/*func (s *SiaStore) Query(q dsq.Query) (dsq.Results, error) {
if q.Orders != nil || q.Filters != nil {
return nil, fmt.Errorf("siads: filters or orders are not supported")
}
limit := q.Limit + q.Offset
if limit == 0 || limit > listMax {
limit = listMax
}
}*/
/*func NewSiaDatastore(conf Config) (*SiaStore, error) {
}*/