00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package com.sleepycat.db.rpcserver;
00011
00012 import com.sleepycat.db.*;
00013 import com.sleepycat.db.internal.DbConstants;
00014 import java.io.*;
00015 import java.util.*;
00016
00020 public class RpcDbEnv extends Timer {
00021 EnvironmentConfig config;
00022 Environment dbenv;
00023 String home;
00024 long idletime, timeout;
00025 int openflags, onflags, offflags;
00026 int refcount = 1;
00027
00028 void dispose() {
00029 if (dbenv != null) {
00030 try {
00031 dbenv.close();
00032 } catch (Throwable t) {
00033 Util.handleException(t);
00034 }
00035 dbenv = null;
00036 }
00037 }
00038
00039 public void close(Dispatcher server,
00040 __env_close_msg args, __env_close_reply reply) {
00041 if (--refcount != 0) {
00042 reply.status = 0;
00043 return;
00044 }
00045
00046 try {
00047 server.delEnv(this, false);
00048 if (dbenv != null)
00049 dbenv.close();
00050 reply.status = 0;
00051 } catch (Throwable t) {
00052 reply.status = Util.handleException(t);
00053 } finally {
00054 dbenv = null;
00055 }
00056 }
00057
00058 public void create(Dispatcher server,
00059 __env_create_msg args, __env_create_reply reply) {
00060 this.idletime = (args.timeout != 0) ? args.timeout : Server.idleto;
00061 this.timeout = Server.defto;
00062 try {
00063 config = new EnvironmentConfig();
00064 config.setErrorStream(Server.errstream);
00065 reply.envcl_id = server.addEnv(this);
00066 reply.status = 0;
00067 } catch (Throwable t) {
00068 reply.status = Util.handleException(t);
00069 }
00070 }
00071
00072 public void dbremove(Dispatcher server,
00073 __env_dbremove_msg args, __env_dbremove_reply reply) {
00074 try {
00075 args.name = (args.name.length() > 0) ? args.name : null;
00076 args.subdb = (args.subdb.length() > 0) ? args.subdb : null;
00077
00078 RpcDbTxn rtxn = server.getTxn(args.txnpcl_id);
00079 Transaction txn = (rtxn != null) ? rtxn.txn : null;
00080 dbenv.removeDatabase(txn, args.name, args.subdb );
00081 reply.status = 0;
00082 } catch (Throwable t) {
00083 reply.status = Util.handleException(t);
00084 }
00085 }
00086
00087 public void dbrename(Dispatcher server,
00088 __env_dbrename_msg args, __env_dbrename_reply reply) {
00089 try {
00090 args.name = (args.name.length() > 0) ? args.name : null;
00091 args.subdb = (args.subdb.length() > 0) ? args.subdb : null;
00092 args.newname = (args.newname.length() > 0) ? args.newname : null;
00093
00094 RpcDbTxn rtxn = server.getTxn(args.txnpcl_id);
00095 Transaction txn = (rtxn != null) ? rtxn.txn : null;
00096 dbenv.renameDatabase(txn, args.name, args.subdb, args.newname );
00097 reply.status = 0;
00098 } catch (Throwable t) {
00099 reply.status = Util.handleException(t);
00100 }
00101 }
00102
00103 private boolean findSharedEnvironment(Dispatcher server, __env_open_reply reply)
00104 throws DatabaseException {
00105 RpcDbEnv rdbenv = null;
00106 boolean matchFound = false;
00107 LocalIterator i = ((Server)server).env_list.iterator();
00108
00109 while (!matchFound && i.hasNext()) {
00110 rdbenv = (RpcDbEnv)i.next();
00111 if (rdbenv != null && rdbenv != this &&
00112 (home == rdbenv.home ||
00113 (home != null && home.equals(rdbenv.home))) &&
00114 openflags == rdbenv.openflags &&
00115 onflags == rdbenv.onflags &&
00116 offflags == rdbenv.offflags)
00117 matchFound = true;
00118 }
00119
00120 if (matchFound) {
00121
00122
00123
00124
00125
00126
00127 if (rdbenv.timeout < timeout)
00128 rdbenv.timeout = timeout;
00129
00130 ++rdbenv.refcount;
00131 reply.envcl_id = ((FreeList.FreeListIterator)i).current;
00132 reply.status = 0;
00133
00134 Server.err.println("Sharing Environment: " + reply.envcl_id);
00135 }
00136
00137 return matchFound;
00138 }
00139
00140 public void get_home(Dispatcher server,
00141 __env_get_home_msg args, __env_get_home_reply reply) {
00142 try {
00143 reply.home = dbenv.getHome().toString();
00144 reply.status = 0;
00145 } catch (Throwable t) {
00146 reply.status = Util.handleException(t);
00147 }
00148 }
00149
00150 public void get_open_flags(Dispatcher server,
00151 __env_get_open_flags_msg args, __env_get_open_flags_reply reply) {
00152 try {
00153 reply.flags = 0;
00154 if (config.getAllowCreate()) reply.flags |= DbConstants.DB_CREATE;
00155 if (config.getInitializeCache()) reply.flags |= DbConstants.DB_INIT_MPOOL;
00156 if (config.getInitializeCDB()) reply.flags |= DbConstants.DB_INIT_CDB;
00157 if (config.getInitializeLocking()) reply.flags |= DbConstants.DB_INIT_LOCK;
00158 if (config.getInitializeLogging()) reply.flags |= DbConstants.DB_INIT_LOG;
00159 if (config.getInitializeReplication()) reply.flags |= DbConstants.DB_INIT_REP;
00160 if (config.getJoinEnvironment()) reply.flags |= DbConstants.DB_JOINENV;
00161 if (config.getLockDown()) reply.flags |= DbConstants.DB_LOCKDOWN;
00162 if (config.getPrivate()) reply.flags |= DbConstants.DB_PRIVATE;
00163 if (config.getRunRecovery()) reply.flags |= DbConstants.DB_RECOVER;
00164 if (config.getRunFatalRecovery()) reply.flags |= DbConstants.DB_RECOVER_FATAL;
00165 if (config.getSystemMemory()) reply.flags |= DbConstants.DB_SYSTEM_MEM;
00166 if (config.getTransactional()) reply.flags |= DbConstants.DB_INIT_TXN;
00167 if (config.getUseEnvironment()) reply.flags |= DbConstants.DB_USE_ENVIRON;
00168 if (config.getUseEnvironmentRoot()) reply.flags |= DbConstants.DB_USE_ENVIRON_ROOT;
00169
00170 reply.status = 0;
00171 } catch (Throwable t) {
00172 reply.status = Util.handleException(t);
00173 }
00174 }
00175
00176 public void open(Dispatcher server,
00177 __env_open_msg args, __env_open_reply reply) {
00178 try {
00179 home = (args.home.length() > 0) ? args.home : null;
00180
00181
00182
00183
00184
00185 if ((args.flags & DbConstants.DB_INIT_LOCK) != 0)
00186 config.setLockDetectMode(LockDetectMode.DEFAULT);
00187
00188
00189 int newflags = (args.flags & ~Server.DB_SERVER_FLAGMASK);
00190 openflags = (newflags & Server.DB_SERVER_ENVFLAGS);
00191
00192 config.setAllowCreate((args.flags & DbConstants.DB_CREATE) != 0);
00193 config.setInitializeCache((args.flags & DbConstants.DB_INIT_MPOOL) != 0);
00194 config.setInitializeCDB((args.flags & DbConstants.DB_INIT_CDB) != 0);
00195 config.setInitializeLocking((args.flags & DbConstants.DB_INIT_LOCK) != 0);
00196 config.setInitializeLogging((args.flags & DbConstants.DB_INIT_LOG) != 0);
00197 config.setInitializeReplication((args.flags & DbConstants.DB_INIT_REP) != 0);
00198 config.setJoinEnvironment((args.flags & DbConstants.DB_JOINENV) != 0);
00199 config.setLockDown((args.flags & DbConstants.DB_LOCKDOWN) != 0);
00200 config.setPrivate((args.flags & DbConstants.DB_PRIVATE) != 0);
00201 config.setRunRecovery((args.flags & DbConstants.DB_RECOVER) != 0);
00202 config.setRunFatalRecovery((args.flags & DbConstants.DB_RECOVER_FATAL) != 0);
00203 config.setSystemMemory((args.flags & DbConstants.DB_SYSTEM_MEM) != 0);
00204 config.setTransactional((args.flags & DbConstants.DB_INIT_TXN) != 0);
00205 config.setUseEnvironment((args.flags & DbConstants.DB_USE_ENVIRON) != 0);
00206 config.setUseEnvironmentRoot((args.flags & DbConstants.DB_USE_ENVIRON_ROOT) != 0);
00207
00208 if (findSharedEnvironment(server, reply))
00209 dbenv = null;
00210 else if (Server.check_home(home)) {
00211 dbenv = new Environment(new File(home), config);
00212
00213 config = dbenv.getConfig();
00214 reply.status = 0;
00215 reply.envcl_id = args.dbenvcl_id;
00216 } else
00217 reply.status = DbConstants.DB_NOSERVER_HOME;
00218 } catch (Throwable t) {
00219 reply.status = Util.handleException(t);
00220 }
00221
00222
00223 }
00224
00225 public void remove(Dispatcher server,
00226 __env_remove_msg args, __env_remove_reply reply) {
00227 Server.err.println("RpcDbEnv.remove(" + args.home + ")");
00228 try {
00229 args.home = (args.home.length() > 0) ? args.home : null;
00230
00231
00232 boolean force = (args.flags & DbConstants.DB_FORCE) != 0;
00233 config.setUseEnvironment((args.flags & DbConstants.DB_USE_ENVIRON) != 0);
00234 config.setUseEnvironmentRoot((args.flags & DbConstants.DB_USE_ENVIRON_ROOT) != 0);
00235
00236 Environment.remove(new File(args.home), force, config);
00237 reply.status = 0;
00238 } catch (Throwable t) {
00239 reply.status = Util.handleException(t);
00240 } finally {
00241 server.delEnv(this, false);
00242 }
00243 }
00244
00245 public void get_cachesize(Dispatcher server,
00246 __env_get_cachesize_msg args, __env_get_cachesize_reply reply) {
00247 try {
00248 long cachesize = config.getCacheSize();
00249 final long GIGABYTE = 1073741824;
00250 reply.gbytes = (int)(cachesize / GIGABYTE);
00251 reply.bytes = (int)(cachesize % GIGABYTE);
00252 reply.ncache = config.getCacheCount();
00253 reply.status = 0;
00254 } catch (Throwable t) {
00255 reply.status = Util.handleException(t);
00256 }
00257 }
00258
00259 public void set_cachesize(Dispatcher server,
00260 __env_set_cachesize_msg args, __env_set_cachesize_reply reply) {
00261 try {
00262 long bytes = (long)args.gbytes * 1024 * 1024 * 1024;
00263 bytes += args.bytes;
00264 config.setCacheSize(bytes);
00265 config.setCacheCount(args.ncache);
00266 reply.status = 0;
00267 } catch (Throwable t) {
00268 reply.status = Util.handleException(t);
00269 }
00270 }
00271
00272 public void get_encrypt_flags(Dispatcher server,
00273 __env_get_encrypt_flags_msg args, __env_get_encrypt_flags_reply reply) {
00274 try {
00275 reply.flags = config.getEncrypted() ? DbConstants.DB_ENCRYPT_AES : 0;
00276 reply.status = 0;
00277 } catch (Throwable t) {
00278 reply.status = Util.handleException(t);
00279 }
00280 }
00281
00282 public void set_encrypt(Dispatcher server,
00283 __env_set_encrypt_msg args, __env_set_encrypt_reply reply) {
00284 try {
00285 config.setEncrypted(args.passwd);
00286 reply.status = 0;
00287 } catch (Throwable t) {
00288 reply.status = Util.handleException(t);
00289 }
00290 }
00291
00292 public void get_flags(Dispatcher server,
00293 __env_get_flags_msg args, __env_get_flags_reply reply) {
00294 try {
00295 reply.flags = 0;
00296 if (config.getCDBLockAllDatabases()) reply.flags |= DbConstants.DB_CDB_ALLDB;
00297 if (config.getDirectDatabaseIO()) reply.flags |= DbConstants.DB_DIRECT_DB;
00298 if (config.getDirectLogIO()) reply.flags |= DbConstants.DB_DIRECT_LOG;
00299 if (config.getInitializeRegions()) reply.flags |= DbConstants.DB_REGION_INIT;
00300 if (config.getLogAutoRemove()) reply.flags |= DbConstants.DB_LOG_AUTOREMOVE;
00301 if (config.getNoLocking()) reply.flags |= DbConstants.DB_NOLOCKING;
00302 if (config.getNoMMap()) reply.flags |= DbConstants.DB_NOMMAP;
00303 if (config.getNoPanic()) reply.flags |= DbConstants.DB_NOPANIC;
00304 if (config.getOverwrite()) reply.flags |= DbConstants.DB_OVERWRITE;
00305 if (config.getTxnNoSync()) reply.flags |= DbConstants.DB_TXN_NOSYNC;
00306 if (config.getTxnNotDurable()) reply.flags |= DbConstants.DB_TXN_NOT_DURABLE;
00307 if (config.getTxnWriteNoSync()) reply.flags |= DbConstants.DB_TXN_WRITE_NOSYNC;
00308 if (config.getYieldCPU()) reply.flags |= DbConstants.DB_YIELDCPU;
00309
00310 reply.status = 0;
00311 } catch (Throwable t) {
00312 reply.status = Util.handleException(t);
00313 }
00314 }
00315
00316 public void set_flags(Dispatcher server,
00317 __env_set_flags_msg args, __env_set_flags_reply reply) {
00318 try {
00319 boolean onoff = (args.onoff != 0);
00320 if (onoff)
00321 onflags |= args.flags;
00322 else
00323 offflags |= args.flags;
00324
00325 if ((args.flags & DbConstants.DB_CDB_ALLDB) != 0) config.setCDBLockAllDatabases(onoff);
00326 if ((args.flags & DbConstants.DB_DIRECT_DB) != 0) config.setDirectDatabaseIO(onoff);
00327 if ((args.flags & DbConstants.DB_DIRECT_LOG) != 0) config.setDirectLogIO(onoff);
00328 if ((args.flags & DbConstants.DB_REGION_INIT) != 0) config.setInitializeRegions(onoff);
00329 if ((args.flags & DbConstants.DB_LOG_AUTOREMOVE) != 0) config.setLogAutoRemove(onoff);
00330 if ((args.flags & DbConstants.DB_NOLOCKING) != 0) config.setNoLocking(onoff);
00331 if ((args.flags & DbConstants.DB_NOMMAP) != 0) config.setNoMMap(onoff);
00332 if ((args.flags & DbConstants.DB_NOPANIC) != 0) config.setNoPanic(onoff);
00333 if ((args.flags & DbConstants.DB_OVERWRITE) != 0) config.setOverwrite(onoff);
00334 if ((args.flags & DbConstants.DB_TXN_NOSYNC) != 0) config.setTxnNoSync(onoff);
00335 if ((args.flags & DbConstants.DB_TXN_NOT_DURABLE) != 0) config.setTxnNotDurable(onoff);
00336 if ((args.flags & DbConstants.DB_TXN_WRITE_NOSYNC) != 0) config.setTxnWriteNoSync(onoff);
00337 if ((args.flags & DbConstants.DB_YIELDCPU) != 0) config.setYieldCPU(onoff);
00338
00339 reply.status = 0;
00340 } catch (Throwable t) {
00341 reply.status = Util.handleException(t);
00342 }
00343 }
00344
00345
00346 public void txn_recover(Dispatcher server,
00347 __env_txn_recover_msg args, __env_txn_recover_reply reply) {
00348 try {
00349 PreparedTransaction[] prep_list = dbenv.recover(args.count, args.flags == DbConstants.DB_NEXT);
00350 if (prep_list != null && prep_list.length > 0) {
00351 int count = prep_list.length;
00352 reply.retcount = count;
00353 reply.txn = new int[count];
00354 reply.gid = new byte[count * DbConstants.DB_XIDDATASIZE];
00355
00356 for (int i = 0; i < count; i++) {
00357 reply.txn[i] = server.addTxn(new RpcDbTxn(this, prep_list[i].getTransaction()));
00358 System.arraycopy(prep_list[i].getGID(), 0, reply.gid, i * DbConstants.DB_XIDDATASIZE, DbConstants.DB_XIDDATASIZE);
00359 }
00360 }
00361
00362 reply.status = 0;
00363 } catch (Throwable t) {
00364 reply.status = Util.handleException(t);
00365 }
00366 }
00367 }