001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.types;
019
020import java.util.Iterator;
021import java.util.NoSuchElementException;
022
023import org.apache.hadoop.hbase.util.PositionedByteRange;
024import org.apache.yetus.audience.InterfaceAudience;
025
026/**
027 * An {@link Iterator} over encoded {@code Struct} members.
028 * <p>
029 * This iterates over each serialized {@code Struct} field from the specified
030 * {@code DataTypes<?>[]} definition. It allows you to read the field or skip
031 * over its serialized bytes using {@link #next()} and {@link #skip()},
032 * respectively. This is in contrast to the {@code Struct} method which allow
033 * you to {@link Struct#decode(PositionedByteRange)} or
034 * {@link Struct#skip(PositionedByteRange)} over the entire {@code Struct} at
035 * once.
036 * </p>
037 * <p>
038 * This iterator may also be used to read bytes from any {@code Struct} for
039 * which the specified {@code DataType<?>[]} is a prefix. For example, if the
040 * specified {@code Struct} definition has a {@link RawInteger} and a
041 * {@link RawStringTerminated} field, you may parse the serialized output
042 * of a {@code Struct} whose fields are {@link RawInteger},
043 * {@link RawStringTerminated}, and {@link RawBytes}. The iterator would
044 * return a number followed by a {@code String}. The trailing {@code byte[]}
045 * would be ignored.
046 * </p>
047 */
048@InterfaceAudience.Public
049public class StructIterator implements Iterator<Object> {
050
051  protected final PositionedByteRange src;
052  protected int idx = 0;
053  @SuppressWarnings("rawtypes")
054  protected final DataType[] types;
055
056  /**
057   * Construct {@code StructIterator} over the values encoded in {@code src}
058   * using the specified {@code types} definition.
059   * @param src The buffer from which to read encoded values.
060   * @param types The sequence of types to use as the schema for this
061   *          {@code Struct}.
062   */
063  public StructIterator(PositionedByteRange src, @SuppressWarnings("rawtypes") DataType[] types) {
064    this.src = src;
065    this.types = types;
066  }
067
068  @Override
069  public boolean hasNext() {
070    // hasNext can return true when position == length in the case of a
071    // nullable field trailing a struct.
072    return idx < types.length && src.getPosition() <= src.getLength();
073  }
074
075  @Override
076  public void remove() {
077    throw new UnsupportedOperationException();
078  }
079
080  @Override
081  public Object next() {
082    if (!hasNext()) {
083      throw new NoSuchElementException();
084    }
085    DataType<?> t = types[idx++];
086    if (src.getPosition() == src.getLength() && t.isNullable()) {
087      return null;
088    }
089    return t.decode(src);
090  }
091
092  /**
093   * Bypass the next encoded value.
094   * @return the number of bytes skipped.
095   */
096  public int skip() {
097    if (!hasNext()) {
098      throw new NoSuchElementException();
099    }
100    DataType<?> t = types[idx++];
101    if (src.getPosition() == src.getLength() && t.isNullable()) {
102      return 0;
103    }
104    return t.skip(src);
105  }
106}