View Javadoc

1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.rest;
22  
23  import java.io.IOException;
24  import java.net.URI;
25  import java.util.Collections;
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import javax.ws.rs.Consumes;
30  import javax.ws.rs.POST;
31  import javax.ws.rs.PUT;
32  import javax.ws.rs.Path;
33  import javax.ws.rs.PathParam;
34  import javax.ws.rs.WebApplicationException;
35  import javax.ws.rs.core.Context;
36  import javax.ws.rs.core.Response;
37  import javax.ws.rs.core.UriBuilder;
38  import javax.ws.rs.core.UriInfo;
39  
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  
43  import org.apache.hadoop.hbase.filter.Filter;
44  import org.apache.hadoop.hbase.rest.model.ScannerModel;
45  
46  public class ScannerResource extends ResourceBase {
47  
48    private static final Log LOG = LogFactory.getLog(ScannerResource.class);
49  
50    static final Map<String,ScannerInstanceResource> scanners =
51     Collections.synchronizedMap(new HashMap<String,ScannerInstanceResource>());
52  
53    String tableName;
54  
55    /**
56     * Constructor
57     * @param table
58     * @throws IOException
59     */
60    public ScannerResource(String table) throws IOException {
61      super();
62      this.tableName = table;
63    }
64  
65    static void delete(final String id) {
66      ScannerInstanceResource instance = scanners.remove(id);
67      if (instance != null) {
68        instance.generator.close();
69      }
70    }
71  
72    Response update(final ScannerModel model, final boolean replace, 
73        final UriInfo uriInfo) {
74      servlet.getMetrics().incrementRequests(1);
75      byte[] endRow = model.hasEndRow() ? model.getEndRow() : null;
76      RowSpec spec = new RowSpec(model.getStartRow(), endRow,
77        model.getColumns(), model.getStartTime(), model.getEndTime(), 1);
78      try {
79        Filter filter = ScannerResultGenerator.buildFilterFromModel(model);
80        ScannerResultGenerator gen = 
81          new ScannerResultGenerator(tableName, spec, filter);
82        String id = gen.getID();
83        ScannerInstanceResource instance = 
84          new ScannerInstanceResource(tableName, id, gen, model.getBatch());
85        scanners.put(id, instance);
86        if (LOG.isDebugEnabled()) {
87          LOG.debug("new scanner: " + id);
88        }
89        UriBuilder builder = uriInfo.getAbsolutePathBuilder();
90        URI uri = builder.path(id).build();
91        return Response.created(uri).build();
92      } catch (IOException e) {
93        throw new WebApplicationException(e,
94                Response.Status.SERVICE_UNAVAILABLE);
95      } catch (Exception e) {
96        throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
97      }
98    }
99  
100   @PUT
101   @Consumes({MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF})
102   public Response put(final ScannerModel model, 
103       final @Context UriInfo uriInfo) {
104     if (LOG.isDebugEnabled()) {
105       LOG.debug("PUT " + uriInfo.getAbsolutePath());
106     }
107     return update(model, true, uriInfo);
108   }
109 
110   @POST
111   @Consumes({MIMETYPE_XML, MIMETYPE_JSON, MIMETYPE_PROTOBUF})
112   public Response post(final ScannerModel model,
113       final @Context UriInfo uriInfo) {
114     if (LOG.isDebugEnabled()) {
115       LOG.debug("POST " + uriInfo.getAbsolutePath());
116     }
117     return update(model, false, uriInfo);
118   }
119 
120   @Path("{scanner: .+}")
121   public ScannerInstanceResource getScannerInstanceResource(
122       final @PathParam("scanner") String id) {
123     ScannerInstanceResource instance = scanners.get(id);
124     if (instance == null) {
125       throw new WebApplicationException(Response.Status.NOT_FOUND);
126     }
127     return instance;
128   }
129 }