Project

General

Profile

« Previous | Next » 

Revision 8343

Added by Jing Tao over 10 years ago

If an object was archived, the solr index will be removed for it.

View differences:

SolrIndex.java
19 19
package edu.ucsb.nceas.metacat.index;
20 20

  
21 21
import java.io.ByteArrayInputStream;
22
import java.io.FileInputStream;
22 23
import java.io.FileNotFoundException;
23 24
import java.io.IOException;
24 25
import java.io.InputStream;
......
55 56
import org.dataone.cn.indexer.parser.SolrField;
56 57
import org.dataone.cn.indexer.resourcemap.ResourceEntry;
57 58
import org.dataone.cn.indexer.resourcemap.ResourceMap;
59
import org.dataone.cn.indexer.resourcemap.ResourceMapFactory;
58 60
import org.dataone.cn.indexer.solrhttp.SolrDoc;
59 61
import org.dataone.cn.indexer.solrhttp.SolrElementField;
60 62
import org.dataone.service.exceptions.NotFound;
......
320 322
     * @param data
321 323
     * @throws SolrServerException
322 324
     */
323
    private void checkParams(String pid, SystemMetadata systemMetadata, InputStream data) throws SolrServerException {
324
        if(pid == null || pid.trim().equals("")) {
325
    private void checkParams(Identifier pid, SystemMetadata systemMetadata, InputStream data) throws SolrServerException {
326
        if(pid == null || pid.getValue() == null || pid.getValue().trim().equals("")) {
325 327
            throw new SolrServerException("The identifier of the indexed document should not be null or blank.");
326 328
        }
327 329
        if(systemMetadata == null) {
328
            throw new SolrServerException("The system metadata of the indexed document should not be null.");
330
            throw new SolrServerException("The system metadata of the indexed document "+pid.getValue()+ " should not be null.");
329 331
        }
330 332
        if(data == null) {
331
            throw new SolrServerException("The indexed document itself should not be null.");
333
            throw new SolrServerException("The indexed document itself for pid "+pid.getValue()+" should not be null.");
332 334
        }
333 335
    }
334 336
    
......
344 346
     * @throws NotFound 
345 347
     * @throws NotImplemented 
346 348
     */
347
    private synchronized void insert(String pid, SystemMetadata systemMetadata, InputStream data) 
349
    private synchronized void insert(Identifier pid, SystemMetadata systemMetadata, InputStream data) 
348 350
                    throws IOException, SAXException, ParserConfigurationException,
349 351
                    XPathExpressionException, SolrServerException, JiBXException, EncoderException, NotImplemented, NotFound, UnsupportedType {
350 352
        checkParams(pid, systemMetadata, data);
351
        Map<String, SolrDoc> docs = process(pid, systemMetadata, data);
353
        Map<String, SolrDoc> docs = process(pid.getValue(), systemMetadata, data);
352 354
        
353 355
        //transform the Map to the SolrInputDocument which can be used by the solr server
354 356
        if(docs != null) {
......
426 428
    
427 429
    /**
428 430
     * Update the solr index. This method handles the three scenarios:
429
     * 1. Update an existing doc - if the the system metadata shows the value of the archive is false and it has an obsoletes,
431
     * 1. Remove an existing doc - if the the system metadata shows the value of the archive is true,
430 432
     *    remove the index for the previous version(s) and generate new index for the doc.
431
     * 2. Add a new doc - if the system metadata shows the value of the archive is false and it hasn't an obsoletes, generate the
433
     * 2. Add a new doc - if the system metadata shows the value of the archive is false, generate the
432 434
     *    index for the doc.
433
     * @param pid  the id of the document
434
     * @param obsoleteIds  the chain of the obsoletes by this id
435
     * @param systemMetadata  the system metadata associated with the data object
436
     * @param data  the data object itself
437
     * @throws SolrServerException 
438
     * @throws JiBXException 
439
     * @throws EncoderException 
440
     * @throws UnsupportedType 
441
     * @throws NotFound 
442
     * @throws NotImplemented 
443
     * @throws ServiceFailure 
444
     * @throws OREParserException 
445 435
     */
446
    public void update(String pid, SystemMetadata systemMetadata, InputStream data) 
447
                    throws IOException, SAXException, ParserConfigurationException,
448
                    XPathExpressionException, SolrServerException, JiBXException, EncoderException, NotImplemented, NotFound, UnsupportedType, ServiceFailure, OREParserException {
449
        checkParams(pid, systemMetadata, data);
450
        //generate index for either add or update.
451
        insert(pid, systemMetadata, data);
452
        log.info("============================= update index for the identifier "+pid);
453
       
436
    public void update(Identifier pid, SystemMetadata systemMetadata) {
437
        String objectPath = null;
438
        InputStream data = null;
439
        try {
440
            objectPath = DistributedMapsFactory.getObjectPathMap().get(pid);
441
            data = new FileInputStream(objectPath);
442
            update(pid, systemMetadata, data);
443
            EventlogFactory.createIndexEventLog().remove(pid);
444
        } catch (Exception e) {
445
            String error = "SolrIndex.update - could not update the solr index since " + e.getMessage();
446
            writeEventLog(systemMetadata, pid, error);
447
            log.error(error, e);
448
        }
454 449
    }
455 450
    
456 451
    
452
    /**
453
     * Update the solr index. This method handles the three scenarios:
454
     * 1. Remove an existing doc - if the the system metadata shows the value of the archive is true,
455
     *    remove the index for the previous version(s) and generate new index for the doc.
456
     * 2. Add a new doc - if the system metadata shows the value of the archive is false, generate the
457
     *    index for the doc.
458
     * @param pid
459
     * @param systemMetadata
460
     * @param data
461
     * @throws SolrServerException
462
     * @throws ServiceFailure
463
     * @throws XPathExpressionException
464
     * @throws NotImplemented
465
     * @throws NotFound
466
     * @throws UnsupportedType
467
     * @throws IOException
468
     * @throws SAXException
469
     * @throws ParserConfigurationException
470
     * @throws OREParserException
471
     * @throws JiBXException
472
     * @throws EncoderException
473
     */
474
    void update(Identifier pid, SystemMetadata systemMetadata, InputStream data) throws SolrServerException, 
475
                                ServiceFailure, XPathExpressionException, NotImplemented, NotFound, UnsupportedType, 
476
                                IOException, SAXException, ParserConfigurationException, OREParserException, JiBXException, EncoderException {
477
        checkParams(pid, systemMetadata, data);
478
        boolean isArchive = systemMetadata.getArchived();
479
        if(isArchive ) {
480
            //delete the index for the archived objects
481
            remove(pid.getValue(), systemMetadata);
482
            log.info("SolrIndex.update============================= archive the idex for the identifier "+pid);
483
        } else {
484
            //generate index for either add or update.
485
            insert(pid, systemMetadata, data);
486
            log.info("SolrIndex.update============================= insert index for the identifier "+pid);
487
        }
488
    }
457 489
    
458 490
   
459 491

  
460 492
    /*
461 493
     * Is the pid a resource map
462 494
     */
463
    private boolean isDataPackage(String pid) throws FileNotFoundException, ServiceFailure {
495
    private boolean isDataPackage(String pid, SystemMetadata sysmeta) throws FileNotFoundException, ServiceFailure {
464 496
        boolean isDataPackage = false;
465
        SystemMetadata sysmeta = DistributedMapsFactory.getSystemMetadata(pid);
497
        //SystemMetadata sysmeta = DistributedMapsFactory.getSystemMetadata(pid);
466 498
        if(sysmeta != null) {
467 499
            isDataPackage = IndexGenerator.isResourceMap(sysmeta.getFormatId());
468 500
        }
......
479 511
            return false;
480 512
        }
481 513
    }
514
    /**
515
     * Remove the indexed associated with specified pid.
516
     * @param pid  the pid which the indexes are associated with
517
     * @throws IOException
518
     * @throws SolrServerException
519
     * @throws ParserConfigurationException 
520
     * @throws SAXException 
521
     * @throws UnsupportedType 
522
     * @throws NotFound 
523
     * @throws NotImplemented 
524
     * @throws XPathExpressionException 
525
     * @throws ServiceFailure 
526
     * @throws OREParserException 
527
     */
528
    private void remove(String pid, SystemMetadata sysmeta) throws IOException, SolrServerException, ServiceFailure, XPathExpressionException, NotImplemented, NotFound, UnsupportedType, SAXException, ParserConfigurationException, OREParserException {
529
        if (isDataPackage(pid, sysmeta)) {
530
            removeDataPackage(pid);
531
        } else if (isPartOfDataPackage(pid)) {
532
            removeFromDataPackage(pid);
533
        } else {
534
            removeFromIndex(pid);
535
        }
536
    }
537
    
538
    /*
539
     * Remove a resource map pid
540
     */
541
    private void removeDataPackage(String pid) throws ServiceFailure, SAXException, XPathExpressionException, NotImplemented, NotFound, UnsupportedType, SolrServerException, IOException, ParserConfigurationException, OREParserException  {
542
        Document resourceMapDoc = generateXmlDocument(DistributedMapsFactory.getDataObject(pid));
543
        //ResourceMap resourceMap = new ResourceMap(resourceMapDoc);
544
        ResourceMap resourceMap = ResourceMapFactory.buildResourceMap(resourceMapDoc);
545
        List<String> documentIds = resourceMap.getAllDocumentIDs();
546
        List<SolrDoc> indexDocuments =ResourceMapSubprocessor.getSolrDocs(documentIds);
547
        removeFromIndex(pid);
548
        //List<SolrDoc> docsToUpdate = new ArrayList<SolrDoc>();
549
        // for each document in data package:
550
        for (SolrDoc indexDoc : indexDocuments) {
482 551

  
552
            if (indexDoc.getIdentifier().equals(pid)) {
553
                continue; // skipping the resource map, no need update
554
                          // it.
555
                          // will
556
                          // be removed.
557
            }
558

  
559
            // Remove resourceMap reference
560
            indexDoc.removeFieldsWithValue(SolrElementField.FIELD_RESOURCEMAP,
561
                    resourceMap.getIdentifier());
562

  
563
            // // Remove documents/documentedby values for this resource
564
            // map
565
            for (ResourceEntry entry : resourceMap.getMappedReferences()) {
566
                if (indexDoc.getIdentifier().equals(entry.getIdentifier())) {
567
                    for (String documentedBy : entry.getDocumentedBy()) {
568
                        // Using removeOneFieldWithValue in-case same
569
                        // documents
570
                        // are in more than one data package. just
571
                        // remove
572
                        // one
573
                        // instance of data package info.
574
                        indexDoc.removeOneFieldWithValue(SolrElementField.FIELD_ISDOCUMENTEDBY,
575
                                documentedBy);
576
                    }
577
                    for (String documents : entry.getDocuments()) {
578
                        indexDoc.removeOneFieldWithValue(SolrElementField.FIELD_DOCUMENTS,
579
                                documents);
580
                    }
581
                    break;
582
                }
583
            }
584
            removeFromIndex(indexDoc.getIdentifier());
585
            insertToIndex(indexDoc);
586
            //docsToUpdate.add(indexDoc);
587
        }
588
        //SolrElementAdd addCommand = new SolrElementAdd(docsToUpdate);
589
        //httpService.sendUpdate(solrIndexUri, addCommand);
590
    }
591

  
592
    private void removeFromDataPackage(String pid) throws XPathExpressionException, NotImplemented, NotFound, UnsupportedType, SolrServerException, IOException, ParserConfigurationException, SAXException  {
593
        SolrDoc indexedDoc = ResourceMapSubprocessor.getSolrDoc(pid);
594
        removeFromIndex(pid);
595
        List<SolrDoc> docsToUpdate = new ArrayList<SolrDoc>();
596

  
597
        List<String> documents = indexedDoc.getAllFieldValues(SolrElementField.FIELD_DOCUMENTS);
598
        for (String documentsValue : documents) {
599
            SolrDoc solrDoc = ResourceMapSubprocessor.getSolrDoc(documentsValue);
600
            solrDoc.removeFieldsWithValue(SolrElementField.FIELD_ISDOCUMENTEDBY, pid);
601
            removeFromIndex(documentsValue);
602
            insertToIndex(solrDoc);
603
        }
604

  
605
        List<String> documentedBy = indexedDoc
606
                .getAllFieldValues(SolrElementField.FIELD_ISDOCUMENTEDBY);
607
        for (String documentedByValue : documentedBy) {
608
            SolrDoc solrDoc = ResourceMapSubprocessor.getSolrDoc(documentedByValue);
609
            solrDoc.removeFieldsWithValue(SolrElementField.FIELD_DOCUMENTS, documentedByValue);
610
            //docsToUpdate.add(solrDoc);
611
            removeFromIndex(documentedByValue);
612
            insertToIndex(solrDoc);
613
        }
614

  
615
        //SolrElementAdd addCommand = new SolrElementAdd(docsToUpdate);
616
        //httpService.sendUpdate(solrIndexUri, addCommand);
617
    }
618

  
619
    /*
620
     * Remove a pid from the solr index
621
     */
622
    private void removeFromIndex(String pid) throws SolrServerException, IOException {
623
        if(pid != null && !pid.trim().equals("")) {
624
            /*IndexEvent event = new IndexEvent();
625
            event.setDate(Calendar.getInstance().getTime());
626
            Identifier identifier = new Identifier();
627
            identifier.setValue(pid);
628
            event.setIdentifier(identifier);*/
629
            try {
630
                solrServer.deleteById(pid);
631
                solrServer.commit();
632
                /*event.setType(IndexEvent.SUCCESSDELETE);
633
                event.setDescription("Successfully remove the solr index for the id "+identifier.getValue());
634
                try {
635
                    EventlogFactory.createIndexEventLog().write(event);
636
                } catch (Exception e) {
637
                    log.error("SolrIndex.removeFromIndex - IndexEventLog can't log the index deleting event :"+e.getMessage());
638
                }*/
639
            } catch (SolrServerException e) {
640
                /*event.setAction(Event.DELETE);
641
                event.setDescription("Failurely remove the solr index for the id "+identifier.getValue()+" since "+e.getMessage());
642
                try {
643
                    EventlogFactory.createIndexEventLog().write(event);
644
                } catch (Exception ee) {
645
                    log.error("SolrIndex.removeFromIndex - IndexEventLog can't log the index deleting event :"+ee.getMessage());
646
                }*/
647
                throw e;
648
                
649
            } catch (IOException e) {
650
                /*event.setAction(Event.DELETE);
651
                event.setDescription("Failurely remove the solr index for the id "+identifier.getValue()+" since "+e.getMessage());
652
                try {
653
                    EventlogFactory.createIndexEventLog().write(event);
654
                } catch (Exception ee) {
655
                    log.error("SolrIndex.removeFromIndex - IndexEventLog can't log the index deleting event :"+ee.getMessage());
656
                }*/
657
                throw e;
658
            }
659
            
660
        }
661
    }
662

  
483 663
    /**
484 664
     * Get the solrServer
485 665
     * @return
......
517 697
        }
518 698
        return list;
519 699
    }
700
    
701
    private void writeEventLog(SystemMetadata systemMetadata, Identifier pid, String error) {
702
        IndexEvent event = new IndexEvent();
703
        event.setIdentifier(pid);
704
        event.setDate(Calendar.getInstance().getTime());
705
        String action = null;
706
        if (systemMetadata == null ) {
707
            action = Event.CREATE.xmlValue();
708
            event.setAction(Event.CREATE);
709
        }
710
        else if(systemMetadata.getArchived()) {
711
            action = Event.DELETE.xmlValue();
712
            event.setAction(Event.DELETE);
713
        } else {
714
            action = Event.CREATE.xmlValue();
715
            event.setAction(Event.CREATE);
716
        }
717
        event.setDescription("Failed to "+action+"the solr index for the id "+pid.getValue()+" since "+error);
718
        try {
719
            EventlogFactory.createIndexEventLog().write(event);
720
        } catch (Exception ee) {
721
            log.error("SolrIndex.insertToIndex - IndexEventLog can't log the index inserting event :"+ee.getMessage());
722
        }
723
    }
520 724
}

Also available in: Unified diff