github.com/attic-labs/noms@v0.0.0-20210827224422-e5fa29d95e8b/go/datas/database.go (about)

     1  // Copyright 2016 Attic Labs, Inc. All rights reserved.
     2  // Licensed under the Apache License, version 2.0:
     3  // http://www.apache.org/licenses/LICENSE-2.0
     4  
     5  // Package datas defines and implements the database layer used in Noms.
     6  package datas
     7  
     8  import (
     9  	"io"
    10  
    11  	"github.com/attic-labs/noms/go/chunks"
    12  	"github.com/attic-labs/noms/go/types"
    13  )
    14  
    15  // Database provides versioned storage for noms values. While Values can be
    16  // directly read and written from a Database, it is generally more appropriate
    17  // to read data by inspecting the Head of a Dataset and write new data by
    18  // updating the Head of a Dataset via Commit() or similar. Particularly, new
    19  // data is not guaranteed to be persistent until after a Commit (Delete,
    20  // SetHead, or FastForward) operation completes.
    21  // The Database API is stateful, meaning that calls to GetDataset() or
    22  // Datasets() occurring after a call to Commit() (et al) will represent the
    23  // result of the Commit().
    24  type Database interface {
    25  	// To implement types.ValueWriter, Database implementations provide
    26  	// WriteValue(). WriteValue() writes v to this Database, though v is not
    27  	// guaranteed to be be persistent until after a subsequent Commit(). The
    28  	// return value is the Ref of v.
    29  	// Written values won't be persisted until a commit-alike
    30  	types.ValueReadWriter
    31  
    32  	// Close must have no side-effects
    33  	io.Closer
    34  
    35  	// Datasets returns the root of the database which is a
    36  	// Map<String, Ref<Commit>> where string is a datasetID.
    37  	Datasets() types.Map
    38  
    39  	// GetDataset returns a Dataset struct containing the current mapping of
    40  	// datasetID in the above Datasets Map.
    41  	GetDataset(datasetID string) Dataset
    42  
    43  	// Rebase brings this Database's view of the world inline with upstream.
    44  	Rebase()
    45  
    46  	// Commit updates the Commit that ds.ID() in this database points at. All
    47  	// Values that have been written to this Database are guaranteed to be
    48  	// persistent after Commit() returns.
    49  	// The new Commit struct is constructed using v, opts.Parents, and
    50  	// opts.Meta. If opts.Parents is the zero value (types.Set{}) then
    51  	// the current head is used. If opts.Meta is the zero value
    52  	// (types.Struct{}) then a fully initialized empty Struct is passed to
    53  	// NewCommit.
    54  	// The returned Dataset is always the newest snapshot, regardless of
    55  	// success or failure, and Datasets() is updated to match backing storage
    56  	// upon return as well. If the update cannot be performed, e.g., because
    57  	// of a conflict, Commit returns an 'ErrMergeNeeded' error.
    58  	Commit(ds Dataset, v types.Value, opts CommitOptions) (Dataset, error)
    59  
    60  	// CommitValue updates the Commit that ds.ID() in this database points at.
    61  	// All Values that have been written to this Database are guaranteed to be
    62  	// persistent after Commit().
    63  	// The new Commit struct is constructed using `v`, and the current Head of
    64  	// `ds` as the lone Parent.
    65  	// The returned Dataset is always the newest snapshot, regardless of
    66  	// success or failure, and Datasets() is updated to match backing storage
    67  	// upon return as well. If the update cannot be performed, e.g., because
    68  	// of a conflict, Commit returns an 'ErrMergeNeeded' error.
    69  	CommitValue(ds Dataset, v types.Value) (Dataset, error)
    70  
    71  	// Delete removes the Dataset named ds.ID() from the map at the root of
    72  	// the Database. The Dataset data is not necessarily cleaned up at this
    73  	// time, but may be garbage collected in the future.
    74  	// The returned Dataset is always the newest snapshot, regardless of
    75  	// success or failure, and Datasets() is updated to match backing storage
    76  	// upon return as well. If the update cannot be performed, e.g., because
    77  	// of a conflict, Delete returns an 'ErrMergeNeeded' error.
    78  	Delete(ds Dataset) (Dataset, error)
    79  
    80  	// SetHead ignores any lineage constraints (e.g. the current Head being in
    81  	// commit’s Parent set) and force-sets a mapping from datasetID: commit in
    82  	// this database.
    83  	// All Values that have been written to this Database are guaranteed to be
    84  	// persistent after SetHead(). If the update cannot be performed, e.g.,
    85  	// because another process moved the current Head out from under you,
    86  	// error will be non-nil.
    87  	// The newest snapshot of the Dataset is always returned, so the caller an
    88  	// easily retry using the latest.
    89  	// Regardless, Datasets() is updated to match backing storage upon return.
    90  	SetHead(ds Dataset, newHeadRef types.Ref) (Dataset, error)
    91  
    92  	// FastForward takes a types.Ref to a Commit object and makes it the new
    93  	// Head of ds iff it is a descendant of the current Head. Intended to be
    94  	// used e.g. after a call to Pull(). If the update cannot be performed,
    95  	// e.g., because another process moved the current Head out from under
    96  	// you, err will be non-nil.
    97  	// The newest snapshot of the Dataset is always returned, so the caller
    98  	// can easily retry using the latest.
    99  	// Regardless, Datasets() is updated to match backing storage upon return.
   100  	FastForward(ds Dataset, newHeadRef types.Ref) (Dataset, error)
   101  
   102  	// Stats may return some kind of struct that reports statistics about the
   103  	// ChunkStore that backs this Database instance. The type is
   104  	// implementation-dependent, and impls may return nil
   105  	Stats() interface{}
   106  
   107  	// StatsSummary may return a string containing summarized statistics for
   108  	// the ChunkStore that backs this Database. It must return "Unsupported"
   109  	// if this operation is not supported.
   110  	StatsSummary() string
   111  
   112  	Flush()
   113  
   114  	// chunkStore returns the ChunkStore used to read and write
   115  	// groups of values to the database efficiently. This interface is a low-
   116  	// level detail of the database that should infrequently be needed by
   117  	// clients.
   118  	chunkStore() chunks.ChunkStore
   119  }
   120  
   121  func NewDatabase(cs chunks.ChunkStore) Database {
   122  	return newDatabase(cs)
   123  }