Java 类org.hibernate.search.query.engine.spi.HSQuery 实例源码

项目:Hibernate-Search-GenericJPA    文件:StandaloneSearchFactoryImpl.java   
@Override
public HSearchQuery createQuery(Query query, Class<?>... targetedEntities) {
    HSQuery hsQuery = this.searchIntegrator.createHSQuery();
    hsQuery.luceneQuery( query );
    // to make sure no entity is used twice
    hsQuery.targetedEntities( new ArrayList<>( new HashSet<>( Arrays.asList( targetedEntities ) ) ) );
    return new HSearchQueryImpl( hsQuery, this.queryExec, this.searchIntegrator );
}
项目:Hibernate-Search-GenericJPA    文件:DtoQueryExecutor.java   
public <T> List<T> executeHSQuery(HSQuery hsQuery, Class<T> clazz) {
    return this.executeHSQuery( hsQuery, clazz, DtoDescriptor.DtoDescription.DEFAULT_PROFILE );
}
项目:Hibernate-Search-GenericJPA    文件:DtoQueryExecutor.java   
public <T> List<T> executeHSQuery(HSQuery hsQuery, Class<T> returnedType, String profile) {
    DtoDescriptor.DtoDescription desc = this.dtoDescriptions.computeIfAbsent(
            returnedType, this.dtoDescriptor::getDtoDescription
    );
    String[] projectedFieldsBefore = hsQuery.getProjectedFields();
    try {
        List<String> projection = new ArrayList<>();
        List<java.lang.reflect.Field> fields = new ArrayList<>();
        desc.getFieldDescriptionsForProfile( profile ).forEach(
                (fd) -> {
                    projection.add( fd.getFieldName() );
                    fields.add( fd.getField() );
                }
        );
        hsQuery.projection( projection.toArray( new String[0] ) );

        List<T> ret;
        {
            hsQuery.getTimeoutManager().start();

            ret = hsQuery.queryEntityInfos().stream().map(
                    (entityInfo) -> {
                        try {
                            T val = returnedType.newInstance();
                            Object[] projectedValues = entityInfo.getProjection();
                            for ( int i = 0; i < projection.size(); i++ ) {
                                java.lang.reflect.Field field = fields.get( i );
                                field.set( val, projectedValues[i] );
                            }
                            return val;
                        }
                        catch (InstantiationException | IllegalAccessException e) {
                            throw new SearchException( e );
                        }
                    }
            ).collect( Collectors.toList() );

            hsQuery.getTimeoutManager().stop();
        }
        return ret;
    }
    finally {
        hsQuery.projection( projectedFieldsBefore );
    }
}
项目:Hibernate-Search-GenericJPA    文件:HSearchQueryImpl.java   
public HSearchQueryImpl(HSQuery hsquery, DtoQueryExecutor queryExec, SearchIntegrator searchIntegrator) {
    this.hsquery = hsquery;
    this.queryExec = queryExec;
    this.searchIntegrator = searchIntegrator;
}
项目:Hibernate-Search-GenericJPA    文件:IndexUpdater.java   
@Override
public void delete(
        Class<?> entityClass,
        List<Class<?>> inIndexOf,
        Object id,
        EntityProvider entityProvider,
        Transaction tx) {
    for ( Class<?> indexClass : inIndexOf ) {
        RehashedTypeMetadata metadata = IndexUpdater.this.metadataForIndexRoot.get( indexClass );
        List<String> fields = metadata.getIdFieldNamesForType().get( entityClass );
        for ( String field : fields ) {
            DocumentFieldMetadata metaDataForIdField = metadata.getDocumentFieldMetadataForIdFieldName().get(
                    field
            );
            SingularTermDeletionQuery.Type idType = metadata.getSingularTermDeletionQueryTypeForIdFieldName()
                    .get( entityClass );
            Object idValueForDeletion;
            if ( idType == SingularTermDeletionQuery.Type.STRING ) {
                FieldBridge fb = metaDataForIdField.getFieldBridge();
                if ( !(fb instanceof StringBridge) ) {
                    throw new IllegalArgumentException( "no TwoWayStringBridge found for field: " + field );
                }
                idValueForDeletion = ((StringBridge) fb).objectToString( id );
            }
            else {
                idValueForDeletion = id;
            }
            if ( indexClass.equals( entityClass ) ) {
                this.searchIntegrator.getWorker().performWork(
                        new Work(
                                entityClass,
                                (Serializable) id,
                                WorkType.DELETE
                        ), tx
                );
            }
            else {
                HSQuery hsQuery = this.searchIntegrator
                        .createHSQuery()
                        .targetedEntities( Collections.singletonList( indexClass ) )
                        .luceneQuery(
                                this.searchIntegrator.buildQueryBuilder()
                                        .forEntity( indexClass )
                                        .get()
                                        .keyword()
                                        .onField( field )
                                        .matching( idValueForDeletion )
                                        .createQuery()
                        );
                int count = hsQuery.queryResultSize();
                int processed = 0;
                // this was just contained somewhere
                // so we have to update the containing entity
                while ( processed < count ) {
                    for ( EntityInfo entityInfo : hsQuery.firstResult( processed ).projection(
                            ProjectionConstants.ID
                    ).maxResults( HSQUERY_BATCH )
                            .queryEntityInfos() ) {
                        Serializable originalId = (Serializable) entityInfo.getProjection()[0];
                        Object original = entityProvider.get( indexClass, originalId );
                        if ( original != null ) {
                            this.update( original, tx );
                        }
                        else {
                            // original is not available in the
                            // database, but it will be deleted by its
                            // own delete event
                            // TODO: log this?
                        }
                    }
                    processed += HSQUERY_BATCH;
                }
            }
        }
    }
}