| 1 | #!/usr/bin/perl | ||||||||||
| 2 | |||||||||||
| 3 | package KiokuDB::LiveObjects::Scope; | ||||||||||
| 4 | 22 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 22 | ▶ | use namespace::clean -except => 'meta'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | has objects => ( | ||||||||||
| 9 | traits => [qw(Array)], | ||||||||||
| 10 | isa => "ArrayRef", | ||||||||||
| 11 | 3277 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 22 | clearer => "_clear_objects", | |||||||||
| 13 | handles => { | ||||||||||
| 14 | push => "push", | ||||||||||
| 15 | objects => "elements", | ||||||||||
| 16 | clear => "clear", | ||||||||||
| 17 | }, | ||||||||||
| 18 | ); | ||||||||||
| 19 | |||||||||||
| 20 | 22 | has parent => ( | |||||||||
| 21 | isa => __PACKAGE__, | ||||||||||
| 22 | is => "ro", | ||||||||||
| 23 | ); | ||||||||||
| 24 | |||||||||||
| 25 | 22 | has live_objects => ( | |||||||||
| 26 | isa => "KiokuDB::LiveObjects", | ||||||||||
| 27 | is => "ro", | ||||||||||
| 28 | clearer => "_clear_live_objects", | ||||||||||
| 29 | ); | ||||||||||
| 30 | |||||||||||
| 31 | sub DEMOLISH { | ||||||||||
| 32 | 3277 | ▶ | my $self = shift; | ||||||||
|
Subroutine
DEMOLISH: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 33 | |||||||||||
| 34 | # consider possibilities of optimizing live object set removal at this | ||||||||||
| 35 | # point | ||||||||||
| 36 | |||||||||||
| 37 | # problems can arise from an object outliving the scope it was loaded in: | ||||||||||
| 38 | # { my $outer = lookup(...); { my $inner = lookup(...); $outer->foo($inner) } } | ||||||||||
| 39 | |||||||||||
| 40 | 3277 | $self->remove; | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | sub detach { | ||||||||||
| 44 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
detach: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 45 | |||||||||||
| 46 | 1 | ▶ | if ( my $l = $self->live_objects ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 1 | $l->detach_scope($self); | |||||||||
| 48 | } | ||||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | sub remove { | ||||||||||
| 52 | 3278 | ▶ | my $self = shift; | ||||||||
|
Subroutine
remove: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 53 | |||||||||||
| 54 | 3278 | ▶ | if ( my $l = $self->live_objects ) { # can be false under global destruction | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 3277 | $l->remove_scope($self); | |||||||||
| 56 | 3277 | $self->_clear_live_objects; | |||||||||
| 57 | } | ||||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | 22 | __PACKAGE__->meta->make_immutable; | |||||||||
| 61 | |||||||||||
| 62 | 22 | __PACKAGE__ | |||||||||
| 63 | |||||||||||
| 64 | __END__ | ||||||||||
| 65 | |||||||||||
| 66 | =pod | ||||||||||
| 67 | |||||||||||
| 68 | =head1 NAME | ||||||||||
| 69 | |||||||||||
| 70 | KiokuDB::LiveObjects::Scope - Scope helper object | ||||||||||
| 71 | |||||||||||
| 72 | =head1 SYNOPSIS | ||||||||||
| 73 | |||||||||||
| 74 | { | ||||||||||
| 75 | my $scope = $dir->new_scope; | ||||||||||
| 76 | |||||||||||
| 77 | ... do work on $dir ... | ||||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | =head1 DESCRIPTION | ||||||||||
| 81 | |||||||||||
| 82 | Live object scopes exist in order to ensure objects don't die too soon if the | ||||||||||
| 83 | only other references to them are weak. | ||||||||||
| 84 | |||||||||||
| 85 | When scopes are destroyed the refcounts of the objects they refer to go down, | ||||||||||
| 86 | and the parent scope is replaced in the live object set. | ||||||||||
| 87 | |||||||||||
| 88 | =head1 METHODS | ||||||||||
| 89 | |||||||||||
| 90 | =over 4 | ||||||||||
| 91 | |||||||||||
| 92 | =item push | ||||||||||
| 93 | |||||||||||
| 94 | Adds objects or entries, increasing their reference count. | ||||||||||
| 95 | |||||||||||
| 96 | =item clear | ||||||||||
| 97 | |||||||||||
| 98 | Clears the objects from the scope object. | ||||||||||
| 99 | |||||||||||
| 100 | =item detach | ||||||||||
| 101 | |||||||||||
| 102 | Marks this scope as no longer the "current" live object scope, if it is the current one. | ||||||||||
| 103 | |||||||||||
| 104 | This allows keeping branching of scopes, which can be useful under long running | ||||||||||
| 105 | applications. | ||||||||||
| 106 | |||||||||||
| 107 | =item remove | ||||||||||
| 108 | |||||||||||
| 109 | Effectively kills the scope by clearing it and removing it from the live object set. | ||||||||||
| 110 | |||||||||||
| 111 | =back | ||||||||||
| 112 | |||||||||||
| 113 | =cut | ||||||||||
| 114 | |||||||||||
| 115 | |||||||||||