github.com/jbendotnet/noms@v0.0.0-20190904222105-c43e4293ea92/go/datas/dataset.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
     6  
     7  import (
     8  	"regexp"
     9  
    10  	"github.com/attic-labs/noms/go/d"
    11  	"github.com/attic-labs/noms/go/types"
    12  )
    13  
    14  // DatasetRe is a regexp that matches a legal Dataset name anywhere within the
    15  // target string.
    16  var DatasetRe = regexp.MustCompile(`[a-zA-Z0-9\-_/]+`)
    17  
    18  // DatasetFullRe is a regexp that matches a only a target string that is
    19  // entirely legal Dataset name.
    20  var DatasetFullRe = regexp.MustCompile("^" + DatasetRe.String() + "$")
    21  
    22  // Dataset is a named Commit within a Database.
    23  type Dataset struct {
    24  	db   Database
    25  	id   string
    26  	head types.Value
    27  }
    28  
    29  func newDataset(db Database, id string, head types.Value) Dataset {
    30  	d.PanicIfFalse(head == nil || IsCommit(head))
    31  	return Dataset{db, id, head}
    32  }
    33  
    34  // Database returns the Database object in which this Dataset is stored.
    35  // WARNING: This method is under consideration for deprecation.
    36  func (ds Dataset) Database() Database {
    37  	return ds.db
    38  }
    39  
    40  // ID returns the name of this Dataset.
    41  func (ds Dataset) ID() string {
    42  	return ds.id
    43  }
    44  
    45  // MaybeHead returns the current Head Commit of this Dataset, which contains
    46  // the current root of the Dataset's value tree, if available. If not, it
    47  // returns a new Commit and 'false'.
    48  func (ds Dataset) MaybeHead() (types.Struct, bool) {
    49  	if ds.head == nil {
    50  		return types.Struct{}, false
    51  	}
    52  	return ds.head.(types.Struct), true
    53  }
    54  
    55  // Head returns the current head Commit, which contains the current root of
    56  // the Dataset's value tree.
    57  func (ds Dataset) Head() types.Struct {
    58  	c, ok := ds.MaybeHead()
    59  	if !ok {
    60  		d.Panic("Dataset \"%s\" does not exist", ds.id)
    61  	}
    62  	return c
    63  }
    64  
    65  // MaybeHeadRef returns the Ref of the current Head Commit of this Dataset,
    66  // which contains the current root of the Dataset's value tree, if available.
    67  // If not, it returns an empty Ref and 'false'.
    68  func (ds Dataset) MaybeHeadRef() (types.Ref, bool) {
    69  	if ds.head == nil {
    70  		return types.Ref{}, false
    71  	}
    72  	return types.NewRef(ds.head), true
    73  }
    74  
    75  // HasHead() returns 'true' if this dataset has a Head Commit, false otherwise.
    76  func (ds Dataset) HasHead() bool {
    77  	return ds.head != nil
    78  }
    79  
    80  // HeadRef returns the Ref of the current head Commit, which contains the
    81  // current root of the Dataset's value tree.
    82  func (ds Dataset) HeadRef() types.Ref {
    83  	r, ok := ds.MaybeHeadRef()
    84  	if !ok {
    85  		d.Panic("Dataset \"%s\" does not exist", ds.id)
    86  	}
    87  	return r
    88  }
    89  
    90  // MaybeHeadValue returns the Value field of the current head Commit, if
    91  // available. If not it returns nil and 'false'.
    92  func (ds Dataset) MaybeHeadValue() (types.Value, bool) {
    93  	if c, ok := ds.MaybeHead(); ok {
    94  		return c.Get(ValueField), true
    95  	}
    96  	return nil, false
    97  }
    98  
    99  // HeadValue returns the Value field of the current head Commit.
   100  func (ds Dataset) HeadValue() types.Value {
   101  	c := ds.Head()
   102  	return c.Get(ValueField)
   103  }
   104  
   105  func IsValidDatasetName(name string) bool {
   106  	return DatasetFullRe.MatchString(name)
   107  }