Search in sources :

Example 6 with OClassImpl

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

the class OCommandExecutorSQLAlterProperty method execute.

/**
   * Execute the ALTER PROPERTY.
   */
public Object execute(final Map<Object, Object> iArgs) {
    if (attribute == null)
        throw new OCommandExecutionException("Cannot execute the command because it has not yet been parsed");
    final OClassImpl sourceClass = (OClassImpl) getDatabase().getMetadata().getSchema().getClass(className);
    if (sourceClass == null)
        throw new OCommandExecutionException("Source class '" + className + "' not found");
    final OPropertyImpl prop = (OPropertyImpl) sourceClass.getProperty(fieldName);
    if (prop == null)
        throw new OCommandExecutionException("Property '" + className + "." + fieldName + "' not exists");
    if ("null".equalsIgnoreCase(value))
        prop.set(attribute, null);
    else
        prop.set(attribute, value);
    return null;
}
Also used : OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OClassImpl(com.orientechnologies.orient.core.metadata.schema.OClassImpl) OPropertyImpl(com.orientechnologies.orient.core.metadata.schema.OPropertyImpl)

Example 7 with OClassImpl

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

the class OLocalClusterWrapperStrategy method readConfiguration.

protected ODistributedConfiguration readConfiguration() {
    if (cls.isAbstract())
        throw new IllegalArgumentException("Cannot create a new instance of abstract class");
    final ODatabaseDocument db = ODatabaseRecordThreadLocal.INSTANCE.get();
    final int[] clusterIds = cls.getClusterIds();
    final List<String> clusterNames = new ArrayList<String>(clusterIds.length);
    for (int c : clusterIds) clusterNames.add(db.getClusterNameById(c).toLowerCase());
    ODistributedConfiguration cfg = manager.getDatabaseConfiguration(databaseName);
    List<String> bestClusters = cfg.getOwnedClustersByServer(clusterNames, nodeName);
    if (bestClusters.isEmpty()) {
        // REBALANCE THE CLUSTERS
        manager.reassignClustersOwnership(nodeName, databaseName, cfg.modify());
        cfg = manager.getDatabaseConfiguration(databaseName);
        bestClusters = cfg.getOwnedClustersByServer(clusterNames, nodeName);
        if (bestClusters.isEmpty()) {
            // FILL THE MAP CLUSTER/SERVERS
            final StringBuilder buffer = new StringBuilder();
            for (String c : clusterNames) {
                if (buffer.length() > 0)
                    buffer.append(" ");
                buffer.append(" ");
                buffer.append(c);
                buffer.append(":");
                buffer.append(cfg.getServers(c, null));
            }
            ODistributedServerLog.warn(this, manager.getLocalNodeName(), null, ODistributedServerLog.DIRECTION.NONE, "Cannot find best cluster for class '%s'. Configured servers for clusters %s are %s (dCfgVersion=%d)", cls.getName(), clusterNames, buffer.toString(), cfg.getVersion());
            throw new ODatabaseException("Cannot find best cluster for class '" + cls.getName() + "' on server '" + nodeName + "'. ClusterStrategy=" + getName() + " dCfgVersion=" + cfg.getVersion());
        }
    }
    db.activateOnCurrentThread();
    final int[] newBestClusters = new int[bestClusters.size()];
    int i = 0;
    for (String c : bestClusters) newBestClusters[i++] = db.getClusterIdByName(c);
    this.localScopedClass = new OLocalScopedClass((OClassImpl) cls, newBestClusters);
    final ODistributedStorage storage = (ODistributedStorage) manager.getStorage(databaseName);
    lastVersion = storage.getConfigurationUpdated();
    return cfg;
}
Also used : ArrayList(java.util.ArrayList) OClassImpl(com.orientechnologies.orient.core.metadata.schema.OClassImpl) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ODistributedConfiguration(com.orientechnologies.orient.server.distributed.ODistributedConfiguration)

Example 8 with OClassImpl

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

the class OServerCommandGetDatabase method exportClass.

public static void exportClass(final ODatabaseDocument db, final OJSONWriter json, final OClass cls) throws IOException {
    json.beginObject();
    json.writeAttribute("name", cls.getName());
    json.writeAttribute("superClass", cls.getSuperClass() != null ? cls.getSuperClass().getName() : "");
    json.beginCollection("superClasses");
    int i = 0;
    for (OClass oClass : cls.getSuperClasses()) {
        json.write((i > 0 ? "," : "") + "\"" + oClass.getName() + "\"");
        i++;
    }
    json.endCollection();
    json.writeAttribute("alias", cls.getShortName());
    json.writeAttribute("abstract", cls.isAbstract());
    json.writeAttribute("strictmode", cls.isStrictMode());
    json.writeAttribute("clusters", cls.getClusterIds());
    json.writeAttribute("defaultCluster", cls.getDefaultClusterId());
    json.writeAttribute("clusterSelection", cls.getClusterSelection().getName());
    if (cls instanceof OClassImpl) {
        final Map<String, String> custom = ((OClassImpl) cls).getCustomInternal();
        if (custom != null && !custom.isEmpty()) {
            json.writeAttribute("custom", custom);
        }
    }
    try {
        json.writeAttribute("records", db.countClass(cls.getName()));
    } catch (OSecurityAccessException e) {
        json.writeAttribute("records", "? (Unauthorized)");
    } catch (Exception e) {
        json.writeAttribute("records", "? (Error)");
    }
    if (cls.properties() != null && cls.properties().size() > 0) {
        json.beginCollection("properties");
        for (final OProperty prop : cls.properties()) {
            json.beginObject();
            json.writeAttribute("name", prop.getName());
            if (prop.getLinkedClass() != null)
                json.writeAttribute("linkedClass", prop.getLinkedClass().getName());
            if (prop.getLinkedType() != null)
                json.writeAttribute("linkedType", prop.getLinkedType().toString());
            json.writeAttribute("type", prop.getType().toString());
            json.writeAttribute("mandatory", prop.isMandatory());
            json.writeAttribute("readonly", prop.isReadonly());
            json.writeAttribute("notNull", prop.isNotNull());
            json.writeAttribute("min", prop.getMin());
            json.writeAttribute("max", prop.getMax());
            json.writeAttribute("regexp", prop.getRegexp());
            json.writeAttribute("collate", prop.getCollate() != null ? prop.getCollate().getName() : "default");
            json.writeAttribute("defaultValue", prop.getDefaultValue());
            if (prop instanceof OPropertyImpl) {
                final Map<String, String> custom = ((OPropertyImpl) prop).getCustomInternal();
                if (custom != null && !custom.isEmpty()) {
                    json.writeAttribute("custom", custom);
                }
            }
            json.endObject();
        }
        json.endCollection();
    }
    final Set<OIndex<?>> indexes = cls.getIndexes();
    if (!indexes.isEmpty()) {
        json.beginCollection("indexes");
        for (final OIndex<?> index : indexes) {
            json.beginObject();
            json.writeAttribute("name", index.getName());
            json.writeAttribute("type", index.getType());
            final OIndexDefinition indexDefinition = index.getDefinition();
            if (indexDefinition != null && !indexDefinition.getFields().isEmpty())
                json.writeAttribute("fields", indexDefinition.getFields());
            json.endObject();
        }
        json.endCollection();
    }
    json.endObject();
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OIndexDefinition(com.orientechnologies.orient.core.index.OIndexDefinition) OIndex(com.orientechnologies.orient.core.index.OIndex) OClassImpl(com.orientechnologies.orient.core.metadata.schema.OClassImpl) OPropertyImpl(com.orientechnologies.orient.core.metadata.schema.OPropertyImpl) IOException(java.io.IOException) OSecurityAccessException(com.orientechnologies.orient.core.exception.OSecurityAccessException) OSecurityAccessException(com.orientechnologies.orient.core.exception.OSecurityAccessException) OClass(com.orientechnologies.orient.core.metadata.schema.OClass)

Aggregations

OClassImpl (com.orientechnologies.orient.core.metadata.schema.OClassImpl)8 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)5 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)4 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)3 OPropertyImpl (com.orientechnologies.orient.core.metadata.schema.OPropertyImpl)3 OIndex (com.orientechnologies.orient.core.index.OIndex)2 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)1 OSecurityAccessException (com.orientechnologies.orient.core.exception.OSecurityAccessException)1 OIndexDefinition (com.orientechnologies.orient.core.index.OIndexDefinition)1 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)1 OType (com.orientechnologies.orient.core.metadata.schema.OType)1 ODistributedConfiguration (com.orientechnologies.orient.server.distributed.ODistributedConfiguration)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1