mongogateway

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2026 License: MIT Imports: 31 Imported by: 0

README

TreeDB MongoDB Gateway

This package contains the small MongoDB-compatible gateway used to expose TreeDB collections to MongoDB clients.

The standalone server can be run directly from this directory-level entrypoint:

GOWORK=off go run ./TreeDB/mongo_gateway/server.go \
  -addr 127.0.0.1:27017 \
  -dir /tmp/treedb-mongo-gateway \
  -profile command_wal_durable \
  -document-format bson

GOWORK=off keeps go run from resolving the gateway library through a parent workspace that replaces github.com/snissn/gomap with another checkout.

Equivalent Makefile targets from the repository root:

make build-mongo-gateway
make run-mongo-gateway

Useful Makefile overrides:

MONGO_GATEWAY_ADDR=127.0.0.1:27018 \
MONGO_GATEWAY_DIR=/tmp/treedb-mongo-gateway-dev \
MONGO_GATEWAY_PROFILE=command_wal_durable \
make run-mongo-gateway

The server prints a MongoDB URI when it starts. MongoDB clients should connect with direct/single-server mode when their driver supports it, for example:

mongodb://127.0.0.1:27017/?directConnection=true

The gateway implements an intentionally narrow command subset around hello/ping, insert, find/getMore/killCursors, update, delete, and collection/index metadata. It does not provide authentication, replica set behavior, sharding, sessions, transactions, change streams, aggregation, or full MongoDB compatibility.

Documentation

Index

Constants

View Source
const (
	DefaultStandaloneAddr           = "127.0.0.1:27017"
	DefaultStandaloneDocumentFormat = collections.DocumentFormatBSON
)

Variables

This section is empty.

Functions

func EncodePrimaryKey

func EncodePrimaryKey(value bson.RawValue) ([]byte, error)

EncodePrimaryKey encodes a MongoDB _id value into the canonical TreeDB collection primary key used by the Mongo gateway.

Types

type ClusterCatalogVersionProvider

type ClusterCatalogVersionProvider func(context.Context) (uint64, error)

type ServeBuffers

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

ServeBuffers holds reusable per-connection buffers for callers that dispatch individual wire messages without using ServeConn.

ServeBuffers is not safe for concurrent use. Use one instance per logical connection/worker.

type Server

type Server struct {
	MaxMessageLength       int32
	MaxFindScanDocuments   int
	MaxCursorRetainedBytes int
	MaxOpenCursors         int
	CursorIdleTimeout      time.Duration
	// UpdateCoalescingMaxDelay waits for additional same-collection update
	// commands before publishing a batch. Zero means only coalesce already-queued
	// work; negative disables coalescing.
	UpdateCoalescingMaxDelay time.Duration
	// UpdateCoalescingMaxBatch caps one coalesced same-collection update publish.
	// Values above maxUpdateCoalescingBatch are clamped.
	UpdateCoalescingMaxBatch int
	// UpdateCoalescingIdleTTL removes an idle per-collection coalescer after this
	// duration. Zero uses the default; negative disables idle removal.
	UpdateCoalescingIdleTTL time.Duration
	// InsertCoalescingMaxDelay waits for additional same-collection single-document
	// BSON insert commands before publishing a batch. Zero means only coalesce
	// already-queued work; negative disables coalescing.
	InsertCoalescingMaxDelay time.Duration
	// InsertCoalescingMaxBatch caps one coalesced same-collection insert publish.
	// Values above maxInsertCoalescingBatch are clamped.
	InsertCoalescingMaxBatch int
	// InsertCoalescingIdleTTL removes an idle per-collection coalescer after this
	// duration. Zero uses the default; negative disables idle removal.
	InsertCoalescingIdleTTL   time.Duration
	Collections               *collections.CollectionManager
	DefaultCollectionOptions  collections.CollectionOptions
	DefaultIndexStoragePolicy collections.RootStoragePolicy
	ClusterSubmitter          treenativewire.ClusterSubmitter
	ClusterCatalogVersion     ClusterCatalogVersionProvider
	// ClusterIdempotencyNonce scopes generated cluster mutation idempotency
	// keys to one gateway process epoch. NewServer initializes a random nonce
	// so sequence-based keys are not reused after restart.
	ClusterIdempotencyNonce string
	// contains filtered or unexported fields
}

func NewServer

func NewServer() *Server

func (*Server) Close

func (s *Server) Close() error

func (*Server) ListenAndServe

func (s *Server) ListenAndServe(ctx context.Context, addr string) error

ListenAndServe listens on addr and serves MongoDB wire-protocol clients.

func (*Server) Serve

func (s *Server) Serve(ctx context.Context, ln net.Listener) error

Serve accepts MongoDB wire-protocol connections on ln until ctx is canceled, ln is closed, or the gateway returns an accept error. Serve owns ln and closes it before returning.

func (*Server) ServeConn

func (s *Server) ServeConn(ctx context.Context, conn net.Conn) error

func (*Server) ServeOne

func (s *Server) ServeOne(rw io.ReadWriter) error

ServeOne serves a single MongoDB wire message with a one-shot cursor owner. Callers serving a real long-lived connection should use ServeOneWithOwner or ServeConn so cursors survive across getMore/killCursors commands on the same connection owner.

func (*Server) ServeOneWithOwner

func (s *Server) ServeOneWithOwner(rw io.ReadWriter, cursorOwner int64) error

func (*Server) ServeOneWithOwnerBuffered

func (s *Server) ServeOneWithOwnerBuffered(rw io.ReadWriter, cursorOwner int64, buffers *ServeBuffers) error

ServeOneWithOwnerBuffered serves one MongoDB wire message with caller-owned reusable buffers. It is intended for in-process dispatchers and benchmarks that need the same buffer reuse behavior as ServeConn.

type StandaloneOptions

type StandaloneOptions struct {
	Dir     string
	Profile treedb.Profile

	DefaultCollectionOptions  collections.CollectionOptions
	DefaultIndexStoragePolicy collections.RootStoragePolicy

	MaxMessageLength         int32
	MaxFindScanDocuments     int
	MaxCursorRetainedBytes   int
	MaxOpenCursors           int
	CursorIdleTimeout        time.Duration
	UpdateCoalescingMaxDelay time.Duration
	// UpdateCoalescingMaxBatchSet distinguishes an unset zero value from an
	// explicit zero, which disables update coalescing.
	UpdateCoalescingMaxBatchSet bool
	UpdateCoalescingMaxBatch    int
	UpdateCoalescingIdleTTL     time.Duration
	InsertCoalescingMaxDelay    time.Duration
	// InsertCoalescingMaxBatchSet distinguishes an unset zero value from an
	// explicit zero, which disables insert coalescing.
	InsertCoalescingMaxBatchSet bool
	InsertCoalescingMaxBatch    int
	InsertCoalescingIdleTTL     time.Duration
	ClusterSubmitter            treenativewire.ClusterSubmitter
	ClusterCatalogVersion       ClusterCatalogVersionProvider
}

StandaloneOptions configures a TreeDB-backed MongoDB gateway process.

The standalone server intentionally exposes a small MongoDB-compatible subset implemented by Server. It is suitable for local clients and benchmarks, not for claiming full MongoDB feature parity.

func NormalizeStandaloneOptions

func NormalizeStandaloneOptions(opts StandaloneOptions) (StandaloneOptions, error)

NormalizeStandaloneOptions applies standalone defaults and validates options.

type StandaloneServer

type StandaloneServer struct {
	Options     StandaloneOptions
	Backend     *backenddb.DB
	Collections *collections.CollectionManager
	Server      *Server
	// contains filtered or unexported fields
}

StandaloneServer owns the TreeDB backend, collection manager, and MongoDB gateway server used by the standalone gateway executable.

func OpenStandaloneServer

func OpenStandaloneServer(opts StandaloneOptions) (*StandaloneServer, error)

OpenStandaloneServer opens TreeDB and returns a standalone MongoDB gateway.

func (*StandaloneServer) Close

func (s *StandaloneServer) Close() error

Close stops active MongoDB connections and closes the TreeDB backend. It is safe to call multiple times.

func (*StandaloneServer) ListenAndServe

func (s *StandaloneServer) ListenAndServe(ctx context.Context, addr string) error

ListenAndServe listens on addr and serves MongoDB clients for a standalone TreeDB gateway.

func (*StandaloneServer) Serve

func (s *StandaloneServer) Serve(ctx context.Context, ln net.Listener) error

Serve accepts MongoDB clients for a standalone TreeDB gateway. Serve owns ln and closes it before returning, matching Server.Serve.

Directories

Path Synopsis
Package fastclient provides a narrow, low-overhead client for bulk inserts into the TreeDB Mongo gateway.
Package fastclient provides a narrow, low-overhead client for bulk inserts into the TreeDB Mongo gateway.

Jump to

Keyboard shortcuts

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