2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * @file xmltooling/util/StorageService.h
24 * Generic data storage interface.
27 #if !defined(__xmltooling_storage_h__) && !defined(XMLTOOLING_LITE)
28 #define __xmltooling_storage_h__
30 #include <xmltooling/base.h>
34 namespace xmltooling {
37 * Generic data storage facility for use by services that require
38 * some degree of persistence. Implementations will vary in how much
39 * persistence they can supply.
41 * <p>Storage is divided into "contexts" identified by a string label.
42 * Keys need to be unique only within a given context, so multiple
43 * components can share a single storage service safely as long as they
44 * use different labels.
46 class XMLTOOL_API StorageService
48 MAKE_NONCOPYABLE(StorageService);
50 virtual ~StorageService();
53 * Creates a new "short" record in the storage service.
55 * @param context a storage context label
56 * @param key null-terminated unique key of up to 255 bytes
57 * @param value null-terminated value of up to 255 bytes to store
58 * @param expiration an expiration timestamp, after which the record can be purged
59 * @return true iff record was inserted, false iff a duplicate was found
61 * @throws IOException raised if fatal errors occur in the insertion process
63 virtual bool createString(const char* context, const char* key, const char* value, time_t expiration)=0;
66 * Returns an existing "short" record from the storage service.
68 * <p>The version parameter can be set for "If-Modified-Since" semantics.
70 * @param context a storage context label
71 * @param key null-terminated unique key of up to 255 bytes
72 * @param pvalue location in which to return the record value
73 * @param pexpiration location in which to return the expiration timestamp
74 * @param version if > 0, only copy back data if newer than supplied version
75 * @return the version of the record read back, or 0 if no record exists
77 * @throws IOException raised if errors occur in the read process
79 virtual int readString(
80 const char* context, const char* key, std::string* pvalue=nullptr, time_t* pexpiration=nullptr, int version=0
84 * Updates an existing "short" record in the storage service.
86 * @param context a storage context label
87 * @param key null-terminated unique key of up to 255 bytes
88 * @param value null-terminated value of up to 255 bytes to store, or nullptr to leave alone
89 * @param expiration a new expiration timestamp, or 0 to leave alone
90 * @param version if > 0, only update if the current version matches this value
91 * @return the version of the record after update, 0 if no record exists, or -1 if the version
92 * parameter is non-zero and does not match the current version before update (so the caller is out of sync)
94 * @throws IOException raised if errors occur in the update process
96 virtual int updateString(
97 const char* context, const char* key, const char* value=nullptr, time_t expiration=0, int version=0
101 * Deletes an existing "short" record from the storage service.
103 * @param context a storage context label
104 * @param key null-terminated unique key of up to 255 bytes
105 * @return true iff the record existed and was deleted
107 * @throws IOException raised if errors occur in the deletion process
109 virtual bool deleteString(const char* context, const char* key)=0;
112 * Creates a new "long" record in the storage service.
114 * @param context a storage context label
115 * @param key null-terminated unique key of up to 255 bytes
116 * @param value null-terminated value of arbitrary length
117 * @param expiration an expiration timestamp, after which the record can be purged
118 * @return true iff record was inserted, false iff a duplicate was found
120 * @throws IOException raised if errors occur in the insertion process
122 virtual bool createText(const char* context, const char* key, const char* value, time_t expiration)=0;
125 * Returns an existing "long" record from the storage service.
127 * <p>The version parameter can be set for "If-Modified-Since" semantics.
129 * @param context a storage context label
130 * @param key null-terminated unique key of up to 255 bytes
131 * @param pvalue location in which to return the record value
132 * @param pexpiration location in which to return the expiration timestamp
133 * @param version if > 0, only copy back data if newer than supplied version
134 * @return the version of the record read back, or 0 if no record exists
136 * @throws IOException raised if errors occur in the read process
138 virtual int readText(
139 const char* context, const char* key, std::string* pvalue=nullptr, time_t* pexpiration=nullptr, int version=0
143 * Updates an existing "long" record in the storage service.
145 * @param context a storage context label
146 * @param key null-terminated unique key of up to 255 bytes
147 * @param value null-terminated value of arbitrary length to store, or nullptr to leave alone
148 * @param expiration a new expiration timestamp, or 0 to leave alone
149 * @param version if > 0, only update if the current version matches this value
150 * @return the version of the record after update, 0 if no record exists, or -1 if the version
151 * parameter is non-zero and does not match the current version before update (so the caller is out of sync)
153 * @throws IOException raised if errors occur in the update process
155 virtual int updateText(
156 const char* context, const char* key, const char* value=nullptr, time_t expiration=0, int version=0
160 * Deletes an existing "long" record from the storage service.
162 * @param context a storage context label
163 * @param key null-terminated unique key of up to 255 bytes
164 * @return true iff the record existed and was deleted
166 * @throws IOException raised if errors occur in the deletion process
168 virtual bool deleteText(const char* context, const char* key)=0;
171 * Manually trigger a cleanup of expired records.
172 * The method <strong>MAY</strong> return without guaranteeing that
173 * cleanup has already occurred.
175 * @param context a storage context label
177 virtual void reap(const char* context)=0;
180 * Updates the expiration time of all records in the context.
182 * @param context a storage context label
183 * @param expiration a new expiration timestamp
185 virtual void updateContext(const char* context, time_t expiration)=0;
188 * Forcibly removes all records in a given context along with any
189 * associated resources devoted to maintaining the context.
191 * @param context a storage context label
193 virtual void deleteContext(const char* context)=0;
200 * Registers StorageService classes into the runtime.
202 void XMLTOOL_API registerStorageServices();
204 /** StorageService based on in-memory caching. */
205 #define MEMORY_STORAGE_SERVICE "Memory"
208 #endif /* __xmltooling_storage_h__ */