Skip to content

Commit

Permalink
Move around methods
Browse files Browse the repository at this point in the history
  • Loading branch information
VinaiRachakonda committed Jan 12, 2021
1 parent 1d2439f commit 1b7f556
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 70 deletions.
3 changes: 0 additions & 3 deletions go/libraries/doltcore/doltdb/root_val.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ import (
"github.com/dolthub/dolt/go/store/types"
)

//"github.com/dolthub/dolt/go/libraries/doltcore/table"
//"github.com/dolthub/dolt/go/libraries/doltcore/table/typed/noms"

const (
ddbRootStructName = "dolt_db_root"

Expand Down
67 changes: 67 additions & 0 deletions go/libraries/doltcore/env/dolt_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,70 @@ func GetDocsWithNewerTextFromRoot(ctx context.Context, root *doltdb.RootValue, d
}
return docs, nil
}

// UpdateRootWithDocsTable takes in a root value, a drw, and some docs and writes those docs to the dolt_docs table
// (perhaps creating it in the process). The table might not necessarily need to be created if there are no docs in the
// repo yet.
func UpdateRootWithDocsTable(ctx context.Context, drw DocsReadWriter, root *doltdb.RootValue, docDetails []doltdb.DocDetails) (*doltdb.RootValue, error) {
docTbl, _, err := root.GetTable(ctx, doltdb.DocTableName)

if err != nil {
return nil, err
}

docTbl, err = drw.WriteDocsToDisk(ctx, root.VRW(), docTbl, docDetails)

if err != nil {
return nil, err
}

// There might not need be a need to create docs table if not docs have been created yet so check if docTbl != nil.
if docTbl != nil {
return root.PutTable(ctx, doltdb.DocTableName, docTbl)
}

return root, nil
}

// ResetWorkingDocsToStagedDocs resets the `dolt_docs` table on the working root to match the staged root.
// If the `dolt_docs` table does not exist on the staged root, it will be removed from the working root.
func ResetWorkingDocsToStagedDocs(ctx context.Context, ddb *doltdb.DoltDB, rsr RepoStateReader, rsw RepoStateWriter) error {
wrkRoot, err := WorkingRoot(ctx, ddb, rsr)
if err != nil {
return err
}

stgRoot, err := StagedRoot(ctx, ddb, rsr)
if err != nil {
return err
}

stgDocTbl, stgDocsFound, err := stgRoot.GetTable(ctx, doltdb.DocTableName)
if err != nil {
return err
}

_, wrkDocsFound, err := wrkRoot.GetTable(ctx, doltdb.DocTableName)
if err != nil {
return err
}

if wrkDocsFound && !stgDocsFound {
newWrkRoot, err := wrkRoot.RemoveTables(ctx, doltdb.DocTableName)
if err != nil {
return err
}
_, err = UpdateWorkingRoot(ctx, ddb, rsw, newWrkRoot)
return err
}

if stgDocsFound {
newWrkRoot, err := wrkRoot.PutTable(ctx, doltdb.DocTableName, stgDocTbl)
if err != nil {
return err
}
_, err = UpdateWorkingRoot(ctx, ddb, rsw, newWrkRoot)
return err
}
return nil
}
67 changes: 0 additions & 67 deletions go/libraries/doltcore/env/repo_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,70 +286,3 @@ func GetRoots(ctx context.Context, ddb *doltdb.DoltDB, rsr RepoStateReader) (wor

return working, staged, head, nil
}

// UpdateRootWithDocsTable takes in a root value, a drw, and some docs and writes those docs to the dolt_docs table
// (perhaps creating it in the process). The table might not necessarily need to be created if there are no docs in the
// repo yet.
func UpdateRootWithDocsTable(ctx context.Context, drw DocsReadWriter, root *doltdb.RootValue, docDetails []doltdb.DocDetails) (*doltdb.RootValue, error) {
docTbl, _, err := root.GetTable(ctx, doltdb.DocTableName)

if err != nil {
return nil, err
}

docTbl, err = drw.WriteDocsToDisk(ctx, root.VRW(), docTbl, docDetails)

if err != nil {
return nil, err
}

// There might not need be a need to create docs table if not docs have been created yet so check if docTbl != nil.
if docTbl != nil {
return root.PutTable(ctx, doltdb.DocTableName, docTbl)
}

return root, nil
}

// ResetWorkingDocsToStagedDocs resets the `dolt_docs` table on the working root to match the staged root.
// If the `dolt_docs` table does not exist on the staged root, it will be removed from the working root.
func ResetWorkingDocsToStagedDocs(ctx context.Context, ddb *doltdb.DoltDB, rsr RepoStateReader, rsw RepoStateWriter) error {
wrkRoot, err := WorkingRoot(ctx, ddb, rsr)
if err != nil {
return err
}

stgRoot, err := StagedRoot(ctx, ddb, rsr)
if err != nil {
return err
}

stgDocTbl, stgDocsFound, err := stgRoot.GetTable(ctx, doltdb.DocTableName)
if err != nil {
return err
}

_, wrkDocsFound, err := wrkRoot.GetTable(ctx, doltdb.DocTableName)
if err != nil {
return err
}

if wrkDocsFound && !stgDocsFound {
newWrkRoot, err := wrkRoot.RemoveTables(ctx, doltdb.DocTableName)
if err != nil {
return err
}
_, err = UpdateWorkingRoot(ctx, ddb, rsw, newWrkRoot)
return err
}

if stgDocsFound {
newWrkRoot, err := wrkRoot.PutTable(ctx, doltdb.DocTableName, stgDocTbl)
if err != nil {
return err
}
_, err = UpdateWorkingRoot(ctx, ddb, rsw, newWrkRoot)
return err
}
return nil
}

0 comments on commit 1b7f556

Please sign in to comment.