public static void updateCacheFromEntries(
      final OStorage iStorage,
      final OTransaction<?> iTx,
      final Iterable<? extends OTransactionEntry<?>> iEntries)
      throws IOException {
    String rid;
    ORawBuffer cachedBuffer;
    for (OTransactionEntry<? extends ORecord<?>> txEntry : iEntries) {
      rid = txEntry.getRecord().getIdentity().toString();

      cachedBuffer = iStorage.getCache().getRecord(rid);

      if (cachedBuffer != null) {
        // UPDATE CACHE
        cachedBuffer.buffer = txEntry.getRecord().toStream();
        cachedBuffer.version = txEntry.getRecord().getVersion();
        cachedBuffer.recordType = txEntry.getRecord().getRecordType();

      } else if (txEntry.getRecord().isPinned())
        // INSERT NEW ENTRY IN THE CACHE
        iStorage
            .getCache()
            .pushRecord(
                rid,
                new ORawBuffer(
                    txEntry.getRecord().toStream(),
                    txEntry.getRecord().getVersion(),
                    txEntry.getRecord().getRecordType()));
    }
  }
Exemple #2
0
  public OStorage registerStorage(final OStorage iStorage) throws IOException {
    acquireExclusiveLock();
    try {
      for (OOrientListener l : listeners) l.onStorageRegistered(iStorage);

      if (!storages.containsKey(iStorage.getName())) storages.put(iStorage.getName(), iStorage);

    } finally {
      releaseExclusiveLock();
    }
    return iStorage;
  }
Exemple #3
0
  public void shutdown() {
    acquireExclusiveLock();
    try {
      if (!active) return;

      active = false;

      if (shutdownHook != null) shutdownHook.cancel();
      if (profiler != null) profiler.shutdown();

      OLogManager.instance().debug(this, "Orient Engine is shutting down...");

      if (listeners != null)
        // CALL THE SHUTDOWN ON ALL THE LISTENERS
        for (OOrientListener l : listeners) {
          if (l != null) l.onShutdown();
        }

      // SHUTDOWN ENGINES
      for (OEngine engine : engines.values()) {
        engine.shutdown();
      }

      if (databaseFactory != null)
        // CLOSE ALL DATABASES
        databaseFactory.shutdown();

      if (storages != null) {
        // CLOSE ALL THE STORAGES
        final List<OStorage> storagesCopy = new ArrayList<OStorage>(storages.values());
        for (OStorage stg : storagesCopy) {
          OLogManager.instance().info(this, "Shutting down storage: " + stg.getName() + "...");
          stg.close(true);
        }
      }

      if (OMMapManagerLocator.getInstance() != null) OMMapManagerLocator.getInstance().shutdown();

      if (threadGroup != null)
        // STOP ALL THE PENDING THREADS
        threadGroup.interrupt();

      if (listeners != null) listeners.clear();

      OLogManager.instance().info(this, "Orient Engine shutdown complete\n");

    } finally {
      releaseExclusiveLock();
    }
  }
 public void create() throws IOException {
   storage.createRecord(
       0,
       CONFIG_RID,
       new byte[] {0, 0, 0, 0},
       OVersionFactory.instance().createVersion(),
       ORecordBytes.RECORD_TYPE,
       (byte) 0,
       null);
 }
 public void update() throws OSerializationException {
   final byte[] record = toStream();
   storage.updateRecord(
       CONFIG_RID,
       record,
       OVersionFactory.instance().createUntrackedVersion(),
       ORecordBytes.RECORD_TYPE,
       0,
       null);
 }
  /**
   * This method load the record information by the internal cluster segment. It's for compatibility
   * with older database than 0.9.25.
   *
   * @compatibility 0.9.25
   * @return
   * @throws OSerializationException
   */
  public OStorageConfiguration load() throws OSerializationException {
    final byte[] record =
        storage.readRecord(CONFIG_RID, null, false, null, false).getResult().buffer;

    if (record == null)
      throw new OStorageException(
          "Cannot load database's configuration. The database seems to be corrupted.");

    fromStream(record);
    return this;
  }
  private int[] createClusters(String className) {
    className = className.toLowerCase();

    final ODatabaseDocumentInternal database = getDatabase();
    final OStorage storage = database.getStorage();

    int[] clusterIds; // CREATE A NEW CLUSTER(S)
    final int minimumClusters = storage.getConfiguration().getMinimumClusters();

    clusterIds = new int[minimumClusters];
    if (minimumClusters <= 1) {
      clusterIds[0] = database.getClusterIdByName(className);
      if (clusterIds[0] == -1) clusterIds[0] = database.addCluster(className);
    } else
      for (int i = 0; i < minimumClusters; ++i) {
        clusterIds[i] = database.getClusterIdByName(className + "_" + i);
        if (clusterIds[i] == -1) clusterIds[i] = database.addCluster(className + "_" + i);
      }
    return clusterIds;
  }
  /**
   * Removes from memory the pool associated to the closed storage. This avoids pool open against
   * closed storages.
   */
  public void onStorageUnregistered(final OStorage iStorage) {
    final String storageURL = iStorage.getURL();

    synchronized (pools) {
      Set<String> poolToClose = null;

      for (Entry<String, OResourcePool<String, DB>> e : pools.entrySet()) {
        final int pos = e.getKey().indexOf("@");
        final String dbName = e.getKey().substring(pos + 1);
        if (storageURL.equals(dbName)) {
          if (poolToClose == null) poolToClose = new HashSet<String>();

          poolToClose.add(e.getKey());
        }
      }

      if (poolToClose != null) for (String pool : poolToClose) remove(pool);
    }
  }
Exemple #9
0
  /**
   * Drops a database from a remote server instance.
   *
   * @param iDatabaseName The database name
   * @param storageType Storage type between "plocal" or "memory".
   * @return The instance itself. Useful to execute method in chain
   * @throws IOException
   */
  public synchronized OServerAdmin dropDatabase(
      final String iDatabaseName, final String storageType) throws IOException {

    boolean retry = true;
    while (retry) {
      retry =
          networkAdminOperation(
              new OStorageRemoteOperation<Boolean>() {
                @Override
                public Boolean execute(
                    final OChannelBinaryAsynchClient network, OStorageRemoteSession session)
                    throws IOException {
                  try {
                    try {
                      storage.beginRequest(
                          network, OChannelBinaryProtocol.REQUEST_DB_DROP, session);
                      network.writeString(iDatabaseName);
                      network.writeString(storageType);
                    } finally {
                      storage.endRequest(network);
                    }

                    storage.getResponse(network, session);
                    return false;
                  } catch (OModificationOperationProhibitedException oope) {
                    return handleDBFreeze();
                  }
                }
              },
              "Cannot delete the remote storage: " + storage.getName());
    }

    final Set<OStorage> underlyingStorages = new HashSet<OStorage>();

    for (OStorage s : Orient.instance().getStorages()) {
      if (s.getType().equals(storage.getType()) && s.getName().equals(storage.getName())) {
        underlyingStorages.add(s.getUnderlying());
      }
    }

    for (OStorage s : underlyingStorages) {
      s.close(true, true);
    }

    ODatabaseRecordThreadLocal.INSTANCE.remove();

    return this;
  }
  @Override
  public boolean execute(final OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
    checkSyntax(iRequest.url, 1, "Syntax error: server");

    iRequest.data.commandInfo = "Server status";

    try {
      StringWriter jsonBuffer = new StringWriter();
      OJSONWriter json = new OJSONWriter(jsonBuffer);

      json.beginObject();

      json.beginCollection(1, true, "connections");

      String lastCommandOn;
      String connectedOn;

      final List<OClientConnection> conns = OClientConnectionManager.instance().getConnections();
      for (OClientConnection c : conns) {
        final ONetworkProtocolData data = c.data;

        synchronized (dateTimeFormat) {
          lastCommandOn = dateTimeFormat.format(new Date(data.lastCommandReceived));
          connectedOn = dateTimeFormat.format(new Date(c.since));
        }

        json.beginObject(2);
        writeField(json, 2, "connectionId", c.id);
        writeField(
            json,
            2,
            "remoteAddress",
            c.protocol.getChannel() != null ? c.protocol.getChannel().toString() : "Disconnected");
        writeField(json, 2, "db", data.lastDatabase != null ? data.lastDatabase : "-");
        writeField(json, 2, "user", data.lastUser != null ? data.lastUser : "******");
        writeField(json, 2, "totalRequests", data.totalRequests);
        writeField(json, 2, "commandInfo", data.commandInfo);
        writeField(json, 2, "commandDetail", data.commandDetail);
        writeField(json, 2, "lastCommandOn", lastCommandOn);
        writeField(json, 2, "lastCommandInfo", data.lastCommandInfo);
        writeField(json, 2, "lastCommandDetail", data.lastCommandDetail);
        writeField(json, 2, "lastExecutionTime", data.lastCommandExecutionTime);
        writeField(json, 2, "totalWorkingTime", data.totalCommandExecutionTime);
        writeField(json, 2, "connectedOn", connectedOn);
        writeField(json, 2, "protocol", c.protocol.getType());
        writeField(json, 2, "clientId", data.clientId);

        final StringBuilder driver = new StringBuilder();
        if (data.driverName != null) {
          driver.append(data.driverName);
          driver.append(" v");
          driver.append(data.driverVersion);
          driver.append(" Protocol v");
          driver.append(data.protocolVersion);
        }

        writeField(json, 2, "driver", driver.toString());
        json.endObject(2);
      }
      json.endCollection(1, false);

      json.beginCollection(1, true, "dbs");
      Map<String, OResourcePool<String, ODatabaseDocumentTx>> dbPool =
          OSharedDocumentDatabase.getDatabasePools();
      for (Entry<String, OResourcePool<String, ODatabaseDocumentTx>> entry : dbPool.entrySet()) {
        for (ODatabaseDocumentTx db : entry.getValue().getResources()) {

          json.beginObject(2);
          writeField(json, 2, "db", db.getName());
          writeField(json, 2, "user", db.getUser() != null ? db.getUser().getName() : "-");
          writeField(json, 2, "status", db.isClosed() ? "closed" : "open");
          writeField(json, 2, "type", db.getType());
          writeField(json, 2, "storageType", db.getStorage().getType());
          json.endObject(2);
        }
      }
      json.endCollection(1, false);

      json.beginCollection(1, true, "storages");
      Collection<OStorage> storages = Orient.instance().getStorages();
      for (OStorage s : storages) {
        json.beginObject(2);
        writeField(json, 2, "name", s.getName());
        writeField(json, 2, "type", s.getClass().getSimpleName());
        writeField(
            json,
            2,
            "path",
            s instanceof OStorageLocalAbstract
                ? ((OStorageLocalAbstract) s).getStoragePath().replace('\\', '/')
                : "");
        writeField(json, 2, "activeUsers", s.getUsers());
        json.endObject(2);
      }
      json.endCollection(1, false);

      json.beginCollection(2, true, "properties");
      for (OServerEntryConfiguration entry : OServerMain.server().getConfiguration().properties) {
        json.beginObject(3, true, null);
        json.writeAttribute(4, false, "name", entry.name);
        json.writeAttribute(4, false, "value", entry.value);
        json.endObject(3, true);
      }
      json.endCollection(2, true);
      json.endObject();

      iResponse.send(
          OHttpUtils.STATUS_OK_CODE, "OK", OHttpUtils.CONTENT_JSON, jsonBuffer.toString(), null);

    } finally {
    }
    return false;
  }
Exemple #11
0
 public void checkEmbedded(final OStorage storage) {
   if (!(storage.getUnderlying() instanceof OAbstractPaginatedStorage))
     throw new OSchemaException(
         "'Internal' schema modification methods can be used only inside of embedded database");
 }