From 93083bfcc5ee2201a39f728d44ef3dac550a3009 Mon Sep 17 00:00:00 2001 From: Joshua Roys Date: Mon, 7 Jul 2008 21:08:57 -0400 Subject: [PATCH] Basic outline of the commit reel and index --- lib/VCS/Git/Torrent/CommitReel/Entry.pm | 36 ++++++++++++++++++++++ lib/VCS/Git/Torrent/CommitReel/Index.pm | 53 +++++++++++++++++++++++++++++++++ t/43-peer-reel-index.t | 7 +++++ 3 files changed, 96 insertions(+) create mode 100644 lib/VCS/Git/Torrent/CommitReel/Entry.pm create mode 100644 lib/VCS/Git/Torrent/CommitReel/Index.pm diff --git a/lib/VCS/Git/Torrent/CommitReel/Entry.pm b/lib/VCS/Git/Torrent/CommitReel/Entry.pm new file mode 100644 index 0000000..486bf4f --- /dev/null +++ b/lib/VCS/Git/Torrent/CommitReel/Entry.pm @@ -0,0 +1,36 @@ + +# temporary, definitions from VCS::Git +package VCS::Git; + +use Moose::Util::TypeConstraints; + +enum "VCS::Git::object_type" => qw(blob commit tag tree); +subtype "VCS::Git::SHA1" => as "Str" => where { /^[0-9a-f]{40}$/ }; +### + +package VCS::Git::Torrent::CommitReel::Entry; + +use Moose; +use Storable; + +has 'offset' => + isa => 'Int', + is => 'rw'; + +has 'type' => + isa => 'VCS::Git::object_type', + is => 'ro'; + +has 'size' => + isa => 'Int', + is => 'ro'; + +has 'objectid' => + isa => 'VCS::Git::SHA1', + is => 'ro'; + +has 'path' => # for debugging + isa => 'Str', + is => 'ro'; + +1; diff --git a/lib/VCS/Git/Torrent/CommitReel/Index.pm b/lib/VCS/Git/Torrent/CommitReel/Index.pm new file mode 100644 index 0000000..cc2e92a --- /dev/null +++ b/lib/VCS/Git/Torrent/CommitReel/Index.pm @@ -0,0 +1,53 @@ + +package VCS::Git::Torrent::CommitReel::Index; + +use DB_File; +use Moose; +use Storable; + +has 'index' => + isa => 'HashRef', + is => 'rw', + default => sub { + my $self = shift; + $self->open_index; + }; + +sub open_index { + my $self = shift; + my %index; + my $x; + + # define the sort function; the offset is the hash index, so we + # need to force numeric sorting not string compare + $DB_BTREE->{'compare'} = sub { $_[0] <=> $_[1] }; + $x = tie %index, 'DB_File' => 'reel.idx', + O_CREAT|O_RDWR, 0666, $DB_BTREE; + + $self->{db} = $x; + + \%index; +} + +has 'git' => + isa => 'Git', + is => 'ro', + required => 1; + +sub update_index { + my $self = shift; + my ($key, $val); + my $last_sha1; + + if ( $self->{db}->seq($key, $val, R_LAST) ) + { # assume empty? + ; + } + else + { + my $last_entry = thaw $val; + $last_sha1 = $last_entry->objectid; + } +} + +1; diff --git a/t/43-peer-reel-index.t b/t/43-peer-reel-index.t index c9bdd0a..859fb7f 100644 --- a/t/43-peer-reel-index.t +++ b/t/43-peer-reel-index.t @@ -4,7 +4,14 @@ use Test::More no_plan; use strict; use warnings; +BEGIN { use_ok('Git') } +BEGIN { use_ok('VCS::Git::Torrent::CommitReel::Index') } + # This test script tests the "VCS::Git::Torrent::CommitReel::Index" # module +my $git = Git->repository('.'); # TODO: make a fake repo on the fly +ok($git, 'We have a git repo'); +my $reel_index = VCS::Git::Torrent::CommitReel::Index->new( git => $git ); +ok($reel_index, 'The reel sees the repo'); -- 2.11.4.GIT