Skip to content

Commit

Permalink
feat(save): support custom timestamps on commit
Browse files Browse the repository at this point in the history
setting the input dataset commit.Timestamp value will now be preserved instead
of being overwritten with the current time. Input timestamp will ALWAYS be
converted to UTC time zone.

This change makes it much easier to generate dataset histories with commit
timestamps that aren't in chronological order, which is perfectly fine
according to spec, but will cause bugs for any code that relies on such an
assumption.
  • Loading branch information
b5 committed Dec 7, 2020
1 parent 7815004 commit e8c18fa
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
8 changes: 6 additions & 2 deletions base/dsfs/compute_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,12 @@ func (cff *computeFieldsFile) handleRows(ctx context.Context) {
}

cff.Lock()
// assign timestamp early. saving process on large files can take many minutes
cff.ds.Commit.Timestamp = Timestamp()
if cff.ds.Commit.Timestamp.IsZero() {
// assign timestamp early. saving process on large files can take many minutes
cff.ds.Commit.Timestamp = Timestamp()
} else {
cff.ds.Commit.Timestamp = cff.ds.Commit.Timestamp.In(time.UTC)
}
cff.acc = dsstats.NewAccumulator(st)
cff.Unlock()

Expand Down
2 changes: 2 additions & 0 deletions base/dsfs/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ func DerefDataset(ctx context.Context, store qfs.Filesystem, ds *dataset.Dataset

// SaveSwitches represents options for saving a dataset
type SaveSwitches struct {
// Use a custom timestamp, defaults to time.Now if unset
Time time.Time
// Replace is whether the save is a full replacement or a set of patches to previous
Replace bool
// Pin is whether the dataset should be pinned
Expand Down
29 changes: 28 additions & 1 deletion base/dsfs/dataset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ func TestLoadDataset(t *testing.T) {
continue
}
}

}

func TestCreateDataset(t *testing.T) {
Expand Down Expand Up @@ -297,6 +296,34 @@ func TestCreateDataset(t *testing.T) {
// case: previous dataset isn't valid
}

func TestDatasetSaveCustomTimestamp(t *testing.T) {
ctx := context.Background()
fs := qfs.NewMemFS()
privKey := testPeers.GetTestPeerInfo(10).PrivKey

// use a custom timestamp in local zone. should be converted to UTC for saving
ts := time.Date(2100, 1, 2, 3, 4, 5, 6, time.Local)

ds := &dataset.Dataset{
Commit: &dataset.Commit{
Timestamp: ts,
},
Structure: &dataset.Structure{Format: "json", Schema: dataset.BaseSchemaArray},
}
ds.SetBodyFile(qfs.NewMemfileBytes("/body.json", []byte(`[]`)))

path, err := CreateDataset(ctx, fs, fs, ds, nil, privKey, SaveSwitches{})
if err != nil {
t.Fatal(err)
}

got, err := LoadDataset(ctx, fs, path)

if !ts.In(time.UTC).Equal(got.Commit.Timestamp) {
t.Errorf("result timestamp mismatch.\nwant: %q\ngot: %q", ts.In(time.UTC), got.Commit.Timestamp)
}
}

// BaseTabularSchema is the base schema for tabular data
// NOTE: Do not use if possible, prefer github.com/qri-io/dataset/tabular
// TODO(dustmop): Possibly move this to tabular package
Expand Down

0 comments on commit e8c18fa

Please sign in to comment.