This repository has been archived by the owner on Jul 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
148 lines (129 loc) · 2.93 KB
/
index.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
/*
Copyright 2012 Sergey Cherepanov (https://github.com/cheggaaa)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package storage
import (
"errors"
"fmt"
"github.com/cheggaaa/Anteater/src/aelog"
"strings"
"sync"
"sync/atomic"
)
var (
ErrConflict = errors.New("Conflict")
)
type Index struct {
Root *Node
m *sync.Mutex
v, c int64
}
func (i *Index) Init() {
i.m = &sync.Mutex{}
i.Root = &Node{}
}
// Add new file to index
func (i *Index) Add(file *File) (err error) {
i.m.Lock()
defer i.m.Unlock()
return i.add(file)
}
func (i *Index) Get(name string) (f *File, ok bool) {
i.m.Lock()
defer i.m.Unlock()
return i.get(name)
}
func (i *Index) Delete(name string) (f *File, ok bool) {
i.m.Lock()
defer i.m.Unlock()
return i.delete(name)
}
func (i *Index) Rename(name, newName string) (f *File, err error) {
i.m.Lock()
defer i.m.Unlock()
// check file
f, ok := i.get(name)
if !ok {
err = ErrFileNotFound
return
}
// check new name
_, ok = i.get(newName)
if ok {
err = ErrConflict
f = nil
return
}
// rename
f.Name = newName
i.delete(name)
if err = i.add(f); err != nil {
// rollback
f.Name = name
i.add(f)
err = fmt.Errorf("Can't rename %s to %s: %v", name, newName, err)
f = nil
return
}
return
}
func (i *Index) List(prefix string, maxnesting int) (names []string, err error) {
parts := make([]string, 0)
if prefix != "" {
parts = i.explode(prefix)
}
return i.Root.List(parts, 0, maxnesting)
}
func (i *Index) Count() int64 {
return atomic.LoadInt64(&i.c)
}
func (i *Index) Version() int64 {
return atomic.LoadInt64(&i.v)
}
func (i *Index) get(name string) (f *File, ok bool) {
parts := i.explode(name)
var err error
if f, err = i.Root.Get(parts, 0); err == nil {
ok = true
return
}
if err != ErrFileNotFound {
aelog.Warnf("Error while get from index. %s: %v", name, err)
}
return
}
func (i *Index) delete(name string) (f *File, ok bool) {
parts := i.explode(name)
var err error
if f, err = i.Root.Delete(parts, 0); err == nil {
ok = true
atomic.AddInt64(&i.v, 1)
atomic.AddInt64(&i.c, -1)
return
}
if err != ErrFileNotFound {
aelog.Warnf("Error while delete from index. %s: %v", name, err)
}
return
}
func (i *Index) add(file *File) (err error) {
parts := i.explode(file.Name)
if err = i.Root.Add(parts, file, 0); err != nil {
return
}
atomic.AddInt64(&i.v, 1)
atomic.AddInt64(&i.c, 1)
return
}
func (i *Index) explode(name string) (parts []string) {
return strings.Split(name, "/")
}