X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/6eb3d1fb8fe71623fa63da46c250184cf2e4fbb8..816764a283c2cbf2d41b4582113065922b99bd52:/sdk/go/blockdigest/blockdigest.go diff --git a/sdk/go/blockdigest/blockdigest.go b/sdk/go/blockdigest/blockdigest.go index 5c29b902f2..b9ecc45abc 100644 --- a/sdk/go/blockdigest/blockdigest.go +++ b/sdk/go/blockdigest/blockdigest.go @@ -1,12 +1,20 @@ +// Copyright (C) The Arvados Authors. All rights reserved. +// +// SPDX-License-Identifier: Apache-2.0 + // Stores a Block Locator Digest compactly. Can be used as a map key. package blockdigest import ( "fmt" - "log" + "regexp" "strconv" + "strings" ) +var LocatorPattern = regexp.MustCompile( + "^[0-9a-fA-F]{32}\\+[0-9]+(\\+[A-Z][A-Za-z0-9@_-]*)*$") + // Stores a Block Locator Digest compactly, up to 128 bits. // Can be used as a map key. type BlockDigest struct { @@ -14,10 +22,25 @@ type BlockDigest struct { L uint64 } +type DigestWithSize struct { + Digest BlockDigest + Size uint32 +} + +type BlockLocator struct { + Digest BlockDigest + Size int + Hints []string +} + func (d BlockDigest) String() string { return fmt.Sprintf("%016x%016x", d.H, d.L) } +func (w DigestWithSize) String() string { + return fmt.Sprintf("%s+%d", w.Digest.String(), w.Size) +} + // Will create a new BlockDigest unless an error is encountered. func FromString(s string) (dig BlockDigest, err error) { if len(s) != 32 { @@ -38,11 +61,33 @@ func FromString(s string) (dig BlockDigest, err error) { return } -// Will fatal with the error if an error is encountered -func AssertFromString(s string) BlockDigest { - d, err := FromString(s) - if err != nil { - log.Fatalf("Error creating BlockDigest from %s: %v", s, err) +func IsBlockLocator(s string) bool { + return LocatorPattern.MatchString(s) +} + +func ParseBlockLocator(s string) (b BlockLocator, err error) { + if !LocatorPattern.MatchString(s) { + err = fmt.Errorf("String \"%s\" does not match BlockLocator pattern "+ + "\"%s\".", + s, + LocatorPattern.String()) + } else { + tokens := strings.Split(s, "+") + var blockSize int64 + var blockDigest BlockDigest + // We expect both of the following to succeed since LocatorPattern + // restricts the strings appropriately. + blockDigest, err = FromString(tokens[0]) + if err != nil { + return + } + blockSize, err = strconv.ParseInt(tokens[1], 10, 0) + if err != nil { + return + } + b.Digest = blockDigest + b.Size = int(blockSize) + b.Hints = tokens[2:] } - return d + return }