| 1 | package Net::Blossom::Server::Storage; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strictures 2; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use Carp qw(croak); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Scalar::Util qw(blessed); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | my @REQUIRED_METHODS = qw(begin_upload get_blob delete_blob list_blobs); | |||||||||
| 9 | 1 | my @REQUIRED_UPLOAD_METHODS = qw(write commit abort); | |||||||||
| 10 | |||||||||||
| 11 | 0 | ▶ | sub required_methods { | ||||||||
|
Subroutine
required_methods: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 12 | 0 | return @REQUIRED_METHODS; | |||||||||
| 13 | } | ||||||||||
| 14 | |||||||||||
| 15 | 0 | ▶ | sub required_upload_methods { | ||||||||
|
Subroutine
required_upload_methods: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 16 | 0 | return @REQUIRED_UPLOAD_METHODS; | |||||||||
| 17 | } | ||||||||||
| 18 | |||||||||||
| 19 | 0 | ▶ | sub assert_implements { | ||||||||
|
Subroutine
assert_implements: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||
| 20 | 0 | my ($class, $storage) = @_; | |||||||||
| 21 | 0 | ▶ | croak "storage is required" unless defined $storage; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | 0 | ▶ | croak "storage must be an object" unless blessed($storage); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | |||||||||||
| 24 | 0 | for my $method (@REQUIRED_METHODS) { | |||||||||
| 25 | 0 | ▶ | croak "storage must provide $method" unless $storage->can($method); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | 0 | return 1; | |||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | 0 | ▶ | sub assert_upload { | ||||||||
|
Subroutine
assert_upload: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||
| 32 | 0 | my ($class, $upload) = @_; | |||||||||
| 33 | 0 | ▶ | croak "upload writer is required" unless defined $upload; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 0 | ▶ | croak "upload writer must be an object" unless blessed($upload); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | |||||||||||
| 36 | 0 | for my $method (@REQUIRED_UPLOAD_METHODS) { | |||||||||
| 37 | 0 | ▶ | croak "upload writer must provide $method" unless $upload->can($method); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 0 | return 1; | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 1 | 1; | |||||||||
| 44 | |||||||||||
| 45 | =pod | ||||||||||
| 46 | |||||||||||
| 47 | =head1 NAME | ||||||||||
| 48 | |||||||||||
| 49 | Net::Blossom::Server::Storage - Storage contract for Blossom servers | ||||||||||
| 50 | |||||||||||
| 51 | =head1 SYNOPSIS | ||||||||||
| 52 | |||||||||||
| 53 | package My::Storage; | ||||||||||
| 54 | |||||||||||
| 55 | sub begin_upload { ... } | ||||||||||
| 56 | sub get_blob { ... } | ||||||||||
| 57 | sub delete_blob { ... } | ||||||||||
| 58 | sub list_blobs { ... } | ||||||||||
| 59 | |||||||||||
| 60 | Net::Blossom::Server::Storage->assert_implements($storage); | ||||||||||
| 61 | |||||||||||
| 62 | =head1 DESCRIPTION | ||||||||||
| 63 | |||||||||||
| 64 | C<Net::Blossom::Server::Storage> documents and validates the role-style storage | ||||||||||
| 65 | contract used by C<Net::Blossom::Server>. It is not a base class. Storage | ||||||||||
| 66 | implementations only need to provide the required methods. | ||||||||||
| 67 | |||||||||||
| 68 | The Blossom server core owns SHA-256 calculation. Storage implementations do not | ||||||||||
| 69 | calculate or trust blob hashes. For uploads, storage receives bytes through an | ||||||||||
| 70 | upload writer and receives the computed C<sha256>, C<size>, C<type>, | ||||||||||
| 71 | C<uploaded>, and optional C<pubkey> values at commit time. | ||||||||||
| 72 | |||||||||||
| 73 | =head1 IMPLEMENTATION NOTES | ||||||||||
| 74 | |||||||||||
| 75 | Backends normally store blob bytes, descriptor metadata, and owner metadata. The | ||||||||||
| 76 | descriptor URL is deployment policy, so a storage constructor will usually take | ||||||||||
| 77 | a public C<base_url> and use it when building descriptors. | ||||||||||
| 78 | |||||||||||
| 79 | Backends may separate these responsibilities using | ||||||||||
| 80 | L<Net::Blossom::Server::MetadataStore> and | ||||||||||
| 81 | L<Net::Blossom::Server::BlobStore>. A coordinator still implements this | ||||||||||
| 82 | storage contract and keeps metadata and byte changes consistent. | ||||||||||
| 83 | |||||||||||
| 84 | A minimal upload writer records bytes in C<write>, makes the blob and owner | ||||||||||
| 85 | relationship visible in C<commit>, and discards unfinished bytes in C<abort>: | ||||||||||
| 86 | |||||||||||
| 87 | sub begin_upload { ...; return My::Upload->new(...) } | ||||||||||
| 88 | sub write { ... } | ||||||||||
| 89 | sub commit { ...; return { descriptor => $blob, created => $created } } | ||||||||||
| 90 | sub abort { ... } | ||||||||||
| 91 | |||||||||||
| 92 | Database backends can implement pagination by ordering owner rows with: | ||||||||||
| 93 | |||||||||||
| 94 | ORDER BY uploaded DESC, sha256 ASC | ||||||||||
| 95 | |||||||||||
| 96 | When C<cursor> is present, first find that descriptor for the pubkey, then | ||||||||||
| 97 | return rows after it in the same order. | ||||||||||
| 98 | |||||||||||
| 99 | Filesystem backends should write uploads to a temporary path and move bytes into | ||||||||||
| 100 | place only during C<commit>, reusing the existing SHA-256 path for duplicate | ||||||||||
| 101 | bytes. They still need a metadata index for owners, descriptors, ordering, and | ||||||||||
| 102 | cursor lookup; scanning blob files alone is not enough. | ||||||||||
| 103 | |||||||||||
| 104 | =head1 STORAGE METHODS | ||||||||||
| 105 | |||||||||||
| 106 | =head2 begin_upload | ||||||||||
| 107 | |||||||||||
| 108 | my $upload = $storage->begin_upload(%context); | ||||||||||
| 109 | |||||||||||
| 110 | Starts an upload and returns an upload writer object. The context includes | ||||||||||
| 111 | C<type> and may include C<expected_sha256>, C<allowed_sha256>, | ||||||||||
| 112 | C<content_length>, and C<pubkey>. The upload writer must provide C<write>, | ||||||||||
| 113 | C<commit>, and C<abort>. | ||||||||||
| 114 | |||||||||||
| 115 | No blob or owner relationship may become visible until C<commit> succeeds. | ||||||||||
| 116 | |||||||||||
| 117 | =head2 get_blob | ||||||||||
| 118 | |||||||||||
| 119 | my $result = $storage->get_blob($sha256, %opts); | ||||||||||
| 120 | |||||||||||
| 121 | Returns a C<Net::Blossom::Server::BlobResult> for an available blob, or | ||||||||||
| 122 | C<undef> when the blob is not available. The result contains both the | ||||||||||
| 123 | C<Net::Blossom::BlobDescriptor> and the blob body as a scalar, an array | ||||||||||
| 124 | reference of scalar chunks, or a stream object with C<read> or C<getline>. | ||||||||||
| 125 | |||||||||||
| 126 | =head2 head_blob | ||||||||||
| 127 | |||||||||||
| 128 | my $descriptor = $storage->head_blob($sha256); | ||||||||||
| 129 | |||||||||||
| 130 | Optional method used by C<HEAD /E<lt>sha256E<gt>>. Storage implementations with | ||||||||||
| 131 | large blobs should provide this to avoid loading blob bodies for metadata-only | ||||||||||
| 132 | requests. It may return a C<Net::Blossom::BlobDescriptor>, a | ||||||||||
| 133 | C<Net::Blossom::Server::BlobResult>, or C<undef> when the blob is unavailable. | ||||||||||
| 134 | When absent, the server falls back to C<get_blob>. The server closes a | ||||||||||
| 135 | closeable body returned in a C<Net::Blossom::Server::BlobResult>. | ||||||||||
| 136 | |||||||||||
| 137 | =head2 get_blob_range | ||||||||||
| 138 | |||||||||||
| 139 | my $body = $storage->get_blob_range( | ||||||||||
| 140 | $sha256, | ||||||||||
| 141 | offset => $offset, | ||||||||||
| 142 | length => $length, | ||||||||||
| 143 | ); | ||||||||||
| 144 | |||||||||||
| 145 | Optional method for efficient byte-range reads. C<offset> is a zero-based, | ||||||||||
| 146 | non-negative byte offset. C<length> is a positive byte count. It returns exactly | ||||||||||
| 147 | C<length> bytes in any body form accepted by C<get_blob>, or C<undef> when the | ||||||||||
| 148 | blob is unavailable. | ||||||||||
| 149 | |||||||||||
| 150 | Implement this together with C<head_blob>. The server only uses the native range | ||||||||||
| 151 | path when both methods are present. Otherwise it uses C<get_blob> and limits the | ||||||||||
| 152 | response body to the requested bytes. | ||||||||||
| 153 | |||||||||||
| 154 | =head2 delete_blob | ||||||||||
| 155 | |||||||||||
| 156 | my $deleted = $storage->delete_blob($sha256, %opts); | ||||||||||
| 157 | |||||||||||
| 158 | Deletes one owner relationship for a blob. When C<pubkey> is passed in C<%opts>, | ||||||||||
| 159 | only that pubkey's ownership is removed. Storage may remove the underlying bytes | ||||||||||
| 160 | after the final owner is deleted. Returns true when something was deleted and | ||||||||||
| 161 | false when the blob or owner relationship was not available. | ||||||||||
| 162 | |||||||||||
| 163 | =head2 list_blobs | ||||||||||
| 164 | |||||||||||
| 165 | my $blobs = $storage->list_blobs($pubkey, %opts); | ||||||||||
| 166 | |||||||||||
| 167 | Returns an array reference of C<Net::Blossom::BlobDescriptor> objects uploaded | ||||||||||
| 168 | by C<$pubkey>, sorted by C<uploaded> descending and C<sha256> ascending. | ||||||||||
| 169 | C<cursor> is the SHA-256 hash of the last blob from the previous page and must | ||||||||||
| 170 | be excluded from the returned page. The next page starts after that descriptor in | ||||||||||
| 171 | the ordered list. An unknown C<cursor> returns an empty page. C<limit>, when | ||||||||||
| 172 | present, caps the number of descriptors returned and must be honored. | ||||||||||
| 173 | |||||||||||
| 174 | =head1 UPLOAD WRITER METHODS | ||||||||||
| 175 | |||||||||||
| 176 | =head2 write | ||||||||||
| 177 | |||||||||||
| 178 | $upload->write($bytes); | ||||||||||
| 179 | |||||||||||
| 180 | Writes a byte chunk. The server core calls this while it hashes the same bytes. | ||||||||||
| 181 | |||||||||||
| 182 | =head2 commit | ||||||||||
| 183 | |||||||||||
| 184 | my $result = $upload->commit(%metadata); | ||||||||||
| 185 | |||||||||||
| 186 | Commits the upload after the server core has computed and validated the SHA-256 | ||||||||||
| 187 | hash. C<%metadata> includes C<sha256>, C<size>, C<type>, C<uploaded>, and | ||||||||||
| 188 | optionally C<pubkey>. The method should return either a | ||||||||||
| 189 | C<Net::Blossom::Server::UploadResult> object or a hash reference with | ||||||||||
| 190 | C<descriptor> and C<created>. C<descriptor> may be a | ||||||||||
| 191 | C<Net::Blossom::BlobDescriptor> object or a hash reference suitable for | ||||||||||
| 192 | C<Net::Blossom::BlobDescriptor-E<gt>from_hash>. C<created> must be C<1> when | ||||||||||
| 193 | the blob bytes were newly stored and C<0> when the blob already existed. | ||||||||||
| 194 | |||||||||||
| 195 | For pre-release compatibility, raw C<Net::Blossom::BlobDescriptor> objects and | ||||||||||
| 196 | descriptor hash references are accepted and treated as C<< created => 1 >>. | ||||||||||
| 197 | |||||||||||
| 198 | C<commit> must be atomic from the caller's view. After a successful C<commit>, | ||||||||||
| 199 | the blob and owner relationship must be visible. If C<commit> fails, neither may | ||||||||||
| 200 | be visible. | ||||||||||
| 201 | |||||||||||
| 202 | =head2 abort | ||||||||||
| 203 | |||||||||||
| 204 | $upload->abort; | ||||||||||
| 205 | |||||||||||
| 206 | Aborts an upload after validation or write failure. C<abort> should be safe to | ||||||||||
| 207 | call more than once. Aborted uploads must not become visible through C<get_blob> | ||||||||||
| 208 | or C<list_blobs>. | ||||||||||
| 209 | |||||||||||
| 210 | =head1 METHODS | ||||||||||
| 211 | |||||||||||
| 212 | =head2 required_methods | ||||||||||
| 213 | |||||||||||
| 214 | my @methods = Net::Blossom::Server::Storage->required_methods; | ||||||||||
| 215 | |||||||||||
| 216 | Returns the required storage method names. | ||||||||||
| 217 | |||||||||||
| 218 | =head2 required_upload_methods | ||||||||||
| 219 | |||||||||||
| 220 | my @methods = Net::Blossom::Server::Storage->required_upload_methods; | ||||||||||
| 221 | |||||||||||
| 222 | Returns the required upload writer method names. | ||||||||||
| 223 | |||||||||||
| 224 | =head2 assert_implements | ||||||||||
| 225 | |||||||||||
| 226 | Net::Blossom::Server::Storage->assert_implements($storage); | ||||||||||
| 227 | |||||||||||
| 228 | Croaks unless C<$storage> is an object that provides the required storage | ||||||||||
| 229 | methods. Returns true otherwise. | ||||||||||
| 230 | |||||||||||
| 231 | =head2 assert_upload | ||||||||||
| 232 | |||||||||||
| 233 | Net::Blossom::Server::Storage->assert_upload($upload); | ||||||||||
| 234 | |||||||||||
| 235 | Croaks unless C<$upload> is an object that provides the required upload writer | ||||||||||
| 236 | methods. Returns true otherwise. | ||||||||||
| 237 | |||||||||||
| 238 | =head1 BACKEND TESTS | ||||||||||
| 239 | |||||||||||
| 240 | Backend distributions should use | ||||||||||
| 241 | L<Net::Blossom::Server::Storage::Test/run_storage_contract_tests> to verify that | ||||||||||
| 242 | their storage implementation satisfies this contract. | ||||||||||
| 243 | |||||||||||
| 244 | =cut | ||||||||||