}
OLogManager.instance().error(this, "Authentication error of remote client %s:%d: shutdown is aborted.",
channel.socket.getInetAddress(), channel.socket.getPort());
sendError(lastClientTxId, new OSecurityAccessException("Invalid user/password to shutdown the server"));
break;
}
case OChannelBinaryProtocol.REQUEST_CONNECT: {
data.commandInfo = "Connect";
serverLogin(channel.readString(), channel.readString());
channel.acquireExclusiveLock();
try {
sendOk(lastClientTxId);
channel.writeInt(connection.id);
} finally {
channel.releaseExclusiveLock();
}
break;
}
case OChannelBinaryProtocol.REQUEST_DB_OPEN: {
data.commandInfo = "Open database";
String dbURL = channel.readString();
user = channel.readString();
passwd = channel.readString();
openDatabase(dbURL, user, passwd);
if (!(connection.database.getStorage() instanceof OStorageEmbedded) && !loadUserFromSchema(user, passwd)) {
sendError(lastClientTxId, new OSecurityAccessException(connection.database.getName(),
"User or password not valid for database: '" + connection.database.getName() + "'"));
} else {
channel.acquireExclusiveLock();
try {
sendOk(lastClientTxId);
channel.writeInt(connection.id);
sendDatabaseInformation();
if (getClass().equals(ONetworkProtocolBinary.class))
// NO EXTENSIONS (CLUSTER): SEND NULL DOCUMENT
channel.writeBytes(null);
} finally {
channel.releaseExclusiveLock();
}
}
break;
}
case OChannelBinaryProtocol.REQUEST_DB_RELOAD: {
data.commandInfo = "Reload database information";
channel.acquireExclusiveLock();
try {
sendOk(lastClientTxId);
sendDatabaseInformation();
} finally {
channel.releaseExclusiveLock();
}
break;
}
case OChannelBinaryProtocol.REQUEST_DB_CREATE: {
data.commandInfo = "Create database";
String dbName = channel.readString();
String storageMode = channel.readString();
checkServerAccess("database.create");
connection.database = getDatabaseInstance(dbName, storageMode);
createDatabase(connection.database, null, null);
channel.acquireExclusiveLock();
try {
sendOk(lastClientTxId);
} finally {
channel.releaseExclusiveLock();
}
break;
}
case OChannelBinaryProtocol.REQUEST_DB_CLOSE:
data.commandInfo = "Close Database";
if (connection != null) {
connection.close();
OClientConnectionManager.instance().disconnect(connection.id);
// sendShutdown();
}
channel.acquireExclusiveLock();
try {
sendOk(lastClientTxId);
} finally {
channel.releaseExclusiveLock();
}
break;
case OChannelBinaryProtocol.REQUEST_DB_EXIST: {
data.commandInfo = "Exists database";
String dbName = channel.readString();
checkServerAccess("database.exists");
connection.database = getDatabaseInstance(dbName, "local");
channel.acquireExclusiveLock();
try {
sendOk(lastClientTxId);
channel.writeByte((byte) (connection.database.exists() ? 1 : 0));
} finally {
channel.releaseExclusiveLock();
}
break;
}
case OChannelBinaryProtocol.REQUEST_DB_DELETE: {
data.commandInfo = "Delete database";
String dbName = channel.readString();
checkServerAccess("database.delete");
connection.database = getDatabaseInstance(dbName, "local");
OLogManager.instance().info(this, "Dropped database '%s", connection.database.getURL());
connection.database.delete();
channel.acquireExclusiveLock();
try {
sendOk(lastClientTxId);
} finally {
channel.releaseExclusiveLock();
}
break;
}
case OChannelBinaryProtocol.REQUEST_DB_SIZE: {
data.commandInfo = "Database size";
channel.acquireExclusiveLock();
try {
sendOk(lastClientTxId);
channel.writeLong(connection.database.getStorage().getSize());
} finally {
channel.releaseExclusiveLock();
}
break;
}
case OChannelBinaryProtocol.REQUEST_DB_COUNTRECORDS: {
data.commandInfo = "Database count records";
channel.acquireExclusiveLock();
try {
sendOk(lastClientTxId);
channel.writeLong(connection.database.getStorage().countRecords());
} finally {
channel.releaseExclusiveLock();
}
break;
}
case OChannelBinaryProtocol.REQUEST_DATACLUSTER_COUNT: {
data.commandInfo = "Count cluster elements";
int[] clusterIds = new int[channel.readShort()];
for (int i = 0; i < clusterIds.length; ++i)
clusterIds[i] = channel.readShort();
final long count = connection.database.countClusterElements(clusterIds);
channel.acquireExclusiveLock();
try {
sendOk(lastClientTxId);
channel.writeLong(count);
} finally {
channel.releaseExclusiveLock();
}
break;
}
case OChannelBinaryProtocol.REQUEST_DATACLUSTER_DATARANGE: {
data.commandInfo = "Get the begin/end range of data in cluster";
long[] pos = connection.database.getStorage().getClusterDataRange(channel.readShort());
channel.acquireExclusiveLock();
try {
sendOk(lastClientTxId);
channel.writeLong(pos[0]);
channel.writeLong(pos[1]);
} finally {
channel.releaseExclusiveLock();
}
break;
}
case OChannelBinaryProtocol.REQUEST_DATACLUSTER_ADD: {
data.commandInfo = "Add cluster";
final String type = channel.readString();
final String name = channel.readString();
final int num;
OStorage.CLUSTER_TYPE t = OStorage.CLUSTER_TYPE.valueOf(type);
switch (t) {
case PHYSICAL:
num = connection.database.addPhysicalCluster(name, channel.readString(), channel.readInt());
break;
case MEMORY:
num = connection.database.getStorage().addCluster(name, t);
break;
case LOGICAL:
num = connection.database.addLogicalCluster(name, channel.readInt());
break;
default:
throw new IllegalArgumentException("Cluster type " + type + " is not supported");
}
channel.acquireExclusiveLock();
try {
sendOk(lastClientTxId);
channel.writeShort((short) num);
} finally {
channel.releaseExclusiveLock();
}
break;
}
case OChannelBinaryProtocol.REQUEST_DATACLUSTER_REMOVE: {
data.commandInfo = "Remove cluster";
final int id = channel.readShort();
boolean result = connection.database.dropCluster(connection.database.getClusterNameById(id));
channel.acquireExclusiveLock();
try {
sendOk(lastClientTxId);
channel.writeByte((byte) (result ? 1 : 0));
} finally {
channel.releaseExclusiveLock();
}
break;
}
case OChannelBinaryProtocol.REQUEST_RECORD_LOAD: {
data.commandInfo = "Load record";
final ORecordId rid = channel.readRID();
final String fetchPlanString = channel.readString();
if (rid.clusterId == 0 && rid.clusterPosition == 0) {
// @COMPATIBILITY 0.9.25
// SEND THE DB CONFIGURATION INSTEAD SINCE IT WAS ON RECORD 0:0
OFetchHelper.checkFetchPlanValid(fetchPlanString);
channel.acquireExclusiveLock();
try {
sendOk(lastClientTxId);
channel.writeByte((byte) 1);
channel.writeBytes(connection.database.getStorage().getConfiguration().toStream());
channel.writeInt(0);
channel.writeByte(ORecordBytes.RECORD_TYPE);
} finally {
channel.releaseExclusiveLock();
}
} else {
final ORecordInternal<?> record = connection.database.load(rid, fetchPlanString);
// if (rid.equals(((OSchemaImpl) connection.database.getMetadata().getSchema()).getDocument().getIdentity()))
// connection.database.getMetadata().getSchema().reload();
channel.acquireExclusiveLock();
try {
sendOk(lastClientTxId);
if (record != null) {
channel.writeByte((byte) 1);
channel.writeBytes(record.toStream());
channel.writeInt(record.getVersion());
channel.writeByte(record.getRecordType());
if (fetchPlanString.length() > 0) {
// BUILD THE SERVER SIDE RECORD TO ACCES TO THE FETCH
// PLAN
if (record instanceof ODocument) {
final Map<String, Integer> fetchPlan = OFetchHelper.buildFetchPlan(fetchPlanString);
final Set<ODocument> recordsToSend = new HashSet<ODocument>();
OFetchHelper.fetch((ODocument) record, record, fetchPlan, null, 0, -1, new OFetchListener() {
@Override
public int size() {
return recordsToSend.size();
}
// ADD TO THE SET OF OBJECTS TO SEND
@Override
public Object fetchLinked(final ODocument iRoot, final Object iUserObject, final String iFieldName,
final Object iLinked) {
if (iLinked instanceof ODocument) {
if (((ODocument) iLinked).getIdentity().isValid())
return recordsToSend.add((ODocument) iLinked) ? iLinked : null;
return null;
} else if (iLinked instanceof Collection<?>)
return recordsToSend.addAll((Collection<? extends ODocument>) iLinked) ? iLinked : null;
else if (iLinked instanceof Map<?, ?>)
return recordsToSend.addAll(((Map<String, ? extends ODocument>) iLinked).values()) ? iLinked : null;
else
throw new IllegalArgumentException("Unrecognized type while fetching records: " + iLinked);
}
});
// SEND RECORDS TO LOAD IN CLIENT CACHE
for (ODocument doc : recordsToSend) {
if (doc.getIdentity().isValid()) {
channel.writeByte((byte) 2); // CLIENT CACHE
// RECORD. IT ISN'T PART OF THE RESULT SET
writeIdentifiable(doc);
}
}
}
}
}
} finally {
channel.releaseExclusiveLock();
}
}
channel.writeByte((byte) 0); // NO MORE RECORDS
break;
}
case OChannelBinaryProtocol.REQUEST_RECORD_CREATE: {
data.commandInfo = "Create record";
final ORecordId rid = new ORecordId(channel.readShort(), ORID.CLUSTER_POS_INVALID);
final byte[] buffer = channel.readBytes();
final byte recordType = channel.readByte();
final ORecordInternal<?> record = Orient.instance().getRecordFactoryManager().newInstance(connection.database, recordType);
record.fill(connection.database, rid, 0, buffer, true);
connection.database.save(record);
channel.acquireExclusiveLock();
try {
sendOk(lastClientTxId);
channel.writeLong(record.getIdentity().getClusterPosition());
} finally {
channel.releaseExclusiveLock();
}
break;
}
case OChannelBinaryProtocol.REQUEST_RECORD_UPDATE: {
data.commandInfo = "Update record";
final ORecordId rid = channel.readRID();
final byte[] buffer = channel.readBytes();
final int version = channel.readInt();
final byte recordType = channel.readByte();
final ORecordInternal<?> newRecord = Orient.instance().getRecordFactoryManager().newInstance(connection.database, recordType);
newRecord.fill(connection.database, rid, version, buffer, true);
if (((OSchemaProxy) connection.database.getMetadata().getSchema()).getIdentity().equals(rid))
// || ((OIndexManagerImpl) connection.database.getMetadata().getIndexManager()).getDocument().getIdentity().equals(rid)) {
throw new OSecurityAccessException("Can't update internal record " + rid);
final ORecordInternal<?> currentRecord;
if (newRecord instanceof ODocument) {
currentRecord = connection.database.load(rid);