List of usage examples for com.google.common.collect HashBiMap create
public static <K, V> HashBiMap<K, V> create(Map<? extends K, ? extends V> map)
From source file:com.moz.fiji.schema.layout.FijiTableLayout.java
/** * Constructs a FijiTableLayout from an Avro descriptor and an optional reference layout. * * @param desc Avro layout descriptor (relative to the reference layout). * @param reference Optional reference layout, or null. * @throws InvalidLayoutException if the descriptor is invalid or inconsistent wrt reference. *///from www.java 2s.co m private FijiTableLayout(TableLayoutDesc desc, FijiTableLayout reference) throws InvalidLayoutException { // Deep-copy the descriptor to prevent mutating a parameter: mDesc = TableLayoutDesc.newBuilder(Preconditions.checkNotNull(desc)).build(); // Ensure the array of locality groups is mutable: mDesc.setLocalityGroups(Lists.newArrayList(mDesc.getLocalityGroups())); // Check that the version specified in the layout matches the features used. // Any compatibility checks belong in this section. mLayoutVersion = computeLayoutVersion(mDesc.getVersion()); if (!Objects.equal(LAYOUT_PROTOCOL_NAME, mLayoutVersion.getProtocolName())) { final String exceptionMessage; if (Objects.equal(Versions.LAYOUT_FIJI_1_0_0_DEPRECATED.getProtocolName(), mLayoutVersion.getProtocolName())) { // Warn the user if they tried a version number like 'fiji-0.9' or 'fiji-1.1'. exceptionMessage = String.format( "Deprecated layout version protocol '%s' only valid for version '%s'," + " but received version '%s'. You should specify a layout version protocol" + " as '%s-x.y', not '%s-x.y'.", Versions.LAYOUT_FIJI_1_0_0_DEPRECATED.getProtocolName(), Versions.LAYOUT_FIJI_1_0_0_DEPRECATED, mLayoutVersion, LAYOUT_PROTOCOL_NAME, Versions.LAYOUT_FIJI_1_0_0_DEPRECATED.getProtocolName()); } else { exceptionMessage = String.format("Invalid version protocol: '%s'. Expected '%s'.", mLayoutVersion.getProtocolName(), LAYOUT_PROTOCOL_NAME); } throw new InvalidLayoutException(exceptionMessage); } if (Versions.MAX_LAYOUT_VERSION.compareTo(mLayoutVersion) < 0) { throw new InvalidLayoutException("The maximum layout version we support is " + Versions.MAX_LAYOUT_VERSION + "; this layout requires " + mLayoutVersion); } else if (Versions.MIN_LAYOUT_VERSION.compareTo(mLayoutVersion) > 0) { throw new InvalidLayoutException("The minimum layout version we support is " + Versions.MIN_LAYOUT_VERSION + "; this layout requires " + mLayoutVersion); } // max_filesize and memstore_flushsize were introduced in version 1.2. if (Versions.BLOCK_SIZE_LAYOUT_VERSION.compareTo(mLayoutVersion) > 0) { if (mDesc.getMaxFilesize() != null) { // Cannot use max_filesize if this is the case. throw new InvalidLayoutException("Support for specifying max_filesize begins with layout version " + Versions.BLOCK_SIZE_LAYOUT_VERSION.toString()); } if (mDesc.getMemstoreFlushsize() != null) { // Cannot use memstore_flushsize if this is the case. throw new InvalidLayoutException( "Support for specifying memstore_flushsize begins with layout version " + Versions.BLOCK_SIZE_LAYOUT_VERSION); } } else { if (mDesc.getMaxFilesize() != null && mDesc.getMaxFilesize() <= 0) { throw new InvalidLayoutException("max_filesize must be greater than 0"); } if (mDesc.getMemstoreFlushsize() != null && mDesc.getMemstoreFlushsize() <= 0) { throw new InvalidLayoutException("memstore_flushsize must be greater than 0"); } } // Ability to configure column name translation was introduced in version 1.5 if (Versions.CONFIGURE_COLUMN_NAME_TRANSLATION_VERSION.compareTo(mLayoutVersion) > 0) { if (mDesc.getColumnNameTranslator() != ColumnNameTranslator.SHORT) { throw new InvalidLayoutException( "Support for specifiying non-short column name translators begins with layout version " + Versions.CONFIGURE_COLUMN_NAME_TRANSLATION_VERSION); } } // Composite keys and RowKeyFormat2 was introduced in version 1.1. if (Versions.RKF2_LAYOUT_VERSION.compareTo(mLayoutVersion) > 0 && mDesc.getKeysFormat() instanceof RowKeyFormat2) { // Cannot use RowKeyFormat2 if this is the case. throw new InvalidLayoutException( "Support for specifying keys_format as a RowKeyFormat2 begins with layout version " + Versions.RKF2_LAYOUT_VERSION); } if (!isValidName(getName())) { throw new InvalidLayoutException(String.format("Invalid table name: '%s'.", getName())); } if (reference != null) { if (!getName().equals(reference.getName())) { throw new InvalidLayoutException(String.format( "Invalid layout update: layout name '%s' does not match reference layout name '%s'.", getName(), reference.getName())); } if (!mDesc.getKeysFormat().equals(reference.getDesc().getKeysFormat())) { throw new InvalidLayoutException(String.format( "Invalid layout update from reference row keys format '%s' to row keys format '%s'.", reference.getDesc().getKeysFormat(), mDesc.getKeysFormat())); } } // Layout ID: if (mDesc.getLayoutId() == null) { try { final long refLayoutId = (reference == null) ? 0 : Long.parseLong(reference.getDesc().getLayoutId()); final long layoutId = refLayoutId + 1; mDesc.setLayoutId(Long.toString(layoutId)); } catch (NumberFormatException nfe) { throw new InvalidLayoutException( String.format("Reference layout for table '%s' has an invalid layout ID: '%s'", getName(), reference.getDesc().getLayoutId())); } } if (mDesc.getKeysFormat() instanceof RowKeyFormat) { isValidRowKeyFormat1((RowKeyFormat) mDesc.getKeysFormat()); } else if (mDesc.getKeysFormat() instanceof RowKeyFormat2) { // Check validity of row key format. isValidRowKeyFormat2((RowKeyFormat2) mDesc.getKeysFormat()); } // Build localities: /** * Reference map from locality group name to locality group ID. * Entries are removed as we process locality group descriptors in the new layout. * At the end of the process, this map must be empty. */ final BiMap<String, ColumnId> refLGIdMap = (reference == null) ? HashBiMap.<String, ColumnId>create() : HashBiMap.create(reference.mLocalityGroupIdNameMap.inverse()); /** Map of locality groups in the new layout. */ final List<LocalityGroupLayout> localityGroups = Lists.newArrayList(); final Map<String, LocalityGroupLayout> lgMap = Maps.newHashMap(); final BiMap<ColumnId, String> idMap = HashBiMap.create(); /** Locality group with no ID assigned yet. */ final List<LocalityGroupLayout> unassigned = Lists.newArrayList(); /** All the families in the table. */ final List<FamilyLayout> families = Lists.newArrayList(); /** Map from family name or alias to family layout. */ final Map<String, FamilyLayout> familyMap = Maps.newHashMap(); /** All primary column names (including map-type families). */ final Set<FijiColumnName> columnNames = Sets.newTreeSet(); final Map<FijiColumnName, ColumnLayout> columnMap = Maps.newHashMap(); final Iterator<LocalityGroupDesc> itLGDesc = mDesc.getLocalityGroups().iterator(); while (itLGDesc.hasNext()) { final LocalityGroupDesc lgDesc = itLGDesc.next(); final boolean isRename = (lgDesc.getRenamedFrom() != null); final String refLGName = isRename ? lgDesc.getRenamedFrom() : lgDesc.getName(); lgDesc.setRenamedFrom(null); if (isRename && (reference == null)) { throw new InvalidLayoutException(String .format("Invalid rename: no reference table layout for locality group '%s'.", refLGName)); } final LocalityGroupLayout refLGLayout = (reference != null) ? reference.mLocalityGroupMap.get(refLGName) : null; if (isRename && (refLGLayout == null)) { throw new InvalidLayoutException( String.format("Invalid rename: cannot find reference locality group '%s'.", refLGName)); } final ColumnId refLGId = refLGIdMap.remove(refLGName); if (lgDesc.getDelete()) { // This locality group is deleted: if (refLGId == null) { throw new InvalidLayoutException(String.format( "Attempting to delete locality group '%s' unknown in reference layout.", refLGName)); } itLGDesc.remove(); continue; } // BloomType, block_size were introduced in version 1.2. if (Versions.BLOCK_SIZE_LAYOUT_VERSION.compareTo(mLayoutVersion) > 0) { if (lgDesc.getBlockSize() != null) { // Cannot use max_filesize if this is the case. throw new InvalidLayoutException("Support for specifying block_size begins with layout version " + Versions.BLOCK_SIZE_LAYOUT_VERSION); } if (lgDesc.getBloomType() != null) { // Cannot use bloom_type if this is the case. throw new InvalidLayoutException("Support for specifying bloom_type begins with layout version " + Versions.BLOCK_SIZE_LAYOUT_VERSION); } } else { if (lgDesc.getBlockSize() != null && lgDesc.getBlockSize() <= 0) { throw new InvalidLayoutException("block_size must be greater than 0"); } } final LocalityGroupLayout lgLayout = new LocalityGroupLayout(lgDesc, refLGLayout); localityGroups.add(lgLayout); for (String lgName : lgLayout.getNames()) { Preconditions.checkState(lgMap.put(lgName, lgLayout) == null, "Duplicate locality group name: " + lgName); } if (lgLayout.getId() != null) { final String previous = idMap.put(lgLayout.getId(), lgLayout.getName()); Preconditions.checkState(previous == null, String.format("Duplicate locality group ID '%s' associated to '%s' and '%s'.", lgLayout.getId(), lgLayout.getName(), previous)); } else { unassigned.add(lgLayout); } families.addAll(lgLayout.getFamilies()); for (FamilyLayout familyLayout : lgLayout.getFamilies()) { for (String familyName : familyLayout.getNames()) { if (null != familyMap.put(familyName, familyLayout)) { throw new InvalidLayoutException( String.format("Layout for table '%s' contains duplicate family name '%s'.", getName(), familyName)); } } if (familyLayout.isMapType()) { Preconditions.checkState(columnNames.add(FijiColumnName.create(familyLayout.getName(), null))); } for (ColumnLayout columnLayout : familyLayout.getColumns()) { for (String columnName : columnLayout.getNames()) { final FijiColumnName column = FijiColumnName.create(familyLayout.getName(), columnName); if (null != columnMap.put(column, columnLayout)) { throw new InvalidLayoutException(String.format( "Layout for table '%s' contains duplicate column '%s'.", getName(), column)); } } Preconditions.checkState( columnNames.add(FijiColumnName.create(familyLayout.getName(), columnLayout.getName()))); } } } if (!refLGIdMap.isEmpty()) { throw new InvalidLayoutException(String.format("Missing descriptor(s) for locality group(s): %s.", Joiner.on(",").join(refLGIdMap.keySet()))); } mLocalityGroups = ImmutableList.copyOf(localityGroups); mLocalityGroupMap = ImmutableMap.copyOf(lgMap); mFamilies = ImmutableList.copyOf(families); mFamilyMap = ImmutableMap.copyOf(familyMap); mColumnNames = ImmutableSet.copyOf(columnNames); // Assign IDs to locality groups: int nextColumnId = 1; for (LocalityGroupLayout localityGroup : unassigned) { Preconditions.checkState(localityGroup.getId() == null); while (true) { final ColumnId columnId = new ColumnId(nextColumnId); nextColumnId += 1; if (!idMap.containsKey(columnId)) { localityGroup.setId(columnId); idMap.put(columnId, localityGroup.getName()); break; } } } mLocalityGroupIdNameMap = ImmutableBiMap.copyOf(idMap); }
From source file:org.apache.usergrid.persistence.cassandra.EntityManagerImpl.java
public Results loadEntities(Results results, Results.Level resultsLevel, Map<UUID, UUID> associatedMap, int count) throws Exception { results = results.trim(count);// w w w .j a va 2 s. c o m if (resultsLevel.ordinal() <= results.getLevel().ordinal()) { return results; } results.setEntities(getEntities(results.getIds(), null)); if (resultsLevel == Results.Level.LINKED_PROPERTIES) { List<Entity> entities = results.getEntities(); BiMap<UUID, UUID> associatedIds = null; if (associatedMap != null) { associatedIds = HashBiMap.create(associatedMap); } else { associatedIds = HashBiMap.create(entities.size()); for (Entity entity : entities) { Object id = entity.getMetadata(PROPERTY_ASSOCIATED); if (id instanceof UUID) { associatedIds.put(entity.getUuid(), (UUID) id); } } } List<DynamicEntity> linked = getEntities(new ArrayList<UUID>(associatedIds.values()), null); for (DynamicEntity l : linked) { Map<String, Object> p = l.getDynamicProperties(); if ((p != null) && (p.size() > 0)) { Entity e = results.getEntitiesMap().get(associatedIds.inverse().get(l.getUuid())); if (l.getType().endsWith(TYPE_MEMBER)) { e.setProperty(TYPE_MEMBER, p); } else if (l.getType().endsWith(TYPE_CONNECTION)) { e.setProperty(TYPE_CONNECTION, p); } } } } return results; }
From source file:org.usergrid.persistence.cassandra.EntityManagerImpl.java
public Results loadEntities(Results results, Results.Level resultsLevel, Map<UUID, UUID> associatedMap, int count) throws Exception { results = results.trim(count);/*w w w .ja v a 2s. c o m*/ if (resultsLevel.ordinal() <= results.getLevel().ordinal()) { return results; } results.setEntities(getEntities(results.getIds(), null, null)); if (resultsLevel == Results.Level.LINKED_PROPERTIES) { List<Entity> entities = results.getEntities(); BiMap<UUID, UUID> associatedIds = null; if (associatedMap != null) { associatedIds = HashBiMap.create(associatedMap); } else { associatedIds = HashBiMap.create(entities.size()); for (Entity entity : entities) { Object id = entity.getMetadata(PROPERTY_ASSOCIATED); if (id instanceof UUID) { associatedIds.put(entity.getUuid(), (UUID) id); } } } List<DynamicEntity> linked = getEntities(new ArrayList<UUID>(associatedIds.values()), null, null); for (DynamicEntity l : linked) { Map<String, Object> p = l.getDynamicProperties(); if ((p != null) && (p.size() > 0)) { Entity e = results.getEntitiesMap().get(associatedIds.inverse().get(l.getUuid())); if (l.getType().endsWith(TYPE_MEMBER)) { e.setProperty(TYPE_MEMBER, p); } else if (l.getType().endsWith(TYPE_CONNECTION)) { e.setProperty(TYPE_CONNECTION, p); } } } } return results; }
From source file:com.linkedin.pinot.controller.helix.core.PinotHelixResourceManager.java
/** * Provides admin endpoints for the provided data instances * @param instances instances for which to read endpoints * @return returns map of instances to their admin endpoints. * The return value is a bimap because admin instances are typically used for * http requests. So, on response, we need mapping from the endpoint to the * server instances. With BiMap, both mappings are easily available *///from w w w . j av a 2 s. c o m public @Nonnull BiMap<String, String> getDataInstanceAdminEndpoints(@Nonnull Set<String> instances) { Preconditions.checkNotNull(instances); BiMap<String, String> endpointToInstance = HashBiMap.create(instances.size()); for (String instance : instances) { InstanceConfig helixInstanceConfig = getHelixInstanceConfig(instance); ZNRecord record = helixInstanceConfig.getRecord(); String[] hostnameSplit = helixInstanceConfig.getHostName().split("_"); Preconditions.checkState(hostnameSplit.length >= 2); String port = record.getSimpleField(CommonConstants.Helix.Instance.ADMIN_PORT_KEY); endpointToInstance.put(instance, hostnameSplit[1] + ":" + port); } return endpointToInstance; }