| 1 | package CogBase::Connection::FileSystem; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use CogBase::Connection -base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use IO::All; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 0 | ▶ | sub fetch_node { | ||||||||||||||||||||||||||
|
Subroutine
fetch_node: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 8 | 0 | my ($self, $node) = @_; | |||||||||||||||||||||||||||
| 9 | 0 | my $node_path = $self->_node_path($node); | |||||||||||||||||||||||||||
| 10 | 0 | for my $file ($node_path->all_files) { | |||||||||||||||||||||||||||
| 11 | 0 | my ($field, $value) = ($file->filename, $file->all); | |||||||||||||||||||||||||||
| 12 | 0 | ▶ | if ($field eq '!') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 13 | 0 | bless $node, "CogBase::$value"; | |||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||
| 15 | else { | ||||||||||||||||||||||||||||
| 16 | 0 | $node->{$field} = $value; | |||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | sub store_node { | ||||||||||||||||||||||||||
|
Subroutine
store_node: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 22 | 1 | my ($self, $node) = @_; | |||||||||||||||||||||||||||
| 23 | 1 | ▶ | $self->_set_new_id($node) unless ($node->Id); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | my $node_path = $self->_node_path($node); | |||||||||||||||||||||||||||
| 26 | 1 | my $head_path = $self->_head_path($node); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 1 | for my $field ($node->_fields) { | |||||||||||||||||||||||||||
| 29 | 1 | io->catfile($node_path, $field)->assert->print($node->$field); | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | 1 | io->catfile($node_path, '!')->assert->print($node->Type); | |||||||||||||||||||||||||||
| 32 | 1 | symlink($node->Revision, $head_path); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | sub query { | ||||||||||||||||||||||||||
|
Subroutine
query: called CC=5 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 36 | 1 | my ($self, $query) = @_; | |||||||||||||||||||||||||||
| 37 | 1 | ▶ | die "Invalid query '$query'" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | unless $query =~ /^!(\w+)$/; | ||||||||||||||||||||||||||||
| 39 | 1 | my $query_type = $1; | |||||||||||||||||||||||||||
| 40 | 1 | my $nodes_path = io->catdir( $self->db_location, 'nodes' ); | |||||||||||||||||||||||||||
| 41 | 1 | my $results = []; | |||||||||||||||||||||||||||
| 42 | 1 | ▶ | while (my $dir = $nodes_path->readdir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 1 | my $type_file = io->catfile($nodes_path, $dir, '0'); | |||||||||||||||||||||||||||
| 44 | 1 | ▶ | next unless $type_file->exists; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | my $head_revision = $type_file->link->readlink; | |||||||||||||||||||||||||||
| 46 | 0 | ▶ | if (io->catfile($type_file, '!')->all eq $query_type) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | push @$results, "$dir-$head_revision"; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 1 | return @$results; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 1 | ▶ | sub _node_path { | ||||||||||||||||||||||||||
|
Subroutine
_node_path: called CC=3 SCAR=12.5
| |||||||||||||||||||||||||||||
| 54 | 1 | my ($self, $node) = @_; | |||||||||||||||||||||||||||
| 55 | 1 | ▶ | ZZZ "Can't access node without Id and Revision" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$node->Id and $node->Revision
MC/DC0%0/2
$node->Id and $node->Revision
$node->Id
$node->Revision
Truth table33%1/3
A and B
A= $node->IdB= $node->Revision
| |||||||||||||||||||||||||||||
| 56 | unless $node->Id && $node->Revision; | ||||||||||||||||||||||||||||
| 57 | 1 | return io->catdir( | |||||||||||||||||||||||||||
| 58 | $self->db_location, | ||||||||||||||||||||||||||||
| 59 | 'nodes', | ||||||||||||||||||||||||||||
| 60 | $node->Id, | ||||||||||||||||||||||||||||
| 61 | $node->Revision | ||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 1 | ▶ | sub _head_path { | ||||||||||||||||||||||||||
|
Subroutine
_head_path: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 66 | 1 | my ($self, $node) = @_; | |||||||||||||||||||||||||||
| 67 | 1 | return io->catdir( | |||||||||||||||||||||||||||
| 68 | $self->db_location, | ||||||||||||||||||||||||||||
| 69 | 'nodes', | ||||||||||||||||||||||||||||
| 70 | $node->Id, | ||||||||||||||||||||||||||||
| 71 | '0' | ||||||||||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1 | ▶ | sub _set_new_id { | ||||||||||||||||||||||||||
|
Subroutine
_set_new_id: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 76 | 1 | my ($self, $node) = @_; | |||||||||||||||||||||||||||
| 77 | 1 | my $id; | |||||||||||||||||||||||||||
| 78 | 1 | ▶ | while ($id = _generate_id()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 1 | ▶ | $id = $self->_unique_id($id) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | or next; | ||||||||||||||||||||||||||||
| 81 | 1 | last; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 1 | $node->Id($id); | |||||||||||||||||||||||||||
| 84 | 1 | $node->Revision(1); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 1 | ▶ | sub _unique_id { | ||||||||||||||||||||||||||
|
Subroutine
_unique_id: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 88 | 1 | my ($self, $id) = @_; | |||||||||||||||||||||||||||
| 89 | 1 | $id =~ s/(...)/$1-/; | |||||||||||||||||||||||||||
| 90 | 1 | return $id; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 1 | ▶ | sub _generate_id { | ||||||||||||||||||||||||||
|
Subroutine
_generate_id: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||
| 94 | 1 | ▶ | if (defined $CogBase::TEST_COGBASE_IDS) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 1 | my $id = <$CogBase::TEST_COGBASE_IDS>; | |||||||||||||||||||||||||||
| 96 | 1 | chomp $id; | |||||||||||||||||||||||||||
| 97 | 1 | return $id; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | return uc( | ||||||||||||||||||||||||||||
| 100 | 0 | Convert::Base32::encode_base32( | |||||||||||||||||||||||||||
| 101 | Digest::MD5::md5( | ||||||||||||||||||||||||||||
| 102 | Data::UUID->new->create_str() | ||||||||||||||||||||||||||||
| 103 | ) | ||||||||||||||||||||||||||||
| 104 | ) | ||||||||||||||||||||||||||||
| 105 | ); | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 1 | 1; | |||||||||||||||||||||||||||