Search in sources :

Example 21 with OMetadataInternal

use of com.orientechnologies.orient.core.metadata.OMetadataInternal in project orientdb by orientechnologies.

the class OServerCommandGetDatabase method exec.

protected void exec(final OHttpRequest iRequest, final OHttpResponse iResponse, final String[] urlParts) throws InterruptedException, IOException {
    ODatabaseDocumentInternal db = null;
    try {
        if (urlParts.length > 2) {
            db = server.getDatabasePoolFactory().get(urlParts[1], urlParts[2], urlParts[3]).acquire();
        } else
            db = getProfiledDatabaseInstance(iRequest);
        final StringWriter buffer = new StringWriter();
        final OJSONWriter json = new OJSONWriter(buffer);
        json.beginObject();
        json.beginObject("server");
        json.writeAttribute("version", OConstants.ORIENT_VERSION);
        if (OConstants.getBuildNumber() != null)
            json.writeAttribute("build", OConstants.getBuildNumber());
        json.writeAttribute("osName", System.getProperty("os.name"));
        json.writeAttribute("osVersion", System.getProperty("os.version"));
        json.writeAttribute("osArch", System.getProperty("os.arch"));
        json.writeAttribute("javaVendor", System.getProperty("java.vm.vendor"));
        json.writeAttribute("javaVersion", System.getProperty("java.vm.version"));
        json.endObject();
        if (((OMetadataInternal) db.getMetadata()).getImmutableSchemaSnapshot().getClasses() != null) {
            json.beginCollection("classes");
            List<String> classNames = new ArrayList<String>();
            for (OClass cls : db.getMetadata().getSchema().getClasses()) classNames.add(cls.getName());
            Collections.sort(classNames);
            for (String className : classNames) {
                final OClass cls = db.getMetadata().getSchema().getClass(className);
                try {
                    exportClass(db, json, cls);
                } catch (Exception e) {
                    OLogManager.instance().error(this, "Error on exporting class '" + cls + "'", e);
                }
            }
            json.endCollection();
        }
        if (db.getClusterNames() != null) {
            json.beginCollection("clusters");
            OCluster cluster;
            for (String clusterName : db.getClusterNames()) {
                try {
                    cluster = db.getStorage().getClusterById(db.getClusterIdByName(clusterName));
                } catch (IllegalArgumentException e) {
                    OLogManager.instance().error(this, "Cluster '%s' does not exist in database", clusterName);
                    continue;
                }
                try {
                    final String conflictStrategy = cluster.getRecordConflictStrategy() != null ? cluster.getRecordConflictStrategy().getName() : null;
                    json.beginObject();
                    json.writeAttribute("id", cluster.getId());
                    json.writeAttribute("name", clusterName);
                    json.writeAttribute("records", cluster.getEntries() - cluster.getTombstonesCount());
                    json.writeAttribute("conflictStrategy", conflictStrategy);
                    json.writeAttribute("size", "-");
                    json.writeAttribute("filled", "-");
                    json.writeAttribute("maxSize", "-");
                    json.writeAttribute("files", "-");
                } catch (Exception e) {
                    json.writeAttribute("records", "? (Unauthorized)");
                }
                json.endObject();
            }
            json.endCollection();
        }
        if (db.getUser() != null) {
            json.writeAttribute("currentUser", db.getUser().getName());
        // exportSecurityInfo(db, json);
        }
        final OIndexManagerProxy idxManager = db.getMetadata().getIndexManager();
        json.beginCollection("indexes");
        for (OIndex<?> index : idxManager.getIndexes()) {
            json.beginObject();
            try {
                json.writeAttribute("name", index.getName());
                json.writeAttribute("configuration", index.getConfiguration());
            // Exclude index size because it's too costly
            // json.writeAttribute("size", index.getSize());
            } catch (Exception e) {
                OLogManager.instance().error(this, "Cannot serialize index configuration", e);
            }
            json.endObject();
        }
        json.endCollection();
        json.beginObject("config");
        json.beginCollection("values");
        json.writeObjects(null, new Object[] { "name", "dateFormat", "value", db.getStorage().getConfiguration().dateFormat }, new Object[] { "name", "dateTimeFormat", "value", db.getStorage().getConfiguration().dateTimeFormat }, new Object[] { "name", "localeCountry", "value", db.getStorage().getConfiguration().getLocaleCountry() }, new Object[] { "name", "localeLanguage", "value", db.getStorage().getConfiguration().getLocaleLanguage() }, new Object[] { "name", "charSet", "value", db.getStorage().getConfiguration().getCharset() }, new Object[] { "name", "timezone", "value", db.getStorage().getConfiguration().getTimeZone().getID() }, new Object[] { "name", "definitionVersion", "value", db.getStorage().getConfiguration().version }, new Object[] { "name", "clusterSelection", "value", db.getStorage().getConfiguration().getClusterSelection() }, new Object[] { "name", "minimumClusters", "value", db.getStorage().getConfiguration().getMinimumClusters() }, new Object[] { "name", "conflictStrategy", "value", db.getStorage().getConfiguration().getConflictStrategy() });
        json.endCollection();
        json.beginCollection("properties");
        if (db.getStorage().getConfiguration().getProperties() != null)
            for (OStorageEntryConfiguration entry : db.getStorage().getConfiguration().getProperties()) {
                if (entry != null) {
                    json.beginObject();
                    json.writeAttribute("name", entry.name);
                    json.writeAttribute("value", entry.value);
                    json.endObject();
                }
            }
        json.endCollection();
        json.endObject();
        json.endObject();
        json.flush();
        iResponse.send(OHttpUtils.STATUS_OK_CODE, "OK", OHttpUtils.CONTENT_JSON, buffer.toString(), null);
    } finally {
        if (db != null)
            db.close();
    }
}
Also used : OJSONWriter(com.orientechnologies.orient.core.serialization.serializer.OJSONWriter) OMetadataInternal(com.orientechnologies.orient.core.metadata.OMetadataInternal) ArrayList(java.util.ArrayList) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) IOException(java.io.IOException) OSecurityAccessException(com.orientechnologies.orient.core.exception.OSecurityAccessException) OIndexManagerProxy(com.orientechnologies.orient.core.index.OIndexManagerProxy) StringWriter(java.io.StringWriter) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OCluster(com.orientechnologies.orient.core.storage.OCluster) OStorageEntryConfiguration(com.orientechnologies.orient.core.config.OStorageEntryConfiguration)

Example 22 with OMetadataInternal

use of com.orientechnologies.orient.core.metadata.OMetadataInternal in project orientdb by orientechnologies.

the class OServerCommandGetDocumentByClass method execute.

@Override
public boolean execute(final OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
    ODatabaseDocument db = null;
    final String[] urlParts = checkSyntax(iRequest.url, 4, "Syntax error: documentbyclass/<database>/<class-name>/<record-position>[/fetchPlan]");
    final String fetchPlan = urlParts.length > 4 ? urlParts[4] : null;
    iRequest.data.commandInfo = "Load document";
    final ORecord rec;
    try {
        db = getProfiledDatabaseInstance(iRequest);
        if (((OMetadataInternal) db.getMetadata()).getImmutableSchemaSnapshot().getClass(urlParts[2]) == null) {
            throw new IllegalArgumentException("Invalid class '" + urlParts[2] + "'");
        }
        final String rid = db.getClusterIdByName(urlParts[2]) + ":" + urlParts[3];
        rec = db.load(new ORecordId(rid), fetchPlan);
        if (rec == null)
            iResponse.send(OHttpUtils.STATUS_NOTFOUND_CODE, "Not Found", OHttpUtils.CONTENT_JSON, "Record with id '" + rid + "' was not found.", null);
        else if (iRequest.httpMethod.equals("HEAD"))
            // JUST SEND HTTP CODE 200
            iResponse.send(OHttpUtils.STATUS_OK_CODE, OHttpUtils.STATUS_OK_DESCRIPTION, null, null, null);
        else
            // SEND THE DOCUMENT BACK
            iResponse.writeRecord(rec, fetchPlan, null);
    } finally {
        if (db != null)
            db.close();
    }
    return false;
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ORecord(com.orientechnologies.orient.core.record.ORecord) OMetadataInternal(com.orientechnologies.orient.core.metadata.OMetadataInternal) ORecordId(com.orientechnologies.orient.core.id.ORecordId)

Aggregations

OMetadataInternal (com.orientechnologies.orient.core.metadata.OMetadataInternal)22 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)8 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)6 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)6 OCommandRequestText (com.orientechnologies.orient.core.command.OCommandRequestText)4 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)4 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)3 ORecordId (com.orientechnologies.orient.core.id.ORecordId)3 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)2 OQueryParsingException (com.orientechnologies.orient.core.exception.OQueryParsingException)2 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)2 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)2 ORecord (com.orientechnologies.orient.core.record.ORecord)2 OCommandSQLParsingException (com.orientechnologies.orient.core.sql.OCommandSQLParsingException)2 OCluster (com.orientechnologies.orient.core.storage.OCluster)2 OMetadataObject (com.orientechnologies.orient.object.metadata.OMetadataObject)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 OException (com.orientechnologies.common.exception.OException)1 OSystemException (com.orientechnologies.common.exception.OSystemException)1