private Map<String, Object> getAllPartitionIndexValues(EntityConfig config, Object entity) { Map<String, Object> partitionIndexMap = new HashMap<String, Object>(); for (EntityIndexConfig indexConfig : getPartitionIndexConfigs(config)) partitionIndexMap.put( indexConfig.getIndexName(), hive.directory() .getPrimaryIndexKeyOfResourceId( config.getResourceName(), // indexConfig.getIndexName(), config.getId(entity))); return partitionIndexMap; }
@SuppressWarnings("unchecked") private Map<String, Collection<Object>> getAllSecondaryIndexValues( EntityConfig config, Object entity) { Map<String, Collection<Object>> secondaryIndexMap = new HashMap<String, Collection<Object>>(); for (EntityIndexConfig indexConfig : getSecondaryIndexConfigs(config)) secondaryIndexMap.put( indexConfig.getIndexName(), hive.directory() .getSecondaryIndexKeysWithResourceId( config.getResourceName(), indexConfig.getIndexName(), config.getId(entity))); return secondaryIndexMap; }
public void insert(final EntityConfig config, final Object entity) throws HiveLockableException { try { conditionallyInsertPrimaryIndexKey(config, entity); hive.directory() .insertResourceId( config.getResourceName(), config.getId(entity), config.getPrimaryIndexKey(entity)); conditionallyInsertDelegatedResourceIndexes(config, entity); insertSecondaryIndexes(config, entity); } catch (RuntimeException e) { hive.directory().deleteResourceId(config.getResourceName(), config.getId(entity)); throw e; } }
private void updateIndexes(Object entity) { try { final Class<?> resolvedEntityClass = resolveEntityClass(entity.getClass()); if (resolvedEntityClass != null) { final EntityConfig entityConfig = hiveConfig.getEntityConfig(entity.getClass()); if (indexer.idExists(entityConfig, entityConfig.getId(entity))) indexer.updatePartitionDimensionIndexIfNeeded( hiveConfig.getEntityConfig(resolvedEntityClass), entity); indexer.update(entityConfig, entity); } } catch (HiveLockableException e) { throw new CallbackException(e); } }
private void conditionallyInsertDelegatedResourceIndexes(EntityConfig config, Object entity) throws HiveLockableException { for (EntityIndexConfig entityIndexConfig : config.getEntityIndexConfigs()) if (entityIndexConfig.getIndexType().equals(IndexType.Delegates)) { final EntityIndexConfigDelegator delegateEntityConfig = ((EntityIndexConfigDelegator) entityIndexConfig); for (Object value : entityIndexConfig.getIndexValues(entity)) if (!hive.directory() .doesResourceIdExist( delegateEntityConfig.getDelegateEntityConfig().getResourceName(), value)) insert( delegateEntityConfig.getDelegateEntityConfig(), delegateEntityConfig.stubEntityInstance(value, config.getPrimaryIndexKey(entity))); } }
private Collection<EntityIndexConfig> getPartitionIndexConfigs(final EntityConfig config) { return Filter.grep( new Predicate<EntityIndexConfig>() { public boolean f(EntityIndexConfig entityIndexConfig) { return entityIndexConfig.getIndexType().equals(IndexType.Partition); } }, config.getEntityIndexConfigs()); }
private void insertSecondaryIndexes(final EntityConfig config, final Object entity) throws HiveLockableException { Map<String, Collection<Object>> secondaryIndexMap = Transform.toMap( Filter.grep( new Filter.NotNullPredicate<Entry<String, Collection<Object>>>(), Transform.map( new Unary<EntityIndexConfig, Entry<String, Collection<Object>>>() { public Entry<String, Collection<Object>> f( EntityIndexConfig entityIndexConfig) { if (entityIndexConfig.getIndexValues(entity) == null) // Protect against null properties return null; return new Pair<String, Collection<Object>>( entityIndexConfig.getIndexName(), entityIndexConfig.getIndexValues(entity)); } }, getSecondaryIndexConfigs(config)))); hive.directory() .insertSecondaryIndexKeys( config.getResourceName(), secondaryIndexMap, config.getId(entity)); }
public void updatePartitionDimensionIndexIfNeeded(EntityConfig config, Object entity) throws HiveLockableException { // Detect partition key changes Object originalPartitionKey = hive.directory() .getPrimaryIndexKeyOfResourceId(config.getResourceName(), config.getId(entity)); if (!config.getPrimaryIndexKey(entity).equals(originalPartitionKey)) { conditionallyInsertPrimaryIndexKey(config, entity); hive.directory() .updatePrimaryIndexKeyOfResourceId( config.getResourceName(), config.getId(entity), config.getPrimaryIndexKey(entity)); } }
public void update(EntityConfig config, Object entity) throws HiveLockableException { Map<String, Collection<Object>> secondaryIndexValues = getAllSecondaryIndexValues(config, entity); Map<String, Object> partitionIndexValues = getAllPartitionIndexValues(config, entity); Map<String, Collection<Object>> toDelete = Maps.newHashMap(); Map<String, Collection<Object>> toInsert = Maps.newHashMap(); conditionallyInsertDelegatedResourceIndexes(config, entity); for (EntityIndexConfig indexConfig : getPartitionIndexConfigs(config)) { if (!partitionIndexValues .get(indexConfig.getIndexName()) .equals(indexConfig.getIndexValues(entity).iterator().next())) { if (!hive.directory() .doesPrimaryIndexKeyExist(indexConfig.getIndexValues(entity).iterator().next())) { hive.directory() .insertPrimaryIndexKey(indexConfig.getIndexValues(entity).iterator().next()); } hive.directory() .updatePrimaryIndexKeyOfResourceId( config.getResourceName(), config.getId(entity), indexConfig.getIndexValues(entity).iterator().next()); } } // Delete and insert secondary index values that have changed for (EntityIndexConfig indexConfig : getSecondaryIndexConfigs(config)) { Pair<Collection<Object>, Collection<Object>> diff = Collect.diff( secondaryIndexValues.get(indexConfig.getIndexName()), indexConfig.getIndexValues(entity)); toDelete.put(indexConfig.getIndexName(), diff.getKey()); toInsert.put(indexConfig.getIndexName(), diff.getValue()); } hive.directory() .insertSecondaryIndexKeys(config.getResourceName(), toInsert, config.getId(entity)); hive.directory() .deleteSecondaryIndexKeys(config.getResourceName(), toDelete, config.getId(entity)); }
public void conditionallyInsertPrimaryIndexKey(final EntityConfig config, final Object entity) throws HiveLockableException { if (!hive.directory().doesPrimaryIndexKeyExist(config.getPrimaryIndexKey(entity))) hive.directory().insertPrimaryIndexKey(config.getPrimaryIndexKey(entity)); }
public boolean idExists(EntityConfig config, Object id) { return hive.directory().doesResourceIdExist(config.getResourceName(), id); }
public boolean exists(EntityConfig config, Object entity) { return hive.directory().doesResourceIdExist(config.getResourceName(), config.getId(entity)); }
public void delete(EntityConfig config, Object entity) throws HiveLockableException { if (config.isPartitioningResource()) hive.directory().deletePrimaryIndexKey(config.getPrimaryIndexKey(entity)); else hive.directory().deleteResourceId(config.getResourceName(), config.getId(entity)); }