couchdb

package
v0.0.0-...-9818b01 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2017 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ChangesModeNormal is the only mode supported by cozy-stack
	ChangesModeNormal ChangesFeedMode = "normal"
	// ChangesStyleAllDocs pass all revisions including conflicts
	ChangesStyleAllDocs ChangesFeedStyle = "all_docs"
	// ChangesStyleMainOnly only pass the winning revision
	ChangesStyleMainOnly ChangesFeedStyle = "main_only"
)

Variables

View Source
var GlobalDB = SimpleDatabasePrefix("global")

GlobalDB is the prefix used for stack-scoped db

Functions

func CreateDB

func CreateDB(db Database, doctype string) error

CreateDB creates the necessary database for a doctype

func CreateDoc

func CreateDoc(db Database, doc Doc) error

CreateDoc is used to persist the given document in the couchdb database. The document's SetRev and SetID function will be called with the document's new ID and Rev. This function creates a database if this is the first document of its type

func CreateNamedDoc

func CreateNamedDoc(db Database, doc Doc) error

CreateNamedDoc persist a document with an ID. if the document already exist, it will return a 409 error. The document ID should be fillled. The doc SetRev function will be called with the new rev.

func CreateNamedDocWithDB

func CreateNamedDocWithDB(db Database, doc Doc) error

CreateNamedDocWithDB is equivalent to CreateNamedDoc but creates the database if it does not exist

func DefineIndex

func DefineIndex(db Database, doctype string, index mango.Index) error

DefineIndex define the index on the doctype database see query package on how to define an index

func DefineViews

func DefineViews(db Database, doctype string, views Views) error

DefineViews creates a design doc with some views

func Delete

func Delete(db Database, doctype, id, rev string) (string, error)

Delete destroy a document by its doctype and ID . If the document's current rev does not match the one passed, a CouchdbError(409 conflict) will be returned. This functions returns the tombstone revision as string

func DeleteAllDBs

func DeleteAllDBs(db Database) error

DeleteAllDBs will remove all the couchdb doctype databases for a couchdb.DB.

func DeleteDB

func DeleteDB(db Database, doctype string) error

DeleteDB destroy the database for a doctype

func DeleteDoc

func DeleteDoc(db Database, doc Doc) error

DeleteDoc deletes a struct implementing the couchb.Doc interface The document's SetRev will be called with tombstone revision

func ExecView

func ExecView(db Database, doctype, view string, results interface{}) error

ExecView executes the specified view function

func FindDocs

func FindDocs(db Database, doctype string, req *FindRequest, results interface{}) error

FindDocs returns all documents matching the passed FindRequest documents will be unmarshalled in the provided results slice.

func FindDocsRaw

func FindDocsRaw(db Database, doctype string, req interface{}, results interface{}) error

FindDocsRaw find documents TODO: pagination

func GetAllDocs

func GetAllDocs(db Database, doctype string, req *AllDocsRequest, results interface{}) error

GetAllDocs returns all documents of a specified doctype. It filters out the possible _design document. TODO: pagination

func GetDoc

func GetDoc(db Database, doctype, id string, out Doc) error

GetDoc fetch a document by its docType and ID, out is filled with the document by json.Unmarshal-ing

func IsConflictError

func IsConflictError(err error) bool

IsConflictError checks if the given error is a couch conflict error

func IsNoDatabaseError

func IsNoDatabaseError(err error) bool

IsNoDatabaseError checks if the given error is a couch no_db_file error

func IsNotFoundError

func IsNotFoundError(err error) bool

IsNotFoundError checks if the given error is a couch not_found error

func Proxy

func Proxy(db Database, doctype, path string) *httputil.ReverseProxy

Proxy generate a httputil.ReverseProxy which forwards the request to the correct route.

func ResetDB

func ResetDB(db Database, doctype string) error

ResetDB destroy and recreate the database for a doctype

func UpdateDoc

func UpdateDoc(db Database, doc Doc) error

UpdateDoc update a document. The document ID and Rev should be filled. The doc SetRev function will be called with the new rev.

Types

type AllDocsRequest

type AllDocsRequest struct {
	Descending bool     `url:"descending,omitempty"`
	Keys       []string `url:"keys,omitempty"`
	Limit      int      `url:"limit,omitempty"`
	Skip       int      `url:"skip,omitempty"`
	StartKey   string   `url:"start_key,omitempty"`
	EndKey     string   `url:"end_key,omitempty"`
}

AllDocsRequest is used to build a _all_docs request

type AllDocsResponse

type AllDocsResponse struct {
	Offset    int `json:"offset"`
	TotalRows int `json:"total_rows"`
	Rows      []struct {
		ID  string          `json:"id"`
		Doc json.RawMessage `json:"doc"`
	} `json:"rows"`
}

AllDocsResponse is the response we receive from an _all_docs request

type Change

type Change struct {
	DocID   string  `json:"id"`
	Seq     string  `json:"seq"`
	Doc     JSONDoc `json:"doc"`
	Changes []struct {
		Rev string `json:"rev"`
	} `json:"changes"`
}

A Change is an atomic change in couchdb

type ChangesFeedMode

type ChangesFeedMode string

ChangesFeedMode is a value for the feed parameter of a ChangesRequest

func ValidChangesMode

func ValidChangesMode(feed string) (ChangesFeedMode, error)

ValidChangesMode convert any string into a ChangesFeedMode or gives an error if the string is invalid.

type ChangesFeedStyle

type ChangesFeedStyle string

ChangesFeedStyle is a value for the style parameter of a ChangesRequest

func ValidChangesStyle

func ValidChangesStyle(style string) (ChangesFeedStyle, error)

ValidChangesStyle convert any string into a ChangesFeedStyle or gives an error if the string is invalid.

type ChangesRequest

type ChangesRequest struct {
	DocType string `url:"-"`
	// see Changes Feeds. Default is normal.
	Feed ChangesFeedMode `url:"feed,omitempty"`
	// Maximum period in milliseconds to wait for a change before the response
	// is sent, even if there are no results. Only applicable for longpoll or
	// continuous feeds. Default value is specified by httpd/changes_timeout
	// configuration option. Note that 60000 value is also the default maximum
	// timeout to prevent undetected dead connections.
	Timeout int `url:"timeout,omitempty"`
	// Period in milliseconds after which an empty line is sent in the results.
	// Only applicable for longpoll, continuous, and eventsource feeds. Overrides
	// any timeout to keep the feed alive indefinitely. Default is 60000. May be
	// true to use default value.
	Heartbeat int `url:"heartbeat,omitempty"`
	// Includes conflicts information in response. Ignored if include_docs isn’t
	// true. Default is false.
	Conflicts bool `url:"conflicts,omitempty"`
	// Return the change results in descending sequence order (most recent change
	// first). Default is false.
	Descending bool `url:"descending,omitempty"`
	// Include the associated document with each result. If there are conflicts,
	// only the winning revision is returned. Default is false.
	IncludeDocs bool `url:"include_docs,omitempty"`
	// Include the Base64-encoded content of attachments in the documents that
	// are included if include_docs is true. Ignored if include_docs isn’t true.
	// Default is false.
	Attachments bool `url:"attachments,omitempty"`
	// Include encoding information in attachment stubs if include_docs is true
	// and the particular attachment is compressed. Ignored if include_docs isn’t
	// true. Default is false.
	AttEncodingInfo bool `url:"att_encoding_info,omitempty"`
	// Alias of Last-Event-ID header.
	LastEventID int `url:"last,omitempty"`
	// Limit number of result rows to the specified value (note that using 0 here
	//  has the same effect as 1).
	Limit int `url:"limit,omitempty"`
	// Start the results from the change immediately after the given update
	// sequence. Can be valid update sequence or now value. Default is 0.
	Since string `url:"since,omitempty"`
	// Specifies how many revisions are returned in the changes array. The
	// default, main_only, will only return the current “winning” revision;
	// all_docs will return all leaf revisions (including conflicts and deleted
	// former conflicts).
	Style ChangesFeedStyle `url:"style,omitempty"`
	// Reference to a filter function from a design document that will filter
	// whole stream emitting only filtered events. See the section Change
	// Notifications in the book CouchDB The Definitive Guide for more
	// information.
	Filter string `url:"filter,omitempty"`
	// Allows to use view functions as filters. Documents counted as “passed” for
	// view filter in case if map function emits at least one record for them.
	// See _view for more info.
	View string `url:"view,omitempty"`
}

A ChangesRequest are all parameters than can be passed to a changes feed

type ChangesResponse

type ChangesResponse struct {
	LastSeq string   `json:"last_seq"` // Last change update sequence
	Pending int      `json:"pending"`  // Count of remaining items in the feed
	Results []Change `json:"results"`  // Changes made to a database
}

A ChangesResponse is the response provided by a GetChanges call

func GetChanges

func GetChanges(db Database, req *ChangesRequest) (*ChangesResponse, error)

GetChanges returns a list of change in couchdb

type DBStatusResponse

type DBStatusResponse struct {
	DBName    string `json:"db_name"`
	UpdateSeq string `json:"update_seq"`
	Sizes     struct {
		File     int `json:"file"`
		External int `json:"external"`
		Active   int `json:"active"`
	} `json:"sizes"`
	PurgeSeq int `json:"purge_seq"`
	Other    struct {
		DataSize int `json:"data_size"`
	} `json:"other"`
	DocDelCount       int    `json:"doc_del_count"`
	DocCount          int    `json:"doc_count"`
	DiskSize          int    `json:"disk_size"`
	DiskFormatVersion int    `json:"disk_format_version"`
	DataSize          int    `json:"data_size"`
	CompactRunning    bool   `json:"compact_running"`
	InstanceStartTime string `json:"instance_start_time"`
}

DBStatusResponse is the response from DBStatus

func DBStatus

func DBStatus(db Database, doctype string) (*DBStatusResponse, error)

DBStatus re

type Database

type Database interface {
	Prefix() string
}

Database is the type passed to every function in couchdb package for now it is just a string with the database prefix.

func SimpleDatabasePrefix

func SimpleDatabasePrefix(prefix string) Database

SimpleDatabasePrefix returns a Database from a prefix, useful for test

type Doc

type Doc interface {
	ID() string
	Rev() string
	DocType() string

	SetID(id string)
	SetRev(rev string)
}

Doc is the interface that encapsulate a couchdb document, of any serializable type. This interface defines method to set and get the ID of the document.

type Error

type Error struct {
	StatusCode  int
	CouchdbJSON []byte `json:"-"`
	Name        string `json:"error"`
	Reason      string `json:"reason"`
	Original    error
}

Error represent an error from couchdb

func (*Error) Error

func (e *Error) Error() string

func (*Error) JSON

func (e *Error) JSON() map[string]interface{}

JSON returns the json representation of this error

type FindRequest

type FindRequest struct {
	Selector mango.Filter  `json:"selector"`
	UseIndex string        `json:"use_index,omitempty"`
	Limit    int           `json:"limit,omitempty"`
	Skip     int           `json:"skip,omitempty"`
	Sort     *mango.SortBy `json:"sort,omitempty"`
	Fields   []string      `json:"fields,omitempty"`
}

FindRequest is used to build a find request

type IndexCreationResponse

type IndexCreationResponse struct {
	Result string `json:"result,omitempty"`
	Error  string `json:"error,omitempty"`
	Reason string `json:"reason,omitempty"`
	ID     string `json:"id,omitempty"`
	Name   string `json:"name,omitempty"`
}

IndexCreationResponse is the response from couchdb when we create an Index

func DefineIndexRaw

func DefineIndexRaw(db Database, doctype string, index interface{}) (*IndexCreationResponse, error)

DefineIndexRaw defines a index

type JSONDoc

type JSONDoc struct {
	M    map[string]interface{}
	Type string
}

JSONDoc is a map representing a simple json object that implements the Doc interface.

func (JSONDoc) DocType

func (j JSONDoc) DocType() string

DocType returns the document type of the document

"io.cozy.event" == doc.Doctype()

func (JSONDoc) Get

func (j JSONDoc) Get(key string) interface{}

Get returns the value of one of the db fields

func (JSONDoc) ID

func (j JSONDoc) ID() string

ID returns the identifier field of the document

"io.cozy.event/123abc123" == doc.ID()

func (JSONDoc) MarshalJSON

func (j JSONDoc) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaller by proxying to internal map

func (JSONDoc) Rev

func (j JSONDoc) Rev() string

Rev returns the revision field of the document

"3-1234def1234" == doc.Rev()

func (JSONDoc) SetID

func (j JSONDoc) SetID(id string)

SetID is used to set the identifier of the document

func (JSONDoc) SetRev

func (j JSONDoc) SetRev(rev string)

SetRev is used to set the revision of the document

func (*JSONDoc) ToMapWithType

func (j *JSONDoc) ToMapWithType() map[string]interface{}

ToMapWithType returns the JSONDoc internal map including its DocType its used in request response.

func (*JSONDoc) UnmarshalJSON

func (j *JSONDoc) UnmarshalJSON(bytes []byte) error

UnmarshalJSON implements json.Unmarshaller by proxying to internal map

func (JSONDoc) Valid

func (j JSONDoc) Valid(field, value string) bool

Valid implements permissions.Validable on JSONDoc

type View

type View struct {
	Map    string `json:"map"`
	Reduce string `json:"reduce,omitempty"`
}

View is the map/reduce thing in CouchDB

type ViewResponse

type ViewResponse struct {
	Rows []struct {
		ID    string `json:"id"`
		Key   string `json:"key"`
		Value int64  `json:"value"`
	} `json:"rows"`
}

ViewResponse is the response we receive when executing a view

type Views

type Views map[string]View

Views is a map of name/views

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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