Exemplo n.º 1
0
  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));
  }
Exemplo n.º 2
0
 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)));
     }
 }