Simplify access to records and prevent reads of expired data.
[shibboleth/cpp-xmltooling.git] / xmltooling / util / StorageService.h
1 /*\r
2  *  Copyright 2001-2006 Internet2\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 /**\r
18  * @file xmltooling/util/StorageService.h\r
19  * \r
20  * Generic data storage interface\r
21  */\r
22 \r
23 #ifndef __xmltooling_storage_h__\r
24 #define __xmltooling_storage_h__\r
25 \r
26 #include <xmltooling/XMLObject.h>\r
27 \r
28 #include <ctime>\r
29 \r
30 namespace xmltooling {\r
31 \r
32     /**\r
33      * Generic data storage facility for use by services that require\r
34      * some degree of persistence. Implementations will vary in how much\r
35      * persistence they can supply.\r
36      * \r
37      * <p>Storage is divided into "contexts" identified by a string label.\r
38      * Keys need to be unique only within a given context, so multiple\r
39      * components can share a single storage service safely as long as they\r
40      * use different labels.\r
41      */\r
42     class XMLTOOL_API StorageService\r
43     {\r
44         MAKE_NONCOPYABLE(StorageService);\r
45     public:\r
46         virtual ~StorageService() {}\r
47         \r
48         /**\r
49          * Creates a new "short" record in the storage service.\r
50          * \r
51          * @param context       a storage context label\r
52          * @param key           null-terminated unique key of up to 255 bytes\r
53          * @param value         null-terminated value of up to 255 bytes to store\r
54          * @param expiration    an expiration timestamp, after which the record can be purged\r
55          * \r
56          * @throws IOException  raised if errors occur in the insertion process \r
57          */\r
58         virtual void createString(const char* context, const char* key, const char* value, time_t expiration)=0;\r
59         \r
60         /**\r
61          * Returns an existing "short" record from the storage service.\r
62          * \r
63          * @param context       a storage context label\r
64          * @param key           null-terminated unique key of up to 255 bytes\r
65          * @param pvalue        location in which to return the record value\r
66          * @param pexpiration   location in which to return the expiration timestamp\r
67          * @return  true iff a valid record exists and was returned   \r
68          * \r
69          * @throws IOException  raised if errors occur in the read process \r
70          */\r
71         virtual bool readString(const char* context, const char* key, std::string* pvalue=NULL, time_t* pexpiration=NULL)=0;\r
72 \r
73         /**\r
74          * Updates an existing "short" record in the storage service.\r
75          * \r
76          * @param context       a storage context label\r
77          * @param key           null-terminated unique key of up to 255 bytes\r
78          * @param value         null-terminated value of up to 255 bytes to store, or NULL to leave alone\r
79          * @param expiration    a new expiration timestamp, or 0 to leave alone\r
80          * @return true iff the record exists and was updated\r
81          *    \r
82          * @throws IOException  raised if errors occur in the update process \r
83          */\r
84         virtual bool updateString(const char* context, const char* key, const char* value=NULL, time_t expiration=0)=0;\r
85         \r
86         /**\r
87          * Deletes an existing "short" record from the storage service.\r
88          * \r
89          * @param context       a storage context label\r
90          * @param key           null-terminated unique key of up to 255 bytes\r
91          * @return true iff the record existed and was deleted\r
92          *    \r
93          * @throws IOException  raised if errors occur in the deletion process \r
94          */\r
95         virtual bool deleteString(const char* context, const char* key)=0;\r
96         \r
97         /**\r
98          * Creates a new "long" record in the storage service.\r
99          * \r
100          * @param context       a storage context label\r
101          * @param key           null-terminated unique key of up to 255 bytes\r
102          * @param value         null-terminated value of arbitrary length\r
103          * @param expiration    an expiration timestamp, after which the record can be purged\r
104          * \r
105          * @throws IOException  raised if errors occur in the insertion process \r
106          */\r
107         virtual void createText(const char* context, const char* key, const char* value, time_t expiration)=0;\r
108         \r
109         /**\r
110          * Returns an existing "long" record from the storage service.\r
111          * \r
112          * @param context       a storage context label\r
113          * @param key           null-terminated unique key of up to 255 bytes\r
114          * @param pvalue        location in which to return the record value\r
115          * @param pexpiration   location in which to return the expiration timestamp\r
116          * @return  true iff a valid record exists and was returned   \r
117          * \r
118          * @throws IOException  raised if errors occur in the read process \r
119          */\r
120         virtual bool readText(const char* context, const char* key, std::string* pvalue=NULL, time_t* pexpiration=NULL)=0;\r
121 \r
122         /**\r
123          * Updates an existing "long" record in the storage service.\r
124          * \r
125          * @param context       a storage context label\r
126          * @param key           null-terminated unique key of up to 255 bytes\r
127          * @param value         null-terminated value of arbitrary length to store, or NULL to leave alone\r
128          * @param expiration    a new expiration timestamp, or 0 to leave alone\r
129          * @return true iff the record exists and was updated\r
130          *    \r
131          * @throws IOException  raised if errors occur in the update process \r
132          */\r
133         virtual bool updateText(const char* context, const char* key, const char* value=NULL, time_t expiration=0)=0;\r
134         \r
135         /**\r
136          * Deletes an existing "long" record from the storage service.\r
137          * \r
138          * @param context       a storage context label\r
139          * @param key           null-terminated unique key of up to 255 bytes\r
140          * @return true iff the record existed and was deleted\r
141          *    \r
142          * @throws IOException  raised if errors occur in the deletion process \r
143          */\r
144         virtual bool deleteText(const char* context, const char* key)=0;\r
145         \r
146         /**\r
147          * Manually trigger a cleanup of expired records.\r
148          * The method <strong>MAY</strong> return without guaranteeing that\r
149          * cleanup has already occurred.\r
150          * \r
151          * @param context       a storage context label\r
152          */\r
153         virtual void reap(const char* context)=0;\r
154         \r
155         /**\r
156          * Forcibly removes all records in a given context along with any\r
157          * associated resources devoted to maintaining the context.\r
158          * \r
159          * @param context       a storage context label\r
160          */\r
161         virtual void deleteContext(const char* context)=0;\r
162 \r
163     protected:\r
164         StorageService() {}\r
165     };\r
166 \r
167     /**\r
168      * Registers StorageService classes into the runtime.\r
169      */\r
170     void XMLTOOL_API registerStorageServices();\r
171 \r
172     /** StorageService based on in-memory caching. */\r
173     #define MEMORY_STORAGE_SERVICE  "org.opensaml.xmlooling.MemoryStorageService"\r
174 };\r
175 \r
176 #endif /* __xmltooling_storage_h__ */\r