Project

General

Profile

« Previous | Next » 

Revision 8854

Added by Jing Tao over 10 years ago

Make the delete method work.

View differences:

src/edu/ucsb/nceas/metacat/DocumentImpl.java
3315 3315
        int serialNumber = -1;
3316 3316
        PreparedStatement pstmt = null;
3317 3317
        boolean isXML   = true;
3318
        boolean inRevisionTable = false;
3318 3319
        try {
3319 3320
            //check out DBConnection
3320 3321
            conn = DBConnectionPool.getDBConnection("DocumentImpl.delete");
......
3326 3327
            int rev = DocumentUtil.getRevisionFromAccessionNumber(accnum);;
3327 3328

  
3328 3329
            // Check if the document exists.
3329
            pstmt = conn.prepareStatement("SELECT * FROM xml_documents WHERE docid = ?");
3330
            pstmt.setString(1, docid);
3331
            logMetacat.debug("DocumentImpl.delete - executing SQL: " + pstmt.toString());
3332
            pstmt.execute();
3333
            ResultSet rs = pstmt.getResultSet();
3334
            if(!rs.next()){
3335
                rs.close();
3336
                pstmt.close();
3337
                conn.increaseUsageCount(1);
3338
                throw new McdbDocNotFoundException("Docid " + accnum  + 
3339
                  " does not exist. Please check that you have also " +
3340
                  "specified the revision number of the document.");
3341
            }
3342
            rs.close();
3343
            pstmt.close();
3344
            conn.increaseUsageCount(1);
3330
            if(!removeAll) {
3331
            	//this only archives a document from xml_documents to xml_revisions (also archive the xml_nodes table as well)
3332
            	 logMetacat.info("DocumentImp.delete - archive the document "+accnum);
3333
            	 pstmt = conn.prepareStatement("SELECT rev, docid FROM xml_documents WHERE docid = ?");
3334
            	 pstmt.setString(1, docid);
3335
                 logMetacat.debug("DocumentImpl.delete - executing SQL: " + pstmt.toString());
3336
                 pstmt.execute();
3337
                 ResultSet rs = pstmt.getResultSet();
3338
                 if(!rs.next()){
3339
                	 rs.close();
3340
                     pstmt.close();
3341
                     conn.increaseUsageCount(1);                 	
3342
                     throw new McdbDocNotFoundException("Docid " + accnum  + 
3343
                           " does not exist. Please check that you have also " +
3344
                           "specified the revision number of the document.");
3345
                 } else {
3346
                	//Get the rev from the xml_table. If the value is greater than the one user specified, we will use this one.
3347
                	 //In ReplicationHandler.handleDeleteSingleDocument method, the code use "1" as the revision number not matther what is the actual value
3348
                	 int revFromTable = rs.getInt(1);
3349
                	 if(revFromTable > rev) {
3350
                		 logMetacat.info("DocumentImpl.delete - in the archive the user specified rev - "+rev +"is less than the version in xml_document table - "+revFromTable+
3351
                				 ". We will use the one from table.");
3352
                		 rev = revFromTable;             		 
3353
                	 }
3354
                	 rs.close();
3355
                     pstmt.close();
3356
                     conn.increaseUsageCount(1);
3357
                	 
3358
                 }
3359
            } else {          	
3360
            	logMetacat.info("DocumentImp.delete - complete delete the document "+accnum);
3361
           	 	pstmt = conn.prepareStatement("SELECT * FROM xml_documents WHERE docid = ? and rev = ?");
3362
           	 	pstmt.setString(1, docid);
3363
           	 	pstmt.setInt(2, rev);
3364
                logMetacat.debug("DocumentImpl.delete - executing SQL: " + pstmt.toString());
3365
                pstmt.execute();
3366
                ResultSet rs = pstmt.getResultSet();
3367
                if(!rs.next()){
3368
                	//look at the xml_revisions table
3369
            		logMetacat.debug("DocumentImpl.delete - look at the docid "+ accnum+" in the xml_revision table");
3370
            		 pstmt = conn.prepareStatement("SELECT * FROM xml_revisions WHERE docid = ? AND rev = ?");
3371
                     pstmt.setString(1, docid);
3372
                     pstmt.setInt(2, rev);
3373
                     logMetacat.debug("DocumentImpl.delete - executing SQL: " + pstmt.toString());
3374
                     pstmt.execute();
3375
                     rs = pstmt.getResultSet();
3376
                     if(!rs.next()) {
3377
                    	 rs.close();
3378
                         pstmt.close();
3379
                         conn.increaseUsageCount(1);
3380
                         throw new McdbDocNotFoundException("Docid " + accnum  + 
3381
                               " does not exist. Please check and try to delete it again.");
3382
                     } else {
3383
                    	 rs.close();
3384
                         pstmt.close();
3385
                         conn.increaseUsageCount(1);
3386
                    	 inRevisionTable=true;
3387
                     }
3388
                } else {
3389
                	 rs.close();
3390
                     pstmt.close();
3391
                     conn.increaseUsageCount(1);               	
3392
                }
3393
            }     
3345 3394

  
3346 3395
            // get the type of deleting docid, this will be used in forcereplication
3347
            String type = getDocTypeFromDB(conn, docid);
3396
            String type = null;
3397
            if(!inRevisionTable) {
3398
            	type = getDocTypeFromDB(conn, "xml_documents", docid);
3399
            } else {
3400
            	type = getDocTypeFromDB(conn, "xml_revisions", docid);
3401
            }
3402
            logMetacat.info("DocumentImpl.delete - the deleting doc type is " + type+ "...");
3348 3403
            if (type != null && type.trim().equals("BIN")) {
3349 3404
            	isXML = false;
3350 3405
            }
......
3362 3417
            }
3363 3418

  
3364 3419
            conn.setAutoCommit(false);
3365
            
3366
            // Copy the record to the xml_revisions table if not a full delete
3367
            if (!removeAll) {
3368
            	DocumentImpl.archiveDocAndNodesRevision(conn, docid, user, null);
3369
                logMetacat.info("DocumentImpl.delete - calling archiveDocAndNodesRevision");
3370

  
3371
            }
3372
            double afterArchiveDocAndNode = System.currentTimeMillis()/1000;
3373
            logMetacat.info("DocumentImpl.delete - The time for archiveDocAndNodesRevision is "+(afterArchiveDocAndNode - start));
3374
            
3375 3420
            // make sure we don't have a pending index task
3376 3421
            removeDocidFromIndexingQueue(docid, String.valueOf(rev));
3377
            
3378
            // Now delete it from the xml_index table
3379
            pstmt = conn.prepareStatement("DELETE FROM xml_index WHERE docid = ?");
3380
            pstmt.setString(1, docid);
3381
            logMetacat.debug("DocumentImpl.delete - executing SQL: " + pstmt.toString());
3382
            pstmt.execute();
3383
            pstmt.close();
3384
            conn.increaseUsageCount(1);
3385
            
3386
            double afterDeleteIndex = System.currentTimeMillis()/1000;
3387
            logMetacat.info("DocumentImpl.delete - The deleting xml_index time is "+(afterDeleteIndex - afterArchiveDocAndNode ));
3388
            
3389
            // Now delete it from xml_access table
3390
            /*************** DO NOT DELETE ACCESS - need to archive this ******************/
3391
            double afterDeleteXmlAccess2 = System.currentTimeMillis()/1000;
3392
            /******* END DELETE ACCESS *************/            
3393
            
3394
            // Delete enteries from xml_queryresult
3395
            try {
3396
                XMLQueryresultAccess xmlQueryresultAccess = new XMLQueryresultAccess();
3397
                xmlQueryresultAccess.deleteXMLQueryresulForDoc(docid);
3398
            } catch (AccessException ae) {
3399
            	throw new SQLException("Problem deleting xml query result for docid " + 
3400
            			docid + " : " + ae.getMessage());
3401
            }
3402
            double afterDeleteQueryResult = System.currentTimeMillis()/1000;
3403
            logMetacat.info("DocumentImpl.delete - The deleting xml_queryresult time is "+(afterDeleteQueryResult - afterDeleteXmlAccess2));
3404
            // Delete it from relation table
3405
            pstmt = conn.prepareStatement("DELETE FROM xml_relation WHERE docid = ?");
3406
            //increase usage count
3407
            pstmt.setString(1, docid);
3408
            logMetacat.debug("DocumentImpl.delete - running sql: " + pstmt.toString());
3409
            pstmt.execute();
3410
            pstmt.close();
3411
            conn.increaseUsageCount(1);
3412
            double afterXMLRelation = System.currentTimeMillis()/1000;
3413
            logMetacat.info("DocumentImpl.delete - The deleting time  relation is "+ (afterXMLRelation - afterDeleteQueryResult) );
3422
            if(!inRevisionTable) {
3423
            	   // Copy the record to the xml_revisions table if not a full delete
3424
                if (!removeAll) {
3425
                	DocumentImpl.archiveDocAndNodesRevision(conn, docid, user, null);
3426
                    logMetacat.info("DocumentImpl.delete - calling archiveDocAndNodesRevision");
3414 3427

  
3415
            // Delete it from xml_path_index table
3416
            logMetacat.info("DocumentImpl.delete - deleting from xml_path_index");
3417
            pstmt = conn.prepareStatement("DELETE FROM xml_path_index WHERE docid = ?");
3418
            //increase usage count
3419
            pstmt.setString(1, docid);
3420
            logMetacat.debug("DocumentImpl.delete - running sql: " + pstmt.toString());
3421
            pstmt.execute();
3422
            pstmt.close();
3423
            conn.increaseUsageCount(1);
3428
                }
3429
                double afterArchiveDocAndNode = System.currentTimeMillis()/1000;
3430
                logMetacat.info("DocumentImpl.delete - The time for archiveDocAndNodesRevision is "+(afterArchiveDocAndNode - start));
3431
                       
3432
                // Now delete it from the xml_index table
3433
                pstmt = conn.prepareStatement("DELETE FROM xml_index WHERE docid = ?");
3434
                pstmt.setString(1, docid);
3435
                logMetacat.debug("DocumentImpl.delete - executing SQL: " + pstmt.toString());
3436
                pstmt.execute();
3437
                pstmt.close();
3438
                conn.increaseUsageCount(1);
3439
                
3440
                double afterDeleteIndex = System.currentTimeMillis()/1000;
3441
                logMetacat.info("DocumentImpl.delete - The deleting xml_index time is "+(afterDeleteIndex - afterArchiveDocAndNode ));
3442
                
3443
                // Now delete it from xml_access table
3444
                /*************** DO NOT DELETE ACCESS - need to archive this ******************/
3445
                double afterDeleteXmlAccess2 = System.currentTimeMillis()/1000;
3446
                /******* END DELETE ACCESS *************/            
3447
                
3448
                // Delete enteries from xml_queryresult
3449
                try {
3450
                    XMLQueryresultAccess xmlQueryresultAccess = new XMLQueryresultAccess();
3451
                    xmlQueryresultAccess.deleteXMLQueryresulForDoc(docid);
3452
                } catch (AccessException ae) {
3453
                	throw new SQLException("Problem deleting xml query result for docid " + 
3454
                			docid + " : " + ae.getMessage());
3455
                }
3456
                double afterDeleteQueryResult = System.currentTimeMillis()/1000;
3457
                logMetacat.info("DocumentImpl.delete - The deleting xml_queryresult time is "+(afterDeleteQueryResult - afterDeleteXmlAccess2));
3458
                // Delete it from relation table
3459
                pstmt = conn.prepareStatement("DELETE FROM xml_relation WHERE docid = ?");
3460
                //increase usage count
3461
                pstmt.setString(1, docid);
3462
                logMetacat.debug("DocumentImpl.delete - running sql: " + pstmt.toString());
3463
                pstmt.execute();
3464
                pstmt.close();
3465
                conn.increaseUsageCount(1);
3466
                double afterXMLRelation = System.currentTimeMillis()/1000;
3467
                logMetacat.info("DocumentImpl.delete - The deleting time  relation is "+ (afterXMLRelation - afterDeleteQueryResult) );
3424 3468

  
3425
            logMetacat.info("DocumentImpl.delete - deleting from xml_accesssubtree");
3426
            // Delete it from xml_accesssubtree table
3427
            pstmt = conn.prepareStatement("DELETE FROM xml_accesssubtree WHERE docid = ?");
3428
            //increase usage count
3429
            pstmt.setString(1, docid);
3430
            logMetacat.debug("DocumentImpl.delete - running sql: " + pstmt.toString());
3431
            pstmt.execute();
3432
            pstmt.close();
3433
            conn.increaseUsageCount(1);
3469
                // Delete it from xml_path_index table
3470
                logMetacat.info("DocumentImpl.delete - deleting from xml_path_index");
3471
                pstmt = conn.prepareStatement("DELETE FROM xml_path_index WHERE docid = ?");
3472
                //increase usage count
3473
                pstmt.setString(1, docid);
3474
                logMetacat.debug("DocumentImpl.delete - running sql: " + pstmt.toString());
3475
                pstmt.execute();
3476
                pstmt.close();
3477
                conn.increaseUsageCount(1);
3434 3478

  
3435
            // Delete it from xml_documents table
3436
            logMetacat.info("DocumentImpl.delete - deleting from xml_documents");
3437
            pstmt = conn.prepareStatement("DELETE FROM xml_documents WHERE docid = ?");
3438
            pstmt.setString(1, docid);
3439
            logMetacat.debug("DocumentImpl.delete - running sql: " + pstmt.toString());
3440
            pstmt.execute();
3441
            pstmt.close();
3442
            //Usaga count increase 1
3443
            conn.increaseUsageCount(1);
3444
            double afterDeleteDoc = System.currentTimeMillis()/1000;
3445
            logMetacat.info("DocumentImpl.delete - the time to delete  xml_path_index,  xml_accesssubtree, xml_documents time is "+ 
3446
            		(afterDeleteDoc - afterXMLRelation ));
3447
            // Delete the old nodes in xml_nodes table...
3448
            pstmt = conn.prepareStatement("DELETE FROM xml_nodes WHERE docid = ?");
3479
                logMetacat.info("DocumentImpl.delete - deleting from xml_accesssubtree");
3480
                // Delete it from xml_accesssubtree table
3481
                pstmt = conn.prepareStatement("DELETE FROM xml_accesssubtree WHERE docid = ?");
3482
                //increase usage count
3483
                pstmt.setString(1, docid);
3484
                logMetacat.debug("DocumentImpl.delete - running sql: " + pstmt.toString());
3485
                pstmt.execute();
3486
                pstmt.close();
3487
                conn.increaseUsageCount(1);
3449 3488

  
3450
            // Increase dbconnection usage count
3451
            conn.increaseUsageCount(1);
3452
            // Bind the values to the query and execute it
3453
            pstmt.setString(1, docid);
3454
            logMetacat.debug("DocumentImpl.delete - running sql: " + pstmt.toString());
3455
            pstmt.execute();
3456
            pstmt.close();
3489
                // Delete it from xml_documents table
3490
                logMetacat.info("DocumentImpl.delete - deleting from xml_documents");
3491
                pstmt = conn.prepareStatement("DELETE FROM xml_documents WHERE docid = ?");
3492
                pstmt.setString(1, docid);
3493
                logMetacat.debug("DocumentImpl.delete - running sql: " + pstmt.toString());
3494
                pstmt.execute();
3495
                pstmt.close();
3496
                //Usaga count increase 1
3497
                conn.increaseUsageCount(1);
3498
                double afterDeleteDoc = System.currentTimeMillis()/1000;
3499
                logMetacat.info("DocumentImpl.delete - the time to delete  xml_path_index,  xml_accesssubtree, xml_documents time is "+ 
3500
                		(afterDeleteDoc - afterXMLRelation ));
3501
                // Delete the old nodes in xml_nodes table...
3502
                pstmt = conn.prepareStatement("DELETE FROM xml_nodes WHERE docid = ?");
3457 3503

  
3458
            double afterDeleteXMLNodes = System.currentTimeMillis()/1000;
3459
            logMetacat.info("DocumentImpl.delete - Deleting xml_nodes time is "+(afterDeleteXMLNodes-afterDeleteDoc));
3460
            
3461
            // remove the file if called for
3462
            if (removeAll) {
3463
            	deleteFromFileSystem(accnum, isXML);
3504
                // Increase dbconnection usage count
3505
                conn.increaseUsageCount(1);
3506
                // Bind the values to the query and execute it
3507
                pstmt.setString(1, docid);
3508
                logMetacat.debug("DocumentImpl.delete - running sql: " + pstmt.toString());
3509
                pstmt.execute();
3510
                pstmt.close();
3511

  
3512
                double afterDeleteXMLNodes = System.currentTimeMillis()/1000;
3513
                logMetacat.info("DocumentImpl.delete - Deleting xml_nodes time is "+(afterDeleteXMLNodes-afterDeleteDoc));
3514
            } else {
3515
            	long rootnodeid = getRevisionRootNodeId(conn, docid, rev);
3516
            	logMetacat.info("DocumentImpl.delete - deleting from xml_revisions");
3517
                pstmt = conn.prepareStatement("DELETE FROM xml_revisions WHERE docid = ? AND rev = ?");
3518
                pstmt.setString(1, docid);
3519
                pstmt.setInt(2, rev);
3520
                logMetacat.debug("DocumentImpl.delete - running sql: " + pstmt.toString());
3521
                pstmt.execute();
3522
                pstmt.close();
3523
                conn.increaseUsageCount(1);
3524
                
3525
                logMetacat.info("DocumentImpl.delete - deleting from xml_nodes_revisions");
3526
                pstmt = conn.prepareStatement("DELETE FROM xml_nodes_revisions WHERE rootnodeid = ?");
3527
                pstmt.setLong(1, rootnodeid);
3528
                logMetacat.debug("DocumentImpl.delete - running sql: " + pstmt.toString());
3529
                pstmt.execute();
3530
                pstmt.close();
3531
                conn.increaseUsageCount(1);
3464 3532
            }
3465 3533
            
3466
            // set as archived in the systemMetadata 
3534
                
3535
            // set as archived in the systemMetadata  if it is not a complete removal
3467 3536
            String pid = IdentifierManager.getInstance().getGUID(docid, rev);
3468 3537
            Identifier guid = new Identifier();
3469
        	guid.setValue(pid);
3470
            SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(guid);
3471
            if (sysMeta != null) {
3472
				sysMeta.setSerialVersion(sysMeta.getSerialVersion().add(BigInteger.ONE));
3473
				sysMeta.setArchived(true);
3474
            	sysMeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
3475
				HazelcastService.getInstance().getSystemMetadataMap().put(guid, sysMeta);
3476
                MetacatSolrIndex.getInstance().submit(guid, sysMeta, null, false);
3538
        	guid.setValue(pid);          
3539
            
3540
            //removal the access rules for removeAll.
3541
            if(removeAll) {
3542
            	
3543
                logMetacat.info("DocumentImpl.delete - deleting from xml_access");
3544
                pstmt = conn.prepareStatement("DELETE FROM xml_access WHERE guid = ?");
3545
                pstmt.setString(1, guid.getValue());
3546
                logMetacat.debug("DocumentImpl.delete - running sql: " + pstmt.toString());
3547
                pstmt.execute();
3548
                pstmt.close();
3549
                //Usaga count increase 1
3550
                conn.increaseUsageCount(1);             
3551
            	
3477 3552
            }
3478 3553
            
3554
            
3479 3555
            // clear cache after inserting or updating a document
3480 3556
            if (PropertyService.getProperty("database.queryCacheOn").equals("true")) {
3481 3557
            	//System.out.println("the string stored into cache is "+ resultsetBuffer.toString());
3482 3558
            	DBQuery.clearQueryResultCache();
3483 3559
            }
3484 3560
            
3561
            //update systemmetadata table and solr index
3562
            SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(guid);
3563
            if (sysMeta != null) {
3564
    				sysMeta.setSerialVersion(sysMeta.getSerialVersion().add(BigInteger.ONE));
3565
    				sysMeta.setArchived(true);
3566
                	sysMeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
3567
                	if(!removeAll) {
3568
                		HazelcastService.getInstance().getSystemMetadataMap().put(guid, sysMeta);
3569
                	} else { 
3570
                		try {
3571
                			logMetacat.debug("the system metadata contains the key - guid "+guid.getValue()+" before removing is "+HazelcastService.getInstance().getSystemMetadataMap().containsKey(guid));
3572
                			HazelcastService.getInstance().getSystemMetadataMap().remove(guid);
3573
                			logMetacat.debug("the system metadata contains the guid "+guid.getValue()+" after removing is "+HazelcastService.getInstance().getSystemMetadataMap().containsKey(guid));
3574
                		} catch (RuntimeException ee) {
3575
                			logMetacat.warn("we catch the run time exception in deleting system metadata "+ee.getMessage());
3576
                			throw new Exception("DocumentImpl.delete -"+ee.getMessage());
3577
                		}	
3578
                	}              	
3579
                    MetacatSolrIndex.getInstance().submit(guid, sysMeta, null, false);
3580
            }
3581
            
3485 3582
            // only commit if all of this was successful
3486 3583
            conn.commit();
3487 3584
            conn.setAutoCommit(true);
3585
            
3586
            // remove the file if called for
3587
            if (removeAll) {
3588
            	logMetacat.debug("the identifier set contains "+guid.getValue()+" is "+HazelcastService.getInstance().getIdentifiers().contains(guid));
3589
            	HazelcastService.getInstance().getIdentifiers().remove(guid);
3590
            	logMetacat.debug("the identifier set contains "+guid.getValue()+" after removing is "+HazelcastService.getInstance().getIdentifiers().contains(guid));
3591
            	deleteFromFileSystem(accnum, isXML);
3592
            }
3488 3593
                        
3489 3594
            // add force delete replcation document here.
3490 3595
            String deleteAction = ReplicationService.FORCEREPLICATEDELETE;
......
3520 3625

  
3521 3626
    }
3522 3627

  
3523
    private static String getDocTypeFromDB(DBConnection conn, String docidWithoutRev)
3628
    private static String getDocTypeFromDB(DBConnection conn, String tableName, String docidWithoutRev)
3524 3629
                                 throws SQLException
3525 3630
    {
3526 3631
      String type = null;
3527
      String sql = "SELECT DOCTYPE FROM xml_documents WHERE docid LIKE ?";
3632
      String sql = "SELECT DOCTYPE FROM "+tableName+" WHERE docid LIKE ?";
3528 3633
      PreparedStatement stmt = null;
3529 3634
      stmt = conn.prepareStatement(sql);
3530 3635
      stmt.setString(1, docidWithoutRev);
......
4527 4632
        logMetacat.info("DocumentImpl.deleteXMLNodes - The time to delete xml_nodes in UPDATE is "+(end -start));
4528 4633
     
4529 4634
    }
4635
    
4636
    /*
4637
     * Get the root node id from xml_revisions table by a specified docid and rev
4638
     */
4639
    private static long getRevisionRootNodeId(DBConnection conn, String docid, int rev) throws SQLException {
4640
    	long rootnodeid = -1;
4641
        String sql = "SELECT rootnodeid FROM xml_revisions WHERE docid = ? and rev = ?";
4642
        PreparedStatement stmt = null;
4643
        stmt = conn.prepareStatement(sql);
4644
        stmt.setString(1, docid);
4645
        stmt.setInt(2,  rev);
4646
        ResultSet result = stmt.executeQuery();
4647
        boolean hasResult = result.next();
4648
        if (hasResult)
4649
        {
4650
          rootnodeid = result.getLong(1);
4651
        }
4652
        logMetacat.debug("DocumentImpl.getRevisionRootNodeId - The root node id of docid " + docid+"."+rev +
4653
                                 " is " + rootnodeid);
4654
        return rootnodeid;
4655
    }
4530 4656
}

Also available in: Unified diff