private void deleteDefaultCluster(final OClass clazz) { final ODatabaseDocumentInternal database = getDatabase(); final int clusterId = clazz.getDefaultClusterId(); final OCluster cluster = database.getStorage().getClusterById(clusterId); if (cluster.getName().equalsIgnoreCase(clazz.getName())) database.getStorage().dropCluster(clusterId, true); // FREE THE RECORD CACHE getDatabase().getLocalCache().freeCluster(clusterId); }
/* * (non-Javadoc) * * @see com.orientechnologies.orient.core.metadata.schema.OSchema#dropClass(java.lang.String) */ public void dropClass(final String className) { final ODatabaseDocumentInternal db = getDatabase(); final OStorage storage = db.getStorage(); final StringBuilder cmd; acquireSchemaWriteLock(); try { if (getDatabase().getTransaction().isActive()) throw new IllegalStateException("Cannot drop a class inside a transaction"); if (className == null) throw new IllegalArgumentException("Class name is null"); getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_DELETE); final String key = className.toLowerCase(); OClass cls = classes.get(key); if (cls == null) throw new OSchemaException("Class '" + className + "' was not found in current database"); if (!cls.getSubclasses().isEmpty()) throw new OSchemaException( "Class '" + className + "' cannot be dropped because it has sub classes. Remove the dependencies before trying to drop it again"); cmd = new StringBuilder("drop class "); cmd.append(className); cmd.append(" unsafe"); if (isDistributedCommand()) { final OAutoshardedStorage autoshardedStorage = (OAutoshardedStorage) storage; OCommandSQL commandSQL = new OCommandSQL(cmd.toString()); commandSQL.addExcludedNode(autoshardedStorage.getNodeId()); db.command(commandSQL).execute(); dropClassInternal(className); } else if (storage instanceof OStorageProxy) { final OCommandSQL commandSQL = new OCommandSQL(cmd.toString()); db.command(commandSQL).execute(); reload(); } else dropClassInternal(className); // FREE THE RECORD CACHE getDatabase().getLocalCache().freeCluster(cls.getDefaultClusterId()); } finally { releaseSchemaWriteLock(); } }
@Override public int hashCode() { final int prime = 31; int result = super.hashCode(); result = prime * result + ((owner == null) ? 0 : owner.hashCode()); return result; }
private void dropClassIndexes(final OClass cls) { final ODatabaseDocument database = getDatabase(); final OIndexManager indexManager = database.getMetadata().getIndexManager(); for (final OIndex<?> index : indexManager.getClassIndexes(cls.getName())) indexManager.dropIndex(index.getName()); }
@Override public Set<OClass> getClassesRelyOnCluster(final String clusterName) { getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_READ); acquireSchemaReadLock(); try { final int clusterId = getDatabase().getClusterIdByName(clusterName); final Set<OClass> result = new HashSet<OClass>(); for (OClass c : classes.values()) { if (OArrays.contains(c.getPolymorphicClusterIds(), clusterId)) result.add(c); } return result; } finally { releaseSchemaReadLock(); } }
private void removeClusterClassMap(final OClass cls) { if (!clustersCanNotBeSharedAmongClasses) return; for (int clusterId : cls.getClusterIds()) { if (clusterId < 0) continue; clustersToClasses.remove(clusterId); } }
private void dropClassInternal(final String className) { acquireSchemaWriteLock(); try { if (getDatabase().getTransaction().isActive()) throw new IllegalStateException("Cannot drop a class inside a transaction"); if (className == null) throw new IllegalArgumentException("Class name is null"); getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_DELETE); final String key = className.toLowerCase(); final OClass cls = classes.get(key); if (cls == null) throw new OSchemaException("Class '" + className + "' was not found in current database"); if (!cls.getSubclasses().isEmpty()) throw new OSchemaException( "Class '" + className + "' cannot be dropped because it has sub classes. Remove the dependencies before trying to drop it again"); checkEmbedded(getDatabase().getStorage()); for (OClass superClass : cls.getSuperClasses()) { // REMOVE DEPENDENCY FROM SUPERCLASS ((OClassImpl) superClass).removeBaseClassInternal(cls); } deleteDefaultCluster(cls); dropClassIndexes(cls); classes.remove(key); if (cls.getShortName() != null) // REMOVE THE ALIAS TOO classes.remove(cls.getShortName().toLowerCase()); removeClusterClassMap(cls); } finally { releaseSchemaWriteLock(); } }
@Override public void merge(ImmutableOntologyVocabulary v) { if (v instanceof OntologyVocabularyImpl) { OntologyVocabularyImpl vi = (OntologyVocabularyImpl) v; concepts.putAll(vi.concepts); objectProperties.putAll(vi.objectProperties); dataProperties.putAll(vi.dataProperties); annotationProperties.putAll(vi.annotationProperties); } else { for (OClass oc : v.getClasses()) if (!oc.isTop() && !oc.isBottom()) concepts.put(oc.getName(), oc); for (ObjectPropertyExpression ope : v.getObjectProperties()) if (!ope.isTop() && !ope.isBottom()) objectProperties.put(ope.getName(), ope); for (DataPropertyExpression dpe : v.getDataProperties()) if (!dpe.isTop() && !dpe.isBottom()) dataProperties.put(dpe.getName(), dpe); for (AnnotationProperty ap : v.getAnnotationProperties()) annotationProperties.put(ap.getName(), ap); } }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (!super.equals(obj)) return false; if (getClass() != obj.getClass()) return false; OProperty other = (OProperty) obj; if (owner == null) { if (other.owner != null) return false; } else if (!owner.equals(other.owner)) return false; return true; }
void checkClusterCanBeAdded(int clusterId, OClass cls) { acquireSchemaReadLock(); try { if (!clustersCanNotBeSharedAmongClasses) return; if (clusterId < 0) return; final OClass existingCls = clustersToClasses.get(clusterId); if (existingCls != null && !cls.equals(existingCls)) throw new OSchemaException( "Cluster with id " + clusterId + " already belongs to class " + clustersToClasses.get(clusterId)); } finally { releaseSchemaReadLock(); } }
@OBeforeSerialization public ODocument toStream() { document.field("name", name); document.field("type", type.id); document.field("offset", offset); document.field("mandatory", mandatory); document.field("notNull", notNull); document.field("min", min); document.field("max", max); document.field("linkedClass", linkedClass != null ? linkedClass.getName() : null); document.field("linkedType", linkedType != null ? linkedType.id : null); // SAVE THE INDEX if (index != null) { index.lazySave(); document.field("index", index.getRID()); document.field("index-type", index.getType()); } else { document.field("index", ORecordId.EMPTY_RECORD_ID); } return document; }
void addClusterForClass(final int clusterId, final OClass cls) { acquireSchemaWriteLock(); try { if (!clustersCanNotBeSharedAmongClasses) return; if (clusterId < 0) return; final OStorage storage = getDatabase().getStorage(); checkEmbedded(storage); final OClass existingCls = clustersToClasses.get(clusterId); if (existingCls != null && !cls.equals(existingCls)) throw new OSchemaException( "Cluster with id " + clusterId + " already belongs to class " + clustersToClasses.get(clusterId)); clustersToClasses.put(clusterId, cls); } finally { releaseSchemaWriteLock(); } }
public OProperty setDirty() { document.setDirty(); if (owner != null) owner.setDirty(); return this; }
private OClass doCreateClass( final String className, final int[] clusterIds, int retry, OClass... superClasses) throws ClusterIdsAreEmptyException { OClass result; final ODatabaseDocumentInternal db = getDatabase(); final OStorage storage = db.getStorage(); StringBuilder cmd = null; getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_CREATE); acquireSchemaWriteLock(); try { final String key = className.toLowerCase(); if (classes.containsKey(key) && retry == 0) throw new OSchemaException("Class " + className + " already exists in current database"); if (!isDistributedCommand()) checkClustersAreAbsent(clusterIds); cmd = new StringBuilder("create class "); // if (getDatabase().getStorage().getConfiguration().isStrictSql()) // cmd.append('`'); cmd.append(className); // if (getDatabase().getStorage().getConfiguration().isStrictSql()) // cmd.append('`'); List<OClass> superClassesList = new ArrayList<OClass>(); if (superClasses != null && superClasses.length > 0) { boolean first = true; for (OClass superClass : superClasses) { // Filtering for null if (superClass != null) { if (first) cmd.append(" extends "); else cmd.append(", "); cmd.append(superClass.getName()); first = false; superClassesList.add(superClass); } } } if (clusterIds != null) { if (clusterIds.length == 1 && clusterIds[0] == -1) cmd.append(" abstract"); else { cmd.append(" cluster "); for (int i = 0; i < clusterIds.length; ++i) { if (i > 0) cmd.append(','); else cmd.append(' '); cmd.append(clusterIds[i]); } } } if (isDistributedCommand()) { createClassInternal(className, clusterIds, superClassesList); final OAutoshardedStorage autoshardedStorage = (OAutoshardedStorage) storage; OCommandSQL commandSQL = new OCommandSQL(cmd.toString()); commandSQL.addExcludedNode(autoshardedStorage.getNodeId()); final Object res = db.command(commandSQL).execute(); } else if (storage instanceof OStorageProxy) { db.command(new OCommandSQL(cmd.toString())).execute(); reload(); } else createClassInternal(className, clusterIds, superClassesList); result = classes.get(className.toLowerCase()); // WAKE UP DB LIFECYCLE LISTENER for (Iterator<ODatabaseLifecycleListener> it = Orient.instance().getDbLifecycleListeners(); it.hasNext(); ) it.next().onCreateClass(getDatabase(), result); } finally { releaseSchemaWriteLock(); } return result; }
private OClass createClassInternal( final String className, final int[] clusterIdsToAdd, final List<OClass> superClasses) throws ClusterIdsAreEmptyException { acquireSchemaWriteLock(); try { if (className == null || className.length() == 0) throw new OSchemaException("Found class name null or empty"); if (Character.isDigit(className.charAt(0))) throw new OSchemaException("Found invalid class name. Cannot start with numbers"); final Character wrongCharacter = checkClassNameIfValid(className); if (wrongCharacter != null) throw new OSchemaException( "Found invalid class name. Character '" + wrongCharacter + "' cannot be used in class name."); final ODatabaseDocumentInternal database = getDatabase(); final OStorage storage = database.getStorage(); checkEmbedded(storage); checkClustersAreAbsent(clusterIdsToAdd); final int[] clusterIds; if (clusterIdsToAdd == null || clusterIdsToAdd.length == 0) { throw new ClusterIdsAreEmptyException(); } else clusterIds = clusterIdsToAdd; database.checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_CREATE); final String key = className.toLowerCase(); if (classes.containsKey(key)) throw new OSchemaException("Class " + className + " already exists in current database"); OClassImpl cls = new OClassImpl(this, className, clusterIds); classes.put(key, cls); if (superClasses != null && superClasses.size() > 0) { cls.setSuperClassesInternal(superClasses); for (OClass superClass : superClasses) { // UPDATE INDEXES final int[] clustersToIndex = superClass.getPolymorphicClusterIds(); final String[] clusterNames = new String[clustersToIndex.length]; for (int i = 0; i < clustersToIndex.length; i++) clusterNames[i] = database.getClusterNameById(clustersToIndex[i]); for (OIndex<?> index : superClass.getIndexes()) for (String clusterName : clusterNames) if (clusterName != null) database .getMetadata() .getIndexManager() .addClusterToIndex(clusterName, index.getName()); } } addClusterClassMap(cls); return cls; } finally { releaseSchemaWriteLock(); } }
public OProperty(OClass iOwner) { document = new ODocument(iOwner.getDocument().getDatabase()); owner = iOwner; id = iOwner.properties.size(); }
@Override public OClass createClass(String uri) { OClass cd = new ClassImpl(uri); if (!cd.isBottom() && !cd.isTop()) concepts.put(uri, cd); return cd; }