···277 mempool: mempool,
278 didIndex: didIndex,
279 bundleCache: make(map[int]*Bundle),
280- maxCacheSize: 10, // Keep 10 recent bundles in memory (~50-100 MB)
281 }, nil
282}
283···1345// LoadOperation loads a single operation from a bundle efficiently
1346// This is much faster than LoadBundle() when you only need one operation
1347func (m *Manager) LoadOperation(ctx context.Context, bundleNumber int, position int) (*plc.PLCOperation, error) {
1348- m.logger.Printf("DEBUG: Using LoadOperation (position=%d) - should be faster!", position)
13491350 // Validate bundle exists in index
1351- meta, err := m.index.GetBundle(bundleNumber)
1352 if err != nil {
1353 return nil, fmt.Errorf("bundle not in index: %w", err)
1354 }
···1362 path := filepath.Join(m.config.BundleDir, fmt.Sprintf("%06d.jsonl.zst", bundleNumber))
1363 if !m.operations.FileExists(path) {
1364 return nil, fmt.Errorf("bundle file not found: %s", path)
1365- }
1366-1367- // Verify hash if enabled (same as LoadBundle)
1368- if m.config.VerifyOnLoad {
1369- valid, actualHash, err := m.operations.VerifyHash(path, meta.CompressedHash)
1370- if err != nil {
1371- return nil, fmt.Errorf("failed to verify hash: %w", err)
1372- }
1373- if !valid {
1374- return nil, fmt.Errorf("hash mismatch: expected %s, got %s", meta.CompressedHash, actualHash)
1375- }
1376 }
13771378 // Load just the one operation (efficient!)
···277 mempool: mempool,
278 didIndex: didIndex,
279 bundleCache: make(map[int]*Bundle),
280+ maxCacheSize: 2, // Keep 10 recent bundles in memory (~50-100 MB)
281 }, nil
282}
283···1345// LoadOperation loads a single operation from a bundle efficiently
1346// This is much faster than LoadBundle() when you only need one operation
1347func (m *Manager) LoadOperation(ctx context.Context, bundleNumber int, position int) (*plc.PLCOperation, error) {
1348+ m.logger.Printf("🔍 DEBUG: LoadOperation called (bundle=%d, position=%d)", bundleNumber, position)
13491350 // Validate bundle exists in index
1351+ _, err := m.index.GetBundle(bundleNumber)
1352 if err != nil {
1353 return nil, fmt.Errorf("bundle not in index: %w", err)
1354 }
···1362 path := filepath.Join(m.config.BundleDir, fmt.Sprintf("%06d.jsonl.zst", bundleNumber))
1363 if !m.operations.FileExists(path) {
1364 return nil, fmt.Errorf("bundle file not found: %s", path)
000000000001365 }
13661367 // Load just the one operation (efficient!)
+16-16
bundle/operations.go
···8 "fmt"
9 "io"
10 "os"
011 "time"
1213 gozstd "github.com/DataDog/zstd"
···121 return contentHash, compressedHash, contentSize, compressedSize, nil
122}
12300000000124// LoadOperationAtPosition loads a single operation from a bundle without loading the entire bundle
125// This is much more efficient for single-operation lookups
126func (op *Operations) LoadOperationAtPosition(path string, position int) (*plc.PLCOperation, error) {
···128 return nil, fmt.Errorf("invalid position: %d", position)
129 }
130131- // ✨ Add this debug log
132- startTime := time.Now()
133- defer func() {
134- elapsed := time.Since(startTime)
135- op.logger.Printf("DEBUG: LoadOperationAtPosition(pos=%d) took %v (scanned %d lines)",
136- position, elapsed, position+1)
137- }()
138-139- // Open compressed file
140 file, err := os.Open(path)
141 if err != nil {
142 return nil, fmt.Errorf("failed to open file: %w", err)
143 }
144 defer file.Close()
145146- // Create zstd decompression reader (streaming)
147 reader := gozstd.NewReader(file)
148 defer reader.Close()
149150- // Use scanner to skip to target line
000151 scanner := bufio.NewScanner(reader)
152- buf := make([]byte, 0, 64*1024)
153- scanner.Buffer(buf, 1024*1024)
154155 lineNum := 0
156 for scanner.Scan() {
157 if lineNum == position {
158- // Found target line!
159 line := scanner.Bytes()
160161 var operation plc.PLCOperation
···163 return nil, fmt.Errorf("failed to parse operation at position %d: %w", position, err)
164 }
165166- // Store raw JSON
167 operation.RawJSON = make([]byte, len(line))
168 copy(operation.RawJSON, line)
169···176 return nil, fmt.Errorf("scanner error: %w", err)
177 }
178179- return nil, fmt.Errorf("position %d not found (bundle has %d operations)", position, lineNum)
180}
181182// ========================================
···8 "fmt"
9 "io"
10 "os"
11+ "sync"
12 "time"
1314 gozstd "github.com/DataDog/zstd"
···122 return contentHash, compressedHash, contentSize, compressedSize, nil
123}
124125+// Pool for scanner buffers (reuse across requests)
126+var scannerBufPool = sync.Pool{
127+ New: func() interface{} {
128+ buf := make([]byte, 64*1024)
129+ return &buf
130+ },
131+}
132+133// LoadOperationAtPosition loads a single operation from a bundle without loading the entire bundle
134// This is much more efficient for single-operation lookups
135func (op *Operations) LoadOperationAtPosition(path string, position int) (*plc.PLCOperation, error) {
···137 return nil, fmt.Errorf("invalid position: %d", position)
138 }
139000000000140 file, err := os.Open(path)
141 if err != nil {
142 return nil, fmt.Errorf("failed to open file: %w", err)
143 }
144 defer file.Close()
1450146 reader := gozstd.NewReader(file)
147 defer reader.Close()
148149+ // ✨ Get buffer from pool
150+ bufPtr := scannerBufPool.Get().(*[]byte)
151+ defer scannerBufPool.Put(bufPtr) // Return to pool when done
152+153 scanner := bufio.NewScanner(reader)
154+ scanner.Buffer(*bufPtr, 512*1024)
0155156 lineNum := 0
157 for scanner.Scan() {
158 if lineNum == position {
0159 line := scanner.Bytes()
160161 var operation plc.PLCOperation
···163 return nil, fmt.Errorf("failed to parse operation at position %d: %w", position, err)
164 }
165166+ // Copy raw JSON
167 operation.RawJSON = make([]byte, len(line))
168 copy(operation.RawJSON, line)
169···176 return nil, fmt.Errorf("scanner error: %w", err)
177 }
178179+ return nil, fmt.Errorf("position %d not found", position)
180}
181182// ========================================