Skip to content
This repository has been archived by the owner on Jan 5, 2022. It is now read-only.

Commit

Permalink
1) PUT on /<org-name>/<app-id> will not restore a deleted app and 2) …
Browse files Browse the repository at this point in the history
…/applications?deleted=true will return list of deleted applications.
  • Loading branch information
Dave Johnson committed Feb 21, 2015
1 parent fe8f404 commit f004f5a
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 106 deletions.
Expand Up @@ -22,15 +22,7 @@
import com.yammer.metrics.annotation.Metered;
import static java.lang.String.CASE_INSENSITIVE_ORDER;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -59,6 +51,7 @@
import org.apache.usergrid.persistence.entities.Application;
import org.apache.usergrid.persistence.exceptions.ApplicationAlreadyExistsException;
import org.apache.usergrid.persistence.exceptions.DuplicateUniquePropertyExistsException;
import org.apache.usergrid.persistence.exceptions.EntityNotFoundException;
import org.apache.usergrid.persistence.exceptions.OrganizationAlreadyExistsException;
import org.apache.usergrid.persistence.graph.Edge;
import org.apache.usergrid.persistence.graph.GraphManager;
Expand Down Expand Up @@ -317,6 +310,8 @@ public UUID initializeApplication( String organizationName, UUID applicationId,
@Override
public void deleteApplication(UUID applicationId) throws Exception {

//throw new UnsupportedOperationException("Delete application not supported");

// remove old appinfo Entity, which is in the System App's appinfos collection
EntityManager em = getEntityManager(CpNamingUtils.SYSTEM_APP_ID);
Query q = Query.fromQL(String.format("select * where applicationUuid = '%s'", applicationId.toString()));
Expand All @@ -336,6 +331,43 @@ public void deleteApplication(UUID applicationId) throws Exception {
}


@Override
public void restoreApplication(UUID applicationId) throws Exception {

// remove old delete_appinfos Entity
EntityManager em = getEntityManager(CpNamingUtils.SYSTEM_APP_ID);
Query q = Query.fromQL(String.format("select * where applicationUuid = '%s'", applicationId.toString()));
Results results = em.searchCollection( em.getApplicationRef(), "deleted_appinfos", q);
Entity appToRestore = results.getEntity();

if ( appToRestore == null ) {
throw new EntityNotFoundException("Cannot restore. Deleted Application not found: " + applicationId );
}

em.delete( appToRestore );

// restore entity in appinfo collection
Map<String, Object> appProps = appToRestore.getProperties();
appProps.remove("uuid");
appProps.put("type", "appinfo");
Entity restoredApp = em.create("appinfo", appToRestore.getProperties());

em.refreshIndex();

// rebuild the apps index
this.rebuildApplicationIndexes(applicationId, new ProgressObserver() {
@Override
public void onProgress(EntityRef entity) {
logger.debug("Restored entity {}:{}", entity.getType(), entity.getUuid());
}
@Override
public long getWriteDelayTime() {
return 0;
}
});
}


@Override
public UUID importApplication(
String organization, UUID applicationId,
Expand Down Expand Up @@ -380,31 +412,56 @@ public UUID lookupOrganization( String name ) throws Exception {
public UUID lookupApplication( String name ) throws Exception {
init();

EntityManager em = getEntityManager( CpNamingUtils.SYSTEM_APP_ID );
// TODO: why does this not work for restored apps

// EntityManager em = getEntityManager( CpNamingUtils.SYSTEM_APP_ID );
// final EntityRef alias = em.getAlias( CpNamingUtils.APPINFOS, name );
// if ( alias == null ) {
// return null;
// }
// final Entity entity = em.get( alias );
// if ( entity == null ) {
// return null;
// }
// final UUID property = ( UUID ) entity.getProperty( "applicationUuid" );
// return property;

Query q = Query.fromQL( PROPERTY_NAME + " = '" + name + "'");

final EntityRef alias = em.getAlias( CpNamingUtils.APPINFOS, name );
EntityManager em = getEntityManager(CpNamingUtils.SYSTEM_APP_ID);

if ( alias == null ) {
Results results = em.searchCollection( em.getApplicationRef(), "appinfos", q);

if ( results.isEmpty() ) {
return null;
}

final Entity entity = em.get( alias );

if ( entity == null ) {
return null;
Entity entity = results.iterator().next();
Object uuidObject = entity.getProperty("applicationUuid");
if ( uuidObject instanceof UUID ) {
return (UUID)uuidObject;
}
return UUIDUtils.tryExtractUUID( entity.getProperty("applicationUuid").toString() );
}


final UUID property = ( UUID ) entity.getProperty( "applicationUuid" );
@Override
@Metered(group = "core", name = "EntityManagerFactory_getApplication")
public Map<String, UUID> getApplications() throws Exception {
return getApplications( false );
}

return property;

@Override
@Metered(group = "core", name = "EntityManagerFactory_getApplication")
public Map<String, UUID> getDeletedApplications() throws Exception {
return getApplications( true );
}


@Override
@Metered(group = "core", name = "EntityManagerFactory_getApplication")
public Map<String, UUID> getApplications() throws Exception {
public Map<String, UUID> getApplications(boolean deleted) throws Exception {

Map<String, UUID> appMap = new HashMap<String, UUID>();

Expand All @@ -415,7 +472,15 @@ public Map<String, UUID> getApplications() throws Exception {
Application app = em.getApplication();
Id fromEntityId = new SimpleId( app.getUuid(), app.getType() );

String edgeType = CpNamingUtils.getEdgeTypeFromCollectionName( CpNamingUtils.APPINFOS );
final String scopeName;
final String edgeType;
if ( deleted ) {
edgeType = CpNamingUtils.getEdgeTypeFromCollectionName(CpNamingUtils.DELETED_APPINFOS);
scopeName = CpNamingUtils.getCollectionScopeNameFromCollectionName(CpNamingUtils.DELETED_APPINFOS);
} else {
edgeType = CpNamingUtils.getEdgeTypeFromCollectionName(CpNamingUtils.APPINFOS);
scopeName = CpNamingUtils.getCollectionScopeNameFromCollectionName(CpNamingUtils.APPINFOS);
}

logger.debug("getApplications(): Loading edges of edgeType {} from {}:{}",
new Object[] { edgeType, fromEntityId.getType(), fromEntityId.getUuid() } );
Expand All @@ -436,9 +501,9 @@ public Map<String, UUID> getApplications() throws Exception {
});

CollectionScope collScope = new CollectionScopeImpl(
appScope.getApplication(),
appScope.getApplication(),
CpNamingUtils.getCollectionScopeNameFromCollectionName( CpNamingUtils.APPINFOS ));
appScope.getApplication(),
appScope.getApplication(),
scopeName);

org.apache.usergrid.persistence.model.entity.Entity e =
managerCache.getEntityCollectionManager( collScope ).load( targetId )
Expand Down Expand Up @@ -779,4 +844,5 @@ public Health getEntityStoreHealth() {

return ecm.getHealth();
}

}
Expand Up @@ -66,6 +66,9 @@ public class CpNamingUtils {
* The app infos entity object type. This holds the app name, appId, and org name
*/
public static final String APPINFOS = "appinfos";

public static final String DELETED_APPINFOS = "deleted_appinfos";

/**
* The name of the map that holds our entity id->type mapping
*/
Expand Down
Expand Up @@ -19,6 +19,8 @@

import java.util.Map;
import java.util.UUID;

import com.yammer.metrics.annotation.Metered;
import org.apache.usergrid.persistence.core.util.Health;
import org.apache.usergrid.persistence.index.EntityIndex;
import org.springframework.context.ApplicationContext;
Expand Down Expand Up @@ -95,8 +97,11 @@ public abstract UUID importApplication( String organization, UUID applicationId,
*
* @throws Exception the exception
*/
@Metered(group = "core", name = "EntityManagerFactory_getApplication")
public abstract Map<String, UUID> getApplications() throws Exception;

public Map<String, UUID> getDeletedApplications() throws Exception;

public abstract void setup() throws Exception;

public abstract Map<String, String> getServiceProperties();
Expand Down Expand Up @@ -165,6 +170,8 @@ void rebuildCollectionIndex(

public Health getEntityStoreHealth();

void restoreApplication(UUID applicationId) throws Exception;

public interface ProgressObserver {

public void onProgress( EntityRef entity);
Expand Down
Expand Up @@ -330,8 +330,7 @@ public Map<String, UUID> getApplications() throws Exception {
return applications;
}


@Override
@Override
public boolean setServiceProperty( String name, String value ) {
try {
cass.setColumn( cass.getSystemKeyspace(), PROPERTIES_CF, PROPERTIES_CF, name, value );
Expand Down Expand Up @@ -473,6 +472,22 @@ public void addIndex(UUID appId, String suffix,final int shards,final int replic

@Override
public Health getEntityStoreHealth() {
throw new UnsupportedOperationException("Not supported yet.");
throw new UnsupportedOperationException("Not supported in v1.");
}

@Override
public void restoreApplication(UUID applicationId) throws Exception {
throw new UnsupportedOperationException("Not supported in v1");
}

@Override
public Map<String, UUID> getDeletedApplications() throws Exception {
throw new UnsupportedOperationException("Not supported in v1");
}

@Override
public Map<String, UUID> getApplications(boolean deleted) throws Exception {
throw new UnsupportedOperationException("Not supported in v1");
}

}
Expand Up @@ -218,8 +218,12 @@ public long getWriteDelayTime() {

try {

// do it forwards
setup.getEmf().rebuildCollectionIndex( em.getApplicationId(), "catherders", false, po );

// and backwards, just to make sure both cases are covered
setup.getEmf().rebuildCollectionIndex( em.getApplicationId(), "catherders", true, po );

reporter.report();
registry.remove( meterName );
logger.info("Rebuilt index");
Expand Down Expand Up @@ -312,9 +316,13 @@ public void rebuildIndex() throws Exception {
// ----------------- delete the system and application indexes

logger.debug("Deleting app index and system app index");
//deleteIndex( CpNamingUtils.SYSTEM_APP_ID );

deleteIndex( em.getApplicationId() );

// deleting sytem app index will interfere with other concurrently running tests
//deleteIndex( CpNamingUtils.SYSTEM_APP_ID );


// ----------------- test that we can read them, should fail

logger.debug("Reading data, should fail this time ");
Expand All @@ -335,7 +343,7 @@ public void rebuildIndex() throws Exception {
int counter = 0;

@Override
public void onProgress( final EntityRef entity ) {
public void onProgress( final EntityRef entity ) {

meter.mark();
logger.debug("Indexing {}:{}", entity.getType(), entity.getUuid());
Expand All @@ -345,8 +353,6 @@ public void onProgress( final EntityRef entity ) {
counter++;
}



@Override
public long getWriteDelayTime() {
return 0;
Expand All @@ -355,6 +361,8 @@ public long getWriteDelayTime() {

try {

setup.getEmf().rebuildInternalIndexes( po );

setup.getEmf().rebuildApplicationIndexes( em.getApplicationId(), po );

reporter.report();
Expand Down

0 comments on commit f004f5a

Please sign in to comment.