From ffd0aa67a826e24981e62d6ca28c3571f7a2957b Mon Sep 17 00:00:00 2001 From: Etienne Bergeron Date: Mon, 9 Dec 2013 21:12:21 -0500 Subject: [PATCH] tmf: Remove back-reference from HT-Node to HT-Tree The HT-Node needs to know about the layout of the node (size, n-child) but not about the tree in which it belongs to. Any back reference is typically a bad design. It complicated unittests and maintenance. HT-Tree ---> HTNode--\ \ \ \---------------------> HTConfig This patch-set is one of multiples to refactor the HT-Tree API. The API refactoring will lead us to a easier algorithmic refactoring. Change-Id: Ifa11cec6a89b9b1c698e0a2b2b18ac5ec70a7db0 Signed-off-by: Etienne Bergeron Reviewed-on: https://git.eclipse.org/r/19555 Reviewed-by: Alexandre Montplaisir IP-Clean: Alexandre Montplaisir Tested-by: Alexandre Montplaisir --- .../backends/historytree/CoreNode.java | 20 ++++++------ .../backends/historytree/HTNode.java | 32 +++++++++---------- .../backends/historytree/HT_IO.java | 4 +-- .../backends/historytree/HistoryTree.java | 4 +-- 4 files changed, 30 insertions(+), 30 deletions(-) diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/backends/historytree/CoreNode.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/backends/historytree/CoreNode.java index 882d5e6489..59f9aff29d 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/backends/historytree/CoreNode.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/backends/historytree/CoreNode.java @@ -19,7 +19,7 @@ import java.nio.ByteBuffer; * * It extends HTNode by adding support for child nodes, and also extensions. * - * @author alexmont + * @author Alexandre Montplaisir * */ class CoreNode extends HTNode { @@ -45,8 +45,8 @@ class CoreNode extends HTNode { /** * Initial constructor. Use this to initialize a new EMPTY node. * - * @param tree - * The HistoryTree to which this node belongs + * @param config + * Configuration of the History Tree * @param seqNumber * The (unique) sequence number assigned to this particular node * @param parentSeqNumber @@ -54,11 +54,11 @@ class CoreNode extends HTNode { * @param start * The earliest timestamp stored in this node */ - CoreNode(HistoryTree tree, int seqNumber, int parentSeqNumber, + CoreNode(HTConfig config, int seqNumber, int parentSeqNumber, long start) { - super(tree, seqNumber, parentSeqNumber, start); + super(config, seqNumber, parentSeqNumber, start); this.nbChildren = 0; - int size = getTree().getConfig().getMaxChildren(); + int size = config.getMaxChildren(); /* * We instantiate the two following arrays at full size right away, @@ -72,7 +72,7 @@ class CoreNode extends HTNode { @Override protected void readSpecificHeader(ByteBuffer buffer) { - int size = getTree().getConfig().getMaxChildren(); + int size = getConfig().getMaxChildren(); extension = buffer.getInt(); nbChildren = buffer.getInt(); @@ -96,7 +96,7 @@ class CoreNode extends HTNode { @Override protected void writeSpecificHeader(ByteBuffer buffer) { - int size = getTree().getConfig().getMaxChildren(); + int size = getConfig().getMaxChildren(); buffer.putInt(extension); buffer.putInt(nbChildren); @@ -149,7 +149,7 @@ class CoreNode extends HTNode { * The SHTNode object of the new child */ void linkNewChild(CoreNode childNode) { - assert (this.nbChildren < getTree().getConfig().getMaxChildren()); + assert (this.nbChildren < getConfig().getMaxChildren()); this.children[nbChildren] = childNode.getSequenceNumber(); this.childStart[nbChildren] = childNode.getNodeStart(); @@ -163,7 +163,7 @@ class CoreNode extends HTNode { @Override protected int getTotalHeaderSize() { - int maxChildren = getTree().getConfig().getMaxChildren(); + int maxChildren = getConfig().getMaxChildren(); int specificSize = SIZE_INT /* 1x int (extension node) */ + SIZE_INT /* 1x int (nbChildren) */ diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/backends/historytree/HTNode.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/backends/historytree/HTNode.java index f7b47e26be..2a06d2d116 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/backends/historytree/HTNode.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/backends/historytree/HTNode.java @@ -28,7 +28,7 @@ import org.eclipse.linuxtools.tmf.core.statevalue.TmfStateValue; /** * The base class for all the types of nodes that go in the History Tree. * - * @author alexmont + * @author Alexandre Montplaisir */ abstract class HTNode { @@ -42,8 +42,8 @@ abstract class HTNode { */ protected static final int DATA_ENTRY_SIZE = 25; - /* Reference to the History Tree to whom this node belongs */ - private final HistoryTree ownerTree; + /* Configuration of the History Tree to which belongs this node */ + private final HTConfig config; /* Time range of this node */ private final long nodeStart; @@ -62,13 +62,13 @@ abstract class HTNode { /* Vector containing all the intervals contained in this node */ private final List intervals; - HTNode(HistoryTree tree, int seqNumber, int parentSeqNumber, long start) { - this.ownerTree = tree; + HTNode(HTConfig config, int seqNumber, int parentSeqNumber, long start) { + this.config = config; this.nodeStart = start; this.sequenceNumber = seqNumber; this.parentSequenceNumber = parentSeqNumber; - this.stringSectionOffset = ownerTree.getConfig().getBlockSize(); + this.stringSectionOffset = config.getBlockSize(); this.isDone = false; this.intervals = new ArrayList(); } @@ -77,23 +77,23 @@ abstract class HTNode { * Reader factory constructor. Build a Node object (of the right type) by * reading a block in the file. * - * @param tree - * Reference to the HT which will own this node + * @param config + * Configuration of the History Tree * @param fc * FileChannel to the history file, ALREADY SEEKED at the start * of the node. * @throws IOException */ - static final HTNode readNode(HistoryTree tree, FileChannel fc) + static final HTNode readNode(HTConfig config, FileChannel fc) throws IOException { HTNode newNode = null; int res, i; - ByteBuffer buffer = ByteBuffer.allocate(tree.getConfig().getBlockSize()); + ByteBuffer buffer = ByteBuffer.allocate(config.getBlockSize()); buffer.order(ByteOrder.LITTLE_ENDIAN); buffer.clear(); res = fc.read(buffer); - assert (res == tree.getConfig().getBlockSize()); + assert (res == config.getBlockSize()); buffer.flip(); /* Read the common header part */ @@ -110,7 +110,7 @@ abstract class HTNode { switch (type) { case 1: /* Core nodes */ - newNode = new CoreNode(tree, seqNb, parentSeqNb, start); + newNode = new CoreNode(config, seqNb, parentSeqNb, start); newNode.readSpecificHeader(buffer); break; @@ -146,7 +146,7 @@ abstract class HTNode { } final void writeSelf(FileChannel fc) throws IOException { - final int blockSize = ownerTree.getConfig().getBlockSize(); + final int blockSize = config.getBlockSize(); int curStringsEntryEndPos = blockSize; ByteBuffer buffer = ByteBuffer.allocate(blockSize); @@ -201,8 +201,8 @@ abstract class HTNode { // Accessors // ------------------------------------------------------------------------ - protected HistoryTree getTree() { - return ownerTree; + HTConfig getConfig() { + return config; } long getNodeStart() { @@ -413,7 +413,7 @@ abstract class HTNode { * (used space / total usable space, which excludes the header) */ long getNodeUsagePRC() { - final int blockSize = ownerTree.getConfig().getBlockSize(); + final int blockSize = config.getBlockSize(); float freePercent = (float) this.getNodeFreeSpace() / (float) (blockSize - this.getTotalHeaderSize()) * 100F; diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/backends/historytree/HT_IO.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/backends/historytree/HT_IO.java index 8250c05865..445e0dfc15 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/backends/historytree/HT_IO.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/backends/historytree/HT_IO.java @@ -25,7 +25,7 @@ import java.nio.channels.FileChannel; * tree-file on disk and all the caching mechanisms. Every HistoryTree should * contain 1 and only 1 HT_IO element. * - * @author alexmont + * @author Alexandre Montplaisir * */ class HT_IO { @@ -130,7 +130,7 @@ class HT_IO { /* Lookup on disk */ try { seekFCToNodePos(fcIn, seqNumber); - readNode = HTNode.readNode(tree, fcIn); + readNode = HTNode.readNode(tree.getConfig(), fcIn); /* Put the node in the cache. */ fNodeCache[offset] = readNode; diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/backends/historytree/HistoryTree.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/backends/historytree/HistoryTree.java index 12f14ced82..fc8c382e97 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/backends/historytree/HistoryTree.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/backends/historytree/HistoryTree.java @@ -31,7 +31,7 @@ import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateProvider; * Meta-container for the History Tree. This structure contains all the * high-level data relevant to the tree. * - * @author alexmont + * @author Alexandre Montplaisir * */ class HistoryTree { @@ -468,7 +468,7 @@ class HistoryTree { * @return The newly created node */ private CoreNode initNewCoreNode(int parentSeqNumber, long startTime) { - CoreNode newNode = new CoreNode(this, this.nodeCount, parentSeqNumber, + CoreNode newNode = new CoreNode(config, this.nodeCount, parentSeqNumber, startTime); this.nodeCount++; -- 2.34.1