mirror of
https://github.com/hanzoai/commerce.git
synced 2026-10-30 15:08:42 +00:00
Multi-tenant commerce and billing: orders, subscriptions, invoices, plans, usage metering and a credit ledger. One Go binary. MIT OR Apache-2.0. Claude-Session: https://claude.ai/code/session_01SAtaDFm8VDiWrhYHNNtxMw
52 lines
831 B
Go
52 lines
831 B
Go
package bigquery
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
)
|
|
|
|
type BufferedWrite struct {
|
|
sync.RWMutex
|
|
Rows []Row
|
|
capacity int
|
|
}
|
|
|
|
func (b *BufferedWrite) Append(r Row) error {
|
|
if b.IsFull() {
|
|
return fmt.Errorf("cannot append to buffer, capacity exceeded: %d elements", len(b.Rows))
|
|
}
|
|
b.Lock()
|
|
defer b.Unlock()
|
|
b.Rows = append(b.Rows, r)
|
|
return nil
|
|
}
|
|
|
|
func (b *BufferedWrite) Length() int {
|
|
return len(b.Rows)
|
|
}
|
|
|
|
func (b *BufferedWrite) Capacity() int {
|
|
return b.capacity
|
|
}
|
|
|
|
func (b *BufferedWrite) IsFull() bool {
|
|
return len(b.Rows) == b.capacity
|
|
}
|
|
|
|
func (b *BufferedWrite) Reset() {
|
|
b.Lock()
|
|
defer b.Unlock()
|
|
b.Rows = make([]Row, 0, b.capacity)
|
|
}
|
|
|
|
func (b *BufferedWrite) Flush() []Row {
|
|
defer b.Reset()
|
|
return b.Rows
|
|
}
|
|
|
|
func NewBufferedWrite(c int) *BufferedWrite {
|
|
b := &BufferedWrite{capacity: c}
|
|
b.Reset()
|
|
return b
|
|
}
|