This repository has been archived by the owner on Oct 22, 2021. It is now read-only.
generated from beyondstorage/go-service-example
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobject.go
112 lines (92 loc) · 1.8 KB
/
object.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
package memory
import (
"strings"
"sync"
"github.com/beyondstorage/go-storage/v4/types"
)
type object struct {
mode types.ObjectMode
length int64
name string
parent *object
mu sync.Mutex
child map[string]*object
data []byte
}
func newObject(name string, parent *object, mode types.ObjectMode) *object {
return &object{
mode: mode,
name: name,
parent: parent,
child: make(map[string]*object),
}
}
func (o *object) getChild(name string) *object {
o.mu.Lock()
defer o.mu.Unlock()
x, ok := o.child[name]
if !ok {
return nil
}
return x
}
func (o *object) hasChild(name string) bool {
o.mu.Lock()
defer o.mu.Unlock()
_, ok := o.child[name]
return ok
}
func (o *object) removeChild(name string) {
o.mu.Lock()
defer o.mu.Unlock()
delete(o.child, name)
}
func (o *object) insertChild(name string, c *object) *object {
o.mu.Lock()
defer o.mu.Unlock()
o.child[name] = c
return c
}
func (o *object) getObjectByPath(path string) (ro *object) {
ro = o
ps := strings.Split(path, "/")
for _, v := range ps {
if v == "" {
continue
}
ro = ro.getChild(v)
if ro == nil {
return nil
}
}
return ro
}
func (o *object) insertChildByPath(path string) *object {
p := o
ps := strings.Split(path, "/")
last := len(ps) - 1
p = o.makeDirAll(ps[:last])
if p == nil {
return nil
}
return p.insertChild(ps[last], newObject(ps[last], p, types.ModeRead))
}
func (o *object) makeDirAll(ps []string) *object {
p := o
for _, v := range ps {
if v == "" {
continue
}
ro := p.getChild(v)
// If child not exist, we can create a new dir object.
if ro == nil {
ro = p.insertChild(v, newObject(v, p, types.ModeDir))
}
// If child exist but not a dir, we should return false to indict failed.
if !ro.mode.IsDir() {
return nil
}
p = ro
}
return p
}