mongodb_storage

package module
v0.0.0-...-fd42135 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2023 License: MIT Imports: 13 Imported by: 1

README

Mongodb Storage

一、这是什么

以MongoDB为存储引擎的Storage实现,当前仓库为比较底层的存储层实现,你可以与storage-lock结合使用。

二、安装

go get -u github.com/storage-lock/go-mongodb-storage

三、API示例

3.1 从URI创建MongodbStorage

package main

import (
	"context"
	"fmt"
	mongodb_storage "github.com/storage-lock/go-mongodb-storage"
)

func main() {

	// 使用一个uri形式的数据库连接字符串创建ConnectionManager
	uri := "mongodb://root:UeGqAm8CxYGldMDLoNNt@192.168.128.206:27017/?connectTimeoutMS=300000"
	connectionManager := mongodb_storage.NewMongoConnectionManager(uri)

	// 然后从这个ConnectionManager创建MongodbStorage
	options := mongodb_storage.NewMongoStorageOptions().SetConnectionManager(connectionManager)
	storage, err := mongodb_storage.NewMongoStorage(context.Background(), options)
	if err != nil {
		panic(err)
	}
	fmt.Println(storage.GetName())

}

3.2 从mongo.Client创建MongodbStorage

package main

import (
	"context"
	"fmt"
	mongodb_storage "github.com/storage-lock/go-mongodb-storage"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

func main() {

	// 假设已经在其它地方初始化数据库连接得到了一个*mongo.Client
	uri := "mongodb://root:UeGqAm8CxYGldMDLoNNt@192.168.128.206:27017/?connectTimeoutMS=300000"
	client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(uri))
	if err != nil {
		panic(err)
	}

	// 则可以从这个*mongo.Client中创建一个Mongodb Storage
	connectionManager := mongodb_storage.NewMongoConnectionManagerFromClient(client)
	options := mongodb_storage.NewMongoStorageOptions().SetConnectionManager(connectionManager)
	storage, err := mongodb_storage.NewMongoStorage(context.Background(), options)
	if err != nil {
		panic(err)
	}
	fmt.Println(storage.GetName())

}

Documentation

Index

Constants

View Source
const MongoConnectionManagerName = "mongodb-connection-manager"
View Source
const MongoStorageName = "mongodb-storage"

Variables

View Source
var (

	// ErrDatabaseNameEmpty 参数中数据库名字为空
	ErrDatabaseNameEmpty = fmt.Errorf("DatabaseName can not empty")

	// ErrCollectionNameEmpty 参数中集合名字为空
	ErrCollectionNameEmpty = fmt.Errorf("CollectionName can not empty")

	// ErrConnectionManagerNil 连接管理器没有指定
	ErrConnectionManagerNil = fmt.Errorf("ConnectionManager can not nil")
)

Functions

This section is empty.

Types

type ListMongoLockIterator

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

ListMongoLockIterator 用于迭代列出mongo中的所有的锁

func NewListMongoLockIterator

func NewListMongoLockIterator(cursor *mongo.Cursor) *ListMongoLockIterator

func (*ListMongoLockIterator) Next

func (x *ListMongoLockIterator) Next() bool

func (*ListMongoLockIterator) Value

func (x *ListMongoLockIterator) Value() *storage.LockInformation

type MongoConnectionManager

type MongoConnectionManager struct {

	// 连接到数据库的地址
	URI string
	// contains filtered or unexported fields
}

MongoConnectionManager 负责维护与Mongo数据库的连接

func NewMongoConnectionManagerFromClient

func NewMongoConnectionManagerFromClient(client *mongo.Client) *MongoConnectionManager

NewMongoConnectionManagerFromClient 复用已经存在的mongo client,从其创建连接管理器

func NewMongoConnectionManagerFromURI

func NewMongoConnectionManagerFromURI(uri string) *MongoConnectionManager

NewMongoConnectionManagerFromURI 从Mongo uri创建连接管理器

func (*MongoConnectionManager) Name

func (x *MongoConnectionManager) Name() string

func (*MongoConnectionManager) Return

func (x *MongoConnectionManager) Return(ctx context.Context, connection *mongo.Client) error

func (*MongoConnectionManager) Shutdown

func (x *MongoConnectionManager) Shutdown(ctx context.Context) error

func (*MongoConnectionManager) Take

type MongoLock

type MongoLock struct {

	// 锁的ID,这个字段是一个唯一字段,这个字段会作为Mongo中的Collection的主键字段,保证同一个锁同时只会存在一个
	ID string `bson:"_id"`

	// 锁的当前持有者的ID
	OwnerId string `bson:"owner_id"`

	// 锁的版本,每次修改都会增加1
	Version storage.Version `bson:"version"`

	// 锁的json信息,存储着更上层的通用的锁的信息,这里只需要认为它是一个字符串就可以了
	LockJsonString string `bson:"lock_json_string"`
}

MongoLock 锁在Mongo中存储的结构

type MongoStorage

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

MongoStorage MongoDB的存储引擎实现

func NewMongoStorage

func NewMongoStorage(ctx context.Context, options *MongoStorageOptions) (*MongoStorage, error)

NewMongoStorage 创建一个基于MongoDB的存储引擎

func (*MongoStorage) Close

func (x *MongoStorage) Close(ctx context.Context) error

func (*MongoStorage) CreateWithVersion

func (x *MongoStorage) CreateWithVersion(ctx context.Context, lockId string, version storage.Version, lockInformation *storage.LockInformation) error

func (*MongoStorage) DeleteWithVersion

func (x *MongoStorage) DeleteWithVersion(ctx context.Context, lockId string, exceptedVersion storage.Version, lockInformation *storage.LockInformation) error

func (*MongoStorage) Get

func (x *MongoStorage) Get(ctx context.Context, lockId string) (string, error)

func (*MongoStorage) GetName

func (x *MongoStorage) GetName() string

func (*MongoStorage) GetTime

func (x *MongoStorage) GetTime(ctx context.Context) (time.Time, error)

func (*MongoStorage) Init

func (x *MongoStorage) Init(ctx context.Context) error

func (*MongoStorage) List

func (x *MongoStorage) List(ctx context.Context) (iterator.Iterator[*storage.LockInformation], error)

func (*MongoStorage) UpdateWithVersion

func (x *MongoStorage) UpdateWithVersion(ctx context.Context, lockId string, exceptedVersion, newVersion storage.Version, lockInformation *storage.LockInformation) error

type MongoStorageOptions

type MongoStorageOptions struct {

	// 获取连接
	ConnectionManager storage.ConnectionManager[*mongo.Client]

	// 要存储到的数据库的名称
	DatabaseName string

	// 集合名称
	CollectionName string
}

MongoStorageOptions Mongo的存储选项

func NewMongoStorageOptions

func NewMongoStorageOptions() *MongoStorageOptions

func NewMongoStorageOptionsWithURI

func NewMongoStorageOptionsWithURI(uri string) *MongoStorageOptions

func (*MongoStorageOptions) Check

func (x *MongoStorageOptions) Check() error

func (*MongoStorageOptions) SetCollectionName

func (x *MongoStorageOptions) SetCollectionName(collectionName string) *MongoStorageOptions

func (*MongoStorageOptions) SetConnectionManager

func (x *MongoStorageOptions) SetConnectionManager(connectionProvider storage.ConnectionManager[*mongo.Client]) *MongoStorageOptions

func (*MongoStorageOptions) SetDatabaseName

func (x *MongoStorageOptions) SetDatabaseName(databaseName string) *MongoStorageOptions

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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