summaryrefslogtreecommitdiff
path: root/framework/Log/EventLog/map.php
blob: 2c4f72f7d6b939c22b89aff3dddd9492a29a95b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<?php
/**
 * File containing the ezcLogMap class.
 *
 * @package EventLog
 * @version //autogentag//
 * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved.
 * @license http://ez.no/licenses/new_bsd New BSD License
 * @access private
 */

/**
 * Fast mapping of a mixed variable to an eventType, eventSource and eventCategory.
 *
 * This class is used in ezcLog to map writers to the log messages they
 * are supposed to write. It is important that the get() method is extremely
 * fast since it will be called for each log message.
 *
 * If every combination of eventTypes, eventSources, and eventCategories are
 * combined (product) and placed in one hash-table. The table grows too fast.
 * Imagine: 7 types * 10 sources * 10 categories = 700 entries in the hash.
 *
 * Our solution:
 * Most probably the amount of writers will be limited. So every writer
 * (result) is one bit (of an integer).
 *
 * The hashes: Type, Source, and Category map the input strings to an integer.
 * These integers are AND with each other, and the result is again stored in a
 * hash.
 *
 *
 * @package EventLog
 * @version //autogentag//
 * @access private
 */
class ezcLogMap
{
    /**
     * Hashtable that binds a single eventType to a bitmask. The possible
     * key-values are: 1, 2, 4, 8, ...
     *
     * @var array(integer=>bitmask)
     */
    protected $structure;


    public function __construct()
    {
        $this->result = array();
    }

    /**
     * Map the object $object to the given $eventTypeMask, $eventSources and $eventCategories.
     *
     * Example:
     * <code>
     * [ eventType x eventSource x eventCategory ] |-> [ object ]
     * </code>
     *
     * @param int $eventTypeMask
     * @param array(string) $eventSources
     * @param array(string) $eventCategories
     * @param object $object
     *
     * @return void
     */
    public function map( $eventTypeMask, $eventSources, $eventCategories, $object )
    {
        $eventTypes = $this->getMaskArray( $eventTypeMask );
        $result = array( $object );

        if ( count( $eventTypes ) == 0 )
        {
            $eventTypes = array( "*" );
        }
        if ( count( $eventSources ) == 0 )
        {
            $eventSources = array( "*" );
        }
        if ( count( $eventCategories ) == 0)
        {
            $eventCategories = array( "*" );
        }
        $c = $this->createMinimumHash( $eventCategories, $result );
        $s = $this->createMinimumHash( $eventSources, $c );
        $new = $this->createMinimumHash( $eventTypes, $s );

        $this->mergeHash( $new, $this->structure, $history );
    }

    /**
     * Unmap the object $object to the given $eventTypeMask, $eventSources and $eventCategories.
     *
     * @param int $eventTypeMask
     * @param array(string) $eventSources
     * @param array(string) $eventCategories
     * @param object $object
     *
     * @return void
     */
    public function unmap( $eventTypeMask, $eventSources, $eventCategories, $object)
    {
        $eventTypes = $this->getMaskArray( $eventTypeMask );

        $result = array( $object );

        if ( count( $eventTypes ) == 0 )
        {
            $eventTypes = array( "*" );
        }
        if ( count( $eventSources ) == 0 )
        {
            $eventSources = array( "*" );
        }
        if ( count( $eventCategories ) == 0)
        {
            $eventCategories = array( "*" );
        }

        $c = $this->createMinimumHash( $eventCategories, $result );
        $s = $this->createMinimumHash( $eventSources, $c );
        $new = $this->createMinimumHash( $eventTypes, $s );

        $this->unmergeHash( $this->structure, $new, $history );
    }


    /**
     * Fills the array $out with the keys $inKeys and the value $inValue.
     *
     * @param array inKeys
     * @param mixed $inValue
     * @param array &$out
     * @return array
     */
    protected function createMinimumHash($inKeys, $inValue )
    {
        foreach ( $inKeys as $key )
        {
            $out[$key] = $inValue;
        }
        return $out;
    }

    /**
     *
     *
     * @return void
     **/
    protected function mergeSingle( &$in, &$history )
    {
        $extra = $this->get( $history[0], $history[1], $history[2] );
        $extra = array_merge( $in, $extra );

        $tmp =& $this->structure[$history[0]];
        $tmp =& $tmp[$history[1]];
        $tmp =& $tmp[$history[2]];

        foreach ( $extra as $inKey => $inVal )
        {
            if ( !isset( $tmp ) || !in_array( $inVal, $tmp ) )
            {
                if ( is_numeric( $inKey ) )
                {
                    $tmp[] =  $inVal;
                }
                else
                {
                    $tmp[$inKey] =& $inVal;
                }
            }
         }
     }


     // XXX should change out.. the way it is now.
    protected function mergeHash( &$in, $out, &$history )
    {
        $depth = count( $history );

        if ( $depth == 3 )
        {
            $this->mergeSingle( $in, $history );
        }

        $i = 0;
        foreach ( $in as $inKey => $inVal )
        {
            $history[$depth] = $inKey;

            if ( strcmp( $inKey, "*" ) == 0)
            {
                if ( is_array( $out ) )
                {
                    foreach ( $out as $outKey => $outVal )
                    {
                        $history[$depth] = $outKey;
                        $this->mergeHash( $in[$inKey], $out[$outKey], $history );
                    }
                }
            }

            $history[$depth] = $inKey;
            if ( is_array( $inVal ) )
            {
                if ( is_numeric( $inKey ) )
                {
                    // FIXME
                    $this->mergeHash( $in[$inKey], $out, $history );
                }
                else
                {
                    //$this->mergeHash( $in[$inKey], $out[$inKey], $history );
                    $this->mergeHash( $in[$inKey], $out, $history );
                }
            }

        }
        array_pop( $history );
    }


    // XXX: ugly, should get a rewrite.
    protected function unmergeHash( &$out, &$in, &$history )
    {
        $depth = count( $history );

        if ( $depth == 3 )
        {
            foreach ( $in as $inKey => $inVal )
            {
                if ( !isset( $out ) || in_array( $inVal, $out ) )
                {
                    if ( is_array( $out ) )
                    {
                        foreach ( $out as $outKey => $outVal )
                        {
                            if ( $outVal === $inVal )
                            {
                                unset( $out[$outKey] );
                                $result = true;
                            }
                        }
                    }
                }
            }
        }

        foreach ( $in as $inKey => $inVal )
        {
            $history[$depth] = $inKey;

            if ( strcmp( $inKey, "*" ) == 0)
            {
                if ( is_array( $out ) )
                {
                    foreach ( $out as $outKey => $outVal )
                    {
                        $this->unmergeHash( $out[$outKey], $in[$inKey], $history );
                    }
                }
            }

            if ( is_array( $inVal ) )
            {
                $this->unmergeHash( $out[$inKey], $in[$inKey], $history );
            }
        }
        array_pop( $history );
    }


    /**
     * Returns the bits set in $mask as separate values in an array.
     *
     * @return array(int)
     */
    protected function getMaskArray( $mask )
    {
        $result = array();

        $input = 1;
        while ( $input <= $mask )
        {
            if ( $mask & $input )
            {
                $result[] = $input;
            }
            $input <<= 1;
        }

        return $result;
    }

    /**
     * Returns an array with objects are mapped to the key generated by $eventType, $eventSource and $eventCategory.
     *
     * The eventType is an integer from in the range: 2^x with x -> N+.
     * (1, 2, 4, 8, 16, ... )
     *
     * @param int $eventType
     * @param string  $eventSource
     * @param string  $eventCategory
     * @return array(object).
     */
    public function get( $eventType, $eventSource, $eventCategory)
    {
        $tmp = $this->structure;


        if ( isset( $tmp[$eventType] ) )
        {
            $tmp = $tmp[$eventType];
        }
        else if ( isset( $tmp [ "*" ] ) )
        {
            $tmp = $tmp["*"];
        }
        else
        {
            return array();
        }

        if ( isset( $tmp[$eventSource] ) )
        {
            $tmp = $tmp[$eventSource];
        }
        else if ( isset( $tmp [ "*" ] ) )
        {
            $tmp = $tmp["*"];
        }
        else
        {
            return array();
        }

        if ( isset( $tmp[$eventCategory] ) )
        {
            $tmp = $tmp[$eventCategory];
        }
        else if ( isset( $tmp [ "*" ] ) )
        {
            $tmp = $tmp["*"];
        }
        else
        {
            return array();
        }

        return $tmp;
    }


    public function printStructure()
    {
        $this->printStructureRecursive( $this->structure, $history = array() );
    }

    protected function printStructureRecursive( $structure, &$history )
    {
        $depth = count( $history );
        if ( $depth == 3 )
        {
            $tmp =& $this->structure[$history[0]];
            $tmp =& $tmp[$history[1]];
            $tmp =& $tmp[$history[2]];

            if ( isset( $tmp ) )
            {
                foreach ( $tmp as $key => $val )
                {
                    echo $history[0] . "  " . $history[1] . "  " . $history[2] . " [ $key => $val ]\n";
                }
            }

            return;
        }

        foreach ( $structure as $key => $val )
        {
            $history[$depth] = $key;
            $this->printStructureRecursive( $structure, $history );
        }
    }
}
?>