00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package com.sleepycat.db.rpcserver;
00011
00012 import java.io.FileNotFoundException;
00013
00014 import com.sleepycat.db.*;
00015 import com.sleepycat.db.internal.DbConstants;
00016
00020 public class Util {
00021 static int handleException(Throwable t) {
00022 int ret = Server.EINVAL;
00023
00024 if (t instanceof DatabaseException) {
00025 ret = ((DatabaseException)t).getErrno();
00026 if (ret == DbConstants.DB_LOCK_NOTGRANTED)
00027 ret = DbConstants.DB_LOCK_DEADLOCK;
00028 } else if (t instanceof FileNotFoundException)
00029 ret = Server.ENOENT;
00030
00031 t.printStackTrace(Server.err);
00032 Server.err.println("handleException(" + t + ") returned " + ret);
00033 return ret;
00034 }
00035
00036 static int notSupported(String meth) {
00037 Server.err.println("Unsupported functionality with JE: " + meth);
00038 return Server.EINVAL;
00039 }
00040
00041 static int ignored(String meth) {
00042 Server.err.println("Warning functionality ignored with JE: " + meth);
00043 return 0;
00044 }
00045
00046 static DatabaseEntry makeDatabaseEntry(byte[] data, int dlen, int doff, int ulen, int flags, int multiFlags) throws DatabaseException {
00047 DatabaseEntry dbt;
00048 switch (multiFlags) {
00049 case DbConstants.DB_MULTIPLE:
00050 dbt = new MultipleDataEntry(new byte[ulen]);
00051 break;
00052 case DbConstants.DB_MULTIPLE_KEY:
00053 dbt = new MultipleKeyDataEntry(new byte[ulen]);
00054 break;
00055 default:
00056 dbt = new DatabaseEntry(data);
00057 break;
00058 }
00059 dbt.setPartial(doff, dlen, (flags & DbConstants.DB_DBT_PARTIAL) != 0);
00060 return dbt;
00061 }
00062
00063 static DatabaseEntry makeDatabaseEntry(byte[] data, int dlen, int doff, int ulen, int flags) throws DatabaseException {
00064 return makeDatabaseEntry(data, dlen, doff, ulen, flags, 0);
00065 }
00066
00067 static byte[] returnDatabaseEntry(DatabaseEntry dbt) throws DatabaseException {
00068 if (dbt.getData().length == dbt.getSize())
00069 return dbt.getData();
00070 else {
00071 byte[] newdata = new byte[dbt.getSize()];
00072 System.arraycopy(dbt.getData(), 0, newdata, 0, dbt.getSize());
00073 return newdata;
00074 }
00075 }
00076
00077 private static final String separator = ":::";
00078
00079 static String makeFileName(String file, String database) {
00080 return null;
00081 }
00082
00083 static String makeDatabaseName(String file, String database) {
00084 if (file == null && database == null)
00085 return null;
00086 else if (database.length() == 0 && file.indexOf(separator) >= 0)
00087 return file;
00088 return file + separator + database;
00089 }
00090
00091 static String makeRenameTarget(String file, String database, String newname) {
00092 if (database.length() == 0)
00093 return makeDatabaseName(newname, database);
00094 else
00095 return makeDatabaseName(file, newname);
00096 }
00097
00098 static String getFileName(String fullname) {
00099 if (fullname == null)
00100 return null;
00101 int pos = fullname.indexOf(separator);
00102 return fullname.substring(0, pos);
00103 }
00104
00105 static String getDatabaseName(String fullname) {
00106 if (fullname == null)
00107 return null;
00108 int pos = fullname.indexOf(separator);
00109 return fullname.substring(pos + separator.length());
00110 }
00111
00112 static LockMode getLockMode(int flags) {
00113 switch(flags & Server.DB_MODIFIER_MASK) {
00114 case DbConstants.DB_READ_UNCOMMITTED:
00115 return LockMode.READ_UNCOMMITTED;
00116 case DbConstants.DB_READ_COMMITTED:
00117 return LockMode.READ_COMMITTED;
00118 case DbConstants.DB_RMW:
00119 return LockMode.RMW;
00120 default:
00121 return LockMode.DEFAULT;
00122 }
00123 }
00124
00125 static int getStatus(OperationStatus status) {
00126 if (status == OperationStatus.SUCCESS)
00127 return 0;
00128 else if (status == OperationStatus.KEYEXIST)
00129 return DbConstants.DB_KEYEXIST;
00130 else if (status == OperationStatus.KEYEMPTY)
00131 return DbConstants.DB_KEYEMPTY;
00132 else if (status == OperationStatus.NOTFOUND)
00133 return DbConstants.DB_NOTFOUND;
00134 else
00135 throw new IllegalArgumentException("Unknown status: " + status);
00136 }
00137
00138 static int fromDatabaseType(DatabaseType type) {
00139 if (type == DatabaseType.BTREE)
00140 return DbConstants.DB_BTREE;
00141 else if (type == DatabaseType.HASH)
00142 return DbConstants.DB_HASH;
00143 else if (type == DatabaseType.QUEUE)
00144 return DbConstants.DB_QUEUE;
00145 else if (type == DatabaseType.RECNO)
00146 return DbConstants.DB_RECNO;
00147 else
00148 throw new
00149 IllegalArgumentException("Unknown database type: " + type);
00150 }
00151
00152 static DatabaseType toDatabaseType(int type) {
00153 switch (type) {
00154 case DbConstants.DB_BTREE:
00155 return DatabaseType.BTREE;
00156 case DbConstants.DB_HASH:
00157 return DatabaseType.HASH;
00158 case DbConstants.DB_QUEUE:
00159 return DatabaseType.QUEUE;
00160 case DbConstants.DB_RECNO:
00161 return DatabaseType.RECNO;
00162 case DbConstants.DB_UNKNOWN:
00163 return DatabaseType.UNKNOWN;
00164 default:
00165 throw new IllegalArgumentException("Unknown database type ID: " + type);
00166 }
00167 }
00168
00169
00170 protected Util() {
00171 }
00172 }