001/*
002 * This file is part of the Tuning Fork Visualization Platform
003 *  (http://sourceforge.net/projects/tuningforkvp)
004 *
005 * Copyright (c) 2005 - 2008 IBM Corporation.
006 * All rights reserved. This program and the accompanying materials
007 * are made available under the terms of the Eclipse Public License v1.0
008 * which accompanies this distribution, and is available at
009 * http://www.eclipse.org/legal/epl-v10.html
010 *
011 * Contributors:
012 *     IBM Corporation - initial API and implementation
013 */
014
015package com.ibm.tuningfork.tracegen.chunk;
016
017import org.vmmagic.pragma.Interruptible;
018import org.vmmagic.pragma.Uninterruptible;
019
020@Uninterruptible
021public class FeedletChunk extends Chunk {
022
023    public static final int FEEDLET_TYPE_ID = 2;
024    public static final int FEEDLET_COUNT_OFFSET = Chunk.DATA_OFFSET;
025    public static final int FEEDLET_DATA_OFFSET = FEEDLET_COUNT_OFFSET + 4;
026    public static final int FEEDLET_ADD_OPERATION = 1;
027    public static final int FEEDLET_REMOVE_OPERATION = 2;
028    public static final int FEEDLET_DESCRIBE_OPERATION = 3;
029    public static final String NAME_PROPERTY = "name";
030    public static final String DECSRIPTION_PROPERTY = "description";
031
032    private int feedletOperations = 0;
033
034    public FeedletChunk() {
035        super(FEEDLET_TYPE_ID);
036        seek(FEEDLET_DATA_OFFSET);
037    }
038
039    public boolean hasData() {
040        return feedletOperations > 0;
041    }
042
043    @Interruptible
044    public boolean add(int feedletIndex, String name, String description) {
045      int savedPosition = getPosition();
046      int savedOperationCount = feedletOperations;
047      boolean success = false;
048      try {
049        if (!addInt(FEEDLET_ADD_OPERATION)) return false;
050        if (!addInt(feedletIndex)) return false;
051        feedletOperations++;
052        if (!addProperty(feedletIndex, NAME_PROPERTY, name)) return false;
053        if (!addProperty(feedletIndex, DECSRIPTION_PROPERTY, description)) return false;
054        success = true;
055        return true;
056      } finally {
057        if (!success) {
058          seek(savedPosition);
059          feedletOperations = savedOperationCount;
060        }
061      }
062    }
063
064    public boolean remove(int feedletIndex) {
065      if (!hasRoom(ENCODING_SPACE_INT*2)) return false;
066      addIntUnchecked(FEEDLET_REMOVE_OPERATION);
067      addIntUnchecked(feedletIndex);
068      feedletOperations++;
069      return true;
070    }
071
072    @Interruptible
073    public boolean addProperty(int feedletIndex, String key, String val) {
074      int savedPosition = getPosition();
075      int savedOperationCount = feedletOperations;
076      boolean success = false;
077      try {
078        if (!addInt(FEEDLET_DESCRIBE_OPERATION)) return false;
079        if (!addInt(feedletIndex)) return false;
080        if (!addStringInternal(getChars(key))) return false;
081        if (!addStringInternal(getChars(val))) return false;
082        feedletOperations++;
083        success = true;
084        return true;
085      } finally {
086        if (!success) {
087          seek(savedPosition);
088          feedletOperations = savedOperationCount;
089        }
090      }
091    }
092
093    @Override
094    public void close() {
095        putIntAt(FEEDLET_COUNT_OFFSET, feedletOperations);
096        feedletOperations = 0;
097        super.close();
098    }
099
100    public void reset() {
101        resetImpl();
102        feedletOperations = 0;
103        seek(FEEDLET_DATA_OFFSET);
104    }
105}