-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanvil.go
More file actions
321 lines (269 loc) · 8.01 KB
/
anvil.go
File metadata and controls
321 lines (269 loc) · 8.01 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package anvil
import (
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"sync"
lru "github.com/hashicorp/golang-lru/v2/simplelru"
"github.com/spf13/afero"
"github.com/yehan2002/errors"
)
const (
// ErrExternal returned if the is in an external file.
// This error is only returned if the entry anvil file was opened as a single file.
ErrExternal = errors.Const("anvil: entry is in separate file")
// ErrNotExist returned if the entry does not exist.
ErrNotExist = errors.Const("anvil: entry does not exist")
// ErrSize returned if the size of the anvil file is not a multiple of [SectionSize].
ErrSize = errors.Const("anvil: invalid file size")
// ErrCorrupted the given file contains invalid/corrupted data
ErrCorrupted = errors.Const("anvil: corrupted file")
// ErrClosed the given file has already been closed
ErrClosed = errors.Const("anvil: file closed")
// ErrReadOnly the file was opened in readonly mode.
ErrReadOnly = errors.Const("anvil: file is opened in read-only mode")
)
const (
sectionSizeMask = SectionSize - 1
sectionShift = 12
entryHeaderSize = 5
// Entries the number of Entries in a anvil file
Entries = 32 * 32
// SectionSize the size of a section
SectionSize = 1 << sectionShift
// MaxFileSections the maximum number of sections a file can contain
MaxFileSections = 255 * Entries
)
// Settings settings
type Settings struct {
// Readonly if the file should be opened in readonly mode.
// If this is set, all write operation will return [ErrReadOnly].
// Default: false
ReadOnly bool
// Sync if the file should be opened for synchronous I/O.
// Default: false
Sync bool
// The cache size for [Anvil].
// If this value is -1 the cache will be disabled.
// Default: 20
CacheSize int
// The formatting string to be used to generate the file name for an anvil file
AnvilFmt string
// The formatting string to be used to generate the file name for a chunk that is stored
// separately from and anvil file.
ChunkFmt string
fs afero.Fs
}
var filesystem afero.Fs = &afero.OsFs{}
var defaultSettings = Settings{
CacheSize: 20,
AnvilFmt: "r.%d.%d.mca",
ChunkFmt: "c.%d.%d.mcc",
fs: filesystem,
}
// Anvil a anvil file cache.
type Anvil struct {
inUse map[pos]*file
lru *lru.LRU[pos, *file]
settings Settings
mux sync.RWMutex
}
// Read reads the content of the entry at the given coordinates to a
// a byte slice and returns it.
func (a *Anvil) Read(entryX, entryZ int32) (buf []byte, err error) {
var f *file
if f, err = a.get(entryX>>5, entryZ>>5); err == nil {
defer func() {
if closeErr := a.free(f); closeErr != nil && err != nil {
err = closeErr
}
}()
buf, err = f.Read(uint8(entryX&0x1f), uint8(entryZ&0x1f))
}
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 (a *Anvil) ReadTo(entryX, entryZ int32, reader io.ReaderFrom) (n int64, err error) {
var f *file
if f, err = a.get(entryX>>5, entryZ>>5); err == nil {
defer func() {
if closeErr := a.free(f); closeErr != nil && err != nil {
err = closeErr
}
}()
n, err = f.ReadTo(uint8(entryX&0x1f), uint8(entryZ&0x1f), reader)
}
return
}
// ReadFn 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 (a *Anvil) ReadFn(entryX, entryZ int32, readFn func(io.Reader) error) (err error) {
var f *file
if f, err = a.get(entryX>>5, entryZ>>5); err == nil {
defer func() {
if closeErr := a.free(f); closeErr != nil && err != nil {
err = closeErr
}
}()
err = f.ReadWith(uint8(entryX&0x1f), uint8(entryZ&0x1f), readFn)
}
return
}
// Write writes the chunk data for the given location
func (a *Anvil) Write(entryX, entryZ int32, p []byte) (err error) {
var f *file
if f, err = a.get(entryX>>5, entryZ>>5); err == nil {
defer func() {
if closeErr := a.free(f); closeErr != nil && err != nil {
err = closeErr
}
}()
err = f.Write(uint8(entryX&0x1f), uint8(entryZ&0x1f), p)
}
return
}
// Info gets information stored in the anvil header for the given entry.
func (a *Anvil) Info(entryX, entryZ int32) (entry Entry, exists bool, err error) {
var f *file
if f, err = a.get(entryX>>5, entryZ>>5); err == nil {
defer func() {
if closeErr := a.free(f); closeErr != nil && err != nil {
err = closeErr
}
}()
entry, exists = f.Info(uint8(entryX&0x1f), uint8(entryZ&0x1f))
}
return
}
// File opens the anvil file at rgX, rgZ.
// Callers must close the returned file for it to be removed from the cache.
func (a *Anvil) File(rgX, rgZ int32) (f File, err error) {
c, err := a.get(rgX, rgZ)
if err != nil {
return nil, err
}
cf := &cachedFile{file: c}
runtime.SetFinalizer(cf, func(c *cachedFile) { c.Close() })
return cf, nil
}
// get gets the anvil get for the given coords
func (a *Anvil) get(rgX, rgZ int32) (f *file, err error) {
rg := pos{rgX, rgZ}
a.mux.RLock()
f, ok := a.getFile(rg)
a.mux.RUnlock()
if !ok {
a.mux.Lock()
defer a.mux.Unlock()
// check if the file was opened while we were waiting for the mux
if f, ok = a.getFile(rg); !ok {
if a.lru != nil {
// check if the file is in the lru cache
if f, ok = a.lru.Get(rg); ok {
a.lru.Remove(rg)
}
}
// file wasn't in the cache. read file from the disk
if f == nil {
var r reader
var size int64
filename := fmt.Sprintf(a.settings.AnvilFmt, rg.x, rg.z)
if r, size, err = openFile(filename, a.settings); err == nil {
f, err = newAnvil(rg.x, rg.z, r, size, a.settings)
f.cache = a
}
}
if err == nil {
f.useCount.Add(1)
a.inUse[rg] = f
}
}
}
return
}
func (a *Anvil) free(f *file) (err error) {
a.mux.RLock()
newCount := f.useCount.Add(-1)
a.mux.RUnlock()
if newCount == 0 {
a.mux.Lock()
defer a.mux.Unlock()
if newCount = f.useCount.Load(); newCount == 0 {
if a.lru == nil {
// cache is disabled. close the file
delete(a.inUse, f.pos)
return f.Close()
}
// evict the oldest file from the lru if adding a new element will cause a element to be evicted
// We do this to insure the file gets closed properly and to free all associated resources.
// We cannot use EvictCallback since there is no way to handle error that occur while closing the file.
if a.lru.Len() == a.settings.CacheSize {
if _, old, ok := a.lru.RemoveOldest(); ok {
if err = old.Close(); err != nil {
err = errors.Wrap("anvil.Cache: error occurred while evicting file", err)
}
}
}
evicted := a.lru.Add(f.pos, f)
if evicted {
// This should never happen since we manually evicted the oldest element
panic("anvil.Cache: File was incorrectly evicted")
}
delete(a.inUse, f.pos)
}
}
return
}
func (a *Anvil) getFile(rg pos) (f *file, ok bool) {
f, ok = a.inUse[rg]
if ok {
f.useCount.Add(1)
}
return
}
// Open opens the given directory.
func Open(path string, opt ...Settings) (c *Anvil, err error) {
if path, err = filepath.Abs(path); err == nil {
var info os.FileInfo
if info, err = filesystem.Stat(path); err == nil {
if !info.IsDir() {
return nil, errors.New("anvil: Open: " + path + " is not a directory")
}
return OpenFs(afero.NewBasePathFs(filesystem, path), opt...)
}
}
return
}
// OpenFs opens the given directory.
func OpenFs(fs afero.Fs, opt ...Settings) (c *Anvil, err error) {
settings := getSettings(opt, fs)
cache := Anvil{inUse: map[pos]*file{}, settings: settings}
if settings.CacheSize > 0 {
if cache.lru, err = lru.NewLRU[pos, *file](settings.CacheSize, nil); err != nil {
return nil, err
}
}
return &cache, nil
}
func getSettings(s []Settings, fs afero.Fs) Settings {
var settings = defaultSettings
if len(s) == 1 {
settings = s[0]
if settings.CacheSize == 0 {
settings.CacheSize = defaultSettings.CacheSize
}
if settings.AnvilFmt == "" {
settings.AnvilFmt = defaultSettings.AnvilFmt
}
if settings.ChunkFmt == "" {
settings.ChunkFmt = defaultSettings.ChunkFmt
}
}
settings.fs = fs
return settings
}