forked from commaai/minikeyvalue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.go
More file actions
96 lines (85 loc) · 2.21 KB
/
Copy pathlib.go
File metadata and controls
96 lines (85 loc) · 2.21 KB
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
package main
import (
"bytes"
"errors"
"encoding/base64"
"crypto/md5"
"fmt"
"io"
"io/ioutil"
"net/http"
)
// *** Hash Functions ***
func key2path(key []byte) string {
mkey := md5.Sum(key)
b64key := base64.StdEncoding.EncodeToString(key)
// 2 byte layers deep, meaning a fanout of 256
// optimized for 2^24 = 16M files per volume server
return fmt.Sprintf("/%02x/%02x/%s", mkey[0], mkey[1], b64key)
}
func key2volume(key []byte, volumes []string) string {
// this is an intelligent way to pick the volume server for a file
// stable in the volume server name (not position!)
// and if more are added the correct portion will move (yay md5!)
var best_score []byte = nil
var ret string = ""
for _, v := range volumes {
hash := md5.New()
hash.Write(key)
hash.Write([]byte(v))
score := hash.Sum(nil)
if best_score == nil || bytes.Compare(best_score, score) == -1 {
best_score = score
ret = v
}
}
//fmt.Println(string(key), ret, best_score)
return ret
}
// *** Remote Access Functions ***
func remote_delete(remote string) error {
req, err := http.NewRequest("DELETE", remote, nil)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 204 {
return fmt.Errorf("remote_delete: wrong status code %d", resp.StatusCode)
}
return nil
}
func remote_put(remote string, length int64, body io.Reader) error {
req, err := http.NewRequest("PUT", remote, body)
if err != nil {
return err
}
req.ContentLength = length
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 201 && resp.StatusCode != 204 {
return fmt.Errorf("remote_put: wrong status code %d", resp.StatusCode)
}
return nil
}
func remote_get(remote string) (string, error) {
resp, err := http.Get(remote)
if err != nil {
return "", err
}
if resp.StatusCode != 200 {
return "", errors.New(fmt.Sprintf("remote_get: wrong status code %d", resp.StatusCode))
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}