mongo

package
v0.0.0-...-7f81a18 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2022 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package mongo provides handful wrapper to access and/or modify the mongo database.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DbClient

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

func Dial

func Dial(addr string) *DbClient

func (*DbClient) Open

func (c *DbClient) Open(db, collection string) (*mgo.Collection, *DbSession)

func (*DbClient) OpenWithLongTimeout

func (c *DbClient) OpenWithLongTimeout(db, collection string) (*mgo.Collection, *DbSession)

func (*DbClient) Wait

func (c *DbClient) Wait()

Wait blocks the goroutine until all sessions are closed.

type DbSession

type DbSession struct {
	*mgo.Session
	// contains filtered or unexported fields
}

func Open

func Open(db, c string) (*mgo.Collection, *DbSession)

Open returns a mgo collection and session.

func OpenWithLongTimeout

func OpenWithLongTimeout(db, c string) (*mgo.Collection, *DbSession)

Open returns a mgo collection and session with long session timeout. It's useful for task specific query, which may need dozens of time to complete. NOTE: It's risky to use such session in server, where may lead to infinite no response status.

func (*DbSession) Close

func (d *DbSession) Close()

type Hashable

type Hashable interface {
	GetId() string
}

type Hint

type Hint struct {
	// ServerName is used to specify the server.
	ServerName string `json:"server_name"`

	// Database and Collection is used to determine the whether this server
	// should be used. It follows the rule to choose the server:
	// 1. Both database and collection argument are perfectly matched.
	// 2. If no such match, the LAST hint matching the db.
	// 3. If no such match(no such database in the config), the LAST hint matching the collection.
	// 4. Choose the first server given in Servers section.
	Database   string `json:"database"`
	Collection string `json:"collection"`
}

type LocalRepos

type LocalRepos struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

LocalRepos is a Key/Value map that cached the data from a particular mongodb to local memory, and keep refreshing data in certian intervals. It's useful for small collection with high read I/O.

func NewLocalRepos

func NewLocalRepos(db, col string, entryTmpl Hashable, opts ...ReposOption) *LocalRepos

func (*LocalRepos) AllItems

func (r *LocalRepos) AllItems() []*ReposKVEntry

func (*LocalRepos) AllKeys

func (r *LocalRepos) AllKeys() []string

func (*LocalRepos) AllValues

func (r *LocalRepos) AllValues() []Hashable

func (*LocalRepos) Close

func (r *LocalRepos) Close()

func (*LocalRepos) Get

func (r *LocalRepos) Get(id string) Hashable

func (*LocalRepos) Init

func (r *LocalRepos) Init()

func (*LocalRepos) Len

func (r *LocalRepos) Len() int

type ReposKVEntry

type ReposKVEntry struct {
	Key   string
	Value Hashable
}

type ReposOption

type ReposOption func(*LocalRepos)

func WithClient

func WithClient(client *DbClient) ReposOption

func WithProjection

func WithProjection(project bson.M) ReposOption

func WithQuery

func WithQuery(query bson.M) ReposOption

func WithRefreshInterval

func WithRefreshInterval(dur time.Duration) ReposOption

type Router

type Router struct {
	Servers []*Server `json:"servers"`
	Hints   []*Hint   `json:"hints"`

	HintMap map[string]*Server `json:"-"`
}

Router is a smart proxy to open a proper mongodb client determined by the required database and/or collection combination.

func LoadJsonFileRouter

func LoadJsonFileRouter(filename string) (*Router, error)

func (*Router) DetermineServer

func (r *Router) DetermineServer(db, c string) (ret *Server)

DetermineServer returns a matching server entry.

func (*Router) Init

func (r *Router) Init() error

Init must be called before any usage. It will perform validation to the router configuration.

type Server

type Server struct {
	// Name is required to identify the server. MUST be unique in the configuation.
	Name string `json:"name"`
	// Address will be directly passed to mgo.Dial()
	Address string `json:"address"`
}

Jump to

Keyboard shortcuts

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