anvil

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2022 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ErrExternal returned if the chunk is in an external file.
	// This error is only returned if the anvil file was opened as a single file.
	ErrExternal = errors.Const("anvil: chunk 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 4096.
	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")
)
View Source
const (

	// 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
)
View Source
const DefaultCompression = CompressionZlib

DefaultCompression the default compression method to be used

Variables

This section is empty.

Functions

This section is empty.

Types

type Anvil

type Anvil struct {
	// contains filtered or unexported fields
}

Anvil a anvil file cache.

func Open

func Open(path string, readonly bool, cacheSize int) (c *Anvil, err error)

Open opens the given directory.

func OpenFs

func OpenFs(fs *Fs, readonly bool, cacheSize int) (c *Anvil, err error)

OpenFs opens the given directory.

func (*Anvil) File

func (a *Anvil) File(rgX, rgZ int32) (f *CachedFile, err error)

File opens the anvil file at rgX, rgZ. Callers must close the returned file for it to be removed from the cache.

func (*Anvil) Read

func (a *Anvil) Read(entryX, entryZ int32, read io.ReaderFrom) (n int64, err error)

Read reads the chunk data for the given location

func (*Anvil) Write

func (a *Anvil) Write(entryX, entryZ int32, p []byte) (err error)

Write writes the chunk data for the given location

type CachedFile

type CachedFile struct {
	// contains filtered or unexported fields
}

CachedFile a cached anvil file

func (*CachedFile) Close

func (c *CachedFile) Close() (err error)

Close closes the file. This function can be called multiple times. This will block until all Read and Write calls return.

func (*CachedFile) CompressionMethod

func (c *CachedFile) CompressionMethod(m CompressMethod) (err error)

CompressionMethod sets the compression method to be used by the writer.

func (*CachedFile) Read

func (c *CachedFile) Read(x, z uint8, reader io.ReaderFrom) (n int64, err error)

Read reads the entry at x,z to the given `reader`. `reader` must not retain the io.Reader passed to it. `reader` must not return before reading has completed.

func (*CachedFile) Remove

func (c *CachedFile) Remove(x, z uint8) (err error)

Remove removes the given entry from the file.

func (*CachedFile) Write

func (c *CachedFile) Write(x, z uint8, b []byte) (err error)

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 `Remove(x,z)`.

type CompressMethod

type CompressMethod byte

CompressMethod the compression method used for compressing section data

const (
	CompressionGzip CompressMethod = 1 + iota
	CompressionZlib
	CompressionNone
)

supported methods

func (CompressMethod) String

func (c CompressMethod) String() string

type Entry

type Entry struct {
	// contains filtered or unexported fields
}

Entry an entry in the anvil file

func (*Entry) CompressedSize

func (e *Entry) CompressedSize() int64

CompressedSize the number of sections used by this entry (in sections). To get the size in bytes, multiply this value by `SectionSize`. If this is zero the data does not exist in this file. If the entry is stored in an external file, this will return 1.

func (*Entry) Exists

func (e *Entry) Exists() bool

Exists returns if the entry is stored in this file.

func (*Entry) Modified

func (e *Entry) Modified() time.Time

Modified returns when the entry was last modified.

func (*Entry) Offset

func (e *Entry) Offset() int64

Offset is the offset of the entry in the anvil file (in sections). The maximum offset is (2<<24)-1 sections. To get the size in bytes, multiply this value by `SectionSize`. If this is zero the data does not exist in this file.

type File

type File struct {
	// contains filtered or unexported fields
}

File is a single anvil file. All functions can be called concurrently from multiple goroutines.

func NewAnvil

func NewAnvil(rgx, rgz int32, fs *Fs, r io.ReaderAt, readonly bool, fileSize int64) (a *File, err error)

NewAnvil reads an anvil file from the given ReadAtCloser. This has the same limitations as OpenFile if `fs` is nil. If fileSize is 0, no attempt is made to read any headers.

func OpenFile

func OpenFile(path string, readonly bool) (f *File, err error)

OpenFile opens the given anvil file. If readonly is set any attempts to modify the file will return an error. If any data is stored in external files, any attempt to read it will return ErrExternal. If an attempt is made to write a data that is over 1MB after compression, ErrExternal will be returned. To allow reading and writing to external files use Open instead.

func (*File) Close

func (a *File) Close() (err error)

Close closes the anvil file.

func (*File) CompressionMethod

func (a *File) CompressionMethod(m CompressMethod) (err error)

CompressionMethod sets the compression method to be used by the writer.

func (*File) Read

func (a *File) Read(x, z uint8, reader io.ReaderFrom) (n int64, err error)

Read reads the entry at x,z to the given `reader`. `reader` must not retain the io.Reader passed to it. `reader` must not return before reading has completed.

func (*File) Remove

func (a *File) Remove(x, z uint8) (err error)

Remove removes the given entry from the file.

func (*File) Write

func (a *File) Write(x, z uint8, b []byte) (err error)

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 `Remove(x,z)`.

type Fs

type Fs struct {
	AnvilFmt, ChunkFmt string
	// contains filtered or unexported fields
}

Fs handles opening anvil files.

func NewFs

func NewFs(f afero.Fs) *Fs

NewFs creates an Fs from the given afero.Fs.

type Header struct {
	// contains filtered or unexported fields
}

Header the header of the anvil file.

func LoadHeader

func LoadHeader(size, timestamps *[Entries]uint32, fileSections uint) (h *Header, err error)

LoadHeader reads the header from the given arrays. `size` should contains the size and position of entries, with the least significant byte being the number of sections used by the entry and the rest containing the offset where the entry starts. `timestamps` should be an array of timestamps when the entries were last modified as the number of seconds since January 1, 1970 UTC. This function expects `size`, `timestamps` to be in the hosts byte order. `fileSections` is the max amount of sections that can be used by the entries. If `fileSections` is 0, `maxFileSections` is used instead.

func ReadHeader

func ReadHeader(r io.ReaderAt, maxSection uint) (h *Header, err error)

ReadHeader reads a header from the given reader. The given reader must read at least 2*4096 bytes. If `maxSections` != 0, this returns an error if the header references more than `maxSections` sections.

func (*Header) FindSpace

func (h *Header) FindSpace(size uint) (offset uint, found bool)

FindSpace finds the next free space large enough to store `size` sections

func (*Header) Free

func (h *Header) Free()

Free frees the header and puts it into the pool. Callers must not use the header after calling this.

func (*Header) Get

func (h *Header) Get(x, z uint8) *Entry

Get gets the entry at the given x,z coords. If the given x,z values are not between 0 and 31 (inclusive) this panics.

func (*Header) Remove

func (h *Header) Remove(x, z uint8) error

Remove removes the given entry from the header and marks the space used by the given entry in the `used` bitset as unused.

func (*Header) Set

func (h *Header) Set(x, z uint8, c Entry) error

Set updates the entry at x,z and the given marks the space used by the given entry in the `used` bitset as used.

func (*Header) Write

func (h *Header) Write(size, timestamps *[Entries]uint32)

Write writes the header to the given arrays.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL