-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_cached.go
More file actions
114 lines (96 loc) · 2.79 KB
/
file_cached.go
File metadata and controls
114 lines (96 loc) · 2.79 KB
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
package anvil
import (
"io"
"sync"
)
// cachedFile a cached anvil file
type cachedFile struct {
*file
closeMux sync.RWMutex
closed bool
}
// Close closes the file.
// This function can be called multiple times.
// This will block until all Read and Write calls return.
func (c *cachedFile) Close() (err error) {
c.closeMux.Lock()
defer c.closeMux.Unlock()
if !c.closed {
err = c.file.cache.free(c.file)
c.closed = true
}
return
}
// Read reads the content of the entry at the given coordinates to a
// a byte slice and returns it.
func (c *cachedFile) Read(x, z uint8) (buf []byte, err error) {
c.closeMux.RLock()
defer c.closeMux.RUnlock()
if c.closed {
return nil, ErrClosed
}
return c.file.Read(x, z)
}
// Read reads the entry at x,z to using the given readFn.
// `readFn` must not retain the [io.Reader] passed to it.
// `readFn` must not return before reading has completed.
func (c *cachedFile) ReadWith(x, z uint8, readFn func(io.Reader) error) (err error) {
c.closeMux.RLock()
defer c.closeMux.RUnlock()
if c.closed {
return ErrClosed
}
_, err = c.file.ReadTo(x, z, &readFromWrapper{fn: readFn})
return
}
// ReadTo reads the entry at x,z to the given [io.ReaderFrom].
// `reader` must not retain the [io.Reader] passed to it.
// `reader` must not return before reading has completed.
func (c *cachedFile) ReadTo(x, z uint8, reader io.ReaderFrom) (n int64, err error) {
c.closeMux.RLock()
defer c.closeMux.RUnlock()
if c.closed {
return 0, ErrClosed
}
return c.file.ReadTo(x, z, reader)
}
// Write updates the data for the entry at x,z to the given buffer.
// The given buffer is compressed and written to the anvil file.
// The compression method used can be changed using the [CompressMethod] method.
// If the data is larger than 1MB after compression, the data is stored externally.
// Calling this function with an empty buffer is the equivalent of calling [File.Remove](x,z).
func (c *cachedFile) Write(x, z uint8, b []byte) (err error) {
c.closeMux.RLock()
defer c.closeMux.RUnlock()
if c.closed {
return ErrClosed
}
return c.file.Write(x, z, b)
}
// Remove removes the given entry from the file.
func (c *cachedFile) Remove(x, z uint8) (err error) {
c.closeMux.RLock()
defer c.closeMux.RUnlock()
if c.closed {
return ErrClosed
}
return c.file.Remove(x, z)
}
// CompressionMethod sets the compression method to be used by the writer.
func (c *cachedFile) CompressionMethod(m CompressMethod) (err error) {
c.closeMux.RLock()
defer c.closeMux.RUnlock()
if c.closed {
return ErrClosed
}
return c.file.CompressionMethod(m)
}
// Info gets information stored in the anvil header for the given entry.
func (c *cachedFile) Info(x, z uint8) (entry Entry, exists bool) {
c.closeMux.RLock()
defer c.closeMux.RUnlock()
if c.closed {
return
}
return c.file.Info(x, z)
}