github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/datas/dataset.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // This file incorporates work covered by the following copyright and
    16  // permission notice:
    17  //
    18  // Copyright 2016 Attic Labs, Inc. All rights reserved.
    19  // Licensed under the Apache License, version 2.0:
    20  // http://www.apache.org/licenses/LICENSE-2.0
    21  
    22  package datas
    23  
    24  import (
    25  	"regexp"
    26  
    27  	"github.com/dolthub/dolt/go/store/d"
    28  	"github.com/dolthub/dolt/go/store/types"
    29  )
    30  
    31  // DatasetRe is a regexp that matches a legal Dataset name anywhere within the
    32  // target string.
    33  var DatasetRe = regexp.MustCompile(`[a-zA-Z0-9\-_/]+`)
    34  
    35  // DatasetFullRe is a regexp that matches a only a target string that is
    36  // entirely legal Dataset name.
    37  var DatasetFullRe = regexp.MustCompile("^" + DatasetRe.String() + "$")
    38  
    39  // Dataset is a named value within a Database. Different head values may be stored in a dataset. Most commonly, this is
    40  // a commit, but other values are also supported in some cases.
    41  type Dataset struct {
    42  	db   Database
    43  	id   string
    44  	head types.Value
    45  }
    46  
    47  func newDataset(db Database, id string, head types.Value) (Dataset, error) {
    48  	check := head == nil
    49  
    50  	var err error
    51  	if !check {
    52  		check, err = IsCommit(head)
    53  
    54  		if err != nil {
    55  			return Dataset{}, err
    56  		}
    57  	}
    58  
    59  	if !check {
    60  		check, err = IsTag(head)
    61  
    62  		if err != nil {
    63  			return Dataset{}, err
    64  		}
    65  	}
    66  
    67  	if !check {
    68  		check, err = IsWorkingSet(head)
    69  
    70  		if err != nil {
    71  			return Dataset{}, err
    72  		}
    73  	}
    74  
    75  	// precondition checks
    76  	d.PanicIfFalse(check)
    77  	return Dataset{db, id, head}, nil
    78  }
    79  
    80  // Database returns the Database object in which this Dataset is stored.
    81  // WARNING: This method is under consideration for deprecation.
    82  func (ds Dataset) Database() Database {
    83  	return ds.db
    84  }
    85  
    86  // ID returns the name of this Dataset.
    87  func (ds Dataset) ID() string {
    88  	return ds.id
    89  }
    90  
    91  // MaybeHead returns the current Head Commit of this Dataset, which contains
    92  // the current root of the Dataset's value tree, if available. If not, it
    93  // returns a new Commit and 'false'.
    94  func (ds Dataset) MaybeHead() (types.Struct, bool) {
    95  	if ds.head == nil {
    96  		return types.Struct{}, false
    97  	}
    98  	return ds.head.(types.Struct), true
    99  }
   100  
   101  // MaybeHeadRef returns the Ref of the current Head Commit of this Dataset,
   102  // which contains the current root of the Dataset's value tree, if available.
   103  // If not, it returns an empty Ref and 'false'.
   104  func (ds Dataset) MaybeHeadRef() (types.Ref, bool, error) {
   105  	if ds.head == nil {
   106  		return types.Ref{}, false, nil
   107  	}
   108  	ref, err := types.NewRef(ds.head, ds.Database().Format())
   109  
   110  	if err != nil {
   111  		return types.Ref{}, false, err
   112  	}
   113  
   114  	return ref, true, nil
   115  }
   116  
   117  // HasHead() returns 'true' if this dataset has a Head Commit, false otherwise.
   118  func (ds Dataset) HasHead() bool {
   119  	return ds.head != nil
   120  }
   121  
   122  // MaybeHeadValue returns the Value field of the current head Commit, if
   123  // available. If not it returns nil and 'false'.
   124  func (ds Dataset) MaybeHeadValue() (types.Value, bool, error) {
   125  	if c, ok := ds.MaybeHead(); ok {
   126  		return c.MaybeGet(ValueField)
   127  	}
   128  	return nil, false, nil
   129  }
   130  
   131  func IsValidDatasetName(name string) bool {
   132  	return DatasetFullRe.MatchString(name)
   133  }