Examples of OChannelBinaryClient


Examples of com.orientechnologies.orient.enterprise.channel.binary.OChannelBinaryClient

      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(iCommand.getDatabase(), 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, iCommand.getDatabase());
              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();
                }
                iCommand.getDatabase().getLevel1Cache().updateRecord(record);
                break;

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

            case 'r':
              result = readIdentifiable(network, iCommand.getDatabase());
              if (result instanceof ORecord<?>)
                iCommand.getDatabase().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, iCommand.getDatabase());
                if (resultItem instanceof ORecord<?>)
                  iCommand.getDatabase().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;
            }
          }
View Full Code Here

Examples of com.orientechnologies.orient.enterprise.channel.binary.OChannelBinaryClient

  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 {
View Full Code Here

Examples of com.orientechnologies.orient.enterprise.channel.binary.OChannelBinaryClient

  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 {
View Full Code Here

Examples of com.orientechnologies.orient.enterprise.channel.binary.OChannelBinaryClient

  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());
View Full Code Here

Examples of com.orientechnologies.orient.enterprise.channel.binary.OChannelBinaryClient

  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) {
View Full Code Here

Examples of com.orientechnologies.orient.enterprise.channel.binary.OChannelBinaryClient

    }

    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);
    }
View Full Code Here

Examples of com.orientechnologies.orient.enterprise.channel.binary.OChannelBinaryClient

    int port;
    for (OPair<String, String[]> server : serverURLs) {
      port = Integer.parseInt(server.getValue()[server.getValue().length - 1]);

      OLogManager.instance().debug(this, "Trying to connect to the remote host %s:%d...", server.getKey(), port);
      final OChannelBinaryClient network = new OChannelBinaryClient(server.getKey(), port, clientConfiguration);

      OChannelBinaryProtocol.checkProtocolVersion(network);

      return network;
    }
View Full Code Here

Examples of com.orientechnologies.orient.enterprise.channel.binary.OChannelBinaryClient

   * @param iCommand
   * @return
   * @throws IOException
   */
  protected OChannelBinaryClient beginRequest(final byte iCommand) throws IOException {
    OChannelBinaryClient network = null;

    if (debug)
      System.out.println("-> req: " + getSessionId());

    // FIND THE FIRST FREE CHANNEL AVAILABLE
    synchronized (networkPool) {
      final int beginCursor = networkPoolCursor;

      while (network == null) {
        if (networkPool.size() == 0)
          throw new ONetworkProtocolException("Connection pool closed");

        network = networkPool.get(networkPoolCursor);
        if (network.getLockWrite().tryLock())
          break;

        network = null;

        networkPoolCursor++;

        if (networkPoolCursor >= networkPool.size())
          // RESTART FROM THE FIRST ONE
          networkPoolCursor = 0;

        if (networkPoolCursor == beginCursor) {
          // COMPLETE ROUND AND NOT FREE CONNECTIONS FOUND

          if (networkPool.size() < maxPool) {
            // CREATE NEW CONNECTION
            network = createNetworkConnection();
            network.getLockWrite().lock();
            networkPool.add(network);

            if (debug)
              System.out.println("Created new connection " + networkPool.size());
          } else {
            if (debug)
              System.out.println("-> req (waiting) : " + getSessionId());

            final long startToWait = System.currentTimeMillis();
            try {
              networkPool.wait(5000);
            } catch (InterruptedException e) {
              OProfiler.getInstance().updateCounter("network.connectionPool.timeout", +1);
            }

            final long elapsed = OProfiler.getInstance().stopChrono("network.connectionPool.waitingTime", startToWait);

            if (debug)
              System.out.println("Waiting for connection = elapsed: " + elapsed);
          }
        }
      }
    }

    network.writeByte(iCommand);
    network.writeInt(getSessionId());

    return network;
  }
View Full Code Here

Examples of com.orientechnologies.orient.enterprise.channel.binary.OChannelBinaryClient

      }

      // CREATE THE CHANNEL POOL
      if (networkPool.size() == 0) {
        // ALWAYS CREATE AT LEAST ONE CONNECTION
        final OChannelBinaryClient firstChannel = createNetworkConnection();
        networkPool.add(firstChannel);
        serviceThread = new OStorageRemoteServiceThread(new OStorageRemoteThread(this, Integer.MIN_VALUE), firstChannel);

        // CREATE THE MINIMUM POOL
        for (int i = 1; i < minPool; ++i)
View Full Code Here

Examples of com.orientechnologies.orient.enterprise.channel.binary.OChannelBinaryClient

  }

  protected void openRemoteDatabase(final String iUserName, final String iUserPassword) throws IOException {
    createConnectionPool();

    final OChannelBinaryClient network = beginRequest(OChannelBinaryProtocol.REQUEST_DB_OPEN);
    try {
      network.writeString(name).writeString(iUserName).writeString(iUserPassword);
    } finally {
      endRequest(network);
    }

    try {
      beginResponse(network);

      final int sessionId = network.readInt();
      setSessionId(sessionId);

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

      int tot = network.readInt();
      String clusterName;
      for (int i = 0; i < tot; ++i) {
        clusterName = network.readString().toLowerCase();
        clustersIds.put(clusterName, network.readInt());
        clustersTypes.put(clusterName, network.readString());
      }

      // READ CLUSTER CONFIGURATION
      updateClusterConfiguration(network.readBytes());

    } finally {
      endResponse(network);
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.