Package net.sf.basedb.core
Class AnnotationBatcher
java.lang.Object
net.sf.basedb.core.AbstractBatcher
net.sf.basedb.core.AnnotationBatcher
- All Implemented Interfaces:
AutoCloseable
,Batcher
Batcher implementation for adding, updating and removing multiple
annotations to a large number of items in a single transaction
while keeping the memory usage as low as possible.
The batcher bypasses Hibernate where it is possible and uses direct
SQL to access the database. There are some limitations:
- It can only work with items that are already in the database. It is not possible to use the batcher with items that are created in the same transaction.
- It can only work with items of a single
Item
type. - A item can only be processed once (eg.
setCurrentItem(Annotatable)
can only be called once for a given item. Annotatable.getAnnotationSet()
must not have been called on the item before using it with this batcher.- The annotation types to work with must be specified before the
first call to
setCurrentItem(Annotatable)
. It is not possible to work with other annotations. - The batcher only works with
Annotation.Source.PRIMARY
annotations. - The
setValues(AnnotationType, List, Unit, boolean)
can only be called once per item and annotation type.
- Since:
- 3.8
- Author:
- nicklas
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
A flag indicating what type of change that was made to an annotation as a result of callingsetValues(AnnotationType, List, Unit, boolean)
static class
Holds information about an annotation for the current item.(package private) static class
Batcher implementation for inserting into tables.(package private) static class
Batcher implementation for updating and deleting from tables. -
Field Summary
Modifier and TypeFieldDescriptionprivate final int
private final List<AnnotationBatcher.InsertBatcher>
private final List<AnnotationBatcher.UpdateBatcher>
private final Date
private int
private Annotatable
private final AnnotationBatcher.UpdateBatcher
private final AnnotationBatcher.UpdateBatcher
private final AnnotationBatcher.UpdateBatcher
private boolean
private final AnnotationBatcher.UpdateBatcher[]
private final Dialect
private final int[]
private boolean
private final AnnotationBatcher.InsertBatcher
private final AnnotationBatcher.InsertBatcher
private final AnnotationBatcher.InsertBatcher
private final AnnotationBatcher.UpdateBatcher[]
private final boolean
private final Item
private static final Logger
private final LoggingInterceptor
private final AnnotationBatcher.UpdateBatcher
private int
private final Integer
private final Cache
private final StatelessSession
private final Map<String,
UnitConverter> private final AnnotationBatcher.UpdateBatcher
private final AnnotationBatcher.UpdateBatcher
private final AnnotationBatcher.UpdateBatcher
Fields inherited from class net.sf.basedb.core.AbstractBatcher
debugSqlEnabled, logSql
-
Constructor Summary
ModifierConstructorDescriptionAnnotationBatcher
(DbControl dc, Item itemType) Create a new batcher instance.private
AnnotationBatcher
(DbControl dc, Item itemType, Integer projectId) Create a new batcher instance.AnnotationBatcher
(DbControl dc, Item itemType, Project project) Create a new batcher instance. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Add annotation types to work with.private AnnotationBatcher.InsertBatcher
createInsertBatcher
(Connection c, String sql, int... types) private AnnotationBatcher.UpdateBatcher
createUpdateBatcher
(Connection c, String sql, int... types) void
flush()
Flush the batchers.private UnitConverter
getUnitConverter
(Unit from, Unit to) Get/create a unit converter for converting between two units.protected void
Let a subclass cleanup itself before the connection to the database is closed.void
setCurrentItem
(Annotatable item) Set the current item to work with from now on.setValue
(AnnotationType annotationType, Object value, Unit unit, boolean merge) Add, update or delete a single-valued annotation.setValues
(AnnotationType annotationType, List<?> values, Unit unit, boolean merge) Add, update or delete annotation values for the current item.Methods inherited from class net.sf.basedb.core.AbstractBatcher
analyzeTable, close, getBatchSize, getDbControl, getSessionControl, isClosed, setBatchSize, setDbControl, updateLastAccess
-
Field Details
-
log
-
isDebugEnabled
private final boolean isDebugEnabled -
itemType
-
activeProjectId
private final int activeProjectId -
projectId
-
batchDate
-
statelessSession
-
dialect
-
secondLevelCache
-
loggingInterceptor
-
discriminators
private final int[] discriminators -
loadAnnotationInfo
-
loadAnnotationValues
-
insertIntoParameterValues
-
insertIntoAnnotations
-
updateAnnotations
-
updateOverrideId
-
insertIntoAnnotationSets
-
updateItemTable
-
allUpdateBatchers
-
allInsertBatchers
-
deleteFromAnnotations
-
deleteFromParameterValues
-
nullifyClonedReferences
-
deleteInheritedAnnotations
-
deleteValues
-
insertValues
-
numItems
private int numItems -
currentItem
-
currentAnnotationSetId
private int currentAnnotationSetId -
deleteSnapshotIfModifed
private boolean deleteSnapshotIfModifed -
evictFromSecondLevelCacheIfModified
private boolean evictFromSecondLevelCacheIfModified -
valueIds
-
currentInfo
-
snapshotsToDelete
-
unitConvertes
-
-
Constructor Details
-
AnnotationBatcher
Create a new batcher instance. Using this constructor creates a batcher that, for project-specific annotation types, automatically handles either the default or project-specific value depending on the currently active project and wheter a value already exists or not.- Parameters:
dc
- A DbControl to use for database accessitemType
- The type of items to work with. This must be an annotatable item type.
-
AnnotationBatcher
Create a new batcher instance. Using this constructor creates a batcher that, for project-specific annotation types, always work with the values for the given project (or the default values if project=null). The currently active project is ignored.- Parameters:
dc
- A DbControl to use for database accessitemType
- The type of items to work with. This must be an annotatable item type.project
- Specify a project to only consider values for that project when working with a project-specific annotation, or null to only consider default values- Since:
- 3.11
-
AnnotationBatcher
Create a new batcher instance.- Parameters:
dc
- A DbControl to use for database accessitemType
- The type of items to work with. This must be an annotatable item type.projectId
- Specify a project id to only consider values for that project when working with a project-specific annotation, or 0 to only consider default values, or null to automatically use the default or project-specific values for the currently active project- Since:
- 3.11
-
-
Method Details
-
flush
public void flush()Flush the batchers.- See Also:
-
onBeforeClose
protected void onBeforeClose()Description copied from class:AbstractBatcher
Let a subclass cleanup itself before the connection to the database is closed. This method is called by theAbstractBatcher.close()
method afterBatcher.flush()
has been called but before the connection to the database is lost. Typical use of this method is to update properties on parent object (for example, spot count) and to close open SQL statements.- Overrides:
onBeforeClose
in classAbstractBatcher
-
createInsertBatcher
private AnnotationBatcher.InsertBatcher createInsertBatcher(Connection c, String sql, int... types) throws SQLException - Throws:
SQLException
-
createUpdateBatcher
private AnnotationBatcher.UpdateBatcher createUpdateBatcher(Connection c, String sql, int... types) throws SQLException - Throws:
SQLException
-
getUnitConverter
Get/create a unit converter for converting between two units. -
addAnnotationTypes
Add annotation types to work with. It is possible to call this method multiple times, but it must be done beforesetCurrentItem(Annotatable)
is called. The annotation types must be enabled for use with the item type this batcher is working with and the current user must have USE permission for all annotation types. -
setCurrentItem
Set the current item to work with from now on. Before calling this method theaddAnnotationTypes(Collection)
must have specified the annotation types to work with. The item must not have been used with this method before and theAnnotatable.getAnnotationSet()
method must not have been called on the item. If the item can be used, the batcher will immediately load all existing annotations and their values and prepare for adding, updating or removing values. -
setValue
public AnnotationBatcher.Change setValue(AnnotationType annotationType, Object value, Unit unit, boolean merge) Add, update or delete a single-valued annotation. SeesetValues(AnnotationType, List, Unit, boolean)
for more information. -
setValues
public AnnotationBatcher.Change setValues(AnnotationType annotationType, List<?> values, Unit unit, boolean merge) Add, update or delete annotation values for the current item. If the current item doesn't have any values for the annotation type the outcome is one of the following:- ADDED: if there is at least one value in the list
- NO_CHANGE: if the list of values is null or empty
- UPDATED: if the values are different from the current
- DELETED: if the list of values is null or empty
- NO_CHANGE: if the values are the same as the existing values
- Parameters:
annotationType
- The annotation type, which must be one of the annotation types specified when creating the batchervalues
- The values to set or null/empty to remove valuesunit
- The unit of the values. Ignored if the annotation type doesn't use units.merge
- If TRUE and the annotation type allows an unlimted number of annotations (multiplicity=0) merges the new values with the existing values. Ignored if multiplicity > 0.- Returns:
- The resulting change
-