memdocstore

package
v0.0.0-...-b26c292 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2022 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package memdocstore provides an in-process in-memory implementation of the docstore API. It is suitable for local development and testing.

Every document in a memdocstore collection has a unique primary key. The primary key values need not be strings; they may be any comparable Go value.

Action Lists

Action lists are executed concurrently. Each action in an action list is executed in a separate goroutine.

memdocstore calls the BeforeDo function of an ActionList once before executing the actions. Its As function never returns true.

URLs

For docstore.OpenCollection, memdocstore registers for the scheme "mem". To customize the URL opener, or for more details on the URL format, see URLOpener. See https://gocloud.dev/concepts/urls/ for background information.

Example (OpenCollectionFromURL)
package main

import (
	"context"
	"log"

	"gocloud.dev/docstore"
)

func main() {
	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
	// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/docstore/memdocstore"
	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
	ctx := context.Background()

	// docstore.OpenCollection creates a *docstore.Collection from a URL.
	coll, err := docstore.OpenCollection(ctx, "mem://collection/keyField")
	if err != nil {
		log.Fatal(err)
	}
	defer coll.Close()
	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
	
Output:

Index

Examples

Constants

View Source
const Scheme = "mem"

Scheme is the URL scheme memdocstore registers its URLOpener under on docstore.DefaultMux.

Variables

This section is empty.

Functions

func OpenCollection

func OpenCollection(keyField string, opts *Options) (*docstore.Collection, error)

OpenCollection creates a *docstore.Collection backed by memory. keyField is the document field holding the primary key of the collection.

Example
package main

import (
	"log"

	"gocloud.dev/docstore/memdocstore"
)

func main() {
	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.

	coll, err := memdocstore.OpenCollection("keyField", nil)
	if err != nil {
		log.Fatal(err)
	}
	defer coll.Close()
	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
	
Output:

func OpenCollectionWithKeyFunc

func OpenCollectionWithKeyFunc(keyFunc func(docstore.Document) interface{}, opts *Options) (*docstore.Collection, error)

OpenCollectionWithKeyFunc creates a *docstore.Collection backed by memory. keyFunc takes a document and returns the document's primary key. It should return nil if the document is missing the information to construct a key. This will cause all actions, even Create, to fail.

For the collection to be usable with Query.Delete and Query.Update, keyFunc must work with map[string]interface{} as well as whatever struct type the collection normally uses (if any).

Example
package main

import (
	"log"

	"gocloud.dev/docstore"
	"gocloud.dev/docstore/memdocstore"
)

func main() {
	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
	type HighScore struct {
		Game   string
		Player string
	}

	// The name of a document is constructed from the Game and Player fields.
	nameFromDocument := func(doc docstore.Document) interface{} {
		hs := doc.(*HighScore)
		return hs.Game + "|" + hs.Player
	}

	coll, err := memdocstore.OpenCollectionWithKeyFunc(nameFromDocument, nil)
	if err != nil {
		log.Fatal(err)
	}
	defer coll.Close()
	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
	
Output:

Types

type Options

type Options struct {
	// The name of the field holding the document revision.
	// Defaults to docstore.DefaultRevisionField.
	RevisionField string

	// The maximum number of concurrent goroutines started for a single call to
	// ActionList.Do. If less than 1, there is no limit.
	MaxOutstandingActions int

	// The filename associated with this collection.
	// When a collection is opened with a non-nil filename, the collection
	// is loaded from the file if it exists. Otherwise, an empty collection is created.
	// When the collection is closed, its contents are saved to the file.
	Filename string
	// contains filtered or unexported fields
}

Options are optional arguments to the OpenCollection functions.

type URLOpener

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

URLOpener opens URLs like "mem://collection/_id".

The URL's host is the name of the collection. The URL's path is used as the keyField.

The following query parameters are supported:

  • revision_field (optional): the name of the revision field.
  • filename (optional): the filename to store the collection in.

func (*URLOpener) OpenCollectionURL

func (o *URLOpener) OpenCollectionURL(ctx context.Context, u *url.URL) (*docstore.Collection, error)

OpenCollectionURL opens a docstore.Collection based on u.

Jump to

Keyboard shortcuts

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