annotate lib/IMPL/Config/ServicesBag.pm @ 408:5c80e33f1218 ref20150831

added 'coarsen' function
author cin
date Mon, 07 Sep 2015 01:35:25 +0300
parents c6e90e02dd17
children ee36115f6a34
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
1 package IMPL::Config::ServicesBag;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3 require v5.9.5;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use mro;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 use IMPL::Const qw(:prop);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8 use IMPL::declare {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 base => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 'IMPL::Object' => undef
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 ],
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 props => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 _prototype => PROP_RW,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 _nameMap => PROP_RW,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 _typeMap => PROP_RW,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 _props => PROP_RW,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 ]
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21 my ( $this, $prototype ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 $this->_prototype($prototype) if $prototype;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 $this->_nameMap( {} );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 $this->_typeMap( {} );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 sub GetDescriptorByName {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 my ( $this, $name ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 my $d = $this->_nameMap->{$name};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 return $d if $d and $d->{valid};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 my $parent = $this->_prototype;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 if ( $parent and $d = $parent->GetDescriptorByName($name) ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 return $this->_nameMap->{$name} = $d;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 return undef;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 sub GetDescriptorByType {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 my ( $this, $type ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 my $d = $this->_typeMap->{$type};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 return $d if $d and $d->{valid};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 my $parent = $this->_prototype;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 if ( $parent and $d = $parent->GetDescriptorByType($type) ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 return $this->_typeMap->{$type} = $d;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 return undef;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 sub RegisterValue {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 my ( $this, $value, $name, $type ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 my $d = { owner => $this, value => $value, valid => 1 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 if ($type) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 my $map = $this->_typeMap;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 my $isa = mro::get_linear_isa($type);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 $d->{isa} = $isa;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 # the service record which is superseded by the current one
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 my $replaces = $this->GetDescriptorByType($type);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 foreach my $t (@$isa) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 if ( my $prev = $this->GetDescriptorByType($t) ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 # keep previous registrations if they are valid
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74 next if not $replaces or $prev != $replaces;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 $map->{$t} = $d;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80 if ($replaces) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 # invalidate cache
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 $replaces->{owner}->UpdateDescriptor($replaces);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87 if ($name) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 my $prev = $this->_nameMap->{$name};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89 $d->{name} = $name;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90 $this->_nameMap->{$name} = $d;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91 $prev->{owner}->UpdateDescriptor($prev) if $prev;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94 return $d;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97 sub UpdateDescriptor {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98 my ( $this, $d ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100 my $d2 = {};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 # copy descriptor
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103 while ( my ( $k, $v ) = each %$d ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104 $d2->{$k} = $v;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107 # update named entries
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108 my $name = $d->{name};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109 if ( $name and $this->_nameMap->{$name} == $d ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110 $this->_nameMap->{$name} = $d2;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113 # update type entries
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114 if ( my $isa = $d->{isa} ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 my $map = $this->_typeMap;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 foreach my $t (@$isa) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 next unless $map->{$t} == $d;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118 $map->{$t} = $d2;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122 $d->{valid} = 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133 =head1 SYNOPSIS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135 =head1 DESCRIPTION
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137 Коллекция сервисов построена на прототиптровании экземпляров, т.е. при создании
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138 новой коллекции может указваться базовая коллекция в которой будет происходить
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
139 поиск сервисов в случае их отсутсвия в основной. Для оптимизации данного процесса
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140 сервисы кешируются, чтобы избежать можестрвенных операций поиска по иерархии
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
141 коллекций, для этого каждый сервис описывается дескриптором:
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
142
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
143 =over
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
144
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
145 =item * isa массив типов сервиса, если он регистрировался как сервис
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
146
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
147 =item * value значение
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
148
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
149 =item * valid признак того, что дескриптор действителен
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
150
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
151 =item * owner коллекция, которая создала данный дескриптор
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
152
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
153 =back
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
154
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
155 Если запрашиваемый десриптор не найден это является ошибкой, поэтому негативные
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
156 ответы не кешируются
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
157
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
158 =cut