Ejemplo n.º 1
0
  public int addDataSegment(final String iSegmentName, final String iSegmentFileName) {
    checkConnection();

    do {
      try {
        OChannelBinaryClient network = null;
        try {
          network = beginRequest(OChannelBinaryProtocol.REQUEST_DATASEGMENT_ADD);

          network.writeString(iSegmentName).writeString(iSegmentFileName);

        } finally {
          endRequest(network);
        }

        try {
          beginResponse(network);
          return network.readShort();
        } finally {
          endResponse(network);
        }

      } catch (OException e) {
        // PASS THROUGH
        throw e;
      } catch (Exception e) {
        handleException("Error on add new data segment", e);
      }
    } while (true);
  }
Ejemplo n.º 2
0
  public long count(final int[] iClusterIds) {
    checkConnection();

    do {
      try {
        OChannelBinaryClient network = null;
        try {
          network = beginRequest(OChannelBinaryProtocol.REQUEST_DATACLUSTER_COUNT);

          network.writeShort((short) iClusterIds.length);
          for (int i = 0; i < iClusterIds.length; ++i) network.writeShort((short) iClusterIds[i]);

        } finally {
          endRequest(network);
        }

        try {
          beginResponse(network);
          return network.readLong();
        } finally {
          endResponse(network);
        }
      } catch (OException e) {
        // PASS THROUGH
        throw e;
      } catch (Exception e) {
        handleException(
            "Error on read record count in clusters: " + Arrays.toString(iClusterIds), e);
      }
    } while (true);
  }
Ejemplo n.º 3
0
  public long count(final String iClassName) {
    checkConnection();

    do {

      try {
        OChannelBinaryClient network = null;
        try {

          network = beginRequest(OChannelBinaryProtocol.REQUEST_COUNT);
          network.writeString(iClassName);

        } finally {
          endRequest(network);
        }

        try {
          beginResponse(network);
          return network.readLong();
        } finally {
          endResponse(network);
        }
      } catch (OException e) {
        // PASS THROUGH
        throw e;
      } catch (Exception e) {
        handleException("Error on executing count on class: " + iClassName, e);
      }
    } while (true);
  }
Ejemplo n.º 4
0
  public long getSize() {
    checkConnection();

    do {
      try {
        OChannelBinaryClient network = null;
        try {

          network = beginRequest(OChannelBinaryProtocol.REQUEST_DB_SIZE);

        } finally {
          endRequest(network);
        }

        try {
          beginResponse(network);
          return network.readLong();
        } finally {
          endResponse(network);
        }
      } catch (OException e) {
        // PASS THROUGH
        throw e;
      } catch (Exception e) {
        handleException("Error on read database size", e);
      }
    } while (true);
  }
Ejemplo n.º 5
0
  @Override
  public long countRecords() {
    checkConnection();

    do {
      try {
        OChannelBinaryClient network = null;
        try {

          network = beginRequest(OChannelBinaryProtocol.REQUEST_DB_COUNTRECORDS);

        } finally {
          endRequest(network);
        }

        try {
          beginResponse(network);
          return network.readLong();
        } finally {
          endResponse(network);
        }
      } catch (OException e) {
        // PASS THROUGH
        throw e;
      } catch (Exception e) {
        handleException("Error on read database record count", e);
      }
    } while (true);
  }
Ejemplo n.º 6
0
  public void reload() {
    checkConnection();

    do {
      try {

        OChannelBinaryClient network = null;
        try {
          network = beginRequest(OChannelBinaryProtocol.REQUEST_DB_RELOAD);
        } finally {
          endRequest(network);
        }

        try {
          beginResponse(network);

          readDatabaseInformation(network);
          break;

        } finally {
          endResponse(network);
        }

      } catch (OException e) {
        // PASS THROUGH
        throw e;
      } catch (Exception e) {
        handleException("Error on reloading database information", e);
      }
    } while (true);
  }
Ejemplo n.º 7
0
  public long[] getClusterDataRange(final int iClusterId) {
    checkConnection();

    do {
      try {
        OChannelBinaryClient network = null;
        try {
          network = beginRequest(OChannelBinaryProtocol.REQUEST_DATACLUSTER_DATARANGE);

          network.writeShort((short) iClusterId);

        } finally {
          endRequest(network);
        }

        try {
          beginResponse(network);
          return new long[] {network.readLong(), network.readLong()};
        } finally {
          endResponse(network);
        }

      } catch (OException e) {
        // PASS THROUGH
        throw e;
      } catch (Exception e) {
        handleException("Error on getting last entry position count in cluster: " + iClusterId, e);
      }
    } while (true);
  }
Ejemplo n.º 8
0
  public int updateRecord(
      final ORecordId iRid,
      final byte[] iContent,
      final int iVersion,
      final byte iRecordType,
      final ORecordCallback<Integer> iCallback) {
    checkConnection();

    do {
      try {
        final OChannelBinaryClient network =
            beginRequest(OChannelBinaryProtocol.REQUEST_RECORD_UPDATE);
        try {
          network.writeRID(iRid);
          network.writeBytes(iContent);
          network.writeInt(iVersion);
          network.writeByte(iRecordType);

        } finally {
          endRequest(network);
        }

        if (iCallback == null)
          try {
            beginResponse(network);
            return network.readInt();
          } finally {
            endResponse(network);
          }
        else {
          Callable<Object> response =
              new Callable<Object>() {
                public Object call() throws Exception {
                  int result;

                  beginResponse(network);
                  try {
                    result = network.readInt();
                  } finally {
                    endResponse(network);
                  }

                  iCallback.call(result);
                  return null;
                }
              };
          asynchExecutor.submit(new FutureTask<Object>(response));
        }
      } catch (OException e) {
        // PASS THROUGH
        throw e;
      } catch (Exception e) {
        handleException("Error on update record " + iRid, e);
      }
    } while (true);
  }
Ejemplo n.º 9
0
  public long createRecord(
      final ORecordId iRid,
      final byte[] iContent,
      final byte iRecordType,
      final ORecordCallback<Long> iCallback) {
    checkConnection();

    do {
      try {
        final OChannelBinaryClient network =
            beginRequest(OChannelBinaryProtocol.REQUEST_RECORD_CREATE);
        try {
          network.writeShort((short) iRid.clusterId);
          network.writeBytes(iContent);
          network.writeByte(iRecordType);

        } finally {
          endRequest(network);
        }

        if (iCallback == null)
          try {
            beginResponse(network);
            iRid.clusterPosition = network.readLong();
            return iRid.clusterPosition;
          } finally {
            endResponse(network);
          }
        else {
          Callable<Object> response =
              new Callable<Object>() {
                public Object call() throws Exception {
                  final Long result;

                  beginResponse(network);
                  try {
                    result = network.readLong();
                  } finally {
                    endResponse(network);
                  }
                  iCallback.call(result);
                  return null;
                }
              };
          asynchExecutor.submit(new FutureTask<Object>(response));
        }

      } catch (OException e) {
        // PASS THROUGH
        throw e;
      } catch (Exception e) {
        handleException("Error on create record in cluster: " + iRid.clusterId, e);
      }
    } while (true);
  }
Ejemplo n.º 10
0
  public int addCluster(
      final String iClusterName,
      final OStorage.CLUSTER_TYPE iClusterType,
      final Object... iArguments) {
    checkConnection();

    do {
      try {
        OChannelBinaryClient network = null;
        try {
          network = beginRequest(OChannelBinaryProtocol.REQUEST_DATACLUSTER_ADD);

          network.writeString(iClusterType.toString());
          network.writeString(iClusterName);

          switch (iClusterType) {
            case PHYSICAL:
              // FILE PATH + START SIZE
              network
                  .writeString(iArguments.length > 0 ? (String) iArguments[0] : "")
                  .writeInt(iArguments.length > 0 ? (Integer) iArguments[1] : -1);
              break;

            case LOGICAL:
              // PHY CLUSTER ID
              network.writeInt(iArguments.length > 0 ? (Integer) iArguments[0] : -1);
              break;
          }
        } finally {
          endRequest(network);
        }

        try {
          beginResponse(network);
          final int clusterId = network.readShort();

          clustersIds.put(iClusterName.toLowerCase(), clusterId);
          clustersTypes.put(iClusterName.toLowerCase(), iClusterType.toString());
          return clusterId;
        } finally {
          endResponse(network);
        }
      } catch (OException e) {
        // PASS THROUGH
        throw e;
      } catch (Exception e) {
        handleException("Error on add new cluster", e);
      }
    } while (true);
  }
Ejemplo n.º 11
0
  public ORawBuffer readRecord(
      final ORecordId iRid, final String iFetchPlan, final ORecordCallback<ORawBuffer> iCallback) {
    checkConnection();

    if (OStorageRemoteThreadLocal.INSTANCE.get().commandExecuting)
      // PENDING NETWORK OPERATION, CAN'T EXECUTE IT NOW
      return null;

    do {
      try {

        OChannelBinaryClient network = null;
        try {
          network = beginRequest(OChannelBinaryProtocol.REQUEST_RECORD_LOAD);
          network.writeRID(iRid);
          network.writeString(iFetchPlan != null ? iFetchPlan : "");

        } finally {
          endRequest(network);
        }

        try {
          beginResponse(network);

          if (network.readByte() == 0) return null;

          final ORawBuffer buffer =
              new ORawBuffer(network.readBytes(), network.readInt(), network.readByte());

          final ODatabaseRecord database = ODatabaseRecordThreadLocal.INSTANCE.get();
          ORecordInternal<?> record;
          while (network.readByte() == 2) {
            record = (ORecordInternal<?>) readIdentifiable(network);

            // PUT IN THE CLIENT LOCAL CACHE
            database.getLevel1Cache().updateRecord(record);
          }
          return buffer;
        } finally {
          endResponse(network);
        }

      } catch (OException e) {
        // PASS THROUGH
        throw e;
      } catch (Exception e) {
        handleException("Error on read record " + iRid, e);
      }
    } while (true);
  }
Ejemplo n.º 12
0
  protected void openRemoteDatabase() throws IOException {
    minPool = OGlobalConfiguration.CLIENT_CHANNEL_MIN_POOL.getValueAsInteger();
    maxPool = OGlobalConfiguration.CLIENT_CHANNEL_MAX_POOL.getValueAsInteger();

    if (connectionOptions != null && connectionOptions.size() > 0) {
      if (connectionOptions.containsKey(PARAM_MIN_POOL))
        minPool = Integer.parseInt(connectionOptions.get(PARAM_MIN_POOL).toString());
      if (connectionOptions.containsKey(PARAM_MAX_POOL))
        maxPool = Integer.parseInt(connectionOptions.get(PARAM_MAX_POOL).toString());
    }

    setSessionId(-1);
    createConnectionPool();

    OChannelBinaryClient network = null;
    try {
      network = beginRequest(OChannelBinaryProtocol.REQUEST_DB_OPEN);

      network.writeString(name).writeString(connectionUserName).writeString(connectionUserPassword);

    } finally {
      endRequest(network);
    }

    final int sessionId;

    try {
      beginResponse(network);

      sessionId = network.readInt();

      OLogManager.instance().debug(null, "Client connected with session id: " + sessionId);

      readDatabaseInformation(network);

      // READ CLUSTER CONFIGURATION
      clusterConfiguration = new ODocument(network.readBytes());

    } finally {
      endResponse(network);
    }

    setSessionId(sessionId);

    defaultClusterId = clustersIds.get(OStorage.CLUSTER_DEFAULT_NAME);

    status = STATUS.OPEN;
  }
Ejemplo n.º 13
0
  public void close(final boolean iForce) {
    lock.acquireExclusiveLock();

    OChannelBinaryClient network = null;
    try {
      if (networkPool.size() > 0) {
        try {
          network = beginRequest(OChannelBinaryProtocol.REQUEST_DB_CLOSE);
        } finally {
          endRequest(network);
        }

        getResponse(network);
      }

      setSessionId(-1);

      if (!checkForClose(iForce)) return;

      // CLOSE THE CHANNEL
      if (serviceThread != null) {
        serviceThread.sendShutdown();
        serviceThread.interrupt();
      }

      synchronized (networkPool) {
        for (OChannelBinaryClient n : networkPool) n.close();
        networkPool.clear();
      }

      level2Cache.shutdown();
      super.close(iForce);
      status = STATUS.CLOSED;

      Orient.instance().unregisterStorage(this);

    } catch (OException e) {
      // PASS THROUGH
      throw e;
    } catch (Exception e) {
      OLogManager.instance().debug(this, "Error on closing remote connection: %s", network);
    } finally {

      lock.releaseExclusiveLock();
    }
  }
Ejemplo n.º 14
0
  public boolean dropCluster(final int iClusterId) {
    checkConnection();

    do {
      try {
        OChannelBinaryClient network = null;
        try {
          network = beginRequest(OChannelBinaryProtocol.REQUEST_DATACLUSTER_REMOVE);

          network.writeShort((short) iClusterId);

        } finally {
          endRequest(network);
        }

        try {
          beginResponse(network);

          if (network.readByte() == 1) {
            // REMOVE THE CLUSTER LOCALLY
            for (Entry<String, Integer> entry : clustersIds.entrySet())
              if (entry.getValue() != null && entry.getValue().intValue() == iClusterId) {
                clustersIds.remove(entry.getKey());
                clustersTypes.remove(entry.getKey());
                if (configuration.clusters.size() > iClusterId)
                  configuration.clusters.set(iClusterId, null);
                break;
              }
            getLevel2Cache().freeCluster(iClusterId);
            return true;
          }
          return false;
        } finally {
          endResponse(network);
        }

      } catch (OException e) {
        // PASS THROUGH
        throw e;
      } catch (Exception e) {
        handleException("Error on removing of cluster", e);
      }
    } while (true);
  }
Ejemplo n.º 15
0
  public void commit(final OTransaction iTx) {
    checkConnection();

    do {
      try {
        OChannelBinaryClient network = null;
        try {
          network = beginRequest(OChannelBinaryProtocol.REQUEST_TX_COMMIT);

          network.writeInt(((OTransaction) iTx).getId());
          network.writeByte((byte) (((OTransaction) iTx).isUsingLog() ? 1 : 0));

          final List<OTransactionRecordEntry> tmpEntries = new ArrayList<OTransactionRecordEntry>();

          while (iTx.getCurrentRecordEntries().iterator().hasNext()) {
            for (OTransactionRecordEntry txEntry : iTx.getCurrentRecordEntries())
              tmpEntries.add(txEntry);

            iTx.clearRecordEntries();

            if (tmpEntries.size() > 0)
              for (OTransactionRecordEntry txEntry : tmpEntries) commitEntry(network, txEntry);
          }

          // END OF RECORD ENTRIES
          network.writeByte((byte) 0);

          // SEND INDEX ENTRIES
          network.writeBytes(iTx.getIndexChanges().toStream());
        } finally {
          endRequest(network);
        }

        try {
          beginResponse(network);
          final int createdRecords = network.readInt();
          ORecordId currentRid;
          ORecordId createdRid;
          for (int i = 0; i < createdRecords; i++) {
            currentRid = network.readRID();
            createdRid = network.readRID();
            for (OTransactionRecordEntry txEntry : iTx.getAllRecordEntries()) {
              if (txEntry.getRecord().getIdentity().equals(currentRid)) {
                txEntry.getRecord().setIdentity(createdRid);
                break;
              }
            }
          }
          final int updatedRecords = network.readInt();
          ORecordId rid;
          for (int i = 0; i < updatedRecords; ++i) {
            rid = network.readRID();

            // SEARCH THE RECORD WITH THAT ID TO UPDATE THE VERSION
            for (OTransactionRecordEntry txEntry : iTx.getAllRecordEntries()) {
              if (txEntry.getRecord().getIdentity().equals(rid)) {
                txEntry.getRecord().setVersion(network.readInt());
                break;
              }
            }
          }
        } finally {
          endResponse(network);
        }

        // SET ALL THE RECORDS AS UNDIRTY
        for (OTransactionRecordEntry txEntry : iTx.getAllRecordEntries())
          txEntry.getRecord().unload();

        // UPDATE THE CACHE ONLY IF THE ITERATOR ALLOWS IT. USE THE STRATEGY TO ALWAYS REMOVE ALL
        // THE RECORDS SINCE THEY COULD BE
        // CHANGED AS CONTENT IN CASE OF TREE AND GRAPH DUE TO CROSS REFERENCES
        OTransactionAbstract.updateCacheFromEntries(this, iTx, iTx.getAllRecordEntries(), false);

        break;
      } catch (OException e) {
        // PASS THROUGH
        throw e;
      } catch (Exception e) {
        handleException("Error on commit", e);
      }
    } while (true);
  }
Ejemplo n.º 16
0
  /** Execute the command remotely and get the results back. */
  public Object command(final OCommandRequestText iCommand) {
    checkConnection();

    if (!(iCommand instanceof OSerializableStream))
      throw new OCommandExecutionException(
          "Cannot serialize the command to be executed to the server side.");

    OSerializableStream command = iCommand;
    Object result = null;

    final ODatabaseRecord database = ODatabaseRecordThreadLocal.INSTANCE.get();

    do {
      OStorageRemoteThreadLocal.INSTANCE.get().commandExecuting = true;

      try {
        final OCommandRequestText aquery = iCommand;

        final boolean asynch = iCommand instanceof OCommandRequestAsynch;

        OChannelBinaryClient network = null;
        try {
          network = beginRequest(OChannelBinaryProtocol.REQUEST_COMMAND);

          network.writeByte((byte) (asynch ? 'a' : 's')); // ASYNC / SYNC
          network.writeBytes(OStreamSerializerAnyStreamable.INSTANCE.toStream(command));

        } finally {
          endRequest(network);
        }

        try {
          beginResponse(network);

          if (asynch) {
            byte status;

            // ASYNCH: READ ONE RECORD AT TIME
            while ((status = network.readByte()) > 0) {
              final ORecordSchemaAware<?> record =
                  (ORecordSchemaAware<?>) readIdentifiable(network);
              if (record == null) break;

              switch (status) {
                case 1:
                  // PUT AS PART OF THE RESULT SET. INVOKE THE LISTENER
                  try {
                    if (!aquery.getResultListener().result(record)) {
                      // EMPTY THE INPUT CHANNEL
                      while (network.in.available() > 0) network.in.read();

                      break;
                    }
                  } catch (Throwable t) {
                    // ABSORBE ALL THE USER EXCEPTIONS
                    t.printStackTrace();
                  }
                  database.getLevel1Cache().updateRecord(record);
                  break;

                case 2:
                  // PUT IN THE CLIENT LOCAL CACHE
                  database.getLevel1Cache().updateRecord(record);
              }
            }
          } else {
            final byte type = network.readByte();
            switch (type) {
              case 'n':
                result = null;
                break;

              case 'r':
                result = readIdentifiable(network);
                if (result instanceof ORecord<?>)
                  database.getLevel1Cache().updateRecord((ORecordInternal<?>) result);
                break;

              case 'l':
                final int tot = network.readInt();
                final Collection<OIdentifiable> list = new ArrayList<OIdentifiable>();
                for (int i = 0; i < tot; ++i) {
                  final OIdentifiable resultItem = readIdentifiable(network);
                  if (resultItem instanceof ORecord<?>)
                    database.getLevel1Cache().updateRecord((ORecordInternal<?>) resultItem);
                  list.add(resultItem);
                }
                result = list;
                break;

              case 'a':
                final String value = new String(network.readBytes());
                result =
                    ORecordSerializerStringAbstract.fieldTypeFromStream(
                        null, ORecordSerializerStringAbstract.getType(value), value);
                break;
            }
          }
          break;
        } finally {
          endResponse(network);
        }

      } catch (OException e) {
        // PASS THROUGH
        throw e;
      } catch (Exception e) {
        handleException("Error on executing command: " + iCommand, e);

      } finally {
        OStorageRemoteThreadLocal.INSTANCE.get().commandExecuting = false;
      }
    } while (true);

    return result;
  }