Search in sources :

Example 1 with CheckForSigned

use of javax.annotation.CheckForSigned in project phoss-directory by phax.

the class PDStorageManager method getCount.

@CheckForSigned
public int getCount(@Nonnull final Query aQuery) {
    ValueEnforcer.notNull(aQuery, "Query");
    try {
        final TotalHitCountCollector aCollector = new TotalHitCountCollector();
        searchAtomic(aQuery, aCollector);
        return aCollector.getTotalHits();
    } catch (final IOException ex) {
        LOGGER.error("Error counting documents with query " + aQuery, ex);
        return -1;
    }
}
Also used : TotalHitCountCollector(org.apache.lucene.search.TotalHitCountCollector) IOException(java.io.IOException) CheckForSigned(javax.annotation.CheckForSigned)

Example 2 with CheckForSigned

use of javax.annotation.CheckForSigned in project phoss-directory by phax.

the class PDStorageManager method searchAll.

public void searchAll(@Nonnull final Query aQuery, @CheckForSigned final int nMaxResultCount, @Nonnull final Consumer<Document> aConsumer) throws IOException {
    ValueEnforcer.notNull(aQuery, "Query");
    ValueEnforcer.notNull(aConsumer, "Consumer");
    if (nMaxResultCount <= 0) {
        // Search all
        final ObjIntConsumer<Document> aConverter = (aDoc, nDocID) -> aConsumer.accept(aDoc);
        final Collector aCollector = new AllDocumentsCollector(m_aLucene, aConverter);
        searchAtomic(aQuery, aCollector);
    } else {
        // Search top docs only
        // Lucene 8
        // final TopScoreDocCollector aCollector = TopScoreDocCollector.create
        // (nMaxResultCount, Integer.MAX_VALUE);
        final TopScoreDocCollector aCollector = TopScoreDocCollector.create(nMaxResultCount);
        searchAtomic(aQuery, aCollector);
        for (final ScoreDoc aScoreDoc : aCollector.topDocs().scoreDocs) {
            final Document aDoc = m_aLucene.getDocument(aScoreDoc.doc);
            if (aDoc == null)
                throw new IllegalStateException("Failed to resolve Lucene Document with ID " + aScoreDoc.doc);
            // Pass to Consumer
            aConsumer.accept(aDoc);
        }
    }
}
Also used : Query(org.apache.lucene.search.Query) ScoreDoc(org.apache.lucene.search.ScoreDoc) IDocumentTypeIdentifier(com.helger.peppolid.IDocumentTypeIdentifier) FieldType(org.apache.lucene.document.FieldType) LoggerFactory(org.slf4j.LoggerFactory) CommonsTreeMap(com.helger.commons.collection.impl.CommonsTreeMap) CollectionHelper(com.helger.commons.collection.CollectionHelper) CheckForSigned(javax.annotation.CheckForSigned) PDIdentifier(com.helger.pd.businesscard.generic.PDIdentifier) Document(org.apache.lucene.document.Document) Locale(java.util.Locale) TotalHitCountCollector(org.apache.lucene.search.TotalHitCountCollector) PDField(com.helger.pd.indexer.storage.field.PDField) CGlobal(com.helger.commons.CGlobal) Occur(org.apache.lucene.search.BooleanClause.Occur) SimpleParticipantIdentifier(com.helger.peppolid.simple.participant.SimpleParticipantIdentifier) PDExtendedBusinessCard(com.helger.pd.indexer.businesscard.PDExtendedBusinessCard) IThrowingRunnable(com.helger.commons.callback.IThrowingRunnable) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) AuditHelper(com.helger.photon.audit.AuditHelper) PDName(com.helger.pd.businesscard.generic.PDName) ICommonsList(com.helger.commons.collection.impl.ICommonsList) ICommonsMap(com.helger.commons.collection.impl.ICommonsMap) IndexSearcher(org.apache.lucene.search.IndexSearcher) PDBusinessEntity(com.helger.pd.businesscard.generic.PDBusinessEntity) ESuccess(com.helger.commons.state.ESuccess) ObjIntConsumer(java.util.function.ObjIntConsumer) Function(java.util.function.Function) IThrowingSupplier(com.helger.commons.functional.IThrowingSupplier) PDLucene(com.helger.pd.indexer.lucene.PDLucene) IPDStorageManager(com.helger.pd.indexer.mgr.IPDStorageManager) Store(org.apache.lucene.document.Field.Store) ICommonsSortedMap(com.helger.commons.collection.impl.ICommonsSortedMap) IntPoint(org.apache.lucene.document.IntPoint) AllDocumentsCollector(com.helger.pd.indexer.lucene.AllDocumentsCollector) ReturnsMutableCopy(com.helger.commons.annotation.ReturnsMutableCopy) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) TopDocs(org.apache.lucene.search.TopDocs) Logger(org.slf4j.Logger) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) MutableInt(com.helger.commons.mutable.MutableInt) StringHelper(com.helger.commons.string.StringHelper) PDBusinessCard(com.helger.pd.businesscard.generic.PDBusinessCard) PDTWebDateHelper(com.helger.commons.datetime.PDTWebDateHelper) IOException(java.io.IOException) Collector(org.apache.lucene.search.Collector) TopScoreDocCollector(org.apache.lucene.search.TopScoreDocCollector) ValueEnforcer(com.helger.commons.ValueEnforcer) IMutableStatisticsHandlerKeyedTimer(com.helger.commons.statistics.IMutableStatisticsHandlerKeyedTimer) Consumer(java.util.function.Consumer) StatisticsManager(com.helger.commons.statistics.StatisticsManager) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Field(org.apache.lucene.document.Field) StopWatch(com.helger.commons.timing.StopWatch) CommonsLinkedHashMap(com.helger.commons.collection.impl.CommonsLinkedHashMap) TextField(org.apache.lucene.document.TextField) IndexOptions(org.apache.lucene.index.IndexOptions) Immutable(javax.annotation.concurrent.Immutable) PDContact(com.helger.pd.businesscard.generic.PDContact) TopScoreDocCollector(org.apache.lucene.search.TopScoreDocCollector) TotalHitCountCollector(org.apache.lucene.search.TotalHitCountCollector) AllDocumentsCollector(com.helger.pd.indexer.lucene.AllDocumentsCollector) Collector(org.apache.lucene.search.Collector) TopScoreDocCollector(org.apache.lucene.search.TopScoreDocCollector) AllDocumentsCollector(com.helger.pd.indexer.lucene.AllDocumentsCollector) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 3 with CheckForSigned

use of javax.annotation.CheckForSigned in project phoss-directory by phax.

the class PDStorageManager method deleteEntry.

@CheckForSigned
public int deleteEntry(@Nonnull final IParticipantIdentifier aParticipantID, @Nullable final PDStoredMetaData aMetaData, final boolean bVerifyOwner) throws IOException {
    ValueEnforcer.notNull(aParticipantID, "ParticipantID");
    LOGGER.info("Trying to delete entry with participant ID '" + aParticipantID.getURIEncoded() + "'" + (bVerifyOwner && aMetaData != null ? " with owner ID '" + aMetaData.getOwnerID() + "'" : ""));
    Query aParticipantQuery = new TermQuery(PDField.PARTICIPANT_ID.getExactMatchTerm(aParticipantID));
    if (getCount(aParticipantQuery) == 0) {
        // Hack e.g. for 9925:everbinding
        final String sOrigValue = aParticipantID.getValue();
        final String sUpperCaseValue = sOrigValue.toUpperCase(Locale.ROOT);
        if (!sUpperCaseValue.equals(sOrigValue)) {
            // Something changed - try again
            // Force case sensitivity
            final IParticipantIdentifier aNewPID = new SimpleParticipantIdentifier(aParticipantID.getScheme(), sUpperCaseValue);
            final Query aOtherQuery = new TermQuery(PDField.PARTICIPANT_ID.getExactMatchTerm(aNewPID));
            if (getCount(aOtherQuery) > 0) {
                LOGGER.info("Found something with '" + sUpperCaseValue + "' instead of '" + sOrigValue + "'");
                aParticipantQuery = aOtherQuery;
            }
        }
    }
    final Query aDeleteQuery;
    if (bVerifyOwner && aMetaData != null) {
        aDeleteQuery = new BooleanQuery.Builder().add(aParticipantQuery, Occur.MUST).add(new TermQuery(PDField.METADATA_OWNERID.getExactMatchTerm(aMetaData.getOwnerID())), Occur.MUST).build();
    } else
        aDeleteQuery = aParticipantQuery;
    final int nCount = getCount(aDeleteQuery);
    if (m_aLucene.writeLockedAtomic(() -> {
        // Delete
        m_aLucene.deleteDocuments(aDeleteQuery);
    }).isFailure()) {
        LOGGER.error("Failed to delete docs from the index using the query '" + aDeleteQuery + "'");
        return -1;
    }
    LOGGER.info("Deleted " + nCount + " docs from the index using the query '" + aDeleteQuery + "'");
    AuditHelper.onAuditExecuteSuccess("pd-indexer-delete", aParticipantID.getURIEncoded(), Integer.valueOf(nCount), aMetaData, Boolean.toString(bVerifyOwner));
    return nCount;
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) SimpleParticipantIdentifier(com.helger.peppolid.simple.participant.SimpleParticipantIdentifier) IntPoint(org.apache.lucene.document.IntPoint) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) CheckForSigned(javax.annotation.CheckForSigned)

Example 4 with CheckForSigned

use of javax.annotation.CheckForSigned in project phoss-directory by phax.

the class PDStorageManager method deleteAllEntriesMarkedAsDeleted.

@CheckForSigned
public int deleteAllEntriesMarkedAsDeleted() throws IOException {
    LOGGER.info("Trying to delete all entries marked as deleted");
    final Query aDeleteQuery = IntPoint.newExactQuery(CPDStorage.FIELD_DELETED, 1);
    final int nCount = getCount(aDeleteQuery);
    if (m_aLucene.writeLockedAtomic(() -> {
        // Delete
        m_aLucene.deleteDocuments(aDeleteQuery);
    }).isFailure()) {
        LOGGER.error("Failed to delete docs from the index using the query '" + aDeleteQuery + "'");
        return -1;
    }
    LOGGER.info("Deleted " + nCount + " docs from the index using the query '" + aDeleteQuery + "'");
    AuditHelper.onAuditExecuteSuccess("pd-indexer-delete-deleted", Integer.valueOf(nCount));
    return nCount;
}
Also used : Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) IntPoint(org.apache.lucene.document.IntPoint) CheckForSigned(javax.annotation.CheckForSigned)

Aggregations

CheckForSigned (javax.annotation.CheckForSigned)4 IParticipantIdentifier (com.helger.peppolid.IParticipantIdentifier)2 SimpleParticipantIdentifier (com.helger.peppolid.simple.participant.SimpleParticipantIdentifier)2 IOException (java.io.IOException)2 IntPoint (org.apache.lucene.document.IntPoint)2 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)2 Query (org.apache.lucene.search.Query)2 TermQuery (org.apache.lucene.search.TermQuery)2 TotalHitCountCollector (org.apache.lucene.search.TotalHitCountCollector)2 CGlobal (com.helger.commons.CGlobal)1 ValueEnforcer (com.helger.commons.ValueEnforcer)1 ReturnsMutableCopy (com.helger.commons.annotation.ReturnsMutableCopy)1 IThrowingRunnable (com.helger.commons.callback.IThrowingRunnable)1 CollectionHelper (com.helger.commons.collection.CollectionHelper)1 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 CommonsLinkedHashMap (com.helger.commons.collection.impl.CommonsLinkedHashMap)1 CommonsTreeMap (com.helger.commons.collection.impl.CommonsTreeMap)1 ICommonsList (com.helger.commons.collection.impl.ICommonsList)1 ICommonsMap (com.helger.commons.collection.impl.ICommonsMap)1