public static IndexedTypeSet subtraction(IndexedTypeSet referenceSet, IndexedTypeSet subtraend) { if ( referenceSet.isEmpty() || subtraend.isEmpty() ) { return referenceSet; } else { HashSetIndexedTypeSet casted = (HashSetIndexedTypeSet) referenceSet; Set<IndexedTypeIdentifier> cloned = casted.cloneInternalSet(); for ( IndexedTypeIdentifier toRemove : subtraend ) { cloned.remove( toRemove ); } if ( cloned.isEmpty() ) { return empty(); } if ( cloned.size() == 1 ) { return cloned.iterator().next().asTypeSet(); } else { return new HashSetIndexedTypeSet( cloned ); } } }
public static IndexedTypeSet subtraction(IndexedTypeSet referenceSet, IndexedTypeSet subtraend) { if ( referenceSet.isEmpty() || subtraend.isEmpty() ) { return referenceSet; } else { HashSetIndexedTypeSet casted = (HashSetIndexedTypeSet) referenceSet; Set<IndexedTypeIdentifier> cloned = casted.cloneInternalSet(); for ( IndexedTypeIdentifier toRemove : subtraend ) { cloned.remove( toRemove ); } if ( cloned.isEmpty() ) { return empty(); } if ( cloned.size() == 1 ) { return cloned.iterator().next().asTypeSet(); } else { return new HashSetIndexedTypeSet( cloned ); } } }
public static IndexedTypeSet composite(final IndexedTypeSet setA, final IndexedTypeSet setB) { if ( setA.isEmpty() ) { return setB; } else if ( setB.isEmpty() ) { return setA; } else if ( setB.equals( setA ) ) { return setA; } else { HashSet<IndexedTypeIdentifier> newSet = new HashSet<>( setA.size() + setB.size() ); setA.forEach( newSet::add ); setB.forEach( newSet::add ); return fromSafeHashSet( newSet ); } }
public static IndexedTypeSet composite(final IndexedTypeSet setA, final IndexedTypeSet setB) { if ( setA.isEmpty() ) { return setB; } else if ( setB.isEmpty() ) { return setA; } else if ( setB.equals( setA ) ) { return setA; } else { HashSet<IndexedTypeIdentifier> newSet = new HashSet<>( setA.size() + setB.size() ); setA.forEach( newSet::add ); setB.forEach( newSet::add ); return fromSafeHashSet( newSet ); } }
private IndexedTypeSet getQueriedEntityTypes(IndexedTypeSet indexedTargetedEntities) { if ( indexedTargetedEntities == null || indexedTargetedEntities.isEmpty() ) { return extendedIntegrator.getIndexBindings().keySet(); } else { return IndexedTypeSets.fromIdentifiers( indexedTargetedEntities ); } }
private IndexedTypeSet getQueriedEntityTypes(IndexedTypeSet indexedTargetedEntities) { if ( indexedTargetedEntities == null || indexedTargetedEntities.isEmpty() ) { return extendedIntegrator.getIndexBindings().keySet(); } else { return IndexedTypeSets.fromIdentifiers( indexedTargetedEntities ); } }
@Override public IndexReader open(IndexedTypeSet types) { if ( types.isEmpty() ) { throw log.needAtLeastOneIndexedEntityType(); } HashMap<String, IndexManager> indexManagers = new HashMap<>(); for ( IndexedTypeIdentifier type : types ) { collectAllIndexManagersInto( searchFactory.getSafeIndexBindingForEntity( type ), indexManagers ); } return MultiReaderFactory.openReader( indexManagers ); }
@Override public IndexReader open(IndexedTypeSet types) { if ( types.isEmpty() ) { throw log.needAtLeastOneIndexedEntityType(); } HashMap<String, IndexManager> indexManagers = new HashMap<>(); for ( IndexedTypeIdentifier type : types ) { collectAllIndexManagersInto( searchFactory.getSafeIndexBindingForEntity( type ), indexManagers ); } return MultiReaderFactory.openReader( indexManagers ); }
/** * Returns the given type itself if it is indexed, otherwise the first found indexed sub-type. * * @param entityType the type of interest * @param factory search factory * @return the given type itself if it is indexed, otherwise the first found indexed sub-type or {@code null} if * neither the given type nor any of its sub-types are indexed */ private IndexedTypeIdentifier getIndexBoundType(IndexedTypeIdentifier entityType, ExtendedSearchIntegrator factory) { if ( factory.getIndexBinding( entityType ) != null ) { return entityType; } IndexedTypeSet indexedSubTypes = factory.getIndexedTypesPolymorphic( entityType.asTypeSet() ); if ( !indexedSubTypes.isEmpty() ) { return indexedSubTypes.iterator().next(); } return null; }
/** * Returns the given type itself if it is indexed, otherwise the first found indexed sub-type. * * @param entityType the type of interest * @param factory search factory * @return the given type itself if it is indexed, otherwise the first found indexed sub-type or {@code null} if * neither the given type nor any of its sub-types are indexed */ private IndexedTypeIdentifier getIndexBoundType(IndexedTypeIdentifier entityType, ExtendedSearchIntegrator factory) { if ( factory.getIndexBinding( entityType ) != null ) { return entityType; } IndexedTypeSet indexedSubTypes = factory.getIndexedTypesPolymorphic( entityType.asTypeSet() ); if ( !indexedSubTypes.isEmpty() ) { return indexedSubTypes.iterator().next(); } return null; }
@Test public void testCreationOfEmptySets() { //This is more to test about creation from special parameters than to test the isEmpty() method Assert.assertTrue( IndexedTypeSets.fromClasses().isEmpty() ); Assert.assertTrue( IndexedTypeSets.empty().isEmpty() ); Assert.assertTrue( IndexedTypeSets.fromIdentifiers( Collections.emptySet() ).isEmpty() ); }
@Override public <T> void purge(Class<T> entityType, Serializable id) { if ( entityType == null ) { return; } final ExtendedSearchIntegrator searchIntegrator = getSearchIntegrator(); final IndexedTypeIdentifier typeIdentifier = searchIntegrator.getIndexBindings().keyFromPojoType( entityType ); final IndexedTypeSet targetedClasses = getSearchIntegrator().getIndexedTypesPolymorphic( typeIdentifier.asTypeSet() ); if ( targetedClasses.isEmpty() ) { String msg = entityType.getName() + " is not an indexed entity or a subclass of an indexed entity"; throw new IllegalArgumentException( msg ); } for ( IndexedTypeIdentifier type : targetedClasses ) { if ( id == null ) { createAndPerformWork( type, null, WorkType.PURGE_ALL ); } else { createAndPerformWork( type, id, WorkType.PURGE ); } } }
private void initializeIndex() { if ( !indexInitialized ) { /* * The value of this variable is only used for the "CREATE" schema management * strategy, but we store it in any case, just to be consistent. */ indexCreatedByHibernateSearch = initializeIndex( containedEntityTypes ); indexInitialized = true; initializedContainedEntityTypes = IndexedTypeSets.composite( initializedContainedEntityTypes, containedEntityTypes ); } else { IndexedTypeSet notYetInitializedContainedEntityTypes = IndexedTypeSets.subtraction( containedEntityTypes, initializedContainedEntityTypes ); if ( notYetInitializedContainedEntityTypes.isEmpty() ) { return; // Nothing to do } reinitializeIndex( notYetInitializedContainedEntityTypes ); initializedContainedEntityTypes = IndexedTypeSets.composite( initializedContainedEntityTypes, notYetInitializedContainedEntityTypes ); } }
private void initializeIndex() { if ( !indexInitialized ) { /* * The value of this variable is only used for the "CREATE" schema management * strategy, but we store it in any case, just to be consistent. */ indexCreatedByHibernateSearch = initializeIndex( containedEntityTypes ); indexInitialized = true; initializedContainedEntityTypes = IndexedTypeSets.composite( initializedContainedEntityTypes, containedEntityTypes ); } else { IndexedTypeSet notYetInitializedContainedEntityTypes = IndexedTypeSets.subtraction( containedEntityTypes, initializedContainedEntityTypes ); if ( notYetInitializedContainedEntityTypes.isEmpty() ) { return; // Nothing to do } reinitializeIndex( notYetInitializedContainedEntityTypes ); initializedContainedEntityTypes = IndexedTypeSets.composite( initializedContainedEntityTypes, notYetInitializedContainedEntityTypes ); } }
private void assertIsDoubleSet(IndexedTypeSet typeSet) { Assert.assertFalse( "Verify it's not a singleton", typeSet == IndexedTypeSets.empty() ); Assert.assertFalse( typeSet.isEmpty() ); Assert.assertEquals( 2, typeSet.size() ); Iterator<IndexedTypeIdentifier> iterator = typeSet.iterator(); Assert.assertTrue( iterator.hasNext() ); iterator.next(); // increment once iterator.next(); // increment twice Assert.assertFalse( iterator.hasNext() ); iterator.forEachRemaining( l -> Assert.fail( "should never happen" ) ); //no more elements Set<Class<?>> pojosSet = typeSet.toPojosSet(); Assert.assertTrue( pojosSet.contains( TYPE_A.getPojoType() ) ); Assert.assertTrue( pojosSet.contains( TYPE_B.getPojoType() ) ); Assert.assertEquals( 2, pojosSet.size() ); }
private void assertIsEmpty(IndexedTypeSet typeSet) { Assert.assertTrue( "Verify the singleton optimisation applies", typeSet == IndexedTypeSets.empty() ); Assert.assertTrue( typeSet.isEmpty() ); Assert.assertEquals( 0, typeSet.size() ); typeSet.iterator().forEachRemaining( l -> Assert.fail( "should never happen" ) ); Assert.assertEquals( 0, typeSet.toPojosSet().size() ); }
private void assertIsSingletonSet(IndexedTypeSet typeSet, Class<?> someType, boolean recursive) { Assert.assertFalse( "Verify it's not a singleton", typeSet == IndexedTypeSets.empty() ); Assert.assertFalse( typeSet.isEmpty() ); Assert.assertEquals( 1, typeSet.size() ); Iterator<IndexedTypeIdentifier> iterator = typeSet.iterator(); Assert.assertTrue( iterator.hasNext() ); IndexedTypeIdentifier firstElement = iterator.next(); // increment once Assert.assertFalse( iterator.hasNext() ); iterator.forEachRemaining( l -> Assert.fail( "should never happen" ) ); //no more elements Set<Class<?>> pojosSet = typeSet.toPojosSet(); Assert.assertTrue( pojosSet.contains( someType ) ); Assert.assertEquals( 1, pojosSet.size() ); IndexedTypeSet typeSet2 = firstElement.asTypeSet(); if ( recursive ) { assertIsSingletonSet( typeSet2, someType, false ); } Assert.assertEquals( typeSet2, typeSet2 ); Assert.assertEquals( firstElement.getPojoType(), someType ); }
private AbstractHSQuery(ExtendedSearchIntegrator extendedIntegrator, IndexedTypeSet targetedEntities, IndexedTypeMap<CustomTypeMetadata> customTypeMetadata) { this.extendedIntegrator = extendedIntegrator; this.timeoutExceptionFactory = extendedIntegrator.getDefaultTimeoutExceptionFactory(); if ( targetedEntities == null ) { targetedEntities = IndexedTypeSets.empty(); } this.targetedEntities = targetedEntities; this.indexedTargetedEntities = extendedIntegrator.getIndexedTypesPolymorphic( targetedEntities ); if ( targetedEntities.size() > 0 && indexedTargetedEntities.size() == 0 ) { IndexedTypeSet configuredTargetEntities = extendedIntegrator.getConfiguredTypesPolymorphic( targetedEntities ); if ( configuredTargetEntities.isEmpty() ) { throw LOG.targetedEntityTypesNotConfigured( StringHelper.join( targetedEntities, "," ) ); } else { throw LOG.targetedEntityTypesNotIndexed( StringHelper.join( targetedEntities, "," ) ); } } if ( customTypeMetadata == null ) { this.customTypeMetadata = IndexedTypeMaps.empty(); } else { this.customTypeMetadata = customTypeMetadata; } }
public HSearchEntityContext(IndexedTypeIdentifier entityType, ExtendedSearchIntegrator factory) { // get a type for meta-data retrieval; if the given type itself is not indexed, one indexed sub-type will // be used; note that this allows to e.g. query for fields not present on the given type but on one of its // sub-types, but we accept this for now indexBoundType = getIndexBoundType( entityType, factory ); if ( indexBoundType == null ) { IndexedTypeSet configuredSubTypes = factory.getConfiguredTypesPolymorphic( entityType.asTypeSet() ); if ( configuredSubTypes.isEmpty() ) { throw log.cantQueryUnconfiguredType( entityType.getName() ); } else { throw log.cantQueryUnindexedType( entityType.getName() ); } } originalAnalyzerReference = factory.getAnalyzerReference( indexBoundType ); queryAnalyzerReferenceBuilder = factory.getAnalyzerReference( indexBoundType ).startCopy(); IndexManagerType indexManagerType = factory.getIndexBinding( indexBoundType ).getIndexManagerType(); analyzerRegistry = factory.getIntegration( indexManagerType ).getAnalyzerRegistry(); }
public HSearchEntityContext(IndexedTypeIdentifier entityType, ExtendedSearchIntegrator factory) { // get a type for meta-data retrieval; if the given type itself is not indexed, one indexed sub-type will // be used; note that this allows to e.g. query for fields not present on the given type but on one of its // sub-types, but we accept this for now indexBoundType = getIndexBoundType( entityType, factory ); if ( indexBoundType == null ) { IndexedTypeSet configuredSubTypes = factory.getConfiguredTypesPolymorphic( entityType.asTypeSet() ); if ( configuredSubTypes.isEmpty() ) { throw log.cantQueryUnconfiguredType( entityType.getName() ); } else { throw log.cantQueryUnindexedType( entityType.getName() ); } } originalAnalyzerReference = factory.getAnalyzerReference( indexBoundType ); queryAnalyzerReferenceBuilder = factory.getAnalyzerReference( indexBoundType ).startCopy(); IndexManagerType indexManagerType = factory.getIndexBinding( indexBoundType ).getIndexManagerType(); analyzerRegistry = factory.getIntegration( indexManagerType ).getAnalyzerRegistry(); }