Changeset 7125


Ignore:
Timestamp:
Apr 20, 2016, 8:28:12 AM (5 years ago)
Author:
Nicklas Nordborg
Message:

References #2000: Batch API for annotation handling

Added a cache with unit converters so we don't have to create a new converter for every value.

More cleanup when closing the batcher.

Added setValue() method for setting single-valued annotations.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/core/AnnotationBatcher.java

    r7124 r7125  
    2929import java.util.ArrayList;
    3030import java.util.Collection;
     31import java.util.Collections;
    3132import java.util.Date;
    3233import java.util.HashMap;
     
    147148  private final List<Integer> snapshotsToDelete;
    148149 
     150  // Cache with unit converters so that we don't need to create
     151  // new ones all the time
     152  private final Map<String, UnitConverter> unitConvertes;
     153 
    149154  /**
    150155    Create a new batcher instance.
     
    183188    // Maps AnnotationType.id -> Annotation information
    184189    this.currentInfo = new HashMap<Integer, CurrentAnnotationInfo>();
     190    this.unitConvertes = new HashMap<String, UnitConverter>();
    185191   
    186192    this.allUpdateBatchers = new ArrayList<UpdateBatcher>();
     
    331337      b.close();
    332338    }
     339    allUpdateBatchers.clear();
    333340    for (InsertBatcher b : allInsertBatchers)
    334341    {
    335342      b.close();
    336343    }
     344    allInsertBatchers.clear();
    337345    for (Integer id : snapshotsToDelete)
    338346    {
    339347      SnapshotManager.removeSnapshot(currentAnnotationSetId);
    340348    }
     349    snapshotsToDelete.clear();
     350    unitConvertes.clear();
     351    currentInfo.clear();
     352    currentItem = null;
    341353  }
    342354  // ---------------------------
     
    360372  }
    361373 
     374  /**
     375    Get/create a unit converter for converting between two units.
     376  */
     377  private UnitConverter getUnitConverter(Unit from, Unit to)
     378  {
     379    String key = from.getId() + ":" + to.getId();
     380    UnitConverter converter = unitConvertes.get(key);
     381    if (converter == null)
     382    {
     383      converter = to.getUnitConverter(from);
     384      unitConvertes.put(key, converter);
     385    }
     386    return converter;
     387  }
    362388 
    363389  /**
     
    519545 
    520546  /**
     547    Add, update or delete a single-valued annotation.
     548    See {@link #setValues(AnnotationType, List, Unit)} for
     549    more information.
     550  */
     551  public Change setValue(AnnotationType annotationType, Object value, Unit unit)
     552  {
     553    return setValues(annotationType, value == null ? null : Collections.singletonList(value), unit);
     554  }
     555 
     556  /**
    521557    Add, update or delete annotation values for the current item.
    522558    If the current item doesn't have any values for the annotation type
     
    610646        if (unit != null && info.defaultUnit != null && !unit.equals(info.defaultUnit))
    611647        {
    612           // Two different unit, convert the values to the default unit
    613           converter = info.defaultUnit.getUnitConverter(unit);
     648          // Two different units, convert the values to the default unit
     649          converter = getUnitConverter(unit, info.defaultUnit);
    614650        }
    615651       
Note: See TracChangeset for help on using the changeset viewer.