-
Notifications
You must be signed in to change notification settings - Fork 6
/
fsstore.go
170 lines (143 loc) · 4.59 KB
/
fsstore.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
package store
import (
"context"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"sync"
"github.com/DanEngelbrecht/golongtail/lib"
)
type fsFileStorage struct {
}
func (fileStorage *fsFileStorage) ReadFromPath(ctx context.Context, path string) ([]byte, error) {
return ioutil.ReadFile(path)
}
func (fileStorage *fsFileStorage) WriteToPath(ctx context.Context, path string, data []byte) error {
err := os.MkdirAll(filepath.Dir(path), os.ModePerm)
if err != nil {
return err
}
return ioutil.WriteFile(path, data, 0644)
}
func (fileStorage *fsFileStorage) Close() {
}
// NewFSFileStorage ...
func NewFSFileStorage() (FileStorage, error) {
s := &fsFileStorage{}
return s, nil
}
type fsPutBlockMessage struct {
storedBlock lib.Longtail_StoredBlock
asyncCompleteAPI lib.Longtail_AsyncPutStoredBlockAPI
}
type fsGetBlockMessage struct {
blockHash uint64
asyncCompleteAPI lib.Longtail_AsyncGetStoredBlockAPI
}
type fsGetIndexMessage struct {
defaultHashAPIIdentifier uint32
asyncCompleteAPI lib.Longtail_AsyncGetIndexAPI
}
type fsStopMessage struct {
}
type fsBlockStore struct {
fsRoot string
fsBlockStore lib.Longtail_BlockStoreAPI
jobAPI lib.Longtail_JobAPI
defaultHashAPI uint32
workerCount int
putBlockChan chan fsPutBlockMessage
getBlockChan chan fsGetBlockMessage
getIndexChan chan fsGetIndexMessage
stopChan chan fsStopMessage
workerWaitGroup sync.WaitGroup
}
// String() ...
func (s *fsBlockStore) String() string {
return s.fsRoot
}
func fsWorker(
s *fsBlockStore,
fsPutBlockMessages <-chan fsPutBlockMessage,
fsGetBlockMessages <-chan fsGetBlockMessage,
fsGetIndexMessages <-chan fsGetIndexMessage,
fsStopMessages <-chan fsStopMessage) error {
run := true
for run {
select {
case putMsg := <-fsPutBlockMessages:
errno := s.fsBlockStore.PutStoredBlock(putMsg.storedBlock, putMsg.asyncCompleteAPI)
if errno != 0 {
log.Printf("WARNING: PutStoredBlock returned: %d", errno)
}
case getMsg := <-fsGetBlockMessages:
errno := s.fsBlockStore.GetStoredBlock(getMsg.blockHash, getMsg.asyncCompleteAPI)
if errno != 0 {
log.Printf("WARNING: GetStoredBlock returned: %d", errno)
}
case getMsg := <-fsGetIndexMessages:
errno := s.fsBlockStore.GetIndex(getMsg.defaultHashAPIIdentifier, getMsg.asyncCompleteAPI)
if errno != 0 {
log.Printf("WARNING: GetStoredBlock returned: %d", errno)
}
case _ = <-fsStopMessages:
run = false
}
}
select {
case putMsg := <-fsPutBlockMessages:
errno := s.fsBlockStore.PutStoredBlock(putMsg.storedBlock, putMsg.asyncCompleteAPI)
if errno != 0 {
log.Panicf("WARNING: putStoredBlock returned: %d", errno)
}
default:
}
s.workerWaitGroup.Done()
return nil
}
// NewFSBlockStore ...
func NewFSBlockStore(path string, defaultHashAPI uint32, jobAPI lib.Longtail_JobAPI) (lib.BlockStoreAPI, error) {
s := &fsBlockStore{fsRoot: path, jobAPI: jobAPI, defaultHashAPI: defaultHashAPI}
storageAPI := lib.CreateFSStorageAPI()
s.fsBlockStore = lib.CreateFSBlockStoreAPI(storageAPI, path)
s.workerCount = runtime.NumCPU() * 4
s.putBlockChan = make(chan fsPutBlockMessage, s.workerCount*4096)
s.getBlockChan = make(chan fsGetBlockMessage, s.workerCount*4096)
s.getIndexChan = make(chan fsGetIndexMessage, 16)
s.stopChan = make(chan fsStopMessage, s.workerCount)
for i := 0; i < s.workerCount; i++ {
go fsWorker(s, s.putBlockChan, s.getBlockChan, s.getIndexChan, s.stopChan)
}
s.workerWaitGroup.Add(s.workerCount)
return s, nil
}
// PutStoredBlock ...
func (s *fsBlockStore) PutStoredBlock(storedBlock lib.Longtail_StoredBlock, asyncCompleteAPI lib.Longtail_AsyncPutStoredBlockAPI) int {
if len(s.putBlockChan) < cap(s.putBlockChan) {
s.putBlockChan <- fsPutBlockMessage{storedBlock: storedBlock, asyncCompleteAPI: asyncCompleteAPI}
return 0
}
return s.fsBlockStore.PutStoredBlock(storedBlock, asyncCompleteAPI)
}
// GetStoredBlock ...
func (s *fsBlockStore) GetStoredBlock(blockHash uint64, asyncCompleteAPI lib.Longtail_AsyncGetStoredBlockAPI) int {
if len(s.getBlockChan) < cap(s.getBlockChan) {
s.getBlockChan <- fsGetBlockMessage{blockHash: blockHash, asyncCompleteAPI: asyncCompleteAPI}
return 0
}
return s.fsBlockStore.GetStoredBlock(blockHash, asyncCompleteAPI)
}
// GetIndex ...
func (s *fsBlockStore) GetIndex(defaultHashAPIIdentifier uint32, asyncCompleteAPI lib.Longtail_AsyncGetIndexAPI) int {
s.getIndexChan <- fsGetIndexMessage{defaultHashAPIIdentifier: defaultHashAPIIdentifier, asyncCompleteAPI: asyncCompleteAPI}
return 0
}
// Close ...
func (s *fsBlockStore) Close() {
for i := 0; i < s.workerCount; i++ {
s.stopChan <- fsStopMessage{}
}
s.workerWaitGroup.Wait()
}