Beispiel #1
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);
  }
Beispiel #2
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;
  }