00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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 #include "base/Log.h"
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
00060
00061
00062
00063
00064
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
00076
00077
00078
00079
00080
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
00114
00115
00116
00117
00118
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
00132
00133
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