Categorygithub.com/uplo-tech/writeaheadlog
modulepackage
0.0.0-20210218183703-1b66bc291a5e
Repository: https://github.com/uplo-tech/writeaheadlog.git
Documentation: pkg.go.dev

# README

A general purpose, high performance write-ahead-log

A write-ahead-log (WAL) ensures durability and atomicity for updating data on disk if used correctly. Instructions are marshaled and passed to the WAL before they are applied to disk. The WAL makes sure that the instructions are synced to the WAL file before the user applies them. That way the wal can notify the user about unfinished updates due to a sudden power outage. It is up to the caller to guarantee that the instructions are idempotent and consistent.

Usage

First the wal needs to be opened by calling New(string path). This will create a new WAL at the provided path or load an existing one. The latter will recover the WAL and return all the transactions that were not completed.

// Open the WAL.
recoveredTxns, wal, err := New(walPath)
if err != nil {
	return err
}

if len(recoveredTxns) != 0 {
	// Apparently the system crashed. Handle the unfinished updates
	// accordingly.
	applyUpdates(recoveredTxns)

	// After the recovery is complete we can signal the WAL that we are
	// done and that the updates were applied.
	// NOTE: This is optional. If for some reason an update cannot be
	// applied right away it may be skipped and applied later
	for _, txn := range recoveredTxns {
		if err := txn.SignalUpdatesApplied; err != nil {
			return err
		}
	}
}

The wal can then be used to create a Transaction like this using a set of updates:

// Create the WAL transaction.
tx, err := ca.wal.NewTransaction(updates)
if err != nil {
	return err
}

An Update consists of a Name, Version and Instructions. One transaction can hold multiple updates. After the Transaction is created the caller might want to do some kind of setup. Once completed the next step is to signal the WAL that the setup is complete.

// Signal completed setup and then wait for the commitment to finish. This
// will cause the WAL to call fsync on it's underlying file.
errChan := tx.SignalSetupComplete()
err = <-errChan
if err != nil {
	return err
}

This will write the updates to disk and commit them. The caller needs to wait on the returned channel to ensure a successful commit. After the commit it is safe for the caller to apply the updates to disk and once finished, signal the wal that it can now mark the transaction as applied and recycle the transaction.

// Signal that the updates were applied. This also causes the WAL to fsync and
// allows it to recycle used pages. The caller might run this in a goroutine
// but if the system crashes again before the call finishes the caller might
// receive the already applied instructions when recovering the WAL.
err = tx.SignalUpdatesApplied()
if err != nil {
	return err
}

# Functions

ApplyDeleteUpdate parses and applies a delete update.
ApplyTruncateUpdate parses and applies a truncate update.
ApplyUpdates can be used to apply the common update types provided by the writeaheadlog.
ApplyWriteAtUpdate parses and applies a writeat update.
DeleteUpdate creates an update that deletes the file at the specified path.
New will open a WAL.
NewWithOptions opens a WAL like New but takes an Options struct as an additional argument to customize some of the WAL's behavior like logging.
TruncateUpdate is a helper function which creates a writeaheadlog update for truncating the specified file.
WriteAtUpdate is a helper function which creates a writeaheadlog update for writing the specified data to the provided index of a file.

# Constants

MaxPayloadSize is the number of bytes that can fit into a single page.

# Variables

NameDeleteUpdate is the name of an idempotent update that deletes a file or folder from a given path on disk.
NameTruncateUpdate is the name of an idempotent update that truncates a file to have a certain size.
NameWriteAtUpdate is the name of an idempotent update that writes data to a file at the specified offset.

# Structs

No description provided by the author
Transaction defines a series of updates that are to be performed atomically.
Update defines a single update that can be sent to the WAL and saved atomically.
No description provided by the author