src/include/posix/client/BlockingSQLiteKeyValueStore.h

00001 /*
00002  * Funambol is a mobile platform developed by Funambol, Inc. 
00003  * Copyright (C) 2008 Funambol, Inc.
00004  * 
00005  * This program is free software; you can redistribute it and/or modify it under
00006  * the terms of the GNU Affero General Public License version 3 as published by
00007  * the Free Software Foundation with the addition of the following permission 
00008  * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
00009  * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE 
00010  * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
00011  * 
00012  * This program is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00015  * details.
00016  * 
00017  * You should have received a copy of the GNU Affero General Public License 
00018  * along with this program; if not, see http://www.gnu.org/licenses or write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00020  * MA 02110-1301 USA.
00021  * 
00022  * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite 
00023  * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
00024  * 
00025  * The interactive user interfaces in modified source and object code versions
00026  * of this program must display Appropriate Legal Notices, as required under
00027  * Section 5 of the GNU Affero General Public License version 3.
00028  * 
00029  * In accordance with Section 7(b) of the GNU Affero General Public License
00030  * version 3, these Appropriate Legal Notices must retain the display of the
00031  * "Powered by Funambol" logo. If the display of the logo is not reasonably 
00032  * feasible for technical reasons, the Appropriate Legal Notices must display
00033  * the words "Powered by Funambol".
00034  */
00035 
00036 #ifndef INCL_BLOCKING_SQLLITE_KEY_VALUE_STORE
00037 #define INCL_BLOCKING_SQLLITE_KEY_VALUE_STORE
00038 
00042 #include <sqlite3.h>
00043 
00044 #include <semaphore.h>
00045 
00046 #include "client/SQLiteKeyValueStore.h"
00047 
00048 BEGIN_NAMESPACE
00049 
00050 class BlockingSQLiteKeyValueStore : public SQLiteKeyValueStore {
00051 
00052     mutable sem_t sema;
00053     mutable sem_t sema_save;
00054 
00055 private:
00056 
00057     
00058     /*
00059      * Execute a query to get a value, given the key.   If a connection to
00060      * the database is not open, open it. 
00061      *
00062      * @param sql   - The sql command to execute.
00063      *
00064      * @return      - The result of the query - an Enumeration of KeyValuePair s
00065      */
00066     virtual Enumeration & query(const StringBuffer & sql) const
00067     {
00068         sem_wait(&sema);
00069         Enumeration& e = SQLiteKeyValueStore::query(sql);
00070         sem_post(&sema);
00071         return e;
00072     }
00073     
00074     /*
00075      * Execute a non-select query.  If a connection to the database is not open,
00076      * open it.
00077      *
00078      * @param sql   - The sql command to execute.
00079      *
00080      * @return      - Success or Failure
00081      */
00082     virtual int execute(const StringBuffer & sql)
00083     {
00084         int ret;
00085         sem_wait(&sema);
00086         ret = SQLiteKeyValueStore::execute(sql);
00087         sem_post(&sema);
00088         return ret;
00089     }
00090     
00091     virtual bool enumeration_hasMoreElement()
00092     {
00093         bool ret;
00094         sem_wait(&sema);
00095         ret = SQLiteKeyValueStore::enumeration_hasMoreElement();
00096         sem_post(&sema);
00097         return ret;
00098     }
00099     
00100     virtual ArrayElement* enumeration_getNextElement()
00101     {
00102         ArrayElement * ret;
00103         sem_wait(&sema);
00104         ret = SQLiteKeyValueStore::enumeration_getNextElement();
00105         sem_post(&sema);
00106         return ret;
00107     }
00108     
00109 public:
00110    
00111     
00112     /*
00113      * Constructor
00114      *
00115      * @param uri       - The location of the server
00116      * @param colKey    - The name of the column for the key
00117      * @param colValue  - The name of the column of the value
00118      * @param path      - The name of the full path to the database to use
00119      *
00120      */
00121     BlockingSQLiteKeyValueStore(const StringBuffer & table, const StringBuffer & colKey, const StringBuffer & colValue,
00122                         const StringBuffer & path)
00123                         : SQLiteKeyValueStore(table,colKey,colValue,path)
00124     {
00125         LOG.debug("BlockingSQLiteKeyValueStore: path = %s", path.c_str());
00126         sem_init(&sema,0,1);
00127         sem_init(&sema_save,0,1);
00128     }
00129     
00130     /*
00131      * Destructor
00132      *
00133      * Subclasses MUST disconnect in their own destructors
00134      */
00135     virtual ~BlockingSQLiteKeyValueStore()
00136     {
00137     }
00138     
00142     virtual Enumeration& getProperties()
00143     {
00144         sem_wait(&sema);
00145         Enumeration & e = SQLiteKeyValueStore::getProperties();
00146         sem_post(&sema);
00147         return e;
00148     }
00149     
00157     virtual int close()
00158     {
00159         int ret;
00160         sem_wait(&sema_save);
00161         ret = SQLiteKeyValueStore::close();
00162         sem_post(&sema_save);
00163         return ret;
00164     }
00165     
00166     virtual int removeAllProperties(){
00167         sem_wait(&sema);
00168         int e = SQLiteKeyValueStore::removeAllProperties();
00169         sem_post(&sema);
00170         return e;
00171     }
00172 
00173 };
00174 
00175 
00176 END_NAMESPACE
00177 
00180 #endif

Generated on Wed Jan 14 17:15:37 2009 for Funambol C++ Client Library by  doxygen 1.5.2