1 Star 0 Fork 0

青文杰/mongo-go-driver

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
options.go 19.07 KB
一键复制 编辑 原始数据 按行查看 历史
Divjot Arora 提交于 2018-07-25 23:12 . Add support for GridFS.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
package gridfs
import (
"reflect"
"time"
"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/core/readconcern"
"github.com/mongodb/mongo-go-driver/core/readpref"
"github.com/mongodb/mongo-go-driver/core/writeconcern"
"github.com/mongodb/mongo-go-driver/mongo/findopt"
)
var bucketBundle = new(BucketBundle)
var uploadBundle = new(UploadBundle)
var findBundle = new(FindBundle)
var nameBundle = new(NameBundle)
// BucketOptioner represents all options that can be used to configure a GridFS bucket.
type BucketOptioner interface {
bucketOption(*Bucket) error
}
// UploadOptioner represents all options that can be used to configure a file upload.
type UploadOptioner interface {
uploadOption(*Upload) error
}
// FindOptioner represents all options that can be used to configure a find command.
type FindOptioner interface {
convertFindOption() findopt.Find
}
// NameOptioner represents all options that can be used for a download by name command.
type NameOptioner interface {
nameOption()
}
// BucketBundle is a bundle of BucketOptioner options
type BucketBundle struct {
option BucketOptioner
next *BucketBundle
}
// bucketOption implements the BucketOptioner interface
func (*BucketBundle) bucketOption(*Bucket) error {
return nil
}
// BundleBucket bundles BucketOptioner options.
func BundleBucket(opts ...BucketOptioner) *BucketBundle {
head := bucketBundle
for _, opt := range opts {
newBundle := BucketBundle{
option: opt,
next: head,
}
head = &newBundle
}
return head
}
// BucketName specifies the name of the bucket. Defaults to 'fs' if not specified.
func (bb *BucketBundle) BucketName(name string) *BucketBundle {
bundle := &BucketBundle{
option: BucketName(name),
next: bb,
}
return bundle
}
// ChunkSizeBytes specifies the chunk size for the bucket. Defaults to 255KB if not specified.
func (bb *BucketBundle) ChunkSizeBytes(b int32) *BucketBundle {
bundle := &BucketBundle{
option: ChunkSizeBytes(b),
next: bb,
}
return bundle
}
// WriteConcern specifies the write concern for the bucket.
func (bb *BucketBundle) WriteConcern(wc *writeconcern.WriteConcern) *BucketBundle {
bundle := &BucketBundle{
option: WriteConcern(wc),
next: bb,
}
return bundle
}
// ReadConcern specifies the read concern for the bucket.
func (bb *BucketBundle) ReadConcern(rc *readconcern.ReadConcern) *BucketBundle {
bundle := &BucketBundle{
option: ReadConcern(rc),
next: bb,
}
return bundle
}
// ReadPreference specifies the read preference for the bucket.
func (bb *BucketBundle) ReadPreference(rp *readpref.ReadPref) *BucketBundle {
bundle := &BucketBundle{
option: ReadPreference(rp),
next: bb,
}
return bundle
}
// Unbundle transforms a bundle into a slice of BucketOptioner options, deduplicating if specified.
func (bb *BucketBundle) Unbundle(deduplicate bool) ([]BucketOptioner, error) {
options, err := bb.unbundle()
if err != nil {
return nil, err
}
if !deduplicate {
return options, nil
}
// iterate backwards and make dedup slice
optionsSet := make(map[reflect.Type]struct{})
for i := len(options) - 1; i >= 0; i-- {
currOption := options[i]
optionType := reflect.TypeOf(currOption)
if _, ok := optionsSet[optionType]; ok {
// option already found
options = append(options[:i], options[i+1:]...)
continue
}
optionsSet[optionType] = struct{}{}
}
return options, nil
}
// Helper that recursively unwraps bundle into slice of options
func (bb *BucketBundle) unbundle() ([]BucketOptioner, error) {
if bb == nil {
return nil, nil
}
listLen := bb.bundleLength()
options := make([]BucketOptioner, listLen)
index := listLen - 1
for listHead := bb; listHead != nil && listHead.option != nil; listHead = listHead.next {
if converted, ok := listHead.option.(*BucketBundle); ok {
nestedOptions, err := converted.unbundle()
if err != nil {
return nil, err
}
// where to start inserting nested options
startIndex := index - len(nestedOptions) + 1
// add nested options in order
for _, nestedOp := range nestedOptions {
options[startIndex] = nestedOp
startIndex++
}
index -= len(nestedOptions)
continue
}
options[index] = listHead.option
index--
}
return options, nil
}
// Calculates the total length of a bundle, accounting for nested bundles.
func (bb *BucketBundle) bundleLength() int {
if bb == nil {
return 0
}
bundleLen := 0
for ; bb != nil && bb.option != nil; bb = bb.next {
if converted, ok := bb.option.(*BucketBundle); ok {
// nested bundle
bundleLen += converted.bundleLength()
continue
}
bundleLen++
}
return bundleLen
}
// UploadBundle is a bundle of UploadOptioner options.
type UploadBundle struct {
option UploadOptioner
next *UploadBundle
}
// uploadOption implements the UploadOptioner interface.
func (ub *UploadBundle) uploadOption(*Upload) error {
return nil
}
// BundleUpload bundles UploadOptioner options.
func BundleUpload(opts ...UploadOptioner) *UploadBundle {
head := uploadBundle
for _, opt := range opts {
newBundle := UploadBundle{
option: opt,
next: head,
}
head = &newBundle
}
return head
}
// ChunkSizeBytes specifies the chunk size for the bucket. Defaults to 255KB if not specified.
func (ub *UploadBundle) ChunkSizeBytes(b int32) *UploadBundle {
bundle := &UploadBundle{
option: ChunkSizeBytes(b),
next: ub,
}
return bundle
}
// Metadata specifies the metadata for a file upload.
func (ub *UploadBundle) Metadata(doc *bson.Document) *UploadBundle {
bundle := &UploadBundle{
option: Metadata(doc),
next: ub,
}
return bundle
}
// Unbundle transforms a bundle into a slice of BucketOptioner options, deduplicating if specified.
func (ub *UploadBundle) Unbundle(deduplicate bool) ([]UploadOptioner, error) {
options, err := ub.unbundle()
if err != nil {
return nil, err
}
if !deduplicate {
return options, nil
}
// iterate backwards and make dedup slice
optionsSet := make(map[reflect.Type]struct{})
for i := len(options) - 1; i >= 0; i-- {
currOption := options[i]
optionType := reflect.TypeOf(currOption)
if _, ok := optionsSet[optionType]; ok {
// option already found
options = append(options[:i], options[i+1:]...)
continue
}
optionsSet[optionType] = struct{}{}
}
return options, nil
}
// Helper that recursively unwraps bundle into slice of options
func (ub *UploadBundle) unbundle() ([]UploadOptioner, error) {
if ub == nil {
return nil, nil
}
listLen := ub.bundleLength()
options := make([]UploadOptioner, listLen)
index := listLen - 1
for listHead := ub; listHead != nil && listHead.option != nil; listHead = listHead.next {
if converted, ok := listHead.option.(*UploadBundle); ok {
nestedOptions, err := converted.unbundle()
if err != nil {
return nil, err
}
// where to start inserting nested options
startIndex := index - len(nestedOptions) + 1
// add nested options in order
for _, nestedOp := range nestedOptions {
options[startIndex] = nestedOp
startIndex++
}
index -= len(nestedOptions)
continue
}
options[index] = listHead.option
index--
}
return options, nil
}
// Calculates the total length of a bundle, accounting for nested bundles.
func (ub *UploadBundle) bundleLength() int {
if ub == nil {
return 0
}
bundleLen := 0
for ; ub != nil && ub.option != nil; ub = ub.next {
if converted, ok := ub.option.(*UploadBundle); ok {
// nested bundle
bundleLen += converted.bundleLength()
continue
}
bundleLen++
}
return bundleLen
}
// FindBundle represents a bundle of GridFS Find options.
type FindBundle struct {
option FindOptioner
next *FindBundle
}
func (fb *FindBundle) convertFindOption() findopt.Find {
return nil
}
// BundleFind bundles FindOptioner options.
func BundleFind(opts ...FindOptioner) *FindBundle {
head := findBundle
for _, opt := range opts {
newBundle := FindBundle{
option: opt,
next: head,
}
head = &newBundle
}
return head
}
// BatchSize specifies the number of documents to return per batch.
func (fb *FindBundle) BatchSize(i int32) *FindBundle {
return &FindBundle{
option: BatchSize(i),
next: fb,
}
}
// Limit specifies the maximum number of documents to return.
func (fb *FindBundle) Limit(i int32) *FindBundle {
return &FindBundle{
option: Limit(i),
next: fb,
}
}
// MaxTime specifies the maximum amount of time to allow the query to run.
func (fb *FindBundle) MaxTime(d time.Duration) *FindBundle {
return &FindBundle{
option: MaxTime(d),
next: fb,
}
}
// NoCursorTimeout specifies that the server should not time out after an inactivity period.
func (fb *FindBundle) NoCursorTimeout(b bool) *FindBundle {
return &FindBundle{
option: NoCursorTimeout(b),
next: fb,
}
}
// Skip specifies the number of documents to skip before returning.
func (fb *FindBundle) Skip(i int32) *FindBundle {
return &FindBundle{
option: Skip(i),
next: fb,
}
}
// Sort specifies the order by which to sort results.
func (fb *FindBundle) Sort(sort interface{}) *FindBundle {
return &FindBundle{
option: Sort(sort),
next: fb,
}
}
// Unbundle transforms a bundle into a slice of BucketOptioner options, deduplicating if specified.
func (fb *FindBundle) Unbundle(deduplicate bool) ([]FindOptioner, error) {
options, err := fb.unbundle()
if err != nil {
return nil, err
}
if !deduplicate {
return options, nil
}
// iterate backwards and make dedup slice
optionsSet := make(map[reflect.Type]struct{})
for i := len(options) - 1; i >= 0; i-- {
currOption := options[i]
optionType := reflect.TypeOf(currOption)
if _, ok := optionsSet[optionType]; ok {
// option already found
options = append(options[:i], options[i+1:]...)
continue
}
optionsSet[optionType] = struct{}{}
}
return options, nil
}
// Helper that recursively unwraps bundle into slice of options
func (fb *FindBundle) unbundle() ([]FindOptioner, error) {
if fb == nil {
return nil, nil
}
listLen := fb.bundleLength()
options := make([]FindOptioner, listLen)
index := listLen - 1
for listHead := fb; listHead != nil && listHead.option != nil; listHead = listHead.next {
if converted, ok := listHead.option.(*FindBundle); ok {
nestedOptions, err := converted.unbundle()
if err != nil {
return nil, err
}
// where to start inserting nested options
startIndex := index - len(nestedOptions) + 1
// add nested options in order
for _, nestedOp := range nestedOptions {
options[startIndex] = nestedOp
startIndex++
}
index -= len(nestedOptions)
continue
}
options[index] = listHead.option
index--
}
return options, nil
}
// Calculates the total length of a bundle, accounting for nested bundles.
func (fb *FindBundle) bundleLength() int {
if fb == nil {
return 0
}
bundleLen := 0
for ; fb != nil && fb.option != nil; fb = fb.next {
if converted, ok := fb.option.(*FindBundle); ok {
// nested bundle
bundleLen += converted.bundleLength()
continue
}
bundleLen++
}
return bundleLen
}
// NameBundle bundles valid options for download by name commands.
type NameBundle struct {
option NameOptioner
next *NameBundle
}
func (nb *NameBundle) nameOption() {}
// BundleName bundles NameOptioner options.
func BundleName(opts ...NameOptioner) *NameBundle {
head := nameBundle
for _, opt := range opts {
newBundle := NameBundle{
option: opt,
next: head,
}
head = &newBundle
}
return head
}
// Revision specifies which revision of the file to retrieve. Defaults to -1.
// * Revision numbers are defined as follows:
// * 0 = the original stored file
// * 1 = the first revision
// * 2 = the second revision
// * etc…
// * -2 = the second most recent revision
// * -1 = the most recent revision
func (nb *NameBundle) Revision(i int32) *NameBundle {
return &NameBundle{
option: Revision(i),
next: nb,
}
}
// Unbundle transforms a bundle into a slice of BucketOptioner options, deduplicating if specified.
func (nb *NameBundle) Unbundle(deduplicate bool) ([]NameOptioner, error) {
options, err := nb.unbundle()
if err != nil {
return nil, err
}
if !deduplicate {
return options, nil
}
// iterate backwards and make dedup slice
optionsSet := make(map[reflect.Type]struct{})
for i := len(options) - 1; i >= 0; i-- {
currOption := options[i]
optionType := reflect.TypeOf(currOption)
if _, ok := optionsSet[optionType]; ok {
// option already found
options = append(options[:i], options[i+1:]...)
continue
}
optionsSet[optionType] = struct{}{}
}
return options, nil
}
// Helper that recursively unwraps bundle into slice of options
func (nb *NameBundle) unbundle() ([]NameOptioner, error) {
if nb == nil {
return nil, nil
}
listLen := nb.bundleLength()
options := make([]NameOptioner, listLen)
index := listLen - 1
for listHead := nb; listHead != nil && listHead.option != nil; listHead = listHead.next {
if converted, ok := listHead.option.(*NameBundle); ok {
nestedOptions, err := converted.unbundle()
if err != nil {
return nil, err
}
// where to start inserting nested options
startIndex := index - len(nestedOptions) + 1
// add nested options in order
for _, nestedOp := range nestedOptions {
options[startIndex] = nestedOp
startIndex++
}
index -= len(nestedOptions)
continue
}
options[index] = listHead.option
index--
}
return options, nil
}
// Calculates the total length of a bundle, accounting for nested bundles.
func (nb *NameBundle) bundleLength() int {
if nb == nil {
return 0
}
bundleLen := 0
for ; nb != nil && nb.option != nil; nb = nb.next {
if converted, ok := nb.option.(*NameBundle); ok {
// nested bundle
bundleLen += converted.bundleLength()
continue
}
bundleLen++
}
return bundleLen
}
// BucketName specifies the name of the bucket. Defaults to 'fs' if not specified.
func BucketName(name string) OptBucketName {
return OptBucketName(name)
}
// ChunkSizeBytes specifies the chunk size for the bucket. Defaults to 255KB if not specified.
func ChunkSizeBytes(b int32) OptChunkSizeBytes {
return OptChunkSizeBytes(b)
}
// WriteConcern specifies the write concern for the bucket.
func WriteConcern(wc *writeconcern.WriteConcern) OptWriteConcern {
return OptWriteConcern{
WriteConcern: wc,
}
}
// ReadConcern specifies the read concern for the bucket.
func ReadConcern(rc *readconcern.ReadConcern) OptReadConcern {
return OptReadConcern{
ReadConcern: rc,
}
}
// ReadPreference specifies the read preference for the bucket.
func ReadPreference(rp *readpref.ReadPref) OptReadPreference {
return OptReadPreference{
ReadPref: rp,
}
}
// Metadata specifies the metadata for a file upload.
func Metadata(doc *bson.Document) OptMetadata {
return OptMetadata{
Document: doc,
}
}
// BatchSize specifies the number of documents to return per batch.
func BatchSize(i int32) OptBatchSize {
return OptBatchSize(i)
}
// Limit specifies the maximum number of documents to return.
func Limit(i int32) OptLimit {
return OptLimit(i)
}
// MaxTime specifies the maximum amount of time to allow the query to run.
func MaxTime(d time.Duration) OptMaxTime {
return OptMaxTime(d)
}
// NoCursorTimeout specifies that the server should not time out after an inactivity period.
func NoCursorTimeout(b bool) OptNoCursorTimeout {
return OptNoCursorTimeout(b)
}
// Skip specifies the number of documents to skip before returning.
func Skip(i int32) OptSkip {
return OptSkip(i)
}
// Sort specifies the order by which to sort results.
func Sort(sort interface{}) OptSort {
return OptSort{
Sort: sort,
}
}
// Revision specifies which revision of the file to retrieve. Defaults to -1.
// * Revision numbers are defined as follows:
// * 0 = the original stored file
// * 1 = the first revision
// * 2 = the second revision
// * etc…
// * -2 = the second most recent revision
// * -1 = the most recent revision
func Revision(i int32) OptRevision {
return OptRevision(i)
}
// OptBucketName specifies the name of the bucket. Defaults to 'fs' if not specified.
type OptBucketName string
// bucketOption implements the BucketOptioner interface.
func (opt OptBucketName) bucketOption(bucket *Bucket) error {
bucket.name = string(opt)
return nil
}
// OptChunkSizeBytes specifies the chunk size for the bucket. Defaults to 255KB if not specified.
type OptChunkSizeBytes int32
// bucketOption implements the BucketOptioner interface.
func (opt OptChunkSizeBytes) bucketOption(bucket *Bucket) error {
bucket.chunkSize = int32(opt)
return nil
}
// uploadOption implements the UploadOptioner interface.
func (opt OptChunkSizeBytes) uploadOption(upload *Upload) error {
upload.chunkSize = int32(opt)
return nil
}
// OptWriteConcern specifies the write concern for the bucket.
type OptWriteConcern struct {
*writeconcern.WriteConcern
}
// bucketOption implements the BucketOptioner interface.
func (opt OptWriteConcern) bucketOption(bucket *Bucket) error {
bucket.wc = opt.WriteConcern
return nil
}
// OptReadConcern specifies the read concern for the bucket.
type OptReadConcern struct {
*readconcern.ReadConcern
}
// bucketOption implements the BucketOptioner interface.
func (opt OptReadConcern) bucketOption(bucket *Bucket) error {
bucket.rc = opt.ReadConcern
return nil
}
// OptReadPreference specifies the read preference for the bucket.
type OptReadPreference struct {
*readpref.ReadPref
}
// bucketOption implements the BucketOptioner interface.
func (opt OptReadPreference) bucketOption(bucket *Bucket) error {
bucket.rp = opt.ReadPref
return nil
}
// OptMetadata specifies the metadata for a file upload.
type OptMetadata struct {
*bson.Document
}
// uploadOption implements the UploadOptioner interface.
func (opt OptMetadata) uploadOption(upload *Upload) error {
upload.metadata = opt.Document
return nil
}
// OptBatchSize specifies the number of documents to return per batch.
type OptBatchSize int32
func (opt OptBatchSize) convertFindOption() findopt.Find {
return findopt.BatchSize(int32(opt))
}
// OptLimit specifies the maximum number of documents to return.
type OptLimit int32
func (opt OptLimit) convertFindOption() findopt.Find {
return findopt.Limit(int64(opt))
}
// OptMaxTime specifies the maximum amount of time to allow the query to run.
type OptMaxTime time.Duration
func (opt OptMaxTime) convertFindOption() findopt.Find {
return findopt.MaxTime(time.Duration(opt))
}
// OptNoCursorTimeout specifies that the server should not time out after an inactivity period.
type OptNoCursorTimeout bool
func (opt OptNoCursorTimeout) convertFindOption() findopt.Find {
return findopt.NoCursorTimeout(bool(opt))
}
// OptSkip specifies the number of documents to skip before returning.
type OptSkip int32
func (opt OptSkip) convertFindOption() findopt.Find {
return findopt.Skip(int64(opt))
}
// OptSort specifies the order by which to sort results.
type OptSort struct {
Sort interface{}
}
func (opt OptSort) convertFindOption() findopt.Find {
return findopt.Sort(opt.Sort)
}
// OptRevision specifies which revision of the file to retrieve. Defaults to -1.
// * Revision numbers are defined as follows:
// * 0 = the original stored file
// * 1 = the first revision
// * 2 = the second revision
// * etc…
// * -2 = the second most recent revision
// * -1 = the most recent revision
type OptRevision int32
func (OptRevision) nameOption() {}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/qingwenjie/mongo-go-driver.git
git@gitee.com:qingwenjie/mongo-go-driver.git
qingwenjie
mongo-go-driver
mongo-go-driver
v0.0.17

搜索帮助