Skip to content

Commit 6804884

Browse files
committed
Added GetFileLastModified method to the DataStore interface.
1 parent e8aa374 commit 6804884

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

services/galexie/internal/integration_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ func (s *GalexieTestSuite) TestAppend() {
111111
err := rootCmd.ExecuteContext(s.ctx)
112112
require.NoError(err)
113113

114+
datastore, err := datastore.NewDataStore(s.ctx, s.config.DataStoreConfig)
115+
require.NoError(err)
116+
117+
lastModified, err := datastore.GetFileLastModified(s.ctx, "FFFFFFFF--0-9/FFFFFFF6--9.xdr.zstd")
118+
require.NoError(err)
119+
114120
// now run an append of overalapping range, it will resume past existing ledgers
115121
rootCmd.SetArgs([]string{"append", "--start", "6", "--end", "9", "--config-file", s.tempConfigFile})
116122
var errWriter bytes.Buffer
@@ -125,8 +131,10 @@ func (s *GalexieTestSuite) TestAppend() {
125131
s.T().Log(output)
126132
s.T().Log(errOutput)
127133

128-
datastore, err := datastore.NewDataStore(s.ctx, s.config.DataStoreConfig)
134+
// check that the file was not modified
135+
newLastModified, err := datastore.GetFileLastModified(s.ctx, "FFFFFFFF--0-9/FFFFFFF6--9.xdr.zstd")
129136
require.NoError(err)
137+
require.Equal(lastModified, newLastModified, "file should not be modified on append of overlapping range")
130138

131139
_, err = datastore.GetFile(s.ctx, "FFFFFFFF--0-9/FFFFFFF6--9.xdr.zstd")
132140
require.NoError(err)

support/datastore/datastore.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package datastore
33
import (
44
"context"
55
"io"
6+
"time"
67

78
"github.com/stellar/go/support/errors"
89
)
@@ -16,6 +17,7 @@ type DataStoreConfig struct {
1617
// DataStore defines an interface for interacting with data storage
1718
type DataStore interface {
1819
GetFileMetadata(ctx context.Context, path string) (map[string]string, error)
20+
GetFileLastModified(ctx context.Context, filePath string) (time.Time, error)
1921
GetFile(ctx context.Context, path string) (io.ReadCloser, error)
2022
PutFile(ctx context.Context, path string, in io.WriterTo, metaData map[string]string) error
2123
PutFileIfNotExists(ctx context.Context, path string, in io.WriterTo, metaData map[string]string) (bool, error)

support/datastore/gcs_datastore.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os"
1212
"path"
1313
"strings"
14+
"time"
1415

1516
"cloud.google.com/go/storage"
1617
"google.golang.org/api/googleapi"
@@ -65,18 +66,35 @@ func FromGCSClient(ctx context.Context, client *storage.Client, bucketPath strin
6566
return &GCSDataStore{client: client, bucket: bucket, prefix: prefix, schema: schema}, nil
6667
}
6768

68-
// GetFileMetadata retrieves the metadata for the specified file in the GCS bucket.
69-
func (b GCSDataStore) GetFileMetadata(ctx context.Context, filePath string) (map[string]string, error) {
69+
func (b GCSDataStore) GetFileAttrs(ctx context.Context, filePath string) (*storage.ObjectAttrs, error) {
7070
filePath = path.Join(b.prefix, filePath)
7171
attrs, err := b.bucket.Object(filePath).Attrs(ctx)
7272
if err != nil {
7373
if errors.Is(err, storage.ErrObjectNotExist) {
7474
return nil, os.ErrNotExist
7575
}
7676
}
77+
return attrs, nil
78+
}
79+
80+
// GetFileMetadata retrieves the metadata for the specified file in the GCS bucket.
81+
func (b GCSDataStore) GetFileMetadata(ctx context.Context, filePath string) (map[string]string, error) {
82+
attrs, err := b.GetFileAttrs(ctx, filePath)
83+
if err != nil {
84+
return nil, err
85+
}
7786
return attrs.Metadata, nil
7887
}
7988

89+
// GetFileLastModified retrieves the last modified time of a file in the GCS bucket.
90+
func (b GCSDataStore) GetFileLastModified(ctx context.Context, filePath string) (time.Time, error) {
91+
attrs, err := b.GetFileAttrs(ctx, filePath)
92+
if err != nil {
93+
return time.Time{}, err
94+
}
95+
return attrs.Updated, nil
96+
}
97+
8098
// GetFile retrieves a file from the GCS bucket.
8199
func (b GCSDataStore) GetFile(ctx context.Context, filePath string) (io.ReadCloser, error) {
82100
filePath = path.Join(b.prefix, filePath)

support/datastore/s3_datastore.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"path"
1111
"strings"
12+
"time"
1213

1314
"github.com/aws/aws-sdk-go-v2/aws"
1415
"github.com/aws/aws-sdk-go-v2/config"
@@ -77,8 +78,7 @@ func FromS3Client(ctx context.Context, client *s3.Client, bucketPath string, sch
7778
return S3DataStore{client: client, uploader: uploader, bucket: bucketName, prefix: prefix, schema: schema}, nil
7879
}
7980

80-
// GetFileMetadata retrieves the metadata for the specified file in the S3-compatible bucket.
81-
func (b S3DataStore) GetFileMetadata(ctx context.Context, filePath string) (map[string]string, error) {
81+
func (b S3DataStore) HeadObject(ctx context.Context, filePath string) (*s3.HeadObjectOutput, error) {
8282
filePath = path.Join(b.prefix, filePath)
8383
input := &s3.HeadObjectInput{
8484
Bucket: aws.String(b.bucket),
@@ -93,7 +93,25 @@ func (b S3DataStore) GetFileMetadata(ctx context.Context, filePath string) (map[
9393
return nil, err
9494
}
9595

96-
return output.Metadata, nil
96+
return output, nil
97+
}
98+
99+
// GetFileMetadata retrieves the metadata for the specified file in the S3-compatible bucket.
100+
func (b S3DataStore) GetFileMetadata(ctx context.Context, filePath string) (map[string]string, error) {
101+
attrs, err := b.HeadObject(ctx, filePath)
102+
if err != nil {
103+
return nil, err
104+
}
105+
return attrs.Metadata, nil
106+
}
107+
108+
// GetFileLastModified retrieves the last modified time of a file in the S3-compatible bucket.
109+
func (b S3DataStore) GetFileLastModified(ctx context.Context, filePath string) (time.Time, error) {
110+
attrs, err := b.HeadObject(ctx, filePath)
111+
if err != nil {
112+
return time.Time{}, err
113+
}
114+
return *attrs.LastModified, nil
97115
}
98116

99117
// GetFile retrieves a file from the S3-compatible bucket.

0 commit comments

Comments
 (0)