[Jifty-commit] r6769 - in jifty/trunk/lib/Jifty: .

Jifty commits jifty-commit at lists.jifty.org
Thu Apr 9 23:10:06 EDT 2009


Author: alexmv
Date: Thu Apr  9 23:10:05 2009
New Revision: 6769

Added:
   jifty/trunk/lib/Jifty/CAS/
   jifty/trunk/lib/Jifty/CAS/Blob.pm
Modified:
   jifty/trunk/lib/Jifty/CAS.pm

Log:
Split Jifty::CAS::Blob out, and document

Modified: jifty/trunk/lib/Jifty/CAS.pm
==============================================================================
--- jifty/trunk/lib/Jifty/CAS.pm	(original)
+++ jifty/trunk/lib/Jifty/CAS.pm	Thu Apr  9 23:10:05 2009
@@ -23,49 +23,66 @@
 
 =head1 DESCRIPTION
 
-
+Provides an in-memory C<md5>-addressed content store.  Content is
+stored under a "domain", and can be addressed using wither the "key",
+which is an C<md5> sum, or the "name", which simply stores the most
+recent key provided with that name.
 
 =cut
 
 my %CONTAINER;
 
-use Digest::MD5 'md5_hex';
-use Compress::Zlib ();
+use Jifty::CAS::Blob;
+
+=head1 METHODS
+
+=head2 publish DOMAIN NAME CONTENT METADATA
+
+Publishes the given C<CONTENT> at the address C<DOMAIN> and C<NAME>.
+C<METADATA> is an arbitrary hash; see L<Jifty::CAS::Blob> for more.
+Returns the key.
+
+=cut
 
 sub publish {
     my ($class, $domain, $name, $content, $opt) = @_;
     my $db = $CONTAINER{$domain} ||= {};
+    $opt ||= {};
 
-    my $key = md5_hex( delete $opt->{hash_with} || $content );
-    $db->{DB}{$key} = Jifty::CAS::Blob->new
-        ( { content => $content,
-            metadata => $opt } );
+    my $blob = Jifty::CAS::Blob->new(
+        {   content  => $content,
+            metadata => $opt,
+        }
+    );
+    my $key = $blob->key;
+    $db->{DB}{$key} = $blob;
     $db->{KEYS}{$name} = $key;
 
     return $key;
 }
 
+=head2 key DOMAIN NAME
+
+Returns the most recent key for the given pair of C<DOMAIN> and
+C<NAME>, or undef if none such exists.
+
+=cut
+
 sub key {
     my ($class, $domain, $name) = @_;
     return $CONTAINER{$domain}{KEYS}{$name};
 }
 
-sub retrieve {
-    my ($class, $domain, $key) = @_;
-    return $CONTAINER{$domain}{DB}{$key};
-}
-
-package Jifty::CAS::Blob;
+=head2 retrieve DOMAIN KEY
 
-use base 'Class::Accessor::Fast';
+Returns a L<Jifty::CAS::Blob> for the given pair of C<DOMAIN> and
+C<KEY>, or undef if none such exists.
 
-__PACKAGE__->mk_accessors(qw(content content_deflated metadata));
+=cut
 
-sub new {
-    my $class = shift;
-    my $self = $class->SUPER::new(@_);
-    $self->content_deflated(Compress::Zlib::memGzip($self->content)) if $self->metadata->{deflate};
-    return $self;
+sub retrieve {
+    my ($class, $domain, $key) = @_;
+    return $CONTAINER{$domain}{DB}{$key};
 }
 
 1;

Added: jifty/trunk/lib/Jifty/CAS/Blob.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/lib/Jifty/CAS/Blob.pm	Thu Apr  9 23:10:05 2009
@@ -0,0 +1,79 @@
+package Jifty::CAS::Blob;
+use strict;
+
+use base 'Class::Accessor::Fast';
+use Digest::MD5 'md5_hex';
+use Compress::Zlib ();
+
+=head1 NAME
+
+Jifty::CAS::Blob - An object in Jifty's content-addressed store
+
+=head1 SYNOPSIS
+
+  my $blob = Jifty::CAS->retrieve('js', $key);
+  my $content = $blob->content;
+  my $meta = $blob->metadata;
+  my $key = $blob->key;
+
+=head1 DESCRIPTION
+
+Objects in the content-addressed store can have arbitrary metadata
+associated with them, in addition to storing their contents.
+
+=head1 METHODS
+
+=head2 new HASHREF
+
+Takes a HASHREF, with possible keys L</content> and L</metadata>, and
+creates a new object.  Possible special keys in the metadata include:
+
+=over
+
+=item hash_with
+
+Provides the data to hash to generate the address.  If no C<hash_with>
+is provided, the content itself is hashed.
+
+=item deflate
+
+If set to a true value, deflates the content using
+L<Compress::Zlib/memGzip>, and stores that in L</content_deflated>.
+
+=back
+
+=head2 content
+
+Returns the content of the blob.
+
+=head2 content_deflated
+
+If L</deflate> in the metadata was set, contains the deflated content.
+
+=head2 metadata
+
+Returns a hashref of metadata.
+
+=head2 key
+
+Retuens the key calculated for this content.
+
+=cut
+
+__PACKAGE__->mk_accessors(qw(content content_deflated metadata key));
+
+sub new {
+    my $class = shift;
+    my $args = shift;
+    my $self  = $class->SUPER::new( {
+        content => "",
+        metadata => {},
+        %$args,
+    } );
+    $self->key( md5_hex( $self->metadata->{hash_with} || $self->content ) );
+    $self->content_deflated( Compress::Zlib::memGzip( $self->content ) )
+        if $self->metadata->{deflate};
+    return $self;
+}
+
+1;


More information about the Jifty-commit mailing list