| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2013 Ericsson |
| 3 | * |
| 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 | * |
| 9 | * Contributors: |
| 10 | * Marc-Andre Laperle - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.internal.tmf.core.trace.indexer; |
| 14 | |
| 15 | import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.ITmfCheckpoint; |
| 16 | |
| 17 | /** |
| 18 | * A visitor that searches for a specific checkpoint |
| 19 | * |
| 20 | * @author Marc-Andre Laperle |
| 21 | */ |
| 22 | public class BTreeCheckpointVisitor implements IBTreeVisitor { |
| 23 | |
| 24 | private long rank = -1; |
| 25 | private ITmfCheckpoint found; |
| 26 | private ITmfCheckpoint search; |
| 27 | private boolean exactFound = false; |
| 28 | |
| 29 | /** |
| 30 | * Constructs the checkpoint visitor |
| 31 | * |
| 32 | * @param search |
| 33 | * the checkpoint to search for |
| 34 | */ |
| 35 | public BTreeCheckpointVisitor(ITmfCheckpoint search) { |
| 36 | this.search = search; |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | public int compare(ITmfCheckpoint currentCheckpoint) { |
| 41 | int compareTo = currentCheckpoint.compareTo(search); |
| 42 | if (compareTo <= 0 && !exactFound) { |
| 43 | rank = currentCheckpoint.getCheckpointRank(); |
| 44 | found = currentCheckpoint; |
| 45 | if (compareTo == 0) { |
| 46 | exactFound = true; |
| 47 | } |
| 48 | } |
| 49 | return compareTo; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Return the found checkpoint |
| 54 | * |
| 55 | * @return the found checkpoint |
| 56 | */ |
| 57 | public ITmfCheckpoint getCheckpoint() { |
| 58 | return found; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Returns the checkpoint rank of the searched checkpoint, if it is |
| 63 | * contained in the index; otherwise, (-(insertion point) - 1). |
| 64 | * |
| 65 | * @return the checkpoint rank of the searched checkpoint, if it is |
| 66 | * contained in the index; otherwise, (-(insertion point) - 1). |
| 67 | */ |
| 68 | public long getCheckpointRank() { |
| 69 | if (!exactFound) { |
| 70 | long insertionPoint = rank + 1; |
| 71 | return -insertionPoint - 1; |
| 72 | } |
| 73 | |
| 74 | return rank; |
| 75 | } |
| 76 | } |