Files
zeekay ab9f9f1f8c fix(deps): revert wire rename, keep github.com/FerretDB/wire
The rebrand commit (deb28452) renamed github.com/FerretDB/wire to
github.com/hanzoai/docdb-wire, but the hanzoai/docdb-wire repo does
not exist. Reverting that rename so the build works at HEAD and so
future merges with FerretDB upstream stay clean.

Per docdb LLM convention, the wire dep stays as FerretDB/wire
upstream. Only the FerretDB project itself is rebranded to docdb.
2026-05-18 23:22:52 -07:00

90 lines
2.1 KiB
Go

// Copyright 2021 Hanzo AI Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package logging
import (
"fmt"
"log/slog"
"sync"
"github.com/AlekSi/lazyerrors"
"github.com/FerretDB/wire/wirebson"
)
// circularBuffer is a storage of log records in memory.
type circularBuffer struct {
mu sync.RWMutex
records []*slog.Record
index int
}
// newCircularBuffer creates a circular buffer for log records in memory.
func newCircularBuffer(size int) *circularBuffer {
if size < 1 {
panic(fmt.Sprintf("buffer size must be at least 1, but %d provided", size))
}
return &circularBuffer{
records: make([]*slog.Record, size),
}
}
// add adds an entry in circularBuffer.
func (cb *circularBuffer) add(record *slog.Record) {
cb.mu.Lock()
defer cb.mu.Unlock()
cb.records[cb.index] = record
cb.index = (cb.index + 1) % len(cb.records)
}
// get returns entries from circularBuffer.
func (cb *circularBuffer) get() []*slog.Record {
cb.mu.RLock()
defer cb.mu.RUnlock()
l := len(cb.records)
res := make([]*slog.Record, 0, l)
for n := range l {
i := (cb.index + n) % l
if r := cb.records[i]; r != nil {
res = append(res, r)
}
}
return res
}
// getArray is a version of [circularBuffer.get] that returns an array as expected by mongosh.
func (cb *circularBuffer) getArray() (*wirebson.Array, error) {
records := cb.get()
res := wirebson.MakeArray(len(records))
for _, r := range records {
b, err := mongoLogFromRecord(*r, nil, nil).Marshal()
if err != nil {
return nil, lazyerrors.Error(err)
}
if err = res.Add(string(b)); err != nil {
return nil, lazyerrors.Error(err)
}
}
return res, nil
}