db

package
v0.0.0-...-e3a33ee Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2016 License: Apache-2.0 Imports: 34 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultCachePendingSeqMaxNum  = 10000            // Max number of waiting sequences
	DefaultCachePendingSeqMaxWait = 5 * time.Second  // Max time we'll wait for a pending sequence before sending to missed queue
	DefaultSkippedSeqMaxWait      = 60 * time.Minute // Max time we'll wait for an entry in the missing before purging
)
View Source
const (
	WaiterClosed uint32 = iota
	WaiterHasChanges
	WaiterCheckTerminated
)
View Source
const (
	DBOffline uint32 = iota
	DBStarting
	DBOnline
	DBStopping
	DBResyncing
)
View Source
const (
	DesignDocSyncGateway      = "sync_gateway"
	DesignDocSyncHousekeeping = "sync_housekeeping"
	ViewPrincipals            = "principals"
	ViewChannels              = "channels"
	ViewAccess                = "access"
	ViewAccessVbSeq           = "access_vbseq"
	ViewRoleAccess            = "role_access"
	ViewAllBits               = "all_bits"
	ViewAllDocs               = "all_docs"
	ViewImport                = "import"
	ViewOldRevs               = "old_revs"
	ViewSessions              = "sessions"
)
View Source
const (
	Seq_NotInChannel = iota
	Seq_InChannel
	Seq_Removed
)

Bit flag values

View Source
const (
	Undefined = SequenceType(iota)
	IntSequenceType
	ClockSequenceType
)
View Source
const DefaultRevsLimit = 1000
View Source
const KDefaultRevisionCacheCapacity = 5000

Number of recently-accessed doc revisions to cache in RAM

View Source
const KSyncKeyPrefix = "_sync:"

All special/internal documents the gateway creates have this prefix in their keys.

View Source
const NoSeq = uint64(0x7FFFFFFFFFFFFFFF)

Variables

View Source
var (
	DefaultChannelCacheMinLength = 50               // Keep at least this many entries in cache
	DefaultChannelCacheMaxLength = 500              // Don't put more than this many entries in cache
	DefaultChannelCacheAge       = 60 * time.Second // Keep entries at least this long
)
View Source
var ByteCachePollingTime = 1000 // initial polling time for notify, ms
View Source
var EnableStarChannelLog = true

Enable keeping a channel-log for the "*" channel (channel.UserStarChannel). The only time this channel is needed is if someone has access to "*" (e.g. admin-party) and tracks its changes feed.

View Source
var MaxSequenceID = SequenceID{
	Seq: math.MaxUint64,
	// contains filtered or unexported fields
}
View Source
var RunStateString = []string{
	DBOffline:   "Offline",
	DBStarting:  "Starting",
	DBOnline:    "Online",
	DBStopping:  "Stopping",
	DBResyncing: "Resyncing",
}

Functions

func BodyAttachments

func BodyAttachments(body Body) map[string]interface{}

func ClockMatches

func ClockMatches(a, b []uint64) bool

func ConnectToBucket

func ConnectToBucket(spec base.BucketSpec, callback func(bucket string, err error)) (bucket base.Bucket, err error)

Helper function to open a Couchbase connection and return a specific bucket.

func GenerateBitFlagIndex

func GenerateBitFlagIndex(sequence uint64) uint16

func GenerateBlockKey

func GenerateBlockKey(channelName string, sequence uint64, partition uint16) string

func GenerateBlockKeys

func GenerateBlockKeys(channelName string, minSeq uint64, maxSeq uint64, partition uint16) []string

Returns the set of all block keys required to return sequences from minSeq to maxSeq for the channel, partition

func IsNotFoundError

func IsNotFoundError(err error) bool

func NewKvChannelIndex

func NewKvChannelIndex(channelName string, bucket base.Bucket, partitions *base.IndexPartitions, onChangeCallback func(base.Set)) *kvChannelIndex

func NewSequenceHasher

func NewSequenceHasher(options *SequenceHashOptions) (*sequenceHasher, error)

Creates a new sequenceHasher using 2^exp as mod.

func NewUnmarshalEntry

func NewUnmarshalEntry(docID string, docJSON []byte, vbNo uint16, seq uint64, callback processDocFunc) *unmarshalEntry

UnmarshalEntry represents a document being processed by an UnmarshalWorker.

func NewUnmarshalWorker

func NewUnmarshalWorker(output chan<- *LogEntry, unmarshalWorkQueue chan<- *unmarshalEntry, terminator chan struct{}) *unmarshalWorker

UnmarshalWorker is used to unmarshal incoming entries from the DCP feed in parallel, while maintaining ordering of sequences per vbucket. The main DocChanged loop creates one UnmarshalWorker per vbucket.

func ParseIntSequenceComponent

func ParseIntSequenceComponent(component string, allowEmpty bool) (uint64, error)

func ParseRevisions

func ParseRevisions(body Body) []string

Parses a CouchDB _rev or _revisions property into a list of revision IDs

func ReadJSONFromMIME

func ReadJSONFromMIME(headers http.Header, input io.Reader, into interface{}) error

Parses a JSON MIME body, unmarshaling it into "into".

func SearchSequenceQueue

func SearchSequenceQueue(a SkippedSequenceQueue, x uint64) int

Skipped Sequence version of sort.SearchInts - based on http://golang.org/src/sort/search.go?s=2959:2994#L73

func SearchSortedEntrySet

func SearchSortedEntrySet(a SortedEntrySet, x uint64) int

Skipped Sequence version of sort.SearchInts - based on http://golang.org/src/sort/search.go?s=2959:2994#L73

func VacuumAttachments

func VacuumAttachments(bucket base.Bucket) (int, error)

Deletes all orphaned CouchDB attachments not used by any revisions.

func ValidateDatabaseName

func ValidateDatabaseName(dbName string) error

Types

type AllChannelStats

type AllChannelStats struct {
	Channels []ChannelStats `json:"channels"`
}

type AsyncEvent

type AsyncEvent struct {
}

Currently the AsyncEvent type only manages the Synchronous() check. Future enhancements around async processing would leverage this type.

func (AsyncEvent) Synchronous

func (ae AsyncEvent) Synchronous() bool

type AsyncEventHandler

type AsyncEventHandler struct{}

type AttachmentKey

type AttachmentKey string

Key for retrieving an attachment from Couchbase.

type BitFlagBlock

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

func (*BitFlagBlock) AddEntry

func (b *BitFlagBlock) AddEntry(entry *LogEntry) error

func (*BitFlagBlock) AddEntrySet

func (b *BitFlagBlock) AddEntrySet(entries []*LogEntry) error

func (*BitFlagBlock) Cas

func (b *BitFlagBlock) Cas() uint64

func (*BitFlagBlock) GetAllEntries

func (b *BitFlagBlock) GetAllEntries() []*LogEntry

func (*BitFlagBlock) GetEntries

func (b *BitFlagBlock) GetEntries(vbNo uint16, fromSeq uint64, toSeq uint64, includeKeys bool) (entries []*LogEntry, keySet []string)

Block entry retrieval - used by GetEntries and GetEntriesAndKeys.

func (*BitFlagBlock) Key

func (b *BitFlagBlock) Key() string

func (*BitFlagBlock) Marshal

func (b *BitFlagBlock) Marshal() ([]byte, error)

func (*BitFlagBlock) SetCas

func (b *BitFlagBlock) SetCas(cas uint64)

func (*BitFlagBlock) Unmarshal

func (b *BitFlagBlock) Unmarshal(value []byte) error

type BitFlagBlockData

type BitFlagBlockData struct {
	MinSequence uint64            // Starting sequence
	Entries     map[uint16][]byte // Contents of the cache block doc
}

func (*BitFlagBlockData) MaxSequence

func (b *BitFlagBlockData) MaxSequence() uint64

type BitFlagBufferBlock

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

func (*BitFlagBufferBlock) AddEntry

func (b *BitFlagBufferBlock) AddEntry(entry *LogEntry) error

func (*BitFlagBufferBlock) Cas

func (b *BitFlagBufferBlock) Cas() uint64

func (*BitFlagBufferBlock) GetAllEntries

func (b *BitFlagBufferBlock) GetAllEntries() []*LogEntry

func (*BitFlagBufferBlock) GetEntries

func (b *BitFlagBufferBlock) GetEntries(vbNo uint16, fromSeq uint64, toSeq uint64, includeKeys bool) (entries []*LogEntry, keySet []string)

Block entry retrieval - used by GetEntries and GetEntriesAndKeys.

func (*BitFlagBufferBlock) Key

func (b *BitFlagBufferBlock) Key() string

func (*BitFlagBufferBlock) Marshal

func (b *BitFlagBufferBlock) Marshal() ([]byte, error)

func (*BitFlagBufferBlock) SetCas

func (b *BitFlagBufferBlock) SetCas(cas uint64)

func (*BitFlagBufferBlock) Unmarshal

func (b *BitFlagBufferBlock) Unmarshal(value []byte) error

type BitFlagStorage

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

func NewBitFlagStorage

func NewBitFlagStorage(bucket base.Bucket, channelName string, partitions *base.IndexPartitions) *BitFlagStorage

func (*BitFlagStorage) AddEntrySet

func (b *BitFlagStorage) AddEntrySet(entries []*LogEntry) (clockUpdates base.SequenceClock, err error)

Adds a set

func (*BitFlagStorage) GetChanges

func (b *BitFlagStorage) GetChanges(fromSeq base.SequenceClock, toSeq base.SequenceClock) ([]*LogEntry, error)

func (*BitFlagStorage) ReadLogEntry

func (b *BitFlagStorage) ReadLogEntry(vbNo uint16, sequence uint64) (*LogEntry, error)

Reads a single entry from the index

func (*BitFlagStorage) StoresLogEntries

func (b *BitFlagStorage) StoresLogEntries() bool

func (*BitFlagStorage) WriteLogEntry

func (b *BitFlagStorage) WriteLogEntry(entry *LogEntry) error

type BlockSet

type BlockSet map[string][]*LogEntry // Collection of index entries, indexed by block id.

type Body

type Body map[string]interface{}

The body of a CouchDB document/revision as decoded from JSON.

func ReadMultipartDocument

func ReadMultipartDocument(reader *multipart.Reader) (Body, error)

func (Body) FixJSONNumbers

func (body Body) FixJSONNumbers()

Version of FixJSONNumbers (see base/util.go) that operates on a Body

func (Body) ImmutableAttachmentsCopy

func (body Body) ImmutableAttachmentsCopy() Body

func (Body) ShallowCopy

func (body Body) ShallowCopy() Body

type CacheOptions

type CacheOptions struct {
	ChannelCacheOptions
	CachePendingSeqMaxWait time.Duration // Max wait for pending sequence before skipping
	CachePendingSeqMaxNum  int           // Max number of pending sequences before skipping
	CacheSkippedSeqMaxWait time.Duration // Max wait for skipped sequence before abandoning
}

type ChangeEntry

type ChangeEntry struct {
	Seq     SequenceID  `json:"seq"`
	ID      string      `json:"id"`
	Deleted bool        `json:"deleted,omitempty"`
	Removed base.Set    `json:"removed,omitempty"`
	Doc     Body        `json:"doc,omitempty"`
	Changes []ChangeRev `json:"changes"`
	Err     error       `json:"err,omitempty"` // Used to notify feed consumer of errors
	// contains filtered or unexported fields
}

A changes entry; Database.GetChanges returns an array of these. Marshals into the standard CouchDB _changes format.

func (*ChangeEntry) SetBranched

func (ce *ChangeEntry) SetBranched(isBranched bool)

type ChangeIndex

type ChangeIndex interface {

	// Initialize the index
	Init(context *DatabaseContext, lastSequence SequenceID, onChange func(base.Set), cacheOptions *CacheOptions, indexOptions *ChangeIndexOptions) error

	// Stop the index
	Stop()

	// Clear the index
	Clear()

	// Enable/Disable indexing
	EnableChannelIndexing(enable bool)

	// Retrieve changes in a channel
	GetChanges(channelName string, options ChangesOptions) ([]*LogEntry, error)
	// Retrieve in-memory changes in a channel
	GetCachedChanges(channelName string, options ChangesOptions) (validFrom uint64, entries []*LogEntry)

	// Called to add a document to the index
	DocChanged(docID string, docJSON []byte, seq uint64, vbucket uint16)

	// Retrieves stable sequence for index
	GetStableSequence(docID string) SequenceID

	// Retrieves stable sequence for index.  Stale=false forces a reload of the clock from the bucket,
	// stable=true returns cached value (if available)
	GetStableClock(stale bool) (clock base.SequenceClock, err error)
	// contains filtered or unexported methods
}

type ChangeIndexOptions

type ChangeIndexOptions struct {
	Type          IndexType       // Index type
	Spec          base.BucketSpec // Indexing bucket spec
	Bucket        base.Bucket     // Indexing bucket
	Writer        bool            // Cache Writer
	Options       CacheOptions    // Caching options
	NumShards     uint16          // The number of CBGT shards to use]
	HashFrequency uint16          // Hash frequency for changes feeds
}

func (ChangeIndexOptions) ValidateOrPanic

func (c ChangeIndexOptions) ValidateOrPanic()

type ChangeRev

type ChangeRev map[string]string // Key is always "rev", value is rev ID

type ChangesOptions

type ChangesOptions struct {
	Since       SequenceID // sequence # to start _after_
	Limit       int        // Max number of changes to return, if nonzero
	Conflicts   bool       // Show all conflicting revision IDs, not just winning one?
	IncludeDocs bool       // Include doc body of each change?
	Wait        bool       // Wait for results, instead of immediately returning empty result?
	Continuous  bool       // Run continuously until terminated?
	Terminator  chan bool  // Caller can close this channel to terminate the feed
	HeartbeatMs uint64     // How often to send a heartbeat to the client
	TimeoutMs   uint64     // After this amount of time, close the longpoll connection
	ActiveOnly  bool       // If true, only return information on non-deleted, non-removed revisions
}

Options for changes-feeds

type ChannelCacheOptions

type ChannelCacheOptions struct {
	ChannelCacheMinLength int           // Keep at least this many entries in cache
	ChannelCacheMaxLength int           // Don't put more than this many entries in cache
	ChannelCacheAge       time.Duration // Keep entries at least this long
}

type ChannelIndex

type ChannelIndex interface {
	Add(entry *LogEntry) error
	AddSet(entries []*LogEntry) error
	GetClock() (uint64, error)
	SetClock() (uint64, error)
	GetCachedChanges(options ChangesOptions, stableSequence uint64)
	Compact()
}

ChannelIndex defines the API used by the ChangeIndex to interact with the underlying index storage

type ChannelIndexStats

type ChannelIndexStats struct {
	Clock     string `json:"index_clock,omitempty"`
	ClockHash uint64 `json:"index_clock_hash,omitempty"`
}

type ChannelPollingStats

type ChannelPollingStats struct {
	Clock     string `json:"poll_clock,omitempty"`
	ClockHash uint64 `json:"poll_clock_hash,omitempty"`
}

type ChannelStats

type ChannelStats struct {
	Name         string              `json:"channel_name"`
	IndexStats   ChannelIndexStats   `json:"index,omitempty"`
	PollingStats ChannelPollingStats `json:"poll,omitempty"`
}

type ChannelStorage

type ChannelStorage interface {

	// AddEntrySet adds a set of entries to the channel index
	AddEntrySet(entries []*LogEntry) (clockUpdates base.SequenceClock, err error)

	// GetAllEntries returns all entries for the channel in the specified range, for all vbuckets
	GetChanges(fromSeq base.SequenceClock, channelClock base.SequenceClock) ([]*LogEntry, error)

	// If channel storage implementation uses separate storage for log entries and channel presence,
	// WriteLogEntry and ReadLogEntry can be used to read/write.  Useful when changeIndex wants to
	// manage these document outside the scope of a channel.  StoresLogEntries() allows callers to
	// check whether this is available.
	StoresLogEntries() bool
	ReadLogEntry(vbNo uint16, sequence uint64) (*LogEntry, error)
	WriteLogEntry(entry *LogEntry) error
	// contains filtered or unexported methods
}

ChannelStorage implemented as an interface, to support swapping to different underlying storage model without significant refactoring.

func NewChannelStorage

func NewChannelStorage(bucket base.Bucket, channelName string, partitions *base.IndexPartitions) ChannelStorage

type DBStateChangeEvent

type DBStateChangeEvent struct {
	AsyncEvent
	Doc Body
}

DBStateChangeEvent is raised when a DB goes online or offline. Event has name of DB that is firing event, the admin interface address for interacting with the db. The new state, the reason for the state change, the local system time of the change

func (*DBStateChangeEvent) EventType

func (dsce *DBStateChangeEvent) EventType() EventType

func (*DBStateChangeEvent) String

func (dsce *DBStateChangeEvent) String() string

type Database

type Database struct {
	*DatabaseContext
	// contains filtered or unexported fields
}

Represents a simulated CouchDB database. A new instance is created for each HTTP request, so this struct does not have to be thread-safe.

func CreateDatabase

func CreateDatabase(context *DatabaseContext) (*Database, error)

func GetDatabase

func GetDatabase(context *DatabaseContext, user auth.User) (*Database, error)

Makes a Database object given its name and bucket.

func (*Database) AddDocToChangeEntry

func (db *Database) AddDocToChangeEntry(entry *ChangeEntry, options ChangesOptions)

func (*Database) AuthorizeDocID

func (db *Database) AuthorizeDocID(docid, revid string) error

Returns an HTTP 403 error if the User is not allowed to access any of this revision's channels.

func (*Database) Compact

func (db *Database) Compact() (int, error)

Deletes old revisions that have been moved to individual docs

func (*Database) DeleteAllDocs

func (db *Database) DeleteAllDocs(docType string) error

Deletes all documents in the database

func (*Database) DeleteDesignDoc

func (db *Database) DeleteDesignDoc(ddocName string) (err error)

func (*Database) DeleteDoc

func (db *Database) DeleteDoc(docid string, revid string) (string, error)

Deletes a document, by adding a new revision whose "_deleted" property is true.

func (*Database) DeleteSpecial

func (db *Database) DeleteSpecial(doctype string, docid string, revid string) error

func (*Database) DocCount

func (db *Database) DocCount() int

The number of documents in the database.

func (*Database) ForEachDocID

func (db *Database) ForEachDocID(callback ForEachDocIDFunc, resultsOpts ForEachDocIDOptions) error

Iterates over all documents in the database, calling the callback function on each

func (*Database) Get

func (db *Database) Get(docid string) (Body, error)

Returns the body of the current revision of a document

func (*Database) GetAttachment

func (db *Database) GetAttachment(key AttachmentKey) ([]byte, error)

Retrieves an attachment, base64-encoded, given its key.

func (*Database) GetChangeLog

func (db *Database) GetChangeLog(channelName string, afterSeq uint64) []*LogEntry

func (*Database) GetChanges

func (db *Database) GetChanges(channels base.Set, options ChangesOptions) ([]*ChangeEntry, error)

Synchronous convenience function that returns all changes as a simple array.

func (*Database) GetDesignDoc

func (db *Database) GetDesignDoc(ddocName string, result interface{}) (err error)

func (*Database) GetRev

func (db *Database) GetRev(docid, revid string, listRevisions bool, attachmentsSince []string) (Body, error)

Returns the body of a revision of a document. Uses the revision cache. revid may be "", meaning the current revision.

func (*Database) GetRevAndChannels

func (db *Database) GetRevAndChannels(docid, revid string, listRevisions bool) (body Body, channels channels.ChannelMap, access UserAccessMap, roleAccess UserAccessMap, flags uint8, sequence uint64, err error)

Returns the body of a revision of a document, as well as the document's current channels and the user/roles it grants channel access to.

func (*Database) GetSpecial

func (db *Database) GetSpecial(doctype string, docid string) (Body, error)

func (*Database) MultiChangesFeed

func (db *Database) MultiChangesFeed(chans base.Set, options ChangesOptions) (<-chan *ChangeEntry, error)

func (*Database) Post

func (db *Database) Post(body Body) (string, string, error)

Creates a new document, assigning it a random doc ID.

func (*Database) Put

func (db *Database) Put(docid string, body Body) (string, error)

Updates or creates a document. The new body's "_rev" property must match the current revision's, if any.

func (*Database) PutDesignDoc

func (db *Database) PutDesignDoc(ddocName string, ddoc DesignDoc) (err error)

func (*Database) PutExistingRev

func (db *Database) PutExistingRev(docid string, body Body, docHistory []string) error

Adds an existing revision to a document along with its history (list of rev IDs.) This is equivalent to the "new_edits":false mode of CouchDB.

func (*Database) PutSpecial

func (db *Database) PutSpecial(doctype string, docid string, body Body) (string, error)

func (*Database) QueryDesignDoc

func (db *Database) QueryDesignDoc(ddocName string, viewName string, options map[string]interface{}) (*sgbucket.ViewResult, error)

func (*Database) ReloadUser

func (db *Database) ReloadUser() error

Reloads the database's User object, in case its persistent properties have been changed.

func (*Database) RevDiff

func (db *Database) RevDiff(docid string, revids []string) (missing, possible []string)

Given a document ID and a set of revision IDs, looks up which ones are not known.

func (*Database) SameAs

func (db *Database) SameAs(otherdb *Database) bool

func (*Database) SimpleMultiChangesFeed

func (db *Database) SimpleMultiChangesFeed(chans base.Set, options ChangesOptions) (<-chan *ChangeEntry, error)

Returns the (ordered) union of all of the changes made to multiple channels.

func (*Database) UpdateAllDocChannels

func (db *Database) UpdateAllDocChannels(doCurrentDocs bool, doImportDocs bool) (int, error)

Re-runs the sync function on every current document in the database (if doCurrentDocs==true) and/or imports docs in the bucket not known to the gateway (if doImportDocs==true). To be used when the JavaScript sync function changes.

func (*Database) VectorMultiChangesFeed

func (db *Database) VectorMultiChangesFeed(chans base.Set, options ChangesOptions) (<-chan *ChangeEntry, error)

Returns the (ordered) union of all of the changes made to multiple channels.

func (*Database) WriteMultipartDocument

func (db *Database) WriteMultipartDocument(body Body, writer *multipart.Writer, compress bool)

Writes a revision to a MIME multipart writer, encoding large attachments as separate parts.

func (*Database) WriteRevisionAsPart

func (db *Database) WriteRevisionAsPart(revBody Body, isError bool, compressPart bool, writer *multipart.Writer) error

Adds a new part to the given multipart writer, containing the given revision. The revision will be written as a nested multipart body if it has attachments.

type DatabaseContext

type DatabaseContext struct {
	Name       string          // Database name
	Bucket     base.Bucket     // Storage
	BucketSpec base.BucketSpec // The BucketSpec
	BucketLock sync.RWMutex    // Control Access to the underlying bucket object

	ChannelMapper      *channels.ChannelMapper // Runs JS 'sync' function
	StartTime          time.Time               // Timestamp when context was instantiated
	ChangesClientStats Statistics              // Tracks stats of # of changes connections
	RevsLimit          uint32                  // Max depth a document's revision tree can grow to

	Shadower *Shadower // Tracks an external Couchbase bucket

	EventMgr           *EventManager          // Manages notification events
	AllowEmptyPassword bool                   // Allow empty passwords?  Defaults to false
	SequenceHasher     *sequenceHasher        // Used to generate and resolve hash values for vector clock sequences
	SequenceType       SequenceType           // Type of sequences used for this DB (integer or vector clock)
	Options            DatabaseContextOptions // Database Context Options
	AccessLock         sync.RWMutex           // Allows DB offline to block until synchronous calls have completed
	State              uint32                 // The runtime state of the DB from a service perspective
	ExitChanges        chan struct{}          // Active _changes feeds on the DB will close when this channel is closed
	// contains filtered or unexported fields
}

Basic description of a database. Shared between all Database objects on the same database. This object is thread-safe so it can be shared between HTTP handlers.

func NewDatabaseContext

func NewDatabaseContext(dbName string, bucket base.Bucket, autoImport bool, options DatabaseContextOptions) (*DatabaseContext, error)

Creates a new DatabaseContext on a bucket. The bucket will be closed when this context closes.

func (*DatabaseContext) AllPrincipalIDs

func (db *DatabaseContext) AllPrincipalIDs() (users, roles []string, err error)

Returns the IDs of all users and roles

func (*DatabaseContext) Authenticator

func (context *DatabaseContext) Authenticator() *auth.Authenticator

func (*DatabaseContext) Close

func (context *DatabaseContext) Close()

func (*DatabaseContext) ComputeChannelsForPrincipal

func (context *DatabaseContext) ComputeChannelsForPrincipal(princ auth.Principal) (channels.TimedSet, error)

Recomputes the set of channels a User/Role has been granted access to by sync() functions. This is part of the ChannelComputer interface defined by the Authenticator.

func (*DatabaseContext) ComputeRolesForUser

func (context *DatabaseContext) ComputeRolesForUser(user auth.User) (channels.TimedSet, error)

Recomputes the set of roles a User has been granted access to by sync() functions. This is part of the ChannelComputer interface defined by the Authenticator.

func (*DatabaseContext) ComputeSequenceChannelsForPrincipal

func (context *DatabaseContext) ComputeSequenceChannelsForPrincipal(princ auth.Principal) (channels.TimedSet, error)

Recomputes the set of channels a User/Role has been granted access to by sync() functions. This is part of the ChannelComputer interface defined by the Authenticator.

func (*DatabaseContext) ComputeVbSequenceChannelsForPrincipal

func (context *DatabaseContext) ComputeVbSequenceChannelsForPrincipal(princ auth.Principal) (channels.TimedSet, error)

Recomputes the set of channels a User/Role has been granted access to by sync() functions. This is part of the ChannelComputer interface defined by the Authenticator.

func (*DatabaseContext) CreateCBGTIndex

func (context *DatabaseContext) CreateCBGTIndex() error

func (*DatabaseContext) DeleteUserSessions

func (db *DatabaseContext) DeleteUserSessions(userName string) error

Deletes all session documents for a user

func (*DatabaseContext) GetCBGTIndexNameForBucket

func (context *DatabaseContext) GetCBGTIndexNameForBucket(bucket base.Bucket) (indexName string)

func (*DatabaseContext) GetDoc

func (db *DatabaseContext) GetDoc(docid string) (*document, error)

Lowest-level method that reads a document from the bucket.

func (*DatabaseContext) GetIndexBucket

func (context *DatabaseContext) GetIndexBucket() base.Bucket

Helper method for API unit test retrieval of index bucket

func (*DatabaseContext) GetPrincipal

func (dbc *DatabaseContext) GetPrincipal(name string, isUser bool) (info *PrincipalConfig, err error)

func (*DatabaseContext) GetStableClock

func (context *DatabaseContext) GetStableClock() (clock base.SequenceClock, err error)

func (*DatabaseContext) GetUserViewsEnabled

func (context *DatabaseContext) GetUserViewsEnabled() bool

func (*DatabaseContext) IndexAllChannelStats

func (db *DatabaseContext) IndexAllChannelStats() ([]*ChannelStats, error)

func (*DatabaseContext) IndexChannelStats

func (db *DatabaseContext) IndexChannelStats(channelName string) (*ChannelStats, error)

func (*DatabaseContext) IndexStats

func (db *DatabaseContext) IndexStats() (indexStats *IndexStats, err error)

func (*DatabaseContext) IsClosed

func (context *DatabaseContext) IsClosed() bool

func (*DatabaseContext) LastSequence

func (context *DatabaseContext) LastSequence() (uint64, error)

func (*DatabaseContext) NotifyUser

func (context *DatabaseContext) NotifyUser(username string)

func (*DatabaseContext) ParseSequenceID

func (dbc *DatabaseContext) ParseSequenceID(str string) (s SequenceID, err error)

func (*DatabaseContext) ReserveSequences

func (context *DatabaseContext) ReserveSequences(numToReserve uint64) error

func (*DatabaseContext) RestartListener

func (context *DatabaseContext) RestartListener() error

For testing only!

func (*DatabaseContext) SetUserViewsEnabled

func (context *DatabaseContext) SetUserViewsEnabled(value bool)

func (*DatabaseContext) TakeDbOffline

func (dc *DatabaseContext) TakeDbOffline(reason string) error

func (*DatabaseContext) TapListener

func (context *DatabaseContext) TapListener() changeListener

func (*DatabaseContext) UpdatePrincipal

func (dbc *DatabaseContext) UpdatePrincipal(newInfo PrincipalConfig, isUser bool, allowReplace bool) (replaced bool, err error)

Updates or creates a principal from a PrincipalConfig structure.

func (*DatabaseContext) UpdateSyncFun

func (context *DatabaseContext) UpdateSyncFun(syncFun string) (changed bool, err error)

Sets the database context's sync function based on the JS code from config. Returns a boolean indicating whether the function is different from the saved one. If multiple gateway instances try to update the function at the same time (to the same new value) only one of them will get a changed=true result.

func (*DatabaseContext) UseGlobalSequence

func (context *DatabaseContext) UseGlobalSequence() bool

func (*DatabaseContext) WaitForPendingChanges

func (context *DatabaseContext) WaitForPendingChanges() (err error)

Wait until the change-cache has caught up with the latest writes to the database.

func (*DatabaseContext) WaitForSequence

func (context *DatabaseContext) WaitForSequence(sequence uint64) (err error)

Wait until the change-cache has caught up with the latest writes to the database.

func (*DatabaseContext) WaitForSequenceWithMissing

func (context *DatabaseContext) WaitForSequenceWithMissing(sequence uint64) (err error)

Wait until the change-cache has caught up with the latest writes to the database.

type DatabaseContextOptions

type DatabaseContextOptions struct {
	CacheOptions          *CacheOptions
	IndexOptions          *ChangeIndexOptions
	SequenceHashOptions   *SequenceHashOptions
	RevisionCacheCapacity uint32
	AdminInterface        *string
	UnsupportedOptions    *UnsupportedOptions
}

type DesignDoc

type DesignDoc sgbucket.DesignDoc

type DocumentChangeEvent

type DocumentChangeEvent struct {
	AsyncEvent
	Doc      Body
	OldDoc   string
	Channels base.Set
}

DocumentChangeEvent is raised when a document has been successfully written to the backing data store. Event has the document body and channel set as properties.

func (*DocumentChangeEvent) EventType

func (dce *DocumentChangeEvent) EventType() EventType

func (*DocumentChangeEvent) String

func (dce *DocumentChangeEvent) String() string

type Event

type Event interface {
	Synchronous() bool
	EventType() EventType
	String() string
}

An event that can be raised during SG processing.

type EventHandler

type EventHandler interface {
	HandleEvent(event Event)
	String() string
}

EventHandler interface represents an instance of an event handler defined in the database config

type EventManager

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

EventManager routes raised events to corresponding event handlers. Incoming events are just dumped in the eventChannel to minimize time spent blocking whatever process is raising the event. The event queue worker goroutine works the event channel and sends events to the appropriate handlers

func NewEventManager

func NewEventManager() *EventManager

Creates a new event manager. Sets up the event channel for async events, and the goroutine to monitor and process that channel.

func (*EventManager) HasHandlerForEvent

func (em *EventManager) HasHandlerForEvent(eventType EventType) bool

Checks whether a handler of the given type has been registered to the event manager.

func (*EventManager) ProcessEvent

func (em *EventManager) ProcessEvent(event Event)

Concurrent processing of all async event handlers registered for the event type

func (*EventManager) RaiseDBStateChangeEvent

func (em *EventManager) RaiseDBStateChangeEvent(dbName string, state string, reason string, adminInterface string) error

Raises a DB state change event based on the db name, admininterface, new state, reason and local system time. If the event manager doesn't have a listener for this event, ignores.

func (*EventManager) RaiseDocumentChangeEvent

func (em *EventManager) RaiseDocumentChangeEvent(body Body, oldBodyJSON string, channels base.Set) error

Raises a document change event based on the the document body and channel set. If the event manager doesn't have a listener for this event, ignores.

func (*EventManager) RegisterEventHandler

func (em *EventManager) RegisterEventHandler(handler EventHandler, eventType EventType)

Register a new event handler to the EventManager. The event manager will route events of type eventType to the handler.

func (*EventManager) Start

func (em *EventManager) Start(maxProcesses uint, waitTime int)

Starts the listener queue for the event manager

type EventType

type EventType uint8

Event type

const (
	DocumentChange EventType = iota
	DBStateChange
	UserAdd
)

type ForEachDocIDFunc

type ForEachDocIDFunc func(id IDAndRev, channels []string) bool

type ForEachDocIDOptions

type ForEachDocIDOptions struct {
	Startkey string
	Endkey   string
	Limit    uint64
}

The ForEachDocID options for limiting query results

type IDAndRev

type IDAndRev struct {
	DocID    string
	RevID    string
	Sequence uint64
}

type IndexBlock

type IndexBlock interface {
	AddEntry(entry *LogEntry) error
	Key() string
	Marshal() ([]byte, error)
	Unmarshal(value []byte) error
	Cas() uint64
	SetCas(cas uint64)
	GetEntries(vbNo uint16, fromSeq uint64, toSeq uint64, includeKeys bool) (entries []*LogEntry, keySet []string)
	GetAllEntries() []*LogEntry
}

IndexBlock interface - defines interactions with a block

func NewIndexBlock

func NewIndexBlock(channelName string, sequence uint64, partition uint16, partitions *base.IndexPartitions) IndexBlock

type IndexPartitionsFunc

type IndexPartitionsFunc func() (*base.IndexPartitions, error)

type IndexStats

type IndexStats struct {
	PartitionStats PartitionStats `json:"partitions"`
}

type IndexType

type IndexType uint8

Index type

const (
	KVIndex IndexType = iota
	MemoryCache
)

type JSEventFunction

type JSEventFunction struct {
	*sgbucket.JSServer
}

A thread-safe wrapper around a jsEventTask, i.e. an event function.

func NewJSEventFunction

func NewJSEventFunction(fnSource string) *JSEventFunction

func (*JSEventFunction) CallFunction

func (ef *JSEventFunction) CallFunction(event Event) (interface{}, error)

Calls a jsEventFunction returning an interface{}

func (*JSEventFunction) CallValidateFunction

func (ef *JSEventFunction) CallValidateFunction(event Event) (bool, error)

Calls a jsEventFunction returning bool.

type LogEntries

type LogEntries []*LogEntry

type LogEntry

type LogEntry channels.LogEntry

type LogPriorityQueue

type LogPriorityQueue []*LogEntry

A priority-queue of LogEntries, kept ordered by increasing sequence #.

func (LogPriorityQueue) Len

func (h LogPriorityQueue) Len() int

func (LogPriorityQueue) Less

func (h LogPriorityQueue) Less(i, j int) bool

func (*LogPriorityQueue) Pop

func (h *LogPriorityQueue) Pop() interface{}

func (*LogPriorityQueue) Push

func (h *LogPriorityQueue) Push(x interface{})

func (LogPriorityQueue) Swap

func (h LogPriorityQueue) Swap(i, j int)

type PartitionMapStats

type PartitionMapStats struct {
	Storage base.PartitionStorageSet `json:"partitions"`
}

type PartitionStats

type PartitionStats struct {
	PartitionMap    PartitionMapStats `json:"index_partitions"`
	CBGTMap         PartitionMapStats `json:"cbgt_partitions"`
	PartitionsMatch bool              `json:"matches"`
}

type PrincipalConfig

type PrincipalConfig struct {
	Name             *string  `json:"name,omitempty"`
	ExplicitChannels base.Set `json:"admin_channels,omitempty"`
	Channels         base.Set `json:"all_channels"`
	// Fields below only apply to Users, not Roles:
	Email             string   `json:"email,omitempty"`
	Disabled          bool     `json:"disabled,omitempty"`
	Password          *string  `json:"password,omitempty"`
	ExplicitRoleNames []string `json:"admin_roles,omitempty"`
	RoleNames         []string `json:"roles,omitempty"`
}

Struct that configures settings of a User/Role, for UpdatePrincipal. Also used in the rest package as a JSON object that defines a User/Role within a DbConfig and structures the request/response body in the admin REST API for /db/_user/*

func (PrincipalConfig) IsPasswordValid

func (p PrincipalConfig) IsPasswordValid(allowEmptyPass bool) (isValid bool, reason string)

Check if the password in this PrincipalConfig is valid. Only allow empty passwords if allowEmptyPass is true.

type PrincipalIndex

type PrincipalIndex struct {
	VbNo             uint16            `json:"vbucket"`        // vbucket number for user doc - for convenience
	ExplicitChannels channels.TimedSet `json:"admin_channels"` // Timed Set of channel names to vbucket seq no of first user version that granted access
	ExplicitRoles    channels.TimedSet `json:"admin_roles"`    // Timed Set of role names to vbucket seq no of first user version that granted access
}

type ResponseType

type ResponseType uint8
const (
	StringResponse ResponseType = iota
	JSObjectResponse
)

type RevInfo

type RevInfo struct {
	ID       string
	Parent   string
	Deleted  bool
	Body     []byte
	Channels base.Set
}

Information about a single revision.

type RevKey

type RevKey string

type RevTree

type RevTree map[string]*RevInfo

A revision tree maps each revision ID to its RevInfo.

func (RevTree) GetLeaves

func (tree RevTree) GetLeaves() []string

Returns the leaf revision IDs (those that have no children.)

func (RevTree) MarshalJSON

func (tree RevTree) MarshalJSON() ([]byte, error)

func (RevTree) UnmarshalJSON

func (tree RevTree) UnmarshalJSON(inputjson []byte) (err error)

type RevisionCache

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

An LRU cache of document revision bodies, together with their channel access.

func NewRevisionCache

func NewRevisionCache(capacity int, loaderFunc RevisionCacheLoaderFunc) *RevisionCache

Creates a revision cache with the given capacity and an optional loader function.

func (*RevisionCache) Get

func (rc *RevisionCache) Get(docid, revid string) (Body, Body, base.Set, error)

Looks up a revision from the cache. Returns the body of the revision, its history, and the set of channels it's in. If the cache has a loaderFunction, it will be called if the revision isn't in the cache; any error returned by the loaderFunction will be returned from Get.

func (*RevisionCache) Put

func (rc *RevisionCache) Put(body Body, history Body, channels base.Set)

Adds a revision to the cache.

type RevisionCacheLoaderFunc

type RevisionCacheLoaderFunc func(id IDAndRev) (body Body, history Body, channels base.Set, err error)

type SeqHashCacheLoaderFunc

type SeqHashCacheLoaderFunc func(hashValue uint64) (clocks *storedClocks, err error)

type SequenceHashOptions

type SequenceHashOptions struct {
	Bucket        base.Bucket // Hash lookup bucket
	Size          uint8       // Hash keyset size log 2
	Expiry        *uint32     // Expiry for untouched hash bucket docs
	HashFrequency *int        // Hash frequency for changes feed
}

type SequenceID

type SequenceID struct {
	SeqType          SequenceType       // Sequence Type (Int or Clock)
	TriggeredBy      uint64             // Int sequence: The sequence # that triggered this (0 if none)
	LowSeq           uint64             // Int sequence: Lowest contiguous sequence seen on the feed
	Seq              uint64             // Int sequence: The actual internal sequence
	Clock            base.SequenceClock // Clock sequence: Sequence (distributed index)
	TriggeredByClock base.SequenceClock // Clock sequence: Sequence (distributed index) that triggered this
	ClockHash        string             // String representation of clock hash
	SequenceHasher   *sequenceHasher    // Sequence hasher - used when unmarshalling clock-based sequences

	TriggeredByVbNo uint16 // Vbucket number for triggered by sequence
	LowHash         string // Clock hash used for continuous feed where some entries aren't hashed
	// contains filtered or unexported fields
}

SequenceID doesn't do any clock hash management - it's expected that hashing has already been done (if required) when the clock is set.

func (SequenceID) Before

func (s SequenceID) Before(s2 SequenceID) bool

The most significant value is TriggeredBy, unless it's zero, in which case use Seq. The tricky part is that "n" sorts after "n:m" for any nonzero m

func (SequenceID) IsNonZero

func (s SequenceID) IsNonZero() bool

func (SequenceID) MarshalJSON

func (s SequenceID) MarshalJSON() ([]byte, error)

func (SequenceID) SafeSequence

func (s SequenceID) SafeSequence() uint64

func (SequenceID) String

func (s SequenceID) String() string

Format sequence ID to send to clients. Sequence IDs can be in one of the following formats:

Seq                    - simple sequence
TriggeredBy:Seq        - when TriggeredBy is non-zero, LowSeq is zero
LowSeq:TriggeredBy:Seq - when LowSeq is non-zero.

When LowSeq is non-zero but TriggeredBy is zero, will appear as LowSeq::Seq. When LowSeq is non-zero but is greater than s.Seq (occurs when sending previously skipped sequences), ignore LowSeq.

func (*SequenceID) UnmarshalJSON

func (s *SequenceID) UnmarshalJSON(data []byte) error

func (SequenceID) VbucketSequenceAfter

func (s SequenceID) VbucketSequenceAfter(vbNo uint16, seq uint64) bool

func (SequenceID) VbucketSequenceBefore

func (s SequenceID) VbucketSequenceBefore(vbNo uint16, seq uint64) bool

type SequenceType

type SequenceType int

type Shadower

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

Bidirectional sync with an external Couchbase bucket. Watches the bucket's tap feed and applies changes to the matching managed document. Accepts local change notifications and makes equivalent changes to the external bucket. See: https://github.com/couchbase/sync_gateway/wiki/Bucket-Shadowing

func NewShadower

func NewShadower(context *DatabaseContext, bucket base.Bucket, docIDPattern *regexp.Regexp) (*Shadower, error)

Creates a new Shadower.

func (*Shadower) PushRevision

func (s *Shadower) PushRevision(doc *document)

Saves a new local revision to the external bucket.

func (*Shadower) Stop

func (s *Shadower) Stop()

Stops a Shadower. (Safe to call on a nil receiver)

type SkippedSequence

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

type SkippedSequenceQueue

type SkippedSequenceQueue []*SkippedSequence

An ordered queue that supports the Remove operation

func (SkippedSequenceQueue) Contains

func (h SkippedSequenceQueue) Contains(x uint64) bool

Contains does a simple search to detect presence

func (*SkippedSequenceQueue) Push

We always know that incoming missed sequence numbers will be larger than any previously added, so we don't need to do any sorting - just append to the slice

func (*SkippedSequenceQueue) Remove

func (h *SkippedSequenceQueue) Remove(x uint64) error

Remove does a simple binary search to find and remove.

type SortedEntrySet

type SortedEntrySet []*LogEntry

SortedEntrySet Optimizes removal of entries from a sorted array.

func (*SortedEntrySet) Remove

func (h *SortedEntrySet) Remove(x uint64) error

type Statistics

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

Tracks usage count of a resource, such as the _changes feed. (Thread-safe.)

func (*Statistics) Decrement

func (stats *Statistics) Decrement()

func (*Statistics) Increment

func (stats *Statistics) Increment()

func (*Statistics) MaxCount

func (stats *Statistics) MaxCount() uint32

func (*Statistics) Reset

func (stats *Statistics) Reset()

func (*Statistics) TotalCount

func (stats *Statistics) TotalCount() uint32

type UnsupportedOptions

type UnsupportedOptions struct {
	EnableUserViews bool
}

type UserAccessMap

type UserAccessMap map[string]channels.TimedSet

Maps what users have access to what channels or roles, and when they got that access.

type ViewDoc

type ViewDoc struct {
	Json json.RawMessage // should be type 'document', but that fails to unmarshal correctly
}

type Webhook

type Webhook struct {
	AsyncEventHandler
	// contains filtered or unexported fields
}

Webhook is an implementation of EventHandler that sends an asynchronous HTTP POST

func NewWebhook

func NewWebhook(url string, filterFnString string, timeout *uint64) (*Webhook, error)

Creates a new webhook handler based on the url and filter function.

func (*Webhook) HandleEvent

func (wh *Webhook) HandleEvent(event Event)

Performs an HTTP POST to the url defined for the handler. If a filter function is defined, calls it to determine whether to POST. The payload for the POST is depends on the event type.

func (*Webhook) String

func (wh *Webhook) String() string

Jump to

Keyboard shortcuts

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