Skip to content

Commit 9cc2898

Browse files
committed
v0.3.0 data server change
1 parent 973bdd9 commit 9cc2898

File tree

8 files changed

+247
-143
lines changed

8 files changed

+247
-143
lines changed

‎distributed/data/temp/temp.go‎

Lines changed: 2 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,20 @@ import (
55
"github.com/GuoYuefei/DOStorage1/distributed/config"
66
"github.com/GuoYuefei/DOStorage1/distributed/data/locate"
77
"github.com/GuoYuefei/DOStorage1/distributed/utils"
8-
"github.com/google/uuid"
9-
"io"
108
"io/ioutil"
119
"net/http"
1210
"net/url"
1311
"os"
1412
"path"
15-
"strconv"
16-
"strings"
1713
"time"
1814
)
1915

20-
type tempInfo struct {
21-
Uuid string
22-
Name string // is hash
23-
Size int64
24-
}
25-
26-
func (t *tempInfo) hash() string {
27-
s := strings.Split(t.Name, ".")
28-
return s[0]
29-
}
30-
31-
func (t *tempInfo) id() int {
32-
s := strings.Split(t.Name, ".")
33-
id, _ := strconv.Atoi(s[1])
34-
return id
35-
}
36-
37-
func (t *tempInfo) writeToFile() error {
38-
f, e := os.Create(path.Join(config.TempRoot, t.Uuid))
39-
if e != nil {
40-
return e
41-
}
42-
defer f.Close()
43-
b, _ := json.Marshal(t)
44-
f.Write(b)
45-
return nil
46-
}
47-
4816
func Handler(w http.ResponseWriter, r *http.Request) {
4917
m := r.Method
5018
utils.Log.Printf(utils.Debug, "will call function temp.%s", m)
5119
switch m {
20+
case http.MethodHead: head(w, r)
21+
case http.MethodGet: get(w, r)
5222
case http.MethodPut: put(w, r)
5323
case http.MethodPatch: patch(w, r)
5424
case http.MethodPost: post(w, r)
@@ -58,117 +28,6 @@ func Handler(w http.ResponseWriter, r *http.Request) {
5828
}
5929
}
6030

61-
func post(w http.ResponseWriter, r *http.Request) {
62-
uu := uuid.New().String()
63-
name := strings.Split(r.URL.EscapedPath(), "/")[2]
64-
size, e := strconv.ParseInt(r.Header.Get("size"), 0, 64)
65-
if e != nil {
66-
utils.Log.Println(utils.Err, e)
67-
w.WriteHeader(http.StatusInternalServerError)
68-
return
69-
}
70-
t := tempInfo{
71-
uu,
72-
name,
73-
size,
74-
}
75-
e = t.writeToFile()
76-
if e != nil {
77-
utils.Log.Println(utils.Err, e)
78-
w.WriteHeader(http.StatusInternalServerError)
79-
return
80-
}
81-
_, e = os.Create(path.Join(config.TempRoot, t.Uuid+".dat"))
82-
if e != nil {
83-
utils.Log.Println(utils.Err, e)
84-
w.WriteHeader(http.StatusInternalServerError)
85-
return
86-
}
87-
w.Write([]byte(uu))
88-
}
89-
90-
func patch(w http.ResponseWriter, r *http.Request) {
91-
uu := strings.Split(r.URL.EscapedPath(), "/")[2]
92-
tempinfo, e := readFromFile(uu)
93-
if e != nil {
94-
utils.Log.Println(utils.Err, e)
95-
w.WriteHeader(http.StatusNotFound)
96-
return
97-
}
98-
infoFile := path.Join(config.TempRoot, uu)
99-
datFile := infoFile+".dat"
100-
f, e := os.OpenFile(datFile, os.O_WRONLY|os.O_APPEND, 0)
101-
//f, e := os.Create(datFile)
102-
if e != nil {
103-
utils.Log.Println(utils.Err, e)
104-
w.WriteHeader(http.StatusInternalServerError)
105-
return
106-
}
107-
defer f.Close()
108-
_, e = io.Copy(f, r.Body)
109-
if e != nil {
110-
utils.Log.Println(utils.Err, e)
111-
w.WriteHeader(http.StatusInternalServerError)
112-
return
113-
}
114-
info, e := f.Stat()
115-
if e != nil {
116-
utils.Log.Println(utils.Err, e)
117-
w.WriteHeader(http.StatusInternalServerError)
118-
return
119-
}
120-
actual := info.Size()
121-
if actual > tempinfo.Size {
122-
os.Remove(datFile)
123-
os.Remove(infoFile)
124-
utils.Log.Printf(utils.Debug, "actual size %d, exceeds %d", actual, tempinfo.Size)
125-
w.WriteHeader(http.StatusInternalServerError)
126-
}
127-
}
128-
129-
func put(w http.ResponseWriter, r *http.Request) {
130-
uu := strings.Split(r.URL.EscapedPath(), "/")[2]
131-
tempinfo, e := readFromFile(uu)
132-
if e != nil {
133-
utils.Log.Println(utils.Err, e)
134-
w.WriteHeader(http.StatusNotFound)
135-
return
136-
}
137-
infoFile := path.Join(config.TempRoot, uu)
138-
datFile := infoFile+".dat"
139-
f, e := os.OpenFile(datFile, os.O_WRONLY|os.O_APPEND, 0)
140-
if e != nil {
141-
utils.Log.Println(utils.Err, e)
142-
w.WriteHeader(http.StatusInternalServerError)
143-
return
144-
}
145-
defer f.Close()
146-
info, e := f.Stat()
147-
if e != nil {
148-
utils.Log.Println(utils.Err, e)
149-
w.WriteHeader(http.StatusInternalServerError)
150-
return
151-
}
152-
actual := info.Size()
153-
os.Remove(infoFile)
154-
if actual != tempinfo.Size {
155-
os.Remove(datFile)
156-
utils.Log.Printf(utils.Info, "actual size %d, exceeds %d", actual, tempinfo.Size)
157-
w.WriteHeader(http.StatusInternalServerError)
158-
return
159-
}
160-
commitTempObject(datFile, tempinfo)
161-
w.WriteHeader(http.StatusAccepted)
162-
}
163-
164-
func delete(_ http.ResponseWriter, r *http.Request) {
165-
uu := strings.Split(r.URL.EscapedPath(), "/")[2]
166-
infoFile := path.Join(config.TempRoot, uu)
167-
datFile := infoFile + ".dat"
168-
os.Remove(infoFile)
169-
os.Remove(datFile)
170-
}
171-
17231
func readFromFile(uu string) (*tempInfo, error) {
17332
f, e := os.Open(path.Join(config.TempRoot, uu))
17433
if e != nil {

‎distributed/data/temp/temp_del.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package temp
2+
3+
import (
4+
"github.com/GuoYuefei/DOStorage1/distributed/config"
5+
"net/http"
6+
"os"
7+
"path"
8+
"strings"
9+
)
10+
11+
func delete(_ http.ResponseWriter, r *http.Request) {
12+
uu := strings.Split(r.URL.EscapedPath(), "/")[2]
13+
infoFile := path.Join(config.TempRoot, uu)
14+
datFile := infoFile + ".dat"
15+
os.Remove(infoFile)
16+
os.Remove(datFile)
17+
}

‎distributed/data/temp/temp_get.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package temp
2+
3+
import (
4+
"github.com/GuoYuefei/DOStorage1/distributed/config"
5+
"github.com/GuoYuefei/DOStorage1/distributed/utils"
6+
"io"
7+
"net/http"
8+
"os"
9+
"path"
10+
"strings"
11+
)
12+
13+
func get(w http.ResponseWriter, r *http.Request) {
14+
uuid := strings.Split(r.URL.EscapedPath(), "/")[2]
15+
f, e := os.Open(path.Join(config.ServerData.STORAGE_ROOT, "temp", uuid+".dat"))
16+
if e != nil {
17+
utils.Log.Println(utils.Err, e)
18+
w.WriteHeader(http.StatusNotFound)
19+
return
20+
}
21+
defer f.Close()
22+
io.Copy(w, f)
23+
}
24+

‎distributed/data/temp/temp_head.go‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package temp
2+
3+
import (
4+
"fmt"
5+
"github.com/GuoYuefei/DOStorage1/distributed/config"
6+
"github.com/GuoYuefei/DOStorage1/distributed/utils"
7+
"net/http"
8+
"os"
9+
"path"
10+
"strings"
11+
)
12+
13+
func head(w http.ResponseWriter, r *http.Request) {
14+
uuid := strings.Split(r.URL.EscapedPath(), "/")[2]
15+
// todo 可以选择不打开文件, 而直接stat
16+
f, e := os.Open(path.Join(config.ServerData.STORAGE_ROOT, "temp", uuid+".dat"))
17+
if e != nil {
18+
utils.Log.Println(utils.Err, e)
19+
w.WriteHeader(http.StatusNotFound)
20+
return
21+
}
22+
defer f.Close()
23+
info, e := f.Stat()
24+
if e != nil {
25+
utils.Log.Println(utils.Err, e)
26+
w.WriteHeader(http.StatusInternalServerError)
27+
return
28+
}
29+
w.Header().Set("content-length", fmt.Sprintf("%d", info.Size()))
30+
}

‎distributed/data/temp/temp_info.go‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package temp
2+
3+
import (
4+
"encoding/json"
5+
"github.com/GuoYuefei/DOStorage1/distributed/config"
6+
"os"
7+
"path"
8+
"strconv"
9+
"strings"
10+
)
11+
12+
type tempInfo struct {
13+
Uuid string
14+
Name string // is hash
15+
Size int64
16+
}
17+
18+
func (t *tempInfo) hash() string {
19+
s := strings.Split(t.Name, ".")
20+
return s[0]
21+
}
22+
23+
func (t *tempInfo) id() int {
24+
s := strings.Split(t.Name, ".")
25+
id, _ := strconv.Atoi(s[1])
26+
return id
27+
}
28+
29+
func (t *tempInfo) writeToFile() error {
30+
f, e := os.Create(path.Join(config.TempRoot, t.Uuid))
31+
if e != nil {
32+
return e
33+
}
34+
defer f.Close()
35+
b, _ := json.Marshal(t)
36+
f.Write(b)
37+
return nil
38+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package temp
2+
3+
import (
4+
"github.com/GuoYuefei/DOStorage1/distributed/config"
5+
"github.com/GuoYuefei/DOStorage1/distributed/utils"
6+
"io"
7+
"net/http"
8+
"os"
9+
"path"
10+
"strings"
11+
)
12+
13+
func patch(w http.ResponseWriter, r *http.Request) {
14+
uu := strings.Split(r.URL.EscapedPath(), "/")[2]
15+
tempinfo, e := readFromFile(uu)
16+
if e != nil {
17+
utils.Log.Println(utils.Err, e)
18+
w.WriteHeader(http.StatusNotFound)
19+
return
20+
}
21+
infoFile := path.Join(config.TempRoot, uu)
22+
datFile := infoFile+".dat"
23+
f, e := os.OpenFile(datFile, os.O_WRONLY|os.O_APPEND, 0)
24+
//f, e := os.Create(datFile)
25+
if e != nil {
26+
utils.Log.Println(utils.Err, e)
27+
w.WriteHeader(http.StatusInternalServerError)
28+
return
29+
}
30+
defer f.Close()
31+
_, e = io.Copy(f, r.Body)
32+
if e != nil {
33+
utils.Log.Println(utils.Err, e)
34+
w.WriteHeader(http.StatusInternalServerError)
35+
return
36+
}
37+
info, e := f.Stat()
38+
if e != nil {
39+
utils.Log.Println(utils.Err, e)
40+
w.WriteHeader(http.StatusInternalServerError)
41+
return
42+
}
43+
actual := info.Size()
44+
if actual > tempinfo.Size {
45+
os.Remove(datFile)
46+
os.Remove(infoFile)
47+
utils.Log.Printf(utils.Debug, "actual size %d, exceeds %d", actual, tempinfo.Size)
48+
w.WriteHeader(http.StatusInternalServerError)
49+
}
50+
}

‎distributed/data/temp/temp_post.go‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package temp
2+
3+
import (
4+
"github.com/GuoYuefei/DOStorage1/distributed/config"
5+
"github.com/GuoYuefei/DOStorage1/distributed/utils"
6+
"github.com/google/uuid"
7+
"net/http"
8+
"os"
9+
"path"
10+
"strconv"
11+
"strings"
12+
)
13+
14+
func post(w http.ResponseWriter, r *http.Request) {
15+
uu := uuid.New().String()
16+
name := strings.Split(r.URL.EscapedPath(), "/")[2]
17+
size, e := strconv.ParseInt(r.Header.Get("size"), 0, 64)
18+
if e != nil {
19+
utils.Log.Println(utils.Err, e)
20+
w.WriteHeader(http.StatusInternalServerError)
21+
return
22+
}
23+
t := tempInfo{
24+
uu,
25+
name,
26+
size,
27+
}
28+
e = t.writeToFile()
29+
if e != nil {
30+
utils.Log.Println(utils.Err, e)
31+
w.WriteHeader(http.StatusInternalServerError)
32+
return
33+
}
34+
_, e = os.Create(path.Join(config.TempRoot, t.Uuid+".dat"))
35+
if e != nil {
36+
utils.Log.Println(utils.Err, e)
37+
w.WriteHeader(http.StatusInternalServerError)
38+
return
39+
}
40+
w.Write([]byte(uu))
41+
}

0 commit comments

Comments
 (0)