Sirikata
liboh/include/sirikata/oh/Storage.hpp
Go to the documentation of this file.
00001 /*  Sirikata
00002  *  Storage.hpp
00003  *
00004  *  Copyright (c) 2010, Stanford University
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions are
00009  *  met:
00010  *  * Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  *  * Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in
00014  *    the documentation and/or other materials provided with the
00015  *    distribution.
00016  *  * Neither the name of Sirikata nor the names of its contributors may
00017  *    be used to endorse or promote products derived from this software
00018  *    without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00021  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00022  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00023  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00024  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00026  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 
00033 #ifndef __SIRIKATA_OH_STORAGE_HPP__
00034 #define __SIRIKATA_OH_STORAGE_HPP__
00035 
00036 #include <sirikata/core/util/Platform.hpp>
00037 #include <sirikata/oh/Platform.hpp>
00038 #include <sirikata/oh/HostedObject.hpp>
00039 #include <sirikata/core/util/Factory.hpp>
00040 
00041 namespace Sirikata {
00042 namespace OH {
00043 
00073 class SIRIKATA_OH_EXPORT Storage : public Service {
00074 public:
00075     // A storage implementation contains a set of 'buckets' which
00076     // provide isolated storage for each object.
00077     typedef UUID Bucket;
00078 
00079     // Keys are arbitrary-length strings -- the namespace is flat, but
00080     // it is easy to add human readable hierarchy using a separator
00081     // (e.g. '.') and a bit of escaping.
00082     typedef String Key;
00083 
00087     enum Result {
00088         // Successful. Any data read back should be available in
00089         // corresponding callback arguments.
00090         SUCCESS = 0,
00091 
00092         // Failed to lock database or gain a lease. This rarely
00093         // indicates permanent failure. Rather, it indicates that
00094         // access to the database was blocked, probably temporarily as
00095         // an existing lease is blocking it (possibly to be freed up
00096         // when it automatically expires). Retrying is a reasonable
00097         // course of action here.
00098         LOCK_ERROR = 1,
00099 
00100         // There was an error applying the transaction. This could be due to a
00101         // missing key (for reads), a compare error, or and error writing or
00102         // erasing a key. The particular error is not specified because
00103         // operations can be reordered by the implementation for efficiency.
00104         TRANSACTION_ERROR = 2
00105     };
00106 
00107     typedef std::tr1::unordered_map<Key, String> ReadSet;
00114     typedef std::tr1::function<void(Result result, ReadSet* rs)> CommitCallback;
00115     typedef std::tr1::function<void(Result result, int32 count)> CountCallback;
00116 
00117     virtual ~Storage() {};
00118 
00120     virtual void start() {}
00121     virtual void stop() {}
00122 
00123 
00130     virtual void leaseBucket(const Bucket& bucket) = 0;
00134     virtual void releaseBucket(const Bucket& bucket) = 0;
00135 
00136 
00138     virtual void beginTransaction(const Bucket& bucket) = 0;
00139 
00144     virtual void commitTransaction(const Bucket& bucket, const CommitCallback& cb = 0, const String& timestamp="current") = 0;
00145 
00162     virtual bool erase(const Bucket& bucket, const Key& key, const CommitCallback& cb = 0, const String& timestamp="current") = 0;
00163 
00180     virtual bool write(const Bucket& bucket, const Key& key, const String& value, const CommitCallback& cb = 0, const String& timestamp="current") = 0;
00181 
00192     virtual bool read(const Bucket& bucket, const Key& key, const CommitCallback& cb = 0, const String& timestamp="current") = 0;
00193 
00205     virtual bool rangeRead(const Bucket& bucket, const Key& start, const Key& finish, const CommitCallback& cb = 0, const String& timestamp="current") = 0;
00206 
00207 
00222     virtual bool compare(const Bucket& bucket, const Key& key, const String& value, const CommitCallback& cb = 0, const String& timestamp="current") = 0;
00223 
00235     virtual bool rangeErase(const Bucket& bucket, const Key& start, const Key& finish, const CommitCallback& cb = 0, const String& timestamp="current") = 0;
00236 
00237     // TODO(ewencp) this interface seems broken -- because it uses a different
00238     // callback, it doesn't fit in with transactions. This currently *has* to be
00239     // handled separately from other actions...
00249     virtual bool count(const Bucket& bucket, const Key& start, const Key& finish, const CountCallback& cb = 0, const String& timestamp="current") = 0;
00250 };
00251 
00252 
00253 class SIRIKATA_OH_EXPORT StorageFactory
00254     : public AutoSingleton<StorageFactory>,
00255       public Factory2<Storage*, ObjectHostContext*, const String&>
00256 {
00257 public:
00258     static StorageFactory& getSingleton();
00259     static void destroy();
00260 };
00261 
00262 } //end namespace OH
00263 } //end namespace Sirikata
00264 
00265 #endif //__SIRIKATA_OH_STORAGE_HPP__