1 |
efrain |
1 |
/*
|
|
|
2 |
* Copyright 2020 Google LLC
|
|
|
3 |
*
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
|
6 |
* You may obtain a copy of the License at
|
|
|
7 |
*
|
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
*
|
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13 |
* See the License for the specific language governing permissions and
|
|
|
14 |
* limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
#import <Foundation/Foundation.h>
|
|
|
18 |
|
|
|
19 |
#import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORLifecycle.h"
|
|
|
20 |
#import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORStorageEventSelector.h"
|
|
|
21 |
#import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORTargets.h"
|
|
|
22 |
|
|
|
23 |
@class GDTCOREvent;
|
|
|
24 |
@class GDTCORClock;
|
|
|
25 |
@class GDTCORUploadBatch;
|
|
|
26 |
|
|
|
27 |
@class FBLPromise<ValueType>;
|
|
|
28 |
|
|
|
29 |
NS_ASSUME_NONNULL_BEGIN
|
|
|
30 |
|
|
|
31 |
/** The data type to represent storage size. */
|
|
|
32 |
typedef uint64_t GDTCORStorageSizeBytes;
|
|
|
33 |
|
|
|
34 |
typedef void (^GDTCORStorageBatchBlock)(NSNumber *_Nullable newBatchID,
|
|
|
35 |
NSSet<GDTCOREvent *> *_Nullable batchEvents);
|
|
|
36 |
|
|
|
37 |
/** Defines the interface a storage subsystem is expected to implement. */
|
|
|
38 |
@protocol GDTCORStorageProtocol <NSObject, GDTCORLifecycleProtocol>
|
|
|
39 |
|
|
|
40 |
@required
|
|
|
41 |
|
|
|
42 |
/** Stores an event and calls onComplete with a non-nil error if anything went wrong.
|
|
|
43 |
*
|
|
|
44 |
* @param event The event to store
|
|
|
45 |
* @param completion The completion block to call after an attempt to store the event has been made.
|
|
|
46 |
*/
|
|
|
47 |
- (void)storeEvent:(GDTCOREvent *)event
|
|
|
48 |
onComplete:(void (^_Nullable)(BOOL wasWritten, NSError *_Nullable error))completion;
|
|
|
49 |
|
|
|
50 |
/** Returns YES if some events have been stored for the given target, NO otherwise.
|
|
|
51 |
*
|
|
|
52 |
* @param onComplete The completion block to invoke when determining if there are events is done.
|
|
|
53 |
*/
|
|
|
54 |
- (void)hasEventsForTarget:(GDTCORTarget)target onComplete:(void (^)(BOOL hasEvents))onComplete;
|
|
|
55 |
|
|
|
56 |
/** Constructs an event batch with the given event selector. Events in this batch will not be
|
|
|
57 |
* returned in any queries or other batches until the batch is removed.
|
|
|
58 |
*
|
|
|
59 |
* @param eventSelector The event selector used to find the events.
|
|
|
60 |
* @param expiration The expiration time of the batch. If removeBatchWithID:deleteEvents:onComplete:
|
|
|
61 |
* is not called within this time frame, the batch will be removed with its events deleted.
|
|
|
62 |
* @param onComplete The completion handler to be called when the events have been fetched.
|
|
|
63 |
*/
|
|
|
64 |
- (void)batchWithEventSelector:(nonnull GDTCORStorageEventSelector *)eventSelector
|
|
|
65 |
batchExpiration:(nonnull NSDate *)expiration
|
|
|
66 |
onComplete:(nonnull GDTCORStorageBatchBlock)onComplete;
|
|
|
67 |
|
|
|
68 |
/** Removes the event batch.
|
|
|
69 |
*
|
|
|
70 |
* @param batchID The batchID to remove.
|
|
|
71 |
* @param deleteEvents If YES, the events in this batch are deleted.
|
|
|
72 |
* @param onComplete The completion handler to call when the batch removal process has completed.
|
|
|
73 |
*/
|
|
|
74 |
- (void)removeBatchWithID:(NSNumber *)batchID
|
|
|
75 |
deleteEvents:(BOOL)deleteEvents
|
|
|
76 |
onComplete:(void (^_Nullable)(void))onComplete;
|
|
|
77 |
|
|
|
78 |
/** Finds the batchIDs for the given target and calls the callback block.
|
|
|
79 |
*
|
|
|
80 |
* @param target The target.
|
|
|
81 |
* @param onComplete The block to invoke with the set of current batchIDs.
|
|
|
82 |
*/
|
|
|
83 |
- (void)batchIDsForTarget:(GDTCORTarget)target
|
|
|
84 |
onComplete:(void (^)(NSSet<NSNumber *> *_Nullable batchIDs))onComplete;
|
|
|
85 |
|
|
|
86 |
/** Checks the storage for expired events and batches, deletes them if they're expired. */
|
|
|
87 |
- (void)checkForExpirations;
|
|
|
88 |
|
|
|
89 |
/** Persists the given data with the given key.
|
|
|
90 |
*
|
|
|
91 |
* @param data The data to store.
|
|
|
92 |
* @param key The unique key to store it to.
|
|
|
93 |
* @param onComplete An block to be run when storage of the data is complete.
|
|
|
94 |
*/
|
|
|
95 |
- (void)storeLibraryData:(NSData *)data
|
|
|
96 |
forKey:(NSString *)key
|
|
|
97 |
onComplete:(nullable void (^)(NSError *_Nullable error))onComplete;
|
|
|
98 |
|
|
|
99 |
/** Retrieves the stored data for the given key and optionally sets a new value.
|
|
|
100 |
*
|
|
|
101 |
* @param key The key corresponding to the desired data.
|
|
|
102 |
* @param onFetchComplete The callback to invoke with the data once it's retrieved.
|
|
|
103 |
* @param setValueBlock This optional block can provide a new value to set.
|
|
|
104 |
*/
|
|
|
105 |
- (void)libraryDataForKey:(nonnull NSString *)key
|
|
|
106 |
onFetchComplete:(nonnull void (^)(NSData *_Nullable data,
|
|
|
107 |
NSError *_Nullable error))onFetchComplete
|
|
|
108 |
setNewValue:(NSData *_Nullable (^_Nullable)(void))setValueBlock;
|
|
|
109 |
|
|
|
110 |
/** Removes data from storage and calls the callback when complete.
|
|
|
111 |
*
|
|
|
112 |
* @param key The key of the data to remove.
|
|
|
113 |
* @param onComplete The callback that will be invoked when removing the data is complete.
|
|
|
114 |
*/
|
|
|
115 |
- (void)removeLibraryDataForKey:(NSString *)key
|
|
|
116 |
onComplete:(void (^)(NSError *_Nullable error))onComplete;
|
|
|
117 |
|
|
|
118 |
/** Calculates and returns the total disk size that this storage consumes.
|
|
|
119 |
*
|
|
|
120 |
* @param onComplete The callback that will be invoked once storage size calculation is complete.
|
|
|
121 |
*/
|
|
|
122 |
- (void)storageSizeWithCallback:(void (^)(GDTCORStorageSizeBytes storageSize))onComplete;
|
|
|
123 |
|
|
|
124 |
@end
|
|
|
125 |
|
|
|
126 |
// TODO: Consider complete replacing block based API by promise API.
|
|
|
127 |
|
|
|
128 |
/** Promise based version of API defined in GDTCORStorageProtocol. See API docs for corresponding
|
|
|
129 |
* methods in GDTCORStorageProtocol. */
|
|
|
130 |
@protocol GDTCORStoragePromiseProtocol <GDTCORStorageProtocol>
|
|
|
131 |
|
|
|
132 |
- (FBLPromise<NSSet<NSNumber *> *> *)batchIDsForTarget:(GDTCORTarget)target;
|
|
|
133 |
|
|
|
134 |
- (FBLPromise<NSNull *> *)removeBatchWithID:(NSNumber *)batchID deleteEvents:(BOOL)deleteEvents;
|
|
|
135 |
|
|
|
136 |
- (FBLPromise<NSNull *> *)removeBatchesWithIDs:(NSSet<NSNumber *> *)batchIDs
|
|
|
137 |
deleteEvents:(BOOL)deleteEvents;
|
|
|
138 |
|
|
|
139 |
- (FBLPromise<NSNull *> *)removeAllBatchesForTarget:(GDTCORTarget)target
|
|
|
140 |
deleteEvents:(BOOL)deleteEvents;
|
|
|
141 |
|
|
|
142 |
/** See `hasEventsForTarget:onComplete:`.
|
|
|
143 |
* @return A promise object that is resolved with @YES if there are events for the specified target
|
|
|
144 |
* and @NO otherwise.
|
|
|
145 |
*/
|
|
|
146 |
- (FBLPromise<NSNumber *> *)hasEventsForTarget:(GDTCORTarget)target;
|
|
|
147 |
|
|
|
148 |
/** See `batchWithEventSelector:batchExpiration:onComplete:`
|
|
|
149 |
* The promise is rejected when there are no events for the specified selector.
|
|
|
150 |
*/
|
|
|
151 |
- (FBLPromise<GDTCORUploadBatch *> *)batchWithEventSelector:
|
|
|
152 |
(GDTCORStorageEventSelector *)eventSelector
|
|
|
153 |
batchExpiration:(NSDate *)expiration;
|
|
|
154 |
|
|
|
155 |
@end
|
|
|
156 |
|
|
|
157 |
/** Retrieves the storage instance for the given target.
|
|
|
158 |
*
|
|
|
159 |
* @param target The target.
|
|
|
160 |
* * @return The storage instance registered for the target, or nil if there is none.
|
|
|
161 |
*/
|
|
|
162 |
FOUNDATION_EXPORT
|
|
|
163 |
id<GDTCORStorageProtocol> _Nullable GDTCORStorageInstanceForTarget(GDTCORTarget target);
|
|
|
164 |
|
|
|
165 |
// TODO: Ideally we should remove completion-based API and use promise-based one. Need to double
|
|
|
166 |
// check if it's ok.
|
|
|
167 |
FOUNDATION_EXPORT
|
|
|
168 |
id<GDTCORStoragePromiseProtocol> _Nullable GDTCORStoragePromiseInstanceForTarget(
|
|
|
169 |
GDTCORTarget target);
|
|
|
170 |
|
|
|
171 |
NS_ASSUME_NONNULL_END
|