github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/doltdb/workingset.go (about)

     1  // Copyright 2021 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  package doltdb
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"github.com/dolthub/dolt/go/libraries/doltcore/ref"
    22  	"github.com/dolthub/dolt/go/store/datas"
    23  	"github.com/dolthub/dolt/go/store/types"
    24  )
    25  
    26  const (
    27  	workingMetaStName        = "workingset"
    28  	workingMetaVersionStName = "version"
    29  	workingMetaVersion       = "1.0"
    30  )
    31  
    32  type WorkingSet struct {
    33  	Name      string
    34  	format    *types.NomsBinFormat
    35  	st        types.Struct
    36  	rootValue *RootValue
    37  }
    38  
    39  // NewWorkingSet creates a new WorkingSet object.
    40  func NewWorkingSet(ctx context.Context, name string, vrw types.ValueReadWriter, workingSetSt types.Struct) (*WorkingSet, error) {
    41  	// TODO: meta struct
    42  	// metaSt, ok, err := workingSetSt.MaybeGet(datas.TagMetaField)
    43  	//
    44  	// if err != nil {
    45  	// 	return nil, err
    46  	// }
    47  	// if !ok {
    48  	// 	return nil, fmt.Errorf("tag struct does not have field %s", datas.TagMetaField)
    49  	// }
    50  	//
    51  	// meta, err := tagMetaFromNomsSt(metaSt.(types.Struct))
    52  	//
    53  	// if err != nil {
    54  	// 	return nil, err
    55  	// }
    56  
    57  	rootRef, ok, err := workingSetSt.MaybeGet(datas.WorkingSetRefField)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  	if !ok {
    62  		return nil, fmt.Errorf("workingset struct does not have field %s", datas.WorkingSetRefField)
    63  	}
    64  
    65  	rootValSt, err := rootRef.(types.Ref).TargetValue(ctx, vrw)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	rootVal, err := newRootValue(vrw, rootValSt.(types.Struct))
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  
    75  	return &WorkingSet{
    76  		Name:      name,
    77  		format:    vrw.Format(),
    78  		st:        workingSetSt,
    79  		rootValue: rootVal,
    80  	}, nil
    81  }
    82  
    83  // RootValue returns the root value stored by this workingset
    84  func (t *WorkingSet) RootValue() *RootValue {
    85  	return t.rootValue
    86  }
    87  
    88  // Struct returns the struct used to construct this WorkingSet.
    89  func (t *WorkingSet) Struct() types.Struct {
    90  	return t.st
    91  }
    92  
    93  // Ref returns a WorkingSetRef for this WorkingSet.
    94  func (t *WorkingSet) Ref() ref.WorkingSetRef {
    95  	return ref.NewWorkingSetRef(t.Name)
    96  }
    97  
    98  // WorkingSetMeta contains all the metadata that is associated with a working set
    99  type WorkingSetMeta struct {
   100  	// empty for now
   101  }
   102  
   103  func NewWorkingSetMeta() *WorkingSetMeta {
   104  	return &WorkingSetMeta{}
   105  }
   106  
   107  func (tm *WorkingSetMeta) toNomsStruct(nbf *types.NomsBinFormat) (types.Struct, error) {
   108  	metadata := types.StructData{
   109  		workingMetaVersionStName: types.String(workingMetaVersion),
   110  	}
   111  
   112  	return types.NewStruct(nbf, workingMetaStName, metadata)
   113  }