Skip to content

Commit f92bd1d

Browse files
committed
refactor: move file to pool package
1 parent 38f6076 commit f92bd1d

5 files changed

+32
-36
lines changed

archiver.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313

1414
"github.com/klauspost/compress/flate"
1515
"github.com/pkg/errors"
16-
filebuffer "github.com/pzip/file_buffer"
1716
"github.com/pzip/pool"
1817
)
1918

@@ -26,8 +25,8 @@ type Archiver struct {
2625
Dest *os.File
2726
w *zip.Writer
2827
numberOfWorkers int
29-
fileProcessPool pool.WorkerPool[filebuffer.File]
30-
fileWriterPool pool.WorkerPool[filebuffer.File]
28+
fileProcessPool pool.WorkerPool[pool.File]
29+
fileWriterPool pool.WorkerPool[pool.File]
3130
chroot string
3231
}
3332

@@ -37,7 +36,7 @@ func NewArchiver(archive *os.File) (*Archiver, error) {
3736
numberOfWorkers: runtime.GOMAXPROCS(0),
3837
}
3938

40-
fileProcessExecutor := func(file filebuffer.File) {
39+
fileProcessExecutor := func(file pool.File) {
4140
if !file.Info.IsDir() {
4241
a.compress(&file)
4342
}
@@ -53,7 +52,7 @@ func NewArchiver(archive *os.File) (*Archiver, error) {
5352
}
5453
a.fileProcessPool = fileProcessPool
5554

56-
fileWriterExecutor := func(file filebuffer.File) {
55+
fileWriterExecutor := func(file pool.File) {
5756
a.archive(&file)
5857
}
5958

@@ -90,7 +89,7 @@ func (a *Archiver) ArchiveFiles(files ...string) error {
9089
return errors.Errorf("ERROR: could not get stat of %s", path)
9190
}
9291

93-
f := filebuffer.File{Path: path, Info: info}
92+
f := pool.File{Path: path, Info: info}
9493
a.fileProcessPool.Enqueue(f)
9594
}
9695

@@ -137,7 +136,7 @@ func (a *Archiver) walkDir() error {
137136
return errors.Errorf("ERROR: could not determine relative path of %s", path)
138137
}
139138

140-
f := filebuffer.File{Path: relativeToRoot, Info: info}
139+
f := pool.File{Path: relativeToRoot, Info: info}
141140
a.fileProcessPool.Enqueue(f)
142141
return nil
143142
})
@@ -152,7 +151,7 @@ func (a *Archiver) walkDir() error {
152151
return nil
153152
}
154153

155-
func (a *Archiver) compress(file *filebuffer.File) error {
154+
func (a *Archiver) compress(file *pool.File) error {
156155
buf := bytes.Buffer{}
157156
err := a.compressToBuffer(&buf, file)
158157
if err != nil {
@@ -162,7 +161,7 @@ func (a *Archiver) compress(file *filebuffer.File) error {
162161
return nil
163162
}
164163

165-
func (a *Archiver) compressToBuffer(buf *bytes.Buffer, file *filebuffer.File) error {
164+
func (a *Archiver) compressToBuffer(buf *bytes.Buffer, file *pool.File) error {
166165
f, err := os.Open(file.Path)
167166
if err != nil {
168167
return errors.Errorf("ERROR: could not open file %s", file.Path)
@@ -189,7 +188,7 @@ func (a *Archiver) compressToBuffer(buf *bytes.Buffer, file *filebuffer.File) er
189188
return nil
190189
}
191190

192-
func (a *Archiver) createHeader(file *filebuffer.File) error {
191+
func (a *Archiver) createHeader(file *pool.File) error {
193192
header, err := zip.FileInfoHeader(file.Info)
194193
if err != nil {
195194
return errors.Errorf("ERROR: could not create file header for %s", file.Path)
@@ -240,7 +239,7 @@ func (a *Archiver) dirArchive() bool {
240239
return a.chroot != ""
241240
}
242241

243-
func (a *Archiver) archive(f *filebuffer.File) error {
242+
func (a *Archiver) archive(f *pool.File) error {
244243
fileWriter, err := a.w.CreateRaw(f.Header)
245244
if err != nil {
246245
return errors.Errorf("ERROR: could not write raw header for %s", f.Path)

archiver_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"time"
1111

1212
"github.com/alecthomas/assert/v2"
13-
filebuffer "github.com/pzip/file_buffer"
1413
"github.com/pzip/internal/testutils"
14+
"github.com/pzip/pool"
1515
)
1616

1717
const (
@@ -126,7 +126,7 @@ func TestCompressToBuffer(t *testing.T) {
126126
archiver, err := NewArchiver(archive)
127127
assert.NoError(t, err)
128128
info := testutils.GetFileInfo(t, helloTxtFileFixture)
129-
file := filebuffer.File{Path: helloTxtFileFixture, Info: info}
129+
file := pool.File{Path: helloTxtFileFixture, Info: info}
130130

131131
buf := bytes.Buffer{}
132132
archiver.compressToBuffer(&buf, &file)
@@ -150,7 +150,7 @@ func TestFileWriter(t *testing.T) {
150150

151151
absPath, err := filepath.Abs(helloTxtFileFixture)
152152
assert.NoError(t, err)
153-
file := filebuffer.File{Path: absPath, Info: info}
153+
file := pool.File{Path: absPath, Info: info}
154154

155155
archiver.createHeader(&file)
156156

@@ -165,7 +165,7 @@ func TestFileWriter(t *testing.T) {
165165
assert.NoError(t, err)
166166

167167
info := testutils.GetFileInfo(t, helloTxtFileFixture)
168-
file := filebuffer.File{Path: helloTxtFileFixture, Info: info}
168+
file := pool.File{Path: helloTxtFileFixture, Info: info}
169169

170170
archiver.createHeader(&file)
171171

@@ -183,7 +183,7 @@ func TestFileWriter(t *testing.T) {
183183
filePath := "nested/hello.md"
184184

185185
info := testutils.GetFileInfo(t, filepath.Join(archiver.chroot, filePath))
186-
file := filebuffer.File{Path: filePath, Info: info}
186+
file := pool.File{Path: filePath, Info: info}
187187

188188
archiver.createHeader(&file)
189189

@@ -198,7 +198,7 @@ func TestFileWriter(t *testing.T) {
198198
assert.NoError(t, err)
199199

200200
info := testutils.GetFileInfo(t, helloTxtFileFixture)
201-
file := filebuffer.File{Path: helloTxtFileFixture, Info: info}
201+
file := pool.File{Path: helloTxtFileFixture, Info: info}
202202
archiver.compress(&file)
203203

204204
archiver.createHeader(&file)
@@ -222,7 +222,7 @@ func TestFileWriter(t *testing.T) {
222222
assert.NoError(t, err)
223223

224224
info := testutils.GetFileInfo(t, filepath.Join(helloDirectoryFixture, "/nested"))
225-
file := filebuffer.File{Path: filepath.Join(helloDirectoryFixture, "/nested"), Info: info}
225+
file := pool.File{Path: filepath.Join(helloDirectoryFixture, "/nested"), Info: info}
226226

227227
archiver.createHeader(&file)
228228

file_buffer/file.go pool/file.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package filebuffer
1+
package pool
22

33
import (
44
"archive/zip"

pool/file_worker_pool.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package pool
33
import (
44
"errors"
55
"sync"
6-
7-
filebuffer "github.com/pzip/file_buffer"
86
)
97

108
const (
@@ -13,19 +11,19 @@ const (
1311
)
1412

1513
type FileWorkerPool struct {
16-
tasks chan filebuffer.File
17-
executor func(f filebuffer.File)
14+
tasks chan File
15+
executor func(f File)
1816
wg *sync.WaitGroup
1917
numberOfWorkers int
2018
}
2119

22-
func NewFileWorkerPool(numberOfWorkers int, executor func(f filebuffer.File)) (*FileWorkerPool, error) {
20+
func NewFileWorkerPool(numberOfWorkers int, executor func(f File)) (*FileWorkerPool, error) {
2321
if numberOfWorkers < minNumberOfWorkers {
2422
return nil, errors.New("number of workers must be greater than 0")
2523
}
2624

2725
return &FileWorkerPool{
28-
tasks: make(chan filebuffer.File, capacity),
26+
tasks: make(chan File, capacity),
2927
executor: executor,
3028
wg: new(sync.WaitGroup),
3129
numberOfWorkers: numberOfWorkers,
@@ -57,10 +55,10 @@ func (f FileWorkerPool) PendingFiles() int {
5755
return len(f.tasks)
5856
}
5957

60-
func (f *FileWorkerPool) Enqueue(file filebuffer.File) {
58+
func (f *FileWorkerPool) Enqueue(file File) {
6159
f.tasks <- file
6260
}
6361

6462
func (f *FileWorkerPool) reset() {
65-
f.tasks = make(chan filebuffer.File)
63+
f.tasks = make(chan File)
6664
}

pool/file_worker_pool_test.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"time"
77

88
"github.com/alecthomas/assert/v2"
9-
filebuffer "github.com/pzip/file_buffer"
109
"github.com/pzip/internal/testutils"
1110
"github.com/pzip/pool"
1211
)
@@ -21,18 +20,18 @@ const (
2120

2221
func TestFileWorkerPool(t *testing.T) {
2322
t.Run("can enqueue tasks", func(t *testing.T) {
24-
fileProcessPool, err := pool.NewFileWorkerPool(1, func(f filebuffer.File) {})
23+
fileProcessPool, err := pool.NewFileWorkerPool(1, func(f pool.File) {})
2524
assert.NoError(t, err)
2625

2726
info := testutils.GetFileInfo(t, helloTxtFileFixture)
28-
fileProcessPool.Enqueue(filebuffer.File{Path: helloTxtFileFixture, Info: info})
27+
fileProcessPool.Enqueue(pool.File{Path: helloTxtFileFixture, Info: info})
2928

3029
assert.Equal(t, 1, fileProcessPool.PendingFiles())
3130
})
3231

3332
t.Run("has workers process files to completion", func(t *testing.T) {
3433
output := bytes.Buffer{}
35-
executor := func(_ filebuffer.File) {
34+
executor := func(_ pool.File) {
3635
time.Sleep(5 * time.Millisecond)
3736
output.WriteString("hello, world!")
3837
}
@@ -42,7 +41,7 @@ func TestFileWorkerPool(t *testing.T) {
4241
fileProcessPool.Start()
4342

4443
info := testutils.GetFileInfo(t, helloTxtFileFixture)
45-
fileProcessPool.Enqueue(filebuffer.File{Path: helloTxtFileFixture, Info: info})
44+
fileProcessPool.Enqueue(pool.File{Path: helloTxtFileFixture, Info: info})
4645

4746
fileProcessPool.Close()
4847

@@ -51,15 +50,15 @@ func TestFileWorkerPool(t *testing.T) {
5150
})
5251

5352
t.Run("returns an error if number of workers is less than one", func(t *testing.T) {
54-
executor := func(_ filebuffer.File) {
53+
executor := func(_ pool.File) {
5554
}
5655
_, err := pool.NewFileWorkerPool(0, executor)
5756
assert.Error(t, err)
5857
})
5958

6059
t.Run("can be closed and restarted", func(t *testing.T) {
6160
output := bytes.Buffer{}
62-
executor := func(_ filebuffer.File) {
61+
executor := func(_ pool.File) {
6362
output.WriteString("hello ")
6463
}
6564

@@ -68,13 +67,13 @@ func TestFileWorkerPool(t *testing.T) {
6867
fileProcessPool.Start()
6968

7069
info := testutils.GetFileInfo(t, helloTxtFileFixture)
71-
fileProcessPool.Enqueue(filebuffer.File{Path: helloTxtFileFixture, Info: info})
70+
fileProcessPool.Enqueue(pool.File{Path: helloTxtFileFixture, Info: info})
7271

7372
fileProcessPool.Close()
7473

7574
fileProcessPool.Start()
7675
info = testutils.GetFileInfo(t, helloTxtFileFixture)
77-
fileProcessPool.Enqueue(filebuffer.File{Path: helloTxtFileFixture, Info: info})
76+
fileProcessPool.Enqueue(pool.File{Path: helloTxtFileFixture, Info: info})
7877

7978
fileProcessPool.Close()
8079

0 commit comments

Comments
 (0)