Skip to content

Commit

Permalink
Adds a clone function for meta file (#2185)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyriltovena authored Jul 31, 2023
1 parent 8f8d7a2 commit 6d1fae4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/phlaredb/block/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ func (m *Meta) String() string {
)
}

func (m *Meta) Clone() *Meta {
data, err := json.Marshal(m)
if err != nil {
panic(err)
}
var clone Meta
if err := json.Unmarshal(data, &clone); err != nil {
panic(err)
}
return &clone
}

var ulidEntropy = rand.New(rand.NewSource(time.Now().UnixNano()))

func generateULID() ulid.ULID {
Expand Down
52 changes: 52 additions & 0 deletions pkg/phlaredb/block/metadata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package block

import (
"testing"

"github.com/oklog/ulid"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/tsdb"
"github.com/stretchr/testify/require"
)

func TestClone(t *testing.T) {
require.Equal(t, &Meta{}, (&Meta{}).Clone())
expected := &Meta{
ULID: generateULID(),
MinTime: model.Time(1),
MaxTime: model.Time(2),
Labels: map[string]string{"a": "b"},
Version: MetaVersion2,
Stats: BlockStats{
NumSamples: 1,
NumSeries: 2,
NumProfiles: 3,
},
Files: []File{
{
RelPath: "a",
SizeBytes: 1,
},
},
Source: IngesterSource,
Compaction: tsdb.BlockMetaCompaction{
Level: 1,
Sources: []ulid.ULID{generateULID()},
Deletable: true,
Parents: []tsdb.BlockDesc{
{
ULID: generateULID(),
MinTime: 1,
MaxTime: 2,
},
{
ULID: generateULID(),
MinTime: 2,
MaxTime: 3,
},
},
Hints: []string{"a", "b"},
},
}
require.Equal(t, expected, expected.Clone())
}

0 comments on commit 6d1fae4

Please sign in to comment.