reversible

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package reversible provides compression implementations.

Package reversible provides pipeline integration for reversible compression.

Package reversible provides SQLite-backed storage for compressed entries.

Package reversible implements Claw Compactor's Rewind engine for reversible compression.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ComputeHash

func ComputeHash(content string) string

ComputeHash computes the full SHA-256 hash.

func ComputeShortHash

func ComputeShortHash(content string) string

ComputeShortHash computes the 16-char short hash for markers.

func InjectMarker

func InjectMarker(text string, marker Marker, maxLen int) string

InjectMarker injects a marker into text at a good position.

Types

type AESEncryptor

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

AESEncryptor implements AES-256-GCM encryption.

func NewAESEncryptor

func NewAESEncryptor(key []byte) (*AESEncryptor, error)

NewAESEncryptor creates a new AES encryptor.

func (*AESEncryptor) Decrypt

func (e *AESEncryptor) Decrypt(ciphertext []byte) ([]byte, error)

Decrypt implements Encryptor.

func (*AESEncryptor) Encrypt

func (e *AESEncryptor) Encrypt(plaintext []byte) ([]byte, error)

Encrypt implements Encryptor.

type CLI

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

CLI provides command-line interface for reversible compression.

func NewCLI

func NewCLI(store Store, config Config) *CLI

NewCLI creates a new CLI.

func (*CLI) ClearCommand

func (c *CLI) ClearCommand(olderThan time.Duration) (int64, error)

ClearCommand handles the `rewind clear` command.

func (*CLI) ListCommand

func (c *CLI) ListCommand(filter ListFilter) ([]*Entry, error)

ListCommand handles the `rewind list` command.

func (*CLI) RetrieveCommand

func (c *CLI) RetrieveCommand(hash string) (*Entry, error)

RetrieveCommand handles the `rewind retrieve` command.

func (*CLI) StatsCommand

func (c *CLI) StatsCommand() (StoreStats, error)

StatsCommand handles the `rewind stats` command.

func (*CLI) StoreCommand

func (c *CLI) StoreCommand(content string, command string) (string, error)

StoreCommand handles the `rewind store` command.

func (*CLI) VacuumCommand

func (c *CLI) VacuumCommand() error

VacuumCommand handles the `rewind vacuum` command.

type ClassificationResult

type ClassificationResult struct {
	Type       ContentType
	Confidence float64
	Language   string
}

ClassificationResult contains content classification info.

type Classifier

type Classifier interface {
	// Classify determines the content type.
	Classify(content string) ClassificationResult
}

Classifier provides content classification.

type CommandRecord

type CommandRecord struct {
	Command    string        `json:"command"`
	Hash       string        `json:"hash"`
	Timestamp  time.Time     `json:"timestamp"`
	Duration   time.Duration `json:"duration"`
	Compressed bool          `json:"compressed"`
}

CommandRecord stores command execution history.

type Compressor

type Compressor interface {
	// Compress compresses data and returns the compressed bytes.
	Compress(data []byte) ([]byte, error)
	// Decompress decompresses data.
	Decompress(data []byte) ([]byte, error)
	// Name returns the compressor name.
	Name() string
}

Compressor provides compression functionality.

type Config

type Config struct {
	// StorePath is the path to the SQLite database.
	StorePath string
	// EncryptionKey is the AES-256-GCM key (nil for no encryption).
	EncryptionKey []byte
	// DefaultAlgorithm is the default compression algorithm.
	DefaultAlgorithm string // "zstd", "lz4", "none"
	// MaxEntrySize is the maximum size for a single entry (default 100MB).
	MaxEntrySize int64
	// AutoVacuum enables automatic vacuum after deletes.
	AutoVacuum bool
}

Config provides configuration for the reversible compression system.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a default configuration.

type ContentType

type ContentType int

ContentType represents the classification of content for compression.

const (
	TypeLossless ContentType = iota // No loss acceptable (code, config)
	TypeLossy                       // Minor loss OK (logs with timestamps)
	TypeSemantic                    // Semantic preservation only (docs)
)

type Encryptor

type Encryptor interface {
	// Encrypt encrypts data with the given key.
	Encrypt(plaintext []byte) ([]byte, error)
	// Decrypt decrypts data with the given key.
	Decrypt(ciphertext []byte) ([]byte, error)
}

Encryptor provides encryption functionality.

type Entry

type Entry struct {
	Hash           string      `json:"hash"`
	Original       string      `json:"original"`
	Compressed     string      `json:"compressed,omitempty"` // Not stored if encrypted
	Command        string      `json:"command,omitempty"`
	ContentType    ContentType `json:"content_type"`
	CompressionAlg string      `json:"compression_alg"` // "zstd", "lz4", "none"
	Encrypted      bool        `json:"encrypted"`
	CreatedAt      time.Time   `json:"created_at"`
	AccessedAt     time.Time   `json:"accessed_at"`
	AccessCount    int         `json:"access_count"`
	SizeOriginal   int64       `json:"size_original"`
	SizeCompressed int64       `json:"size_compressed"`
	// These fields are only populated when retrieving, not stored in DB
	CompressedData []byte `json:"-"` // Raw compressed bytes (encrypted if Encrypted=true)
}

Entry represents a stored compressed entry.

func (*Entry) CompressionRatio

func (e *Entry) CompressionRatio() float64

CompressionRatio returns the compression ratio.

func (*Entry) IsExpired

func (e *Entry) IsExpired() bool

IsExpired checks if entry is older than 30 days.

func (*Entry) SavedPercentage

func (e *Entry) SavedPercentage() float64

SavedPercentage returns percentage of space saved.

type Integration

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

Integration provides integration with TokMan's existing filter pipeline.

func NewIntegration

func NewIntegration(store Store, config Config) *Integration

NewIntegration creates a new pipeline integration.

func SetupDefault

func SetupDefault() (*Integration, error)

SetupDefault creates a default integration with SQLite store.

func (*Integration) Disable

func (i *Integration) Disable()

Disable disables the integration.

func (*Integration) Enable

func (i *Integration) Enable()

Enable enables the integration.

func (*Integration) IsEnabled

func (i *Integration) IsEnabled() bool

IsEnabled returns true if integration is enabled.

func (*Integration) ProcessFilterOutput

func (i *Integration) ProcessFilterOutput(original, compressed string, command string) (string, error)

ProcessFilterOutput processes output from the filter pipeline.

func (*Integration) RestoreInput

func (i *Integration) RestoreInput(input string) (string, error)

RestoreInput restores original content from input containing markers.

type LZ4Compressor

type LZ4Compressor struct {
}

LZ4Compressor implements lz4 compression.

func (*LZ4Compressor) Compress

func (l *LZ4Compressor) Compress(data []byte) ([]byte, error)

Compress implements Compressor.

func (*LZ4Compressor) Decompress

func (l *LZ4Compressor) Decompress(data []byte) ([]byte, error)

Decompress implements Compressor.

func (*LZ4Compressor) Name

func (l *LZ4Compressor) Name() string

Name returns the compressor name.

type ListFilter

type ListFilter struct {
	Command     string
	ContentType ContentType
	Since       time.Time
	Before      time.Time
	MinSize     int64
	MaxSize     int64
	Limit       int
}

ListFilter provides filtering for List operations.

type Marker

type Marker struct {
	Hash     string // First 16 chars of SHA-256
	FullHash string // Full 64-char hash for verification
}

Marker represents a hash marker for referencing compressed content.

func ExtractMarkers

func ExtractMarkers(text string) []Marker

ExtractMarkers extracts all markers from text.

func ParseMarker

func ParseMarker(s string) (Marker, error)

ParseMarker parses a marker string in [rewind:hash16] format.

func (Marker) String

func (m Marker) String() string

String returns the marker in [rewind:hash16] format.

type NoOpCompressor

type NoOpCompressor struct{}

NoOpCompressor implements no compression (pass-through).

func (*NoOpCompressor) Compress

func (n *NoOpCompressor) Compress(data []byte) ([]byte, error)

Compress implements Compressor.

func (*NoOpCompressor) Decompress

func (n *NoOpCompressor) Decompress(data []byte) ([]byte, error)

Decompress implements Compressor.

func (*NoOpCompressor) Name

func (n *NoOpCompressor) Name() string

Name returns the compressor name.

type Pipeline

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

Pipeline integrates reversible compression with command execution.

func NewPipeline

func NewPipeline(store Store, config Config) *Pipeline

NewPipeline creates a new reversible compression pipeline.

func (*Pipeline) Execute

func (p *Pipeline) Execute(command string, args []string, input io.Reader) (string, *Entry, error)

Execute runs a command with reversible compression.

func (*Pipeline) ProcessOutput

func (p *Pipeline) ProcessOutput(output string) (string, error)

ProcessOutput processes output text and replaces markers with original content.

func (*Pipeline) Restore

func (p *Pipeline) Restore(markerStr string) (*Entry, error)

Restore retrieves original content from a marker.

func (*Pipeline) ShouldCompress

func (p *Pipeline) ShouldCompress(content string, cmd string) bool

ShouldCompress determines if content should be compressed based on size and type.

type SQLiteStore

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

SQLiteStore implements Store using SQLite.

func NewSQLiteStore

func NewSQLiteStore(config Config) (*SQLiteStore, error)

NewSQLiteStore creates a new SQLite-backed store.

func (*SQLiteStore) Close

func (s *SQLiteStore) Close() error

Close closes the store.

func (*SQLiteStore) Delete

func (s *SQLiteStore) Delete(hash string) error

Delete removes an entry.

func (*SQLiteStore) DeleteOlderThan

func (s *SQLiteStore) DeleteOlderThan(d time.Duration) (int64, error)

DeleteOlderThan deletes entries older than duration.

func (*SQLiteStore) GetCommandHistory

func (s *SQLiteStore) GetCommandHistory(limit int) ([]*CommandRecord, error)

GetCommandHistory returns command history.

func (*SQLiteStore) List

func (s *SQLiteStore) List(filter ListFilter) ([]*Entry, error)

List returns entries matching the filter.

func (*SQLiteStore) RecordCommand

func (s *SQLiteStore) RecordCommand(record *CommandRecord) error

RecordCommand records command execution.

func (*SQLiteStore) Retrieve

func (s *SQLiteStore) Retrieve(hash string) (*Entry, error)

Retrieve gets an entry by hash (short or full).

func (*SQLiteStore) Save

func (s *SQLiteStore) Save(entry *Entry) (string, error)

Save stores an entry and returns its hash.

func (*SQLiteStore) Stats

func (s *SQLiteStore) Stats() (StoreStats, error)

Stats returns store statistics.

func (*SQLiteStore) Vacuum

func (s *SQLiteStore) Vacuum() error

Vacuum reclaims space.

type SimpleClassifier

type SimpleClassifier struct{}

SimpleClassifier implements basic content classification.

func (*SimpleClassifier) Classify

func (c *SimpleClassifier) Classify(content string) ClassificationResult

Classify implements Classifier.

type Store

type Store interface {
	// Save stores an entry and returns its hash.
	Save(entry *Entry) (string, error)
	// Retrieve gets an entry by hash (short or full).
	Retrieve(hash string) (*Entry, error)
	// Delete removes an entry.
	Delete(hash string) error
	// List returns all entries, optionally filtered.
	List(filter ListFilter) ([]*Entry, error)
	// Stats returns store statistics.
	Stats() (StoreStats, error)
	// Vacuum reclaims space.
	Vacuum() error
}

Store provides persistent storage for reversible compression.

type StoreStats

type StoreStats struct {
	TotalEntries  int64
	TotalSizeOrig int64
	TotalSizeComp int64
	OldestEntry   time.Time
	NewestEntry   time.Time
	ByContentType map[ContentType]int64
	ByCommand     map[string]int64
}

StoreStats provides statistics about the store.

type StreamingReader

type StreamingReader interface {
	// Read reads decompressed data.
	Read(p []byte) (n int, err error)
	// Close closes the reader.
	Close() error
}

StreamingReader provides streaming decompression.

type ZstdCompressor

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

ZstdCompressor implements zstd compression.

func (*ZstdCompressor) Compress

func (z *ZstdCompressor) Compress(data []byte) ([]byte, error)

Compress implements Compressor.

func (*ZstdCompressor) Decompress

func (z *ZstdCompressor) Decompress(data []byte) ([]byte, error)

Decompress implements Compressor.

func (*ZstdCompressor) Name

func (z *ZstdCompressor) Name() string

Name returns the compressor name.

Jump to

Keyboard shortcuts

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