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; }
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))); } }
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)); }