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.rest;
019
020import java.io.IOException;
021import java.io.OutputStream;
022import org.apache.hadoop.hbase.client.Result;
023import org.apache.hadoop.hbase.client.ResultScanner;
024import org.apache.hadoop.hbase.rest.model.CellSetModel;
025import org.apache.hadoop.hbase.util.Bytes;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030import org.apache.hbase.thirdparty.javax.ws.rs.WebApplicationException;
031import org.apache.hbase.thirdparty.javax.ws.rs.core.StreamingOutput;
032
033@InterfaceAudience.Private
034public class ProtobufStreamingOutput implements StreamingOutput {
035  private static final Logger LOG = LoggerFactory.getLogger(ProtobufStreamingOutput.class);
036
037  private String contentType;
038  private ResultScanner resultScanner;
039  private int limit;
040  private int fetchSize;
041
042  protected ProtobufStreamingOutput(ResultScanner scanner, String type, int limit, int fetchSize) {
043    this.resultScanner = scanner;
044    this.contentType = type;
045    this.limit = limit;
046    this.fetchSize = fetchSize;
047    if (LOG.isTraceEnabled()) {
048      LOG.trace("Created StreamingOutput with content type = " + this.contentType + " user limit : "
049        + this.limit + " scan fetch size : " + this.fetchSize);
050    }
051  }
052
053  @Override
054  public void write(OutputStream outStream) throws IOException, WebApplicationException {
055    Result[] rowsToSend;
056    if (limit < fetchSize) {
057      rowsToSend = this.resultScanner.next(limit);
058      writeToStream(createModelFromResults(rowsToSend), this.contentType, outStream);
059    } else {
060      int count = limit;
061      while (count > 0) {
062        if (count < fetchSize) {
063          rowsToSend = this.resultScanner.next(count);
064        } else {
065          rowsToSend = this.resultScanner.next(this.fetchSize);
066        }
067        if (rowsToSend.length == 0) {
068          break;
069        }
070        count = count - rowsToSend.length;
071        writeToStream(createModelFromResults(rowsToSend), this.contentType, outStream);
072      }
073    }
074  }
075
076  private void writeToStream(CellSetModel model, String contentType, OutputStream outStream)
077    throws IOException {
078    byte[] objectBytes = model.createProtobufOutput();
079    outStream.write(Bytes.toBytes((short) objectBytes.length));
080    outStream.write(objectBytes);
081    outStream.flush();
082    if (LOG.isTraceEnabled()) {
083      LOG.trace("Wrote " + model.getRows().size() + " rows to stream successfully.");
084    }
085  }
086
087  private CellSetModel createModelFromResults(Result[] results) {
088    CellSetModel cellSetModel = new CellSetModel();
089    for (int i = 0; i < results.length; i++) {
090      if (results[i].isEmpty()) {
091        continue;
092      }
093      cellSetModel.addRow(RestUtil.createRowModelFromResult(results[i]));
094    }
095    return cellSetModel;
096  }
097}