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