# bloombits

Package bloombits implements bloom filtering on batches of data.

Generator

```
// Generator takes a number of bloom filters and generates the rotated bloom bits
// to be used for batched filtering.
type Generator struct {
    blooms   [types.BloomBitLength][]byte // Rotated blooms for per-bit matching
    sections uint                         // Number of sections to batch together
    nextSec  uint                         // Next section to set when adding a bloom
}
```

Matcher

```
// Matcher is a pipelined system of schedulers and logic matchers which perform
// binary AND/OR operations on the bit-streams, creating a stream of potential
// blocks to inspect for data content.
type Matcher struct {
    sectionSize uint64 // Size of the data batches to filter on

    filters    [][]bloomIndexes    // Filter the system is matching for
    schedulers map[uint]*scheduler // Retrieval schedulers for loading bloom bits

    retrievers chan chan uint       // Retriever processes waiting for bit allocations
    counters   chan chan uint       // Retriever processes waiting for task count reports
    retrievals chan chan *Retrieval // Retriever processes waiting for task allocations
    deliveries chan *Retrieval      // Retriever processes waiting for task response deliveries

    running uint32 // Atomic flag whether a session is live or not
}
```

MatcherSession

```
// MatcherSession is returned by a started matcher to be used as a terminator
// for the actively running matching operation.
type MatcherSession struct {
    matcher *Matcher

    closer sync.Once     // Sync object to ensure we only ever close once
    quit   chan struct{} // Quit channel to request pipeline termination
    kill   chan struct{} // Term channel to signal non-graceful forced shutdown

    ctx context.Context // Context used by the light client to abort filtering
    err atomic.Value    // Global error to track retrieval failures deep in the chain

    pend sync.WaitGroup
}
```

scheduler

```
// scheduler handles the scheduling of bloom-filter retrieval operations for
// entire section-batches belonging to a single bloom bit. Beside scheduling the
// retrieval operations, this struct also deduplicates the requests and caches
// the results to minimize network/database overhead even in complex filtering
// scenarios.
type scheduler struct {
    bit       uint                 // Index of the bit in the bloom filter this scheduler is responsible for
    responses map[uint64]*response // Currently pending retrieval requests or already cached responses
    lock      sync.Mutex           // Lock protecting the responses from concurrent access
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kelby.gitbook.io/super-zero-protocol-sero/qu-kuai-lian-ff08-nei-he-ff09/core/bloombits.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
