Changeset 13577


Ignore:
Timestamp:
2007-01-11T16:59:43+13:00 (15 years ago)
Author:
mdewsnip
Message:

Sped up repeated operations on the same metadata.xml file by keeping the last accessed metadata.xml file in memory. This greatly speeds up metadata access on large metadata.xml files, and when multiple files are selected in the Enrich pane.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/metadata/MetadataXMLFile.java

    r13278 r13577  
    4545    static final private String METADATA_ELEMENT = "Metadata";
    4646
     47    // To speed things up a bit we keep the last accessed metadata.xml file in memory
     48    static private File loaded_file = null;
     49    static private Document loaded_file_document = null;
     50
    4751
    4852    public MetadataXMLFile(String metadata_xml_file_path)
     
    5458    public void addMetadata(File file, ArrayList metadata_values)
    5559    {
    56     // Parse the metadata.xml file
    57     Document document = XMLTools.parseXMLFile(this);
    58     if (document == null) {
    59         System.err.println("Error: Could not parse metadata.xml file " + getAbsolutePath());
    60         return;
     60    // If this metadata.xml file isn't the one currently loaded, load it now
     61    if (loaded_file != this) {
     62        // Parse the metadata.xml file
     63        Document document = XMLTools.parseXMLFile(this);
     64        if (document == null) {
     65        System.err.println("Error: Could not parse metadata.xml file " + getAbsolutePath());
     66        return;
     67        }
     68
     69        loaded_file = this;
     70        loaded_file_document = document;
    6171    }
    6272
     
    8393
    8494    // Read all the FileSet elements in the file
    85     NodeList fileset_elements_nodelist = document.getElementsByTagName(FILESET_ELEMENT);
     95    NodeList fileset_elements_nodelist = loaded_file_document.getElementsByTagName(FILESET_ELEMENT);
    8696    for (int i = 0; i < fileset_elements_nodelist.getLength(); i++) {
    8797        Element current_fileset_element = (Element) fileset_elements_nodelist.item(i);
     
    104114    if (appropriate_fileset_element == null) {
    105115        DebugStream.println("Creating new FileSet element for file since none exists...");
    106         appropriate_fileset_element = document.createElement(FILESET_ELEMENT);
    107 
    108         Element new_filename_element = document.createElement(FILENAME_ELEMENT);
    109         new_filename_element.appendChild(document.createTextNode(file_path_regexp));
     116        appropriate_fileset_element = loaded_file_document.createElement(FILESET_ELEMENT);
     117
     118        Element new_filename_element = loaded_file_document.createElement(FILENAME_ELEMENT);
     119        new_filename_element.appendChild(loaded_file_document.createTextNode(file_path_regexp));
    110120        appropriate_fileset_element.appendChild(new_filename_element);
    111121
    112         Element new_description_element = document.createElement(DESCRIPTION_ELEMENT);
     122        Element new_description_element = loaded_file_document.createElement(DESCRIPTION_ELEMENT);
    113123        appropriate_fileset_element.appendChild(new_description_element);
    114124
    115         document.getDocumentElement().appendChild(appropriate_fileset_element);
     125        loaded_file_document.getDocumentElement().appendChild(appropriate_fileset_element);
    116126    }
    117127
     
    151161        if (!metadata_already_assigned) {
    152162        // Create a new Metadata element to record this metadata
    153         Element new_metadata_element = document.createElement(METADATA_ELEMENT);
     163        Element new_metadata_element = loaded_file_document.createElement(METADATA_ELEMENT);
    154164        new_metadata_element.setAttribute("name", metadata_value.getMetadataElement().getFullName());
    155165        new_metadata_element.setAttribute("mode", (metadata_value.isAccumulatingMetadata() ? "accumulate" : "override"));
    156         new_metadata_element.appendChild(document.createTextNode(metadata_value_string));
     166        new_metadata_element.appendChild(loaded_file_document.createTextNode(metadata_value_string));
    157167
    158168        // Accumulating metadata: add at the end
     
    168178
    169179    // Rewrite the metadata.xml file
    170     XMLTools.writeXMLFile(this, document);
     180    XMLTools.writeXMLFile(this, loaded_file_document);
    171181    }
    172182
     
    174184    public ArrayList getMetadataAssignedToFile(File file)
    175185    {
    176     // Parse the metadata.xml file
    177     Document document = XMLTools.parseXMLFile(this);
    178     if (document == null) {
    179         System.err.println("Error: Could not parse metadata.xml file " + getAbsolutePath());
    180         return new ArrayList();
     186    // If this metadata.xml file isn't the one currently loaded, load it now
     187    if (loaded_file != this) {
     188        // Parse the metadata.xml file
     189        Document document = XMLTools.parseXMLFile(this);
     190        if (document == null) {
     191        System.err.println("Error: Could not parse metadata.xml file " + getAbsolutePath());
     192        return new ArrayList();
     193        }
     194
     195        loaded_file = this;
     196        loaded_file_document = document;
    181197    }
    182198
     
    192208
    193209    // Read all the FileSet elements in the file
    194     NodeList fileset_elements_nodelist = document.getElementsByTagName(FILESET_ELEMENT);
     210    NodeList fileset_elements_nodelist = loaded_file_document.getElementsByTagName(FILESET_ELEMENT);
    195211    for (int i = 0; i < fileset_elements_nodelist.getLength(); i++) {
    196212        Element current_fileset_element = (Element) fileset_elements_nodelist.item(i);
     
    301317    public void removeMetadata(File file, ArrayList metadata_values)
    302318    {
    303     // Parse the metadata.xml file
    304     Document document = XMLTools.parseXMLFile(this);
    305     if (document == null) {
    306         System.err.println("Error: Could not parse metadata.xml file " + getAbsolutePath());
    307         return;
     319    // If this metadata.xml file isn't the one currently loaded, load it now
     320    if (loaded_file != this) {
     321        // Parse the metadata.xml file
     322        Document document = XMLTools.parseXMLFile(this);
     323        if (document == null) {
     324        System.err.println("Error: Could not parse metadata.xml file " + getAbsolutePath());
     325        return;
     326        }
     327
     328        loaded_file = this;
     329        loaded_file_document = document;
    308330    }
    309331
     
    330352
    331353    // Read all the FileSet elements in the file
    332     NodeList fileset_elements_nodelist = document.getElementsByTagName(FILESET_ELEMENT);
     354    NodeList fileset_elements_nodelist = loaded_file_document.getElementsByTagName(FILESET_ELEMENT);
    333355    for (int i = 0; i < fileset_elements_nodelist.getLength(); i++) {
    334356        Element current_fileset_element = (Element) fileset_elements_nodelist.item(i);
     
    392414
    393415    // Rewrite the metadata.xml file
    394     XMLTools.writeXMLFile(this, document);
     416    XMLTools.writeXMLFile(this, loaded_file_document);
    395417    }
    396418
     
    398420    public void replaceMetadata(File file, MetadataValue old_metadata_value, MetadataValue new_metadata_value)
    399421    {
    400     // Parse the metadata.xml file
    401     Document document = XMLTools.parseXMLFile(this);
    402     if (document == null) {
    403         System.err.println("Error: Could not parse metadata.xml file " + getAbsolutePath());
    404         return;
     422    // If this metadata.xml file isn't the one currently loaded, load it now
     423    if (loaded_file != this) {
     424        // Parse the metadata.xml file
     425        Document document = XMLTools.parseXMLFile(this);
     426        if (document == null) {
     427        System.err.println("Error: Could not parse metadata.xml file " + getAbsolutePath());
     428        return;
     429        }
     430
     431        loaded_file = this;
     432        loaded_file_document = document;
    405433    }
    406434
     
    434462
    435463    // Read all the FileSet elements in the file
    436     NodeList fileset_elements_nodelist = document.getElementsByTagName(FILESET_ELEMENT);
     464    NodeList fileset_elements_nodelist = loaded_file_document.getElementsByTagName(FILESET_ELEMENT);
    437465    for (int i = 0; i < fileset_elements_nodelist.getLength(); i++) {
    438466        Element current_fileset_element = (Element) fileset_elements_nodelist.item(i);
     
    497525
    498526    // Rewrite the metadata.xml file
    499     XMLTools.writeXMLFile(this, document);
     527    XMLTools.writeXMLFile(this, loaded_file_document);
    500528    }
    501529
Note: See TracChangeset for help on using the changeset viewer.