1 /*******************************************************************************
2 * Copyright (c) 2016 École Polytechnique de Montréal
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.tmf
.core
.statesystem
;
12 import java
.util
.LinkedList
;
13 import java
.util
.PriorityQueue
;
14 import java
.util
.Queue
;
16 import java
.util
.TreeSet
;
18 import org
.eclipse
.jdt
.annotation
.Nullable
;
19 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
20 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystemBuilder
;
21 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
24 * This class allows to recycle state system attributes. Instead of creating a
25 * lot of short-lived attributes, it is sometimes useful to re-use an attribute
26 * (and its whole sub-tree) that was previously used and is no longer required.
27 * This class keeps a list of children attributes of a base quark and grows that
30 * @author Geneviève Bastien
33 public class TmfAttributePool
{
35 private final ITmfStateSystemBuilder fSs
;
36 private final Integer fBaseQuark
;
37 private final Queue
<@Nullable Integer
> fAvailableQuarks
;
38 private final Set
<Integer
> fQuarksInUse
= new TreeSet
<>();
39 private int fCount
= 0;
44 public enum QueueType
{
46 * First In First Out, available attributes are stored and returned in
47 * the order in which they are recycled
51 * Available attributes are returned by their number, so attributes with
52 * lower numbers will be used more often
63 * The base quark under which to add the recyclable attributes
65 public TmfAttributePool(ITmfStateSystemBuilder ss
, Integer baseQuark
) {
66 this(ss
, baseQuark
, QueueType
.FIFO
);
75 * The base quark under which to add the recyclable attributes
77 * The type of queue to use for the attribute pool
79 public TmfAttributePool(ITmfStateSystemBuilder ss
, Integer baseQuark
, QueueType type
) {
82 /* Make sure the base quark is in range */
83 ss
.getParentAttributeQuark(baseQuark
);
84 fBaseQuark
= baseQuark
;
85 } catch (IndexOutOfBoundsException e
) {
86 throw new IllegalArgumentException("The quark used as base for the attribute pool does not exist"); //$NON-NLS-1$
90 fAvailableQuarks
= new LinkedList
<>();
93 fAvailableQuarks
= new PriorityQueue
<>();
96 throw new IllegalArgumentException("Wrong queue type"); //$NON-NLS-1$
101 * Get an available attribute quark. If there is one available, it will be
102 * reused, otherwise a new quark will be created under the base quark. The
103 * name of the attributes is a sequential integer. So the first quark to be
104 * added will be named '0', the next one '1', etc.
106 * @return An available quark
108 public synchronized int getAvailable() {
109 Integer quark
= fAvailableQuarks
.poll();
111 quark
= fSs
.getQuarkRelativeAndAdd(fBaseQuark
, String
.valueOf(fCount
));
114 fQuarksInUse
.add(quark
);
119 * Recycle a quark so that it can be reused by calling the
120 * {@link #getAvailable()} method. The quark has to have been obtained from
121 * a previous call to {@link #getAvailable()}. It will set the quark's value
122 * in the state system to a null value.
124 * It is assumed that it will be reused in the same context each time, so
125 * all children are kept and set to null in this method. The quarks are
126 * still available for the caller, nothing prevents from re-using them
127 * without referring to this class. That means if any attribute's value need
128 * to be non-null after recycling the quark, the caller can do it after
129 * calling this method.
132 * The quark to recycle.
134 * The timestamp at which to close this attribute.
136 public synchronized void recycle(int quark
, long ts
) {
137 if (!fQuarksInUse
.remove(quark
)) {
138 throw new IllegalArgumentException();
141 fSs
.removeAttribute(ts
, quark
);
142 } catch (AttributeNotFoundException e
) {
143 Activator
.logError("Error getting sub-attributes", e
); //$NON-NLS-1$
145 fAvailableQuarks
.add(quark
);