lazyloader

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package lazyloader provides lazy loading functionality for documents

This package implements lazy loading of documents to optimize memory usage by loading content only when needed. It also provides memory management capabilities to control the total memory used by loaded documents.

Example:

// Create a lazy document
doc := lazyloader.NewLazyDocument("path/to/document.pdf")

// Load content on demand
content, err := doc.Load()
if err != nil {
    log.Fatal(err)
}

// Use content...

// Unload to free memory
doc.Unload()

// Create a document manager with memory limit
manager := lazyloader.NewLazyDocumentManager(100 * 1024 * 1024) // 100MB
doc := manager.AddDocument("path/to/document.pdf")
content, err := manager.LoadDocument("path/to/document.pdf")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LazyDocument

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

LazyDocument represents a document that is loaded on demand

A LazyDocument loads its content only when needed, which helps optimize memory usage by avoiding loading large documents until they are actually required.

Example:

// Create a lazy document from a file
doc := NewLazyDocument("path/to/document.pdf")

// Check if loaded
fmt.Println("Is loaded:", doc.IsLoaded()) // false

// Load content on demand
content, err := doc.Load()
if err != nil {
    log.Fatal(err)
}

// Check size
size, err := doc.Size()
if err != nil {
    log.Fatal(err)
}
fmt.Printf("Document size: %d bytes\n", size)

// Unload to free memory
doc.Unload()
fmt.Println("Is loaded:", doc.IsLoaded()) // false

func NewLazyDocument

func NewLazyDocument(path string) *LazyDocument

NewLazyDocument creates a new lazy document

Parameters: - path: Path to the document file

Returns: - *LazyDocument: New lazy document instance

func NewLazyDocumentWithLoader

func NewLazyDocumentWithLoader(path string, loader func() ([]byte, error)) *LazyDocument

NewLazyDocumentWithLoader creates a new lazy document with a custom loader

Parameters: - path: Path to the document - loader: Custom loader function

Returns: - *LazyDocument: New lazy document instance

func (*LazyDocument) IsLoaded

func (d *LazyDocument) IsLoaded() bool

IsLoaded returns whether the document is loaded

Returns: - bool: True if the document is loaded

func (*LazyDocument) Load

func (d *LazyDocument) Load() ([]byte, error)

Load loads the document content

This method loads the document content if it's not already loaded. It uses double-checked locking to ensure thread safety.

Returns: - []byte: Document content - error: Error if loading fails

func (*LazyDocument) LoadWithContext

func (d *LazyDocument) LoadWithContext(ctx context.Context) ([]byte, error)

LoadWithContext loads the document with context cancellation

This method loads the document content with support for context cancellation.

Parameters: - ctx: Context for cancellation

Returns: - []byte: Document content - error: Error if loading fails or context is cancelled

func (*LazyDocument) Path

func (d *LazyDocument) Path() string

Path returns the document path

Returns: - string: Document path

func (*LazyDocument) Reader

func (d *LazyDocument) Reader() (io.Reader, error)

Reader returns an io.Reader for the document

This method returns an io.Reader for the document content, loading the content if it's not already loaded.

Returns: - io.Reader: Reader for the document content - error: Error if loading fails

func (*LazyDocument) Size

func (d *LazyDocument) Size() (int64, error)

Size returns the document size (requires loading metadata)

Returns: - int64: Document size in bytes - error: Error if metadata loading fails

func (*LazyDocument) Unload

func (d *LazyDocument) Unload()

Unload unloads the document content to free memory

This method unloads the document content and resets the loaded flag, freeing up memory occupied by the document.

type LazyDocumentManager

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

LazyDocumentManager manages multiple lazy documents

The LazyDocumentManager manages a collection of lazy documents and tracks memory usage to ensure it stays within the specified limit. It automatically evicts documents when memory usage exceeds the limit.

Example:

// Create a document manager with 100MB memory limit
manager := NewLazyDocumentManager(100 * 1024 * 1024)

// Add documents
doc1 := manager.AddDocument("doc1.pdf")
doc2 := manager.AddDocument("doc2.pdf")

// Load documents
content1, err := manager.LoadDocument("doc1.pdf")
if err != nil {
    log.Fatal(err)
}

// Check memory usage
fmt.Printf("Memory usage: %d bytes\n", manager.GetMemoryUsage())

// Unload all documents
manager.UnloadAll()
fmt.Printf("Memory usage after unload: %d bytes\n", manager.GetMemoryUsage())

func NewLazyDocumentManager

func NewLazyDocumentManager(maxSize int64) *LazyDocumentManager

NewLazyDocumentManager creates a new lazy document manager

Parameters: - maxSize: Maximum memory usage in bytes

Returns: - *LazyDocumentManager: New lazy document manager instance

func (*LazyDocumentManager) AddDocument

func (m *LazyDocumentManager) AddDocument(path string) *LazyDocument

AddDocument adds a document to the manager

This method adds a document to the manager. If the document already exists, it returns the existing document.

Parameters: - path: Path to the document file

Returns: - *LazyDocument: Lazy document instance

func (*LazyDocumentManager) GetDocument

func (m *LazyDocumentManager) GetDocument(path string) (*LazyDocument, bool)

GetDocument gets a document from the manager

Parameters: - path: Path to the document file

Returns: - *LazyDocument: Lazy document instance - bool: True if the document exists

func (*LazyDocumentManager) GetMemoryUsage

func (m *LazyDocumentManager) GetMemoryUsage() int64

GetMemoryUsage returns the current memory usage

Returns: - int64: Current memory usage in bytes

func (*LazyDocumentManager) LoadDocument

func (m *LazyDocumentManager) LoadDocument(path string) ([]byte, error)

LoadDocument loads a document and tracks memory usage

This method loads a document and tracks memory usage. If loading the document would exceed the memory limit, it evicts existing documents to make room.

Parameters: - path: Path to the document file

Returns: - []byte: Document content - error: Error if loading fails or memory limit is exceeded

func (*LazyDocumentManager) RemoveDocument

func (m *LazyDocumentManager) RemoveDocument(path string)

RemoveDocument removes a document from the manager

This method removes a document from the manager and updates the memory usage if the document was loaded.

Parameters: - path: Path to the document file

func (*LazyDocumentManager) UnloadAll

func (m *LazyDocumentManager) UnloadAll()

UnloadAll unloads all documents

This method unloads all documents managed by the manager, freeing up all memory used by loaded documents.

Jump to

Keyboard shortcuts

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