bucket

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2019 License: GPL-3.0 Imports: 20 Imported by: 0

README

Bucket

Build Status Go Report Card Documentation

Simple Couchbase framework.

Project specifically focuses on the one bucket as database approach, and makes it easier to manage complex data sets. It tries to get rid of the embedded jsons per document and separates them into different documents behind the scene.

Disclaimer:

DO NOT USE IN PRODUCTION. This is still a work in progress. We will not take responsibility for any breaks in the code that happen after a new version comes out.

Features:
  • Automatic index generator with indexable tags.
  • Simple usage through the handler.
  • Following best practices of Couchbase usage behind the scene, which doesn't affect the user of the library.
Rules:
  1. Only struct can be referenced
How to use:

Create a new handler with the New function, that needs a configuration.

bucket.New( {bucket.Configuration} )

type Configuration struct {
    // The address of the couchbase server
	ConnectionString string 

    // Username and password to access couchbase
	Username         string 
	Password         string 
	
    // The name and password of the bucket you want to use
	BucketName       string 
	BucketPassword   string 

    // The separator of your choice, this will separate the prefix from the field name
	Separator        string 
}

After that you can use the Insert, Get, Remove, Upsert, Touch, GetAndTouch and Ping methods of the handler.

package main

import (
    "context"
    "fmt"

    "github.com/PumpkinSeed/bucket"
    "github.com/couchbase/gocb"
)

var conf = &bucket.Configuration{
    ConnectionString: "myServer.org:1234",
    Username: "cbUsername",
    Password: "cbPassword",
    BucketName: "testBucket",
    BucketPassword: "testBucketPassword",
    Separator: "::",
}

type myStruct struct {
    justAField string `json:"just_a_field"`
}

func main() {
    var in = &myStruct{
        justAField: "basic",
    }
    var cas bucket.Cas
    var typ = "prefix"
    ctx := context.Background()

    h, err := bucket.New(conf)
    if err != nil {
        // handle error
    }
    
    // insert
    cas, id, err := h.Insert(ctx, typ, "myID", in, 0)
    if err != nil {
        // handle error
    }

    // get
    var out = &myStruct{}
    err = h.Get(ctx, typ, id, out)
    if err != nil {
        // handle error
    }

    // touch
    err = h.Touch(ctx, typ, id, in, 0)
    if err != nil {
        // handle error
    }

    // get and touch
    var secondOut = &myStruct{}
    err = h.GetAndTouch(ctx, typ, id, secondOut, 0)
    if err != nil {
        // handle error
    }

    // ping
    var services []gocb.ServiceType
    res, err := h.Ping(ctx, services)
    if err != nil {
        // handle error
    }

    fmt.Println(res)

    // upsert
    in.justAField = "updated"
    cas, newID, err := h.Upsert(ctx, typ, id, in, 0)

    // remove
    err = h.Remove(ctx, typ, newID, in)
    if err != nil {
        // handle error
    }
}

Important:

  • The typ parameter will be the prefix of the initial struct, so you should use the same value for the same types!
  • IDs should be unique, if the parameter is an empty string ("") a globally unique ID will be automatically generated!
Additional:

Embedded structs can be separated into a a new entry with the cb_referenced tag. The value will decide the typ of the struct.

type example struct {
    refMe       *refMe      `json:"ref_me" cb_referenced:"separate_entry"`
    ignoreMe    *ignoreMe   `json:"ignore_me"`
}

type refMe struct {
    referencedField int `json:"referenced_field"`
}

type ignoreMe struct {
    notReferencedField int `json:"not_referenced_field"`
}

You can also index structs adding the cb_indexable:"true" tag to the field, and then calling bucket.Index({context.Context}, yourStruct).

type example struct {
    indexMe         string `json:"index_me" cb_indexable:"true"`
    butNotThisOne   string `json:"but_not_this_one"`
}

Documentation

Index

Constants

View Source
const (
	FacetDate = iota
	FacetNumeric
	FacetTerm
)

Available facet types

Variables

View Source
var (
	// ErrDocumentTypeAlreadyExists document type already exist
	ErrDocumentTypeAlreadyExists = errors.New("document type already exists")

	// ErrDocumentTypeDoesntExists document type doesn't exist
	ErrDocumentTypeDoesntExists = errors.New("document type doesn't exist")

	// ErrEmptyField field must be filled
	ErrEmptyField = errors.New("field must be filled")

	// ErrEmptyIndex index must be filled
	ErrEmptyIndex = errors.New("index must be filled")

	// ErrEmptyType source type must be filled
	ErrEmptyType = errors.New("source type must set")

	// ErrEmptySource source name must be filled
	ErrEmptySource = errors.New("source name must set")

	// ErrEmptyRefTag referenced tag must be filled
	ErrEmptyRefTag = errors.New("referenced tag must set")

	// ErrConjunctionAndDisjunctionIsNil conjunction and disjunction are nil
	ErrConjunctionAndDisjunctionIsNil = errors.New("conjunction and disjunction are nil")

	// ErrEndAsTimeZero end as Time is zero instant
	ErrEndAsTimeZero = errors.New("endAsTime is zero instant")

	// ErrFirstParameterNotStruct first parameter is not a struct
	ErrFirstParameterNotStruct = errors.New("first parameter is not a struct")

	// ErrInputStructPointer input struct must be a pointer
	ErrInputStructPointer = errors.New("input struct must be pointer")

	// ErrInvalidBulkContainer bulk container type definition error
	ErrInvalidBulkContainer = errors.New("container must be *[]T, with length of ids array")

	// ErrInvalidGetDocumentTypesParam represents value for get document types should be pointer
	ErrInvalidGetDocumentTypesParam = errors.New("internal error: value should be pointer for getDocumentTypes")
)

Functions

This section is empty.

Types

type Cas

type Cas map[string]gocb.Cas

Cas is the container of Cas operation of all documents

type CompoundQueries

type CompoundQueries struct {
	Conjunction []SearchQuery `json:"conjuncts,omitempty"`
	Disjunction []SearchQuery `json:"disjuncts,omitempty"`

	Limit  int `json:"-"`
	Offset int `json:"-"`
}

CompoundQueries is a representation of the available search option for a CompoundSearch

type Configuration

type Configuration struct {
	Username         string `json:"username"`
	Password         string `json:"password"`
	BucketName       string `json:"bucket_name"`
	BucketPassword   string `json:"bucket_password"`
	ConnectionString string `json:"connection_string"`
	Separator        string `json:"separator"`

	Opts Opts `json:"bucket_opts"`
}

Configuration the main library configuration

type FacetDef

type FacetDef struct {
	Name  string
	Type  int
	Field string
	Size  int
}

FacetDef is a configuration helper for the search's facets

type Handler

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

Handler is the main handler

func New

func New(c *Configuration) (*Handler, error)

New creates a new handler from the configuration that handles the operations

func (*Handler) CompoundSearch

func (h *Handler) CompoundSearch(ctx context.Context, index string, q *CompoundQueries) ([]gocb.SearchResultHit, error)

CompoundSearch apply the configuration of CompoundQueries and do the search then returns the hits

func (*Handler) CompoundSearchWithFacets

func (h *Handler) CompoundSearchWithFacets(ctx context.Context, index string, q *CompoundQueries, facets []FacetDef) ([]gocb.SearchResultHit, map[string]gocb.SearchResultFacet, error)

CompoundSearchWithFacets apply the configuration of CompoundQueries and do the search then returns the hits and facets

func (*Handler) CountIndex

func (h *Handler) CountIndex(ctx context.Context, indexName string) (*IndexCount, error)

func (*Handler) CreateFullTextSearchIndex

func (h *Handler) CreateFullTextSearchIndex(ctx context.Context, def *IndexDefinition) error

CreateFullTextSearchIndex ...

func (*Handler) DeleteFullTextSearchIndex

func (h *Handler) DeleteFullTextSearchIndex(ctx context.Context, indexName string) error

DeleteFullTextSearchIndex ...

func (*Handler) EInsert

func (h *Handler) EInsert(ctx context.Context, typ, id string, q interface{}, ttl uint32) (Cas, string, error)

func (*Handler) Get

func (h *Handler) Get(ctx context.Context, typ, id string, ptr interface{}) error

Get retrieves a document from the bucket

func (*Handler) GetAndTouch

func (h *Handler) GetAndTouch(ctx context.Context, typ, id string, ptr interface{}, ttl uint32) error

GetAndTouch retrieves a document and simultaneously updates its expiry times

func (*Handler) GetBulk

func (h *Handler) GetBulk(ctx context.Context, hits []gocb.SearchResultHit, container interface{}) error

GetBulk accepts a set of hits from a search and a container represents the data-structure and fill it up with the hits where the container should be *[]T type

func (*Handler) GetManager

func (h *Handler) GetManager(ctx context.Context) *gocb.BucketManager

GetManager returns a BucketManager for performing management operations on this bucket

func (*Handler) Index

func (h *Handler) Index(ctx context.Context, v interface{}) error

Index runs trough the given interface v and creates secondary indexes for all the with indexable:"true" tags

func (*Handler) IndexStat

func (h *Handler) IndexStat(ctx context.Context, indexName string) (*IndexStat, error)

func (*Handler) Insert

func (h *Handler) Insert(ctx context.Context, typ, id string, q interface{}, ttl uint32) (Cas, string, error)

Insert inserts a new record into couchbase bucket

func (*Handler) InspectFullTextSearchIndex

func (h *Handler) InspectFullTextSearchIndex(ctx context.Context, indexName string) (bool, *IndexDefinition, error)

InspectFullTextSearchIndex checks the availability of the index and returns it if exists

func (*Handler) Ping

func (h *Handler) Ping(ctx context.Context, services []gocb.ServiceType) (*gocb.PingReport, error)

Ping will ping a list of services and verify they are active and responding in an acceptable period of time

func (*Handler) RangeSearch

func (h *Handler) RangeSearch(ctx context.Context, index string, q *RangeQuery) ([]gocb.SearchResultHit, error)

RangeSearch apply the configuration of RangeQuery and do the search then returns the hits

func (*Handler) RangeSearchWithFacets

func (h *Handler) RangeSearchWithFacets(ctx context.Context, index string, q *RangeQuery, facets []FacetDef) ([]gocb.SearchResultHit, map[string]gocb.SearchResultFacet, error)

RangeSearchWithFacets apply the configuration of RangeQuery and do the search then returns the hits and facets

func (*Handler) Remove

func (h *Handler) Remove(ctx context.Context, typ, id string, ptr interface{}) error

Remove removes a document from the bucket

func (*Handler) SetDocumentType

func (h *Handler) SetDocumentType(ctx context.Context, name, prefix string) error

SetDocumentType adds the type of the given field to the state

func (*Handler) SimpleSearch

func (h *Handler) SimpleSearch(ctx context.Context, index string, q *SearchQuery) ([]gocb.SearchResultHit, error)

SimpleSearch apply the configuration of SearchQuery and do the search then returns the hits

func (*Handler) SimpleSearchWithFacets

func (h *Handler) SimpleSearchWithFacets(ctx context.Context, index string, q *SearchQuery, facets []FacetDef) ([]gocb.SearchResultHit, map[string]gocb.SearchResultFacet, error)

SimpleSearchWithFacets apply the configuration of SearchQuery and do the search then returns the hits and facets

func (*Handler) Touch

func (h *Handler) Touch(ctx context.Context, typ, id string, ptr interface{}, ttl uint32) error

Touch touches documents, specifying a new expiry time for it The Cas value must be 0

func (*Handler) Upsert

func (h *Handler) Upsert(ctx context.Context, typ, id string, q interface{}, ttl uint32) (Cas, string, error)

Upsert inserts or replaces a document in the bucket

func (*Handler) ValidateState

func (h *Handler) ValidateState() (bool, error)

ValidateState validates the state of the bucket

type IndexCount

type IndexCount struct {
	Status  string      `json:"status"`
	Count   null.Uint   `json:"count,omitempty"`
	Error   null.String `json:"error,omitempty"`
	Request null.String `json:"request,omitempty"`
}

IndexCount represents index count response

type IndexDefaultMapping

type IndexDefaultMapping struct {
	Dynamic bool `json:"dynamic"`
	Enabled bool `json:"enabled"`
}

IndexDefaultMapping ...

type IndexDefinition

type IndexDefinition struct {
	Type         string          `json:"type"`
	Name         string          `json:"name"`
	UUID         string          `json:"uuid"`
	SourceType   string          `json:"sourceType"`
	SourceName   string          `json:"sourceName"`
	SourceUUID   string          `json:"sourceUUID"`
	SourceParams interface{}     `json:"sourceParams"` // TODO
	PlanParams   IndexPlanParams `json:"planParams"`
	Params       IndexParams     `json:"params"`
}

IndexDefinition ...

func DefaultFullTextSearchIndexDefinition

func DefaultFullTextSearchIndexDefinition(meta IndexMeta) (*IndexDefinition, error)

DefaultFullTextSearchIndexDefinition creates a default index def for full-text search and return it in purpose to change default values manually

type IndexDefs

type IndexDefs struct {
	UUID      string                     `json:"uuid"`
	IndexDefs map[string]IndexDefinition `json:"indexDefs"`
}

IndexDefs ...

type IndexDocConfig

type IndexDocConfig struct {
	DocIDPrefixDelimiter string `json:"docid_prefix_delim"`
	DocIDRegexp          string `json:"docid_regexp"`
	Mode                 string `json:"mode"`
	TypeField            string `json:"type_field"`
}

IndexDocConfig ...

type IndexField

type IndexField struct {
	Analyzer           string `json:"analyzer"`
	IncludeInAll       bool   `json:"include_in_all"`
	IncludeTermVectors bool   `json:"include_term_vectors"`
	Index              bool   `json:"index"`
	Name               string `json:"name"`
	Store              bool   `json:"store"`
	Type               string `json:"type"`
}

IndexField ...

type IndexMapping

type IndexMapping struct {
	DefaultAnalyzer       string               `json:"default_analyzer"`
	DefaultDatetimeParser string               `json:"default_datetime_parser"`
	DefaultField          string               `json:"default_field"`
	DefaultMapping        IndexDefaultMapping  `json:"default_mapping"`
	DefaultType           string               `json:"default_type"`
	DocvaluesDynamic      bool                 `json:"docvalues_dynamic"`
	IndexDynamic          bool                 `json:"index_dynamic"`
	StoreDynamic          bool                 `json:"store_dynamic"`
	TypeField             string               `json:"type_field"`
	Types                 map[string]IndexType `json:"types"`
}

IndexMapping ...

type IndexMeta

type IndexMeta struct {
	Name                 string
	SourceType           string
	SourceName           string
	DocIDPrefixDelimiter string
	DocIDRegexp          string
	TypeField            string
}

IndexMeta ...

type IndexParams

type IndexParams struct {
	DocConfig IndexDocConfig `json:"doc_config"`
	Mapping   IndexMapping   `json:"mapping"`
	Store     IndexStore     `json:"store"`
}

IndexParams ...

type IndexPlanParams

type IndexPlanParams struct {
	MaxPartitionsPerPIndex int64 `json:"maxPartitionsPerPIndex"`
	NumReplicas            int64 `json:"numReplicas"`
}

IndexPlanParams ...

type IndexProperties

type IndexProperties struct {
	Dynamic bool         `json:"dynamic"`
	Enabled bool         `json:"enabled"`
	Fields  []IndexField `json:"fields"`
}

IndexProperties ...

type IndexStat

type IndexStat struct {
	Status     null.String `json:"status,omitempty"`
	Error      null.String `json:"error,omitempty"`
	Request    null.String `json:"request,omitempty"`
	AggStats   null.JSON   `json:"aggStats,omitempty"`
	DocCount   null.Uint   `json:"docCount,omitempty"`
	NodesStats null.JSON   `json:"nodesStats"`
}

IndexStat represents the statistics of the search index

type IndexStore

type IndexStore struct {
	IndexType   string `json:"indexType"`
	KVStoreName string `json:"kvStoreName"`
}

IndexStore ...

type IndexType

type IndexType struct {
	Dynamic         bool                       `json:"dynamic"`
	Enabled         bool                       `json:"enabled"`
	DefaultAnalyzer string                     `json:"default_analyzer,omitempty"`
	Properties      map[string]IndexProperties `json:"properties"`
}

IndexType ...

type NullTimeout

type NullTimeout struct {
	Value time.Duration
	// contains filtered or unexported fields
}

NullTimeout is the library's built in NullTimeout type for Opts

func NullTimeoutFrom

func NullTimeoutFrom(dur time.Duration) NullTimeout

NullTimeoutFrom creates a NullTimeout with the given time.Duration

func NullTimeoutMillisec

func NullTimeoutMillisec(dur uint64) NullTimeout

NullTimeoutMillisec creates a NullTimeout with the given millisec

func NullTimeoutSec

func NullTimeoutSec(dur uint64) NullTimeout

NullTimeoutSec creates a NullTimeout with the given sec

type Opts

type Opts struct {
	OperationTimeout      NullTimeout `json:"operation_timeout"`
	BulkOperationTimeout  NullTimeout `json:"bulk_operation_timeout"`
	DurabilityTimeout     NullTimeout `json:"durability_timeout"`
	DurabilityPollTimeout NullTimeout `json:"durability_poll_timeout"`
	ViewTimeout           NullTimeout `json:"view_timeout"`
	N1qlTimeout           NullTimeout `json:"n1ql_timeout"`
	AnalyticsTimeout      NullTimeout `json:"analytics_timeout"`
}

Opts is the couchbase related configuration such as timeouts

type RangeQuery

type RangeQuery struct {
	StartAsTime time.Time `json:"-"`
	EndAsTime   time.Time `json:"-"`
	Start       string    `json:"start,omitempty"`
	End         string    `json:"end,omitempty"`

	Min int64 `json:"min,omitempty"`
	Max int64 `json:"max,omitempty"`

	InclusiveStart bool `json:"inclusive_start,omitempty"`
	InclusiveEnd   bool `json:"inclusive_end,omitempty"`
	InclusiveMin   bool `json:"inclusive_min,omitempty"`
	InclusiveMax   bool `json:"inclusive_max,omitempty"`

	Field string `json:"field,omitempty"`

	Limit  int `json:"-"`
	Offset int `json:"-"`
}

RangeQuery is a representation of the available search option for a RangeSearch

type SearchQuery

type SearchQuery struct {
	Query       string `json:"query,omitempty"`
	Match       string `json:"match,omitempty"`
	MatchPhrase string `json:"match_phrase,omitempty"`
	Term        string `json:"term,omitempty"`
	Prefix      string `json:"prefix,omitempty"`
	Regexp      string `json:"regexp,omitempty"`
	Wildcard    string `json:"wildcard,omitempty"`

	Field        string `json:"field,omitempty"`
	Analyzer     string `json:"analyzer,omitempty"`
	Fuzziness    int64  `json:"fuzziness,omitempty"`
	PrefixLength int64  `json:"prefix_length,omitempty"`

	Limit  int `json:"-"`
	Offset int `json:"-"`
}

SearchQuery is a representation of the available search option for a SimpleSearch

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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