blockmanager

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 20, 2025 License: MPL-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package blockmanager

(C) Copyright Alex Gaetano Padula

Licensed under the Mozilla Public License, v. 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

https://www.mozilla.org/en-US/MPL/2.0/

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const Allotment = uint64(16) // How many blocks we can allot at once to the file.  We allocate this many blocks once allocationTable is empty
View Source
const BlockSize = uint32(512) // Smaller the better, faster in our tests
View Source
const EndOfChain = uint64(0xFFFFFFFFFFFFFFFF) // Marker for end of blockchain (overflowed block)
View Source
const MagicNumber = uint32(0x57494C44) // "WILD"
View Source
const Version = uint32(1) // Version of the file format

Variables

This section is empty.

Functions

This section is empty.

Types

type BlockHeader

type BlockHeader struct {
	CRC       uint32 // CRC32 checksum of the block header
	BlockID   uint64 // Unique ID of the block
	DataSize  uint64 // Size of the data in the block
	NextBlock uint64 // ID of the next block in the chain (or EndOfChain if this is the last block)
}

BlockHeader represents the header of a block in the file

type BlockManager

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

BlockManager manages the allocation and deallocation of blocks in a file

func Open

func Open(filename string, flag int, perm os.FileMode, syncOpt SyncOption, duration ...time.Duration) (*BlockManager, error)

Open opens a file and initializes the BlockManager.

func (*BlockManager) Append

func (bm *BlockManager) Append(data []byte) (int64, error)

Append writes data to the file by allocating one or more blocks and returns the ID of the first block containing the data.

func (*BlockManager) Close

func (bm *BlockManager) Close() error

Close closes the file and releases any resources held by the BlockManager.

func (*BlockManager) File

func (bm *BlockManager) File() *os.File

File returns the associated file handle for the BlockManager

func (*BlockManager) Iterator

func (bm *BlockManager) Iterator() *Iterator

Iterator returns an iterator for traversing the blocks in the file

func (*BlockManager) Read

func (bm *BlockManager) Read(blockID int64) ([]byte, int64, error)

Read reads data from the file starting at the specified block ID. It follows the chain of blocks if the data spans multiple blocks. Returns the data, the final block ID (if chained) or the first block ID (if not chained), and any error.

type Header struct {
	CRC         uint32 // CRC32 checksum of the header
	MagicNumber uint32 // Magic number to identify the file format
	Version     uint32 // Version of the file format
	BlockSize   uint32 // Size of each block in bytes
	Allotment   uint64 // Number of blocks to allot at once
}

Header represents the header of the file

type Iterator

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

Iterator is used to traverse the blocks in the file

func (*Iterator) BlockManager

func (it *Iterator) BlockManager() *BlockManager

BlockManager returns the block manager pointer from iterator

func (*Iterator) Next

func (it *Iterator) Next() ([]byte, int64, error)

Next moves the iterator to the next block

func (*Iterator) Prev

func (it *Iterator) Prev() ([]byte, int64, error)

Prev moves the iterator to the previous block

type SyncOption

type SyncOption int

SyncOption defines the synchronization options for the file

const (
	SyncNone    SyncOption = iota // Don't sync at all
	SyncFull                      // Do a sync after every write
	SyncPartial                   // Do a sync in the background at intervals
)

Jump to

Keyboard shortcuts

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