## Apache Zeta Components Manual :: File Source for base_meta_data.php

## Source for file base_meta_data.php

Documentation is available at base_meta_data.php

1. <?php
2. /**
3. * File containing the abstract ezcCacheStackBaseMetaData class.
4. *
5. * Licensed to the Apache Software Foundation (ASF) under one
6. * or more contributor license agreements. See the NOTICE file
7. * distributed with this work for additional information
8. * regarding copyright ownership. The ASF licenses this file
9. * to you under the Apache License, Version 2.0 (the
10. * "License"); you may not use this file except in compliance
11. * with the License. You may obtain a copy of the License at
12. *
13. * http://www.apache.org/licenses/LICENSE-2.0
14. *
15. * Unless required by applicable law or agreed to in writing,
16. * software distributed under the License is distributed on an
17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18. * KIND, either express or implied. See the License for the
19. * specific language governing permissions and limitations
20. * under the License.
21. *
22. * @package Cache
23. * @version //autogentag//
24. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
25. * @filesource
26. */
28. /**
29. * Common base class for meta data.
30. *
31. * Common base class for
32. * <ul>
33. * <li>{@link ezcCacheStackLruMetaData}</li>
34. * <li>{@link ezcCacheStackLfuMetaData}</li>
35. * </ul>
36. *
37. * Private to not expose internal APIs, yet. Might become public in future
38. * releases.
39. *
40. * @package Cache
41. * @version //autogen//
42. *
43. * @access private
44. */
45. abstract class ezcCacheStackBaseMetaData implements ezcCacheStackMetaData
46. {
47. /**
48. * Replacement data.
49. *
50. * <code>
51. * array(
52. * '<item_id>' => <replacement_value>,
53. * '<item_id>' => <replacement_value>,
54. * '<item_id>' => <replacement_value>,
55. * // ...
56. * )
57. * </code>
58. *
59. * @var array(string=>int)
60. */
61. protected $replacementData = array();
63. /**
64. * Storage-item assignement.
65. *
66. * <code>
67. * array(
68. * '<storage_id>' => array(
69. * '<item_id>' => true,
70. * '<item_id>' => true,
71. * '<item_id>' => true,
72. * // ...
73. * ),
74. * '<storage_id>' => array(
75. * '<item_id>' => true,
76. * '<item_id>' => true,
77. * '<item_id>' => true,
78. * // ...
79. * ),
80. * // ...
81. * )
82. * </code>
83. *
84. * @var array(string=>array(string=>bool))
85. */
86. protected $storageData = array();
88. /**
89. * No-parameter constructor.
90. *
91. */
92. public function __construct()
93. {
94. }
96. /**
97. * Adds the given $itemId to the storage identified by $storageId.
98. *
99. * This method adds the given $itemId to the storage identified by
100. * $storageId. It must also add the $itemId to the replacement data or
101. * update its status there, if the item is already in there.
102. *
103. * The method calls the abstract {@link self::addItemToReplacementData()}
104. * method to make the item be reflected in the replacement data.
105. *
106. * @param string $storageId
107. * @param string $itemId
108. */
109. public function addItem( $storageId, $itemId )
110. {
111. $this->storageData[$storageId][$itemId] = true;
112. // Abstract call
113. $this->addItemToReplacementData( $itemId );
114. }
116. /**
117. * Adds the given $itemId to the replacement data.
118. *
119. * This method is called by {@link self::addItem()} in order to add the
120. * given $itemId to the replacement data in the correct way. When this
121. * method is called, the item has already been added to the storage data.
122. * The removal from the replacement data happens automatically in {@link }
123. * self::removeItem()} by unsetting the key $itemId in
124. * {self::$replacementData}.
125. *
126. * If the entry for $itemId already exists, it must be updated to reflect a
127. * touching of the data (store/restore).
128. *
129. * @param string $itemId
130. */
131. abstract public function addItemToReplacementData( $itemId );
133. /**
134. * Removes the given $itemId from the storage identified by $storageId.
135. *
136. * Removes the given $itemId from the storage identified by $storageId in
137. * {@link self::$storageData} and also removes the key from {@link }
138. * self::$metaData}.
139. *
140. * @param string $storageId
141. * @param string $itemId
142. */
143. public function removeItem( $storageId, $itemId )
144. {
145. // Remove from storage
146. if ( isset( $this->storageData[$storageId] ) )
147. {
148. // No error if not set
149. unset( $this->storageData[$storageId][$itemId] );
150. // Remove empty storage
151. if ( count ( $this->storageData[$storageId] ) === 0 )
152. {
153. unset( $this->storageData[$storageId] );
154. }
155. }
157. // Check for complete removal
158. foreach ( $this->storageData as $storage => $items )
159. {
160. if ( isset( $items[$itemId] ) )
161. {
162. // Item is available in other storage
163. return;
164. }
165. }
166. // No storage has the item, savely unset it
167. unset( $this->replacementData[$itemId] );
168. }
170. /**
171. * Returns if the given $itemId is available in the storage with $storageId.
172. *
173. * Returns if the $itemId is availalke in the storage that is identified by
174. * $storageId.
175. *
176. * @param string $storageId
177. * @param string $itemId
178. * @return bool
179. */
180. public function hasItem( $storageId, $itemId )
181. {
182. return isset( $this->storageData[$storageId][$itemId] );
183. }
185. /**
186. * Returns if the given $storageId has reached the given $limit of items.
187. *
188. * Returns if the storage identified by $storageId has $limit or more items
189. * stored.
190. *
191. * @param string $storageId
192. * @param int $limit
193. * @return bool
194. */
195. public function reachedItemLimit( $storageId, $limit )
196. {
197. return (
198. isset( $this->storageData[$storageId] )
199. && count ( $this->storageData[$storageId] ) >= $limit
200. );
201. }
203. /**
204. * Returns an array of item IDs ordered in replacement order.
205. *
206. * This method sorts {@link asort()} the {@link self::$replacementData} and
207. * returns it. The data is ordered by the replacement value in ascending
208. * order.
209. *
210. * @return array(string=>int)
211. */
212. public function getReplacementItems()
213. {
214. asort ( $this->replacementData );
215. return $this->replacementData;
216. }
218. /**
219. * Returns the data data as an array.
220. *
221. * @return array
222. */
223. public function getState()
224. {
225. return array(
226. 'replacementData' => $this->replacementData,
227. 'storageData' => $this->storageData,
228. );
229. }
231. /**
232. * Sets the intrnal data from the given array $data.
233. *
234. * The $data array was previously generated by {@link self::getState()}.
235. *
236. * @param array $data
237. */
238. public function setState( array $data )
239. {
240. $this->replacementData = $data['replacementData'];
241. $this->storageData = $data['storageData'];
242. }
243. }
245. ?>

Documentation generated by phpDocumentor 1.4.3