forked from pachyderm/pachyderm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pfs.go
973 lines (904 loc) · 30.3 KB
/
pfs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
package client
import (
"bytes"
"io"
"github.com/pachyderm/pachyderm/src/client/pfs"
"github.com/pachyderm/pachyderm/src/client/pkg/grpcutil"
"github.com/gogo/protobuf/types"
)
// NewRepo creates a pfs.Repo.
func NewRepo(repoName string) *pfs.Repo {
return &pfs.Repo{Name: repoName}
}
// NewCommit creates a pfs.Commit.
func NewCommit(repoName string, commitID string) *pfs.Commit {
return &pfs.Commit{
Repo: NewRepo(repoName),
ID: commitID,
}
}
// NewFile creates a pfs.File.
func NewFile(repoName string, commitID string, path string) *pfs.File {
return &pfs.File{
Commit: NewCommit(repoName, commitID),
Path: path,
}
}
// NewBlock creates a pfs.Block.
func NewBlock(hash string) *pfs.Block {
return &pfs.Block{
Hash: hash,
}
}
// CommitTypes alias pfs.CommitType_*
const (
CommitTypeNone = pfs.CommitType_COMMIT_TYPE_NONE
CommitTypeRead = pfs.CommitType_COMMIT_TYPE_READ
CommitTypeWrite = pfs.CommitType_COMMIT_TYPE_WRITE
)
// CommitStatus alias pfs.CommitStatus_*
const (
CommitStatusNormal = pfs.CommitStatus_NORMAL
CommitStatusArchived = pfs.CommitStatus_ARCHIVED
CommitStatusCancelled = pfs.CommitStatus_CANCELLED
CommitStatusAll = pfs.CommitStatus_ALL
)
// CreateRepo creates a new Repo object in pfs with the given name. Repos are
// the top level data object in pfs and should be used to store data of a
// similar type. For example rather than having a single Repo for an entire
// project you might have seperate Repos for logs, metrics, database dumps etc.
func (c APIClient) CreateRepo(repoName string) error {
_, err := c.PfsAPIClient.CreateRepo(
c.ctx(),
&pfs.CreateRepoRequest{
Repo: NewRepo(repoName),
},
)
return sanitizeErr(err)
}
// InspectRepo returns info about a specific Repo.
func (c APIClient) InspectRepo(repoName string) (*pfs.RepoInfo, error) {
repoInfo, err := c.PfsAPIClient.InspectRepo(
c.ctx(),
&pfs.InspectRepoRequest{
Repo: NewRepo(repoName),
},
)
if err != nil {
return nil, sanitizeErr(err)
}
return repoInfo, nil
}
// ListRepo returns info about all Repos.
// provenance specifies a set of provenance repos, only repos which have ALL of
// the specified repos as provenance will be returned unless provenance is nil
// in which case it is ignored.
func (c APIClient) ListRepo(provenance []string) ([]*pfs.RepoInfo, error) {
request := &pfs.ListRepoRequest{}
for _, repoName := range provenance {
request.Provenance = append(request.Provenance, NewRepo(repoName))
}
repoInfos, err := c.PfsAPIClient.ListRepo(
c.ctx(),
request,
)
if err != nil {
return nil, sanitizeErr(err)
}
return repoInfos.RepoInfo, nil
}
// DeleteRepo deletes a repo and reclaims the storage space it was using. Note
// that as of 1.0 we do not reclaim the blocks that the Repo was referencing,
// this is because they may also be referenced by other Repos and deleting them
// would make those Repos inaccessible. This will be resolved in later
// versions.
// If "force" is set to true, the repo will be removed regardless of errors.
// This argument should be used with care.
func (c APIClient) DeleteRepo(repoName string, force bool) error {
_, err := c.PfsAPIClient.DeleteRepo(
c.ctx(),
&pfs.DeleteRepoRequest{
Repo: NewRepo(repoName),
Force: force,
},
)
return err
}
// StartCommit begins the process of committing data to a Repo. Once started
// you can write to the Commit with PutFile and when all the data has been
// written you must finish the Commit with FinishCommit. NOTE, data is not
// persisted until FinishCommit is called.
// parentCommit specifies the parent Commit, upon creation the new Commit will
// appear identical to the parent Commit, data can safely be added to the new
// commit without affecting the contents of the parent Commit. You may pass ""
// as parentCommit in which case the new Commit will have no parent and will
// initially appear empty.
// branch is a more convenient way to build linear chains of commits. When a
// commit is started with a non empty branch the value of branch becomes an
// alias for the created Commit. This enables a more intuitive access pattern.
// When the commit is started on a branch the previous head of the branch is
// used as the parent of the commit.
func (c APIClient) StartCommit(repoName string, parentCommit string) (*pfs.Commit, error) {
commit, err := c.PfsAPIClient.StartCommit(
c.ctx(),
&pfs.StartCommitRequest{
Parent: &pfs.Commit{
Repo: &pfs.Repo{
Name: repoName,
},
ID: parentCommit,
},
},
)
if err != nil {
return nil, sanitizeErr(err)
}
return commit, nil
}
// ForkCommit is the same as StartCommit except that the commit is created
// on a new branch.
func (c APIClient) ForkCommit(repoName string, parentCommit string, branch string) (*pfs.Commit, error) {
commit, err := c.PfsAPIClient.ForkCommit(
c.ctx(),
&pfs.ForkCommitRequest{
Parent: &pfs.Commit{
Repo: &pfs.Repo{
Name: repoName,
},
ID: parentCommit,
},
Branch: branch,
},
)
if err != nil {
return nil, sanitizeErr(err)
}
return commit, nil
}
// FinishCommit ends the process of committing data to a Repo and persists the
// Commit. Once a Commit is finished the data becomes immutable and future
// attempts to write to it with PutFile will error.
func (c APIClient) FinishCommit(repoName string, commitID string) error {
_, err := c.PfsAPIClient.FinishCommit(
c.ctx(),
&pfs.FinishCommitRequest{
Commit: NewCommit(repoName, commitID),
},
)
return sanitizeErr(err)
}
// ArchiveCommit marks a commit as archived. Archived commits are not listed in
// ListCommit unless commit status is set to Archived or All. Archived commits
// are not considered by FlushCommit either.
func (c APIClient) ArchiveCommit(repoName string, commitID string) error {
_, err := c.PfsAPIClient.ArchiveCommit(
c.ctx(),
&pfs.ArchiveCommitRequest{
Commits: []*pfs.Commit{NewCommit(repoName, commitID)},
},
)
return sanitizeErr(err)
}
// CancelCommit ends the process of committing data to a repo. It differs from
// FinishCommit in that the Commit will not be used as a source for downstream
// pipelines. CancelCommit is used primarily by PPS for the output commits of
// errant jobs.
func (c APIClient) CancelCommit(repoName string, commitID string) error {
_, err := c.PfsAPIClient.FinishCommit(
c.ctx(),
&pfs.FinishCommitRequest{
Commit: NewCommit(repoName, commitID),
Cancel: true,
},
)
return sanitizeErr(err)
}
// InspectCommit returns info about a specific Commit.
func (c APIClient) InspectCommit(repoName string, commitID string) (*pfs.CommitInfo, error) {
commitInfo, err := c.PfsAPIClient.InspectCommit(
c.ctx(),
&pfs.InspectCommitRequest{
Commit: NewCommit(repoName, commitID),
},
)
if err != nil {
return nil, sanitizeErr(err)
}
return commitInfo, nil
}
// ListCommitByRepo lists commits in the given repos.
//
// repoNames defines a set of Repos to consider commits from, if repoNames is left
// nil or empty then the result will be empty.
//
// provenance specifies a set of provenance commits, only commits which have
// ALL of the specified commits as provenance will be returned unless
// provenance is nil in which case it is ignored.
//
// commitType specifies the type of commit you want returned, normally CommitTypeRead is the most useful option
//
// status specifies the status of commit you want returned. By default, cancelled
// or archived commits are not returned.
//
// block, when set to true, will cause ListCommit to block until at least 1 new CommitInfo is available.
// Using repoNames and block you can get subscription semantics from ListCommit.
// commitStatus, controls the statuses of the returned commits. The default
// value `Normal` will filter out archived and cancelled commits.
func (c APIClient) ListCommitByRepo(repoNames []string, provenance []*pfs.Commit,
commitType pfs.CommitType, status pfs.CommitStatus, block bool) ([]*pfs.CommitInfo, error) {
var include []*pfs.Commit
for _, repoName := range repoNames {
include = append(include, &pfs.Commit{
Repo: NewRepo(repoName),
})
}
return c.ListCommit(include, nil, provenance, commitType, status, block)
}
// ListCommit lists commits.
//
// exclude and include are filters that either include or exclude the ancestors of the
// given commits. A commit is considered the ancestor of itself.
// For instance, ListCommit(include("foo/2")) returns commits foo/0, foo/1, and foo/2,
// if they exist. In contrast, ListCommit(exclude("foo/2")) returns commits that are
// *not* foo/0, foo/1, or foo/2.
//
// To get all commits on a given branch, simply include a commit whose ID is the branch
// name: ListCommit(include("foo"))
//
// To get all commits in a repo, use ListCommitByRepo.
//
// To get all commits, simply don't provide include or exclude.
//
// provenance specifies a set of provenance commits, only commits which have
// ALL of the specified commits as provenance will be returned unless
// provenance is nil in which case it is ignored.
//
// commitType specifies the type of commit you want returned, normally CommitTypeRead is the most useful option
//
// status specifies the status of commit you want returned. By default, cancelled
// or archived commits are not returned.
//
// block, when set to true, will cause ListCommit to block until at least 1 new CommitInfo is available.
// Using fromCommits and block you can get subscription semantics from ListCommit.
// commitStatus, controls the statuses of the returned commits. The default
// value `Normal` will filter out archived and cancelled commits.
func (c APIClient) ListCommit(exclude []*pfs.Commit, include []*pfs.Commit,
provenance []*pfs.Commit, commitType pfs.CommitType, status pfs.CommitStatus,
block bool) ([]*pfs.CommitInfo, error) {
commitInfos, err := c.PfsAPIClient.ListCommit(
c.ctx(),
&pfs.ListCommitRequest{
Exclude: exclude,
Include: include,
Provenance: provenance,
CommitType: commitType,
Status: status,
Block: block,
},
)
if err != nil {
return nil, sanitizeErr(err)
}
return commitInfos.CommitInfo, nil
}
// ListBranch lists the active branches on a Repo.
func (c APIClient) ListBranch(repoName string, status pfs.CommitStatus) ([]string, error) {
branches, err := c.PfsAPIClient.ListBranch(
c.ctx(),
&pfs.ListBranchRequest{
Repo: NewRepo(repoName),
Status: status,
},
)
if err != nil {
return nil, sanitizeErr(err)
}
return branches.Branches, nil
}
// DeleteCommit deletes a commit.
// Note it is currently not implemented.
func (c APIClient) DeleteCommit(repoName string, commitID string) error {
_, err := c.PfsAPIClient.DeleteCommit(
c.ctx(),
&pfs.DeleteCommitRequest{
Commit: NewCommit(repoName, commitID),
},
)
return sanitizeErr(err)
}
// FlushCommit blocks until all of the commits which have a set of commits as
// provenance have finished. For commits to be considered they must have all of
// the specified commits as provenance. This in effect waits for all of the
// jobs that are triggered by a set of commits to complete.
// It returns an error if any of the commits it's waiting on are cancelled due
// to one of the jobs encountering an error during runtime.
// If toRepos is not nil then only the commits up to and including those repos
// will be considered, otherwise all repos are considered.
// Note that it's never necessary to call FlushCommit to run jobs, they'll run
// no matter what, FlushCommit just allows you to wait for them to complete and
// see their output once they do.
func (c APIClient) FlushCommit(commits []*pfs.Commit, toRepos []*pfs.Repo) ([]*pfs.CommitInfo, error) {
commitInfos, err := c.PfsAPIClient.FlushCommit(
c.ctx(),
&pfs.FlushCommitRequest{
Commit: commits,
ToRepo: toRepos,
},
)
if err != nil {
return nil, sanitizeErr(err)
}
return commitInfos.CommitInfo, nil
}
// PutBlock takes a reader and splits the data in it into blocks.
// Blocks are guaranteed to be new line delimited.
// Blocks are content addressed and are thus identified by hashes of the content.
// NOTE: this is lower level function that's used internally and might not be
// useful to users.
func (c APIClient) PutBlock(delimiter pfs.Delimiter, reader io.Reader) (blockRefs *pfs.BlockRefs, retErr error) {
writer, err := c.newPutBlockWriteCloser(delimiter)
if err != nil {
return nil, sanitizeErr(err)
}
defer func() {
err := writer.Close()
if err != nil && retErr == nil {
retErr = err
}
if retErr == nil {
blockRefs = writer.blockRefs
}
}()
if _, err := io.Copy(writer, reader); err != nil {
return nil, sanitizeErr(err)
}
// Return value gets set in deferred function
return nil, nil
}
// GetBlock returns the content of a block using it's hash.
// offset specifies a number of bytes that should be skipped in the beginning of the block.
// size limits the total amount of data returned, note you will get fewer bytes
// than size if you pass a value larger than the size of the block.
// If size is set to 0 then all of the data will be returned.
// NOTE: this is lower level function that's used internally and might not be
// useful to users.
func (c APIClient) GetBlock(hash string, offset uint64, size uint64) (io.Reader, error) {
apiGetBlockClient, err := c.BlockAPIClient.GetBlock(
c.ctx(),
&pfs.GetBlockRequest{
Block: NewBlock(hash),
OffsetBytes: offset,
SizeBytes: size,
},
)
if err != nil {
return nil, sanitizeErr(err)
}
return grpcutil.NewStreamingBytesReader(apiGetBlockClient), nil
}
// DeleteBlock deletes a block from the block store.
// NOTE: this is lower level function that's used internally and might not be
// useful to users.
func (c APIClient) DeleteBlock(block *pfs.Block) error {
_, err := c.BlockAPIClient.DeleteBlock(
c.ctx(),
&pfs.DeleteBlockRequest{
Block: block,
},
)
return sanitizeErr(err)
}
// InspectBlock returns info about a specific Block.
func (c APIClient) InspectBlock(hash string) (*pfs.BlockInfo, error) {
blockInfo, err := c.BlockAPIClient.InspectBlock(
c.ctx(),
&pfs.InspectBlockRequest{
Block: NewBlock(hash),
},
)
if err != nil {
return nil, sanitizeErr(err)
}
return blockInfo, nil
}
// ListBlock returns info about all Blocks.
func (c APIClient) ListBlock() ([]*pfs.BlockInfo, error) {
blockInfos, err := c.BlockAPIClient.ListBlock(
c.ctx(),
&pfs.ListBlockRequest{},
)
if err != nil {
return nil, sanitizeErr(err)
}
return blockInfos.BlockInfo, nil
}
// PutObject puts a value into the object store and tags it with 0 or more tags.
func (c APIClient) PutObject(r io.Reader, tags ...string) (object *pfs.Object, retErr error) {
w, err := c.newPutObjectWriteCloser(tags...)
if err != nil {
return nil, sanitizeErr(err)
}
defer func() {
if err := w.Close(); err != nil && retErr == nil {
retErr = sanitizeErr(err)
}
if retErr == nil {
object = w.object
}
}()
if _, err := io.CopyBuffer(w, r, make([]byte, MaxMsgSize/2)); err != nil {
return nil, sanitizeErr(err)
}
// return value set by deferred function
return nil, nil
}
// GetObject gets an object out of the object store by hash.
func (c APIClient) GetObject(hash string, writer io.Writer) error {
getObjectClient, err := c.ObjectAPIClient.GetObject(
c.ctx(),
&pfs.Object{Hash: hash},
)
if err != nil {
return sanitizeErr(err)
}
if err := grpcutil.WriteFromStreamingBytesClient(getObjectClient, writer); err != nil {
return sanitizeErr(err)
}
return nil
}
// ReadObject gets an object by hash and returns it directly as []byte.
func (c APIClient) ReadObject(hash string) ([]byte, error) {
var buffer bytes.Buffer
if err := c.GetObject(hash, &buffer); err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
// TagObject applies a tag to an existing object.
func (c APIClient) TagObject(hash string, tags ...string) error {
var _tags []*pfs.Tag
for _, tag := range tags {
_tags = append(_tags, &pfs.Tag{Name: tag})
}
if _, err := c.ObjectAPIClient.TagObject(
c.ctx(),
&pfs.TagObjectRequest{
Object: &pfs.Object{Hash: hash},
Tags: _tags,
},
); err != nil {
return sanitizeErr(err)
}
return nil
}
// InspectObject returns info about an Object.
func (c APIClient) InspectObject(hash string) (*pfs.ObjectInfo, error) {
value, err := c.ObjectAPIClient.InspectObject(
c.ctx(),
&pfs.Object{Hash: hash},
)
if err != nil {
return nil, sanitizeErr(err)
}
return value, nil
}
// GetTag gets an object out of the object store by tag.
func (c APIClient) GetTag(tag string, writer io.Writer) error {
getTagClient, err := c.ObjectAPIClient.GetTag(
c.ctx(),
&pfs.Tag{Name: tag},
)
if err != nil {
return sanitizeErr(err)
}
if err := grpcutil.WriteFromStreamingBytesClient(getTagClient, writer); err != nil {
return sanitizeErr(err)
}
return nil
}
// ReadTag gets an object by tag and returns it directly as []byte.
func (c APIClient) ReadTag(tag string) ([]byte, error) {
var buffer bytes.Buffer
if err := c.GetTag(tag, &buffer); err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
// Compact forces compaction of objects.
func (c APIClient) Compact() error {
_, err := c.ObjectAPIClient.Compact(
c.ctx(),
&types.Empty{},
)
return err
}
// PutFileWriter writes a file to PFS.
// NOTE: PutFileWriter returns an io.WriteCloser you must call Close on it when
// you are done writing.
func (c APIClient) PutFileWriter(repoName string, commitID string, path string, delimiter pfs.Delimiter) (io.WriteCloser, error) {
return c.newPutFileWriteCloser(repoName, commitID, path, delimiter)
}
// PutFile writes a file to PFS from a reader.
func (c APIClient) PutFile(repoName string, commitID string, path string, reader io.Reader) (_ int, retErr error) {
if c.streamSemaphore != nil {
c.streamSemaphore <- struct{}{}
defer func() { <-c.streamSemaphore }()
}
return c.PutFileWithDelimiter(repoName, commitID, path, pfs.Delimiter_LINE, reader)
}
//PutFileWithDelimiter writes a file to PFS from a reader
// delimiter is used to tell PFS how to break the input into blocks
func (c APIClient) PutFileWithDelimiter(repoName string, commitID string, path string, delimiter pfs.Delimiter, reader io.Reader) (_ int, retErr error) {
writer, err := c.PutFileWriter(repoName, commitID, path, delimiter)
if err != nil {
return 0, sanitizeErr(err)
}
defer func() {
if err := writer.Close(); err != nil && retErr == nil {
retErr = err
}
}()
written, err := io.Copy(writer, reader)
return int(written), err
}
// PutFileURL puts a file using the content found at a URL.
// The URL is sent to the server which performs the request.
// recursive allow for recursive scraping of some types URLs for example on s3:// urls.
func (c APIClient) PutFileURL(repoName string, commitID string, path string, url string, recursive bool) (retErr error) {
putFileClient, err := c.PfsAPIClient.PutFile(c.ctx())
if err != nil {
return sanitizeErr(err)
}
defer func() {
if _, err := putFileClient.CloseAndRecv(); err != nil && retErr == nil {
retErr = sanitizeErr(err)
}
}()
if err := putFileClient.Send(&pfs.PutFileRequest{
File: NewFile(repoName, commitID, path),
FileType: pfs.FileType_FILE_TYPE_REGULAR,
Url: url,
Recursive: recursive,
}); err != nil {
return sanitizeErr(err)
}
return nil
}
// GetFile returns the contents of a file at a specific Commit.
// offset specifies a number of bytes that should be skipped in the beginning of the file.
// size limits the total amount of data returned, note you will get fewer bytes
// than size if you pass a value larger than the size of the file.
// If size is set to 0 then all of the data will be returned.
// fromCommitID lets you get only the data which was added after this Commit.
// shard allows you to downsample the data, returning only a subset of the
// blocks in the file. shard may be left nil in which case the entire file will be returned
func (c APIClient) GetFile(repoName string, commitID string, path string, offset int64,
size int64, fromCommitID string, fullFile bool, shard *pfs.Shard, writer io.Writer) error {
if c.streamSemaphore != nil {
c.streamSemaphore <- struct{}{}
defer func() { <-c.streamSemaphore }()
}
return c.getFile(repoName, commitID, path, offset, size, fromCommitID, fullFile, shard, writer)
}
func (c APIClient) getFile(repoName string, commitID string, path string, offset int64,
size int64, fromCommitID string, fullFile bool, shard *pfs.Shard, writer io.Writer) error {
apiGetFileClient, err := c.PfsAPIClient.GetFile(
c.ctx(),
&pfs.GetFileRequest{
File: NewFile(repoName, commitID, path),
Shard: shard,
OffsetBytes: offset,
SizeBytes: size,
DiffMethod: newDiffMethod(repoName, fromCommitID, fullFile),
},
)
if err != nil {
return sanitizeErr(err)
}
if err := grpcutil.WriteFromStreamingBytesClient(apiGetFileClient, writer); err != nil {
return sanitizeErr(err)
}
return nil
}
// InspectFile returns info about a specific file. fromCommitID lets you get
// only info which was added after this Commit. shard allows you to downsample
// the data, returning info about only a subset of the blocks in the file.
// shard may be left nil in which case info about the entire file will be
// returned
func (c APIClient) InspectFile(repoName string, commitID string, path string,
fromCommitID string, fullFile bool, shard *pfs.Shard) (*pfs.FileInfo, error) {
return c.inspectFile(repoName, commitID, path, fromCommitID, fullFile, shard)
}
func (c APIClient) inspectFile(repoName string, commitID string, path string,
fromCommitID string, fullFile bool, shard *pfs.Shard) (*pfs.FileInfo, error) {
fileInfo, err := c.PfsAPIClient.InspectFile(
c.ctx(),
&pfs.InspectFileRequest{
File: NewFile(repoName, commitID, path),
Shard: shard,
DiffMethod: newDiffMethod(repoName, fromCommitID, fullFile),
},
)
if err != nil {
return nil, sanitizeErr(err)
}
return fileInfo, nil
}
// ListFile returns info about all files in a Commit.
// fromCommitID lets you get only info which was added after this Commit.
// shard allows you to downsample the data, returning info about only a subset
// of the blocks in the files or only a subset of files. shard may be left nil
// in which case info about all the files and all the blocks in those files
// will be returned.
// recurse causes ListFile to accurately report the size of data stored in directories, it makes the call more expensive
func (c APIClient) ListFile(repoName string, commitID string, path string, fromCommitID string, fullFile bool, shard *pfs.Shard, recurse bool) ([]*pfs.FileInfo, error) {
req := &pfs.ListFileRequest{
File: NewFile(repoName, commitID, path),
Shard: shard,
DiffMethod: newDiffMethod(repoName, fromCommitID, fullFile),
}
if recurse {
req.Mode = pfs.ListFileMode_ListFile_RECURSE
} else {
req.Mode = pfs.ListFileMode_ListFile_NORMAL
}
fileInfos, err := c.PfsAPIClient.ListFile(
c.ctx(),
req,
)
if err != nil {
return nil, sanitizeErr(err)
}
return fileInfos.FileInfo, nil
}
// ListFileFast is the same as ListFile except that it doesn't compute the sizes
// of the files. As a result it's faster than ListFile.
func (c APIClient) ListFileFast(repoName string, commitID string, path string, fromCommitID string, fullFile bool, shard *pfs.Shard) ([]*pfs.FileInfo, error) {
req := &pfs.ListFileRequest{
File: NewFile(repoName, commitID, path),
Shard: shard,
DiffMethod: newDiffMethod(repoName, fromCommitID, fullFile),
Mode: pfs.ListFileMode_ListFile_FAST,
}
fileInfos, err := c.PfsAPIClient.ListFile(
c.ctx(),
req,
)
if err != nil {
return nil, sanitizeErr(err)
}
return fileInfos.FileInfo, nil
}
// WalkFn is the type of the function called for each file in Walk.
// Returning a non-nil error from WalkFn will result in Walk aborting and
// returning said error.
type WalkFn func(*pfs.FileInfo) error
// Walk walks the pfs filesystem rooted at path. walkFn will be called for each
// file found under path, this includes both regular files and directories.
func (c APIClient) Walk(repoName string, commitID string, path string, fromCommitID string, fullFile bool, shard *pfs.Shard, walkFn WalkFn) error {
fileInfos, err := c.ListFileFast(repoName, commitID, path, fromCommitID, fullFile, shard)
if err != nil {
return err
}
for _, fileInfo := range fileInfos {
if err := walkFn(fileInfo); err != nil {
return err
}
if fileInfo.FileType == pfs.FileType_FILE_TYPE_DIR {
if err := c.Walk(repoName, commitID, fileInfo.File.Path, fromCommitID, fullFile, shard, walkFn); err != nil {
return err
}
}
}
return nil
}
// DeleteFile deletes a file from a Commit.
// DeleteFile leaves a tombstone in the Commit, assuming the file isn't written
// to later attempting to get the file from the finished commit will result in
// not found error.
// The file will of course remain intact in the Commit's parent.
func (c APIClient) DeleteFile(repoName string, commitID string, path string) error {
_, err := c.PfsAPIClient.DeleteFile(
c.ctx(),
&pfs.DeleteFileRequest{
File: NewFile(repoName, commitID, path),
},
)
return err
}
// MakeDirectory creates a directory in PFS.
// Note directories are created implicitly by PutFile, so you technically never
// need this function unless you want to create an empty directory.
func (c APIClient) MakeDirectory(repoName string, commitID string, path string) (retErr error) {
putFileClient, err := c.PfsAPIClient.PutFile(c.ctx())
if err != nil {
return sanitizeErr(err)
}
defer func() {
if _, err := putFileClient.CloseAndRecv(); err != nil && retErr == nil {
retErr = sanitizeErr(err)
}
}()
return sanitizeErr(putFileClient.Send(
&pfs.PutFileRequest{
File: NewFile(repoName, commitID, path),
FileType: pfs.FileType_FILE_TYPE_DIR,
},
))
}
// SquashCommit copies the content of `fromCommits` to `to`, which needs to be an
// open commit.
func (c APIClient) SquashCommit(repo string, fromCommits []string, to string) error {
var realFromCommits []*pfs.Commit
for _, commitID := range fromCommits {
realFromCommits = append(realFromCommits, NewCommit(repo, commitID))
}
_, err := c.PfsAPIClient.SquashCommit(
c.ctx(),
&pfs.SquashCommitRequest{
FromCommits: realFromCommits,
ToCommit: NewCommit(repo, to),
},
)
if err != nil {
return sanitizeErr(err)
}
return nil
}
// ReplayCommit replays a series of commits on top of commit "to". The replayed commits
// are the ancestors of the commits in "fromCommits", with no duplicates in case of
// common ancestors.
func (c APIClient) ReplayCommit(repo string, fromCommits []string, to string) ([]*pfs.Commit, error) {
var realFromCommits []*pfs.Commit
for _, commitID := range fromCommits {
realFromCommits = append(realFromCommits, NewCommit(repo, commitID))
}
commits, err := c.PfsAPIClient.ReplayCommit(
c.ctx(),
&pfs.ReplayCommitRequest{
FromCommits: realFromCommits,
ToBranch: to,
},
)
if err != nil {
return nil, sanitizeErr(err)
}
return commits.Commit, nil
}
// ArchiveAll archives all commits in all repos.
func (c APIClient) ArchiveAll() error {
_, err := c.PfsAPIClient.ArchiveAll(
c.ctx(),
&types.Empty{},
)
return sanitizeErr(err)
}
type putFileWriteCloser struct {
request *pfs.PutFileRequest
putFileClient pfs.API_PutFileClient
sent bool
}
func (c APIClient) newPutFileWriteCloser(repoName string, commitID string, path string, delimiter pfs.Delimiter) (*putFileWriteCloser, error) {
putFileClient, err := c.PfsAPIClient.PutFile(c.ctx())
if err != nil {
return nil, err
}
return &putFileWriteCloser{
request: &pfs.PutFileRequest{
File: NewFile(repoName, commitID, path),
FileType: pfs.FileType_FILE_TYPE_REGULAR,
Delimiter: delimiter,
},
putFileClient: putFileClient,
}, nil
}
func (w *putFileWriteCloser) Write(p []byte) (int, error) {
bytesWritten := 0
for {
// Buffer the write so that we don't exceed the grpc
// MaxMsgSize. This value includes the whole payload
// including headers, so we're conservative and halve it
ceil := bytesWritten + MaxMsgSize/2
if ceil > len(p) {
ceil = len(p)
}
actualP := p[bytesWritten:ceil]
if len(actualP) == 0 {
break
}
w.request.Value = actualP
if err := w.putFileClient.Send(w.request); err != nil {
return 0, sanitizeErr(err)
}
w.sent = true
w.request.Value = nil
// File is only needed on the first request
w.request.File = nil
bytesWritten += len(actualP)
}
return bytesWritten, nil
}
func (w *putFileWriteCloser) Close() error {
// we always send at least one request, otherwise it's impossible to create
// an empty file
if !w.sent {
if err := w.putFileClient.Send(w.request); err != nil {
return err
}
}
_, err := w.putFileClient.CloseAndRecv()
return sanitizeErr(err)
}
type putBlockWriteCloser struct {
request *pfs.PutBlockRequest
putBlockClient pfs.BlockAPI_PutBlockClient
blockRefs *pfs.BlockRefs
}
func (c APIClient) newPutBlockWriteCloser(delimiter pfs.Delimiter) (*putBlockWriteCloser, error) {
putBlockClient, err := c.BlockAPIClient.PutBlock(c.ctx())
if err != nil {
return nil, sanitizeErr(err)
}
return &putBlockWriteCloser{
request: &pfs.PutBlockRequest{
Delimiter: delimiter,
},
putBlockClient: putBlockClient,
blockRefs: &pfs.BlockRefs{},
}, nil
}
func (w *putBlockWriteCloser) Write(p []byte) (int, error) {
w.request.Value = p
if err := w.putBlockClient.Send(w.request); err != nil {
return 0, sanitizeErr(err)
}
return len(p), nil
}
func (w *putBlockWriteCloser) Close() error {
var err error
w.blockRefs, err = w.putBlockClient.CloseAndRecv()
return sanitizeErr(err)
}
type putObjectWriteCloser struct {
request *pfs.PutObjectRequest
putObjectClient pfs.ObjectAPI_PutObjectClient
object *pfs.Object
}
func (c APIClient) newPutObjectWriteCloser(tags ...string) (*putObjectWriteCloser, error) {
putObjectClient, err := c.ObjectAPIClient.PutObject(c.ctx())
if err != nil {
return nil, sanitizeErr(err)
}
var _tags []*pfs.Tag
for _, tag := range tags {
_tags = append(_tags, &pfs.Tag{Name: tag})
}
return &putObjectWriteCloser{
request: &pfs.PutObjectRequest{
Tags: _tags,
},
putObjectClient: putObjectClient,
}, nil
}
func (w *putObjectWriteCloser) Write(p []byte) (int, error) {
w.request.Value = p
if err := w.putObjectClient.Send(w.request); err != nil {
return 0, sanitizeErr(err)
}
w.request.Tags = nil
return len(p), nil
}
func (w *putObjectWriteCloser) Close() error {
var err error
w.object, err = w.putObjectClient.CloseAndRecv()
return sanitizeErr(err)
}
func newDiffMethod(repoName string, fromCommitID string, fullFile bool) *pfs.DiffMethod {
if fromCommitID != "" {
return &pfs.DiffMethod{
FromCommit: NewCommit(repoName, fromCommitID),
FullFile: fullFile,
}
}
return nil
}