Debian Codegraph @

closure-library/closure/goog/storage/collectablestorage.js

raw
1 // Copyright 2011 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Provides a convenient API for data persistence with data
17 * expiration and user-initiated expired key collection.
18 *
19 */
20
21 goog.provide('goog.storage.CollectableStorage');
22
23 goog.require('goog.array');
24 goog.require('goog.iter');
25 goog.require('goog.storage.ErrorCode');
26 goog.require('goog.storage.ExpiringStorage');
27 goog.require('goog.storage.RichStorage');
28
29
30
31 /**
32 * Provides a storage with expiring keys and a collection method.
33 *
34 * @param {!goog.storage.mechanism.IterableMechanism} mechanism The underlying
35 * storage mechanism.
36 * @constructor
37 * @struct
38 * @extends {goog.storage.ExpiringStorage}
39 */
40 goog.storage.CollectableStorage = function(mechanism) {
41 goog.storage.CollectableStorage.base(this, 'constructor', mechanism);
42 };
43 goog.inherits(goog.storage.CollectableStorage, goog.storage.ExpiringStorage);
44
45
46 /**
47 * Iterate over keys and returns those that expired.
48 *
49 * @param {goog.iter.Iterable} keys keys to iterate over.
50 * @param {boolean=} opt_strict Also return invalid keys.
51 * @return {!Array<string>} Keys of values that expired.
52 * @private
53 */
54 goog.storage.CollectableStorage.prototype.getExpiredKeys_ = function(
55 keys, opt_strict) {
56 var keysToRemove = [];
57 goog.iter.forEach(keys, function(key) {
58 // Get the wrapper.
59 var wrapper;
60
61 try {
62 wrapper = goog.storage.CollectableStorage.prototype.getWrapper.call(
63 this, key, true);
64 } catch (ex) {
65 if (ex == goog.storage.ErrorCode.INVALID_VALUE) {
66 // Bad wrappers are removed in strict mode.
67 if (opt_strict) {
68 keysToRemove.push(key);
69 }
70 // Skip over bad wrappers and continue.
71 return;
72 }
73 // Unknown error, escalate.
74 throw ex;
75 }
76 if (!goog.isDef(wrapper)) {
77 // A value for a given key is no longer available. Clean it up.
78 keysToRemove.push(key);
79 return;
80 }
81 // Remove expired objects.
82 if (goog.storage.ExpiringStorage.isExpired(wrapper)) {
83 keysToRemove.push(key);
84 // Continue with the next key.
85 return;
86 }
87 // Objects which can't be decoded are removed in strict mode.
88 if (opt_strict) {
89
90 try {
91 goog.storage.RichStorage.Wrapper.unwrap(wrapper);
92 } catch (ex) {
93 if (ex == goog.storage.ErrorCode.INVALID_VALUE) {
94 keysToRemove.push(key);
95 // Skip over bad wrappers and continue.
96 return;
97 }
98 // Unknown error, escalate.
99 throw ex;
100 }
101 }
102 }, this);
103 return keysToRemove;
104 };
105
106
107 /**
108 * Cleans up the storage by removing expired keys.
109 *
110 * @param {goog.iter.Iterable} keys List of all keys.
111 * @param {boolean=} opt_strict Also remove invalid keys.
112 * @return {!Array<string>} a list of expired keys.
113 * @protected
114 */
115 goog.storage.CollectableStorage.prototype.collectInternal = function(
116 keys, opt_strict) {
117 var keysToRemove = this.getExpiredKeys_(keys, opt_strict);
118 goog.array.forEach(keysToRemove, function(key) {
119 goog.storage.CollectableStorage.prototype.remove.call(this, key);
120 }, this);
121 return keysToRemove;
122 };
123
124
125 /**
126 * Cleans up the storage by removing expired keys.
127 *
128 * @param {boolean=} opt_strict Also remove invalid keys.
129 */
130 goog.storage.CollectableStorage.prototype.collect = function(opt_strict) {
131 this.collectInternal(
132 /** @type {goog.storage.mechanism.IterableMechanism} */ (this.mechanism)
133 .__iterator__(true),
134 opt_strict);
135 };