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()); }
public List<String> getIndexNames() { final ArrayList<String> names = new ArrayList<String>(indexChain.size()); for (OIndex<?> oIndex : indexChain) { names.add(oIndex.getName()); } return names; }
@Override protected void handleObject(OIndex<?> object) { indexName = object.getName(); OIndexDefinition indexDefinition = object.getDefinition(); if (indexDefinition != null) { String className = indexDefinition.getClassName(); if (className != null) classModel = new OClassModel(className); } }
/** Execute the REMOVE INDEX. */ public Object execute(final Map<Object, Object> iArgs) { if (name == null) throw new OCommandExecutionException( "Cannot execute the command because it has not been parsed yet"); if (name.equals("*")) { long totalIndexed = 0; for (OIndex<?> idx : getDatabase().getMetadata().getIndexManager().getIndexes()) { getDatabase().getMetadata().getIndexManager().dropIndex(idx.getName()); totalIndexed++; } return totalIndexed; } else getDatabase().getMetadata().getIndexManager().dropIndex(name); return 1; }
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 void testDrop() { Assert.assertTrue(cls.getClusterSelection() instanceof OAutoShardingClusterSelectionStrategy); database.getMetadata().getIndexManager().dropIndex(idx.getName()); cls = database.getMetadata().getSchema().getClass("AutoShardingTest"); Assert.assertFalse(cls.getClusterSelection() instanceof OAutoShardingClusterSelectionStrategy); }
public void commit() { checkTransaction(); status = TXSTATUS.COMMITTING; if (OScenarioThreadLocal.INSTANCE.get() != RUN_MODE.RUNNING_DISTRIBUTED && !(database.getStorage() instanceof OStorageEmbedded)) database.getStorage().commit(this, null); else { final List<String> involvedIndexes = getInvolvedIndexes(); if (involvedIndexes != null) Collections.sort(involvedIndexes); for (int retry = 1; retry <= autoRetries; ++retry) { try { // LOCK INVOLVED INDEXES List<OIndexAbstract<?>> lockedIndexes = null; try { if (involvedIndexes != null) for (String indexName : involvedIndexes) { final OIndexAbstract<?> index = (OIndexAbstract<?>) database.getMetadata().getIndexManager().getIndexInternal(indexName); if (lockedIndexes == null) lockedIndexes = new ArrayList<OIndexAbstract<?>>(); index.acquireModificationLock(); lockedIndexes.add(index); } if (!useSBTree) { // SEARCH FOR INDEX BASED ON DOCUMENT TOUCHED final Collection<? extends OIndex<?>> indexes = database.getMetadata().getIndexManager().getIndexes(); List<? extends OIndex<?>> indexesToLock = null; if (indexes != null) { indexesToLock = new ArrayList<OIndex<?>>(indexes); Collections.sort( indexesToLock, new Comparator<OIndex<?>>() { public int compare(final OIndex<?> indexOne, final OIndex<?> indexTwo) { return indexOne.getName().compareTo(indexTwo.getName()); } }); } if (indexesToLock != null && !indexesToLock.isEmpty()) { if (lockedIndexes == null) lockedIndexes = new ArrayList<OIndexAbstract<?>>(); for (OIndex<?> index : indexesToLock) { for (Entry<ORID, ORecordOperation> entry : recordEntries.entrySet()) { final ORecord<?> record = entry.getValue().record.getRecord(); if (record instanceof ODocument) { ODocument doc = (ODocument) record; if (!lockedIndexes.contains(index.getInternal()) && doc.getSchemaClass() != null && index.getDefinition() != null && doc.getSchemaClass() .isSubClassOf(index.getDefinition().getClassName())) { index.getInternal().acquireModificationLock(); lockedIndexes.add((OIndexAbstract<?>) index.getInternal()); } } } } for (OIndexAbstract<?> index : lockedIndexes) index.acquireExclusiveLock(); } } final Map<String, OIndex> indexes = new HashMap<String, OIndex>(); for (OIndex index : database.getMetadata().getIndexManager().getIndexes()) indexes.put(index.getName(), index); final Runnable callback = new Runnable() { @Override public void run() { final ODocument indexEntries = getIndexChanges(); if (indexEntries != null) { final Map<String, OIndexInternal<?>> indexesToCommit = new HashMap<String, OIndexInternal<?>>(); for (Entry<String, Object> indexEntry : indexEntries) { final OIndexInternal<?> index = indexes.get(indexEntry.getKey()).getInternal(); indexesToCommit.put(index.getName(), index.getInternal()); } for (OIndexInternal<?> indexInternal : indexesToCommit.values()) indexInternal.preCommit(); for (Entry<String, Object> indexEntry : indexEntries) { final OIndexInternal<?> index = indexesToCommit.get(indexEntry.getKey()).getInternal(); if (index == null) { OLogManager.instance() .error( this, "Index with name " + indexEntry.getKey() + " was not found."); throw new OIndexException( "Index with name " + indexEntry.getKey() + " was not found."); } else index.addTxOperation((ODocument) indexEntry.getValue()); } try { for (OIndexInternal<?> indexInternal : indexesToCommit.values()) indexInternal.commit(); } finally { for (OIndexInternal<?> indexInternal : indexesToCommit.values()) indexInternal.postCommit(); } } } }; final String storageType = database.getStorage().getType(); if (storageType.equals(OEngineLocal.NAME) || storageType.equals(OEngineLocalPaginated.NAME)) database.getStorage().commit(OTransactionOptimistic.this, callback); else { database .getStorage() .callInLock( new Callable<Object>() { @Override public Object call() throws Exception { database.getStorage().commit(OTransactionOptimistic.this, null); callback.run(); return null; } }, true); } // OK break; } finally { // RELEASE INDEX LOCKS IF ANY if (lockedIndexes != null) { if (!useSBTree) { for (OIndexAbstract<?> index : lockedIndexes) index.releaseExclusiveLock(); } for (OIndexAbstract<?> index : lockedIndexes) index.releaseModificationLock(); } } } catch (OTimeoutException e) { if (autoRetries == 0) { OLogManager.instance() .debug( this, "Caught timeout exception during commit, but no automatic retry has been set", e); throw e; } else if (retry == autoRetries) { OLogManager.instance() .debug( this, "Caught timeout exception during %d/%d. Retry limit is exceeded.", retry, autoRetries); throw e; } else { OLogManager.instance() .debug( this, "Caught timeout exception during commit retrying %d/%d...", retry, autoRetries); } } } } status = TXSTATUS.COMPLETED; }