From ace2be6b007ae8df8d4f7614a5e99bf784f8aabd Mon Sep 17 00:00:00 2001 From: Andrew Kyle Purtell Date: Mon, 27 Jul 2009 21:39:44 +0000 Subject: [PATCH] HBASE-1695 [stargate] differentiate PUT and POST processing for schema upload git-svn-id: https://svn.apache.org/repos/asf/hadoop/hbase/trunk@798297 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop/hbase/stargate/SchemaResource.java | 77 +++++++++++++++---- 1 file changed, 60 insertions(+), 17 deletions(-) diff --git a/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/SchemaResource.java b/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/SchemaResource.java index 473e926593d..f03bfb9a2e4 100644 --- a/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/SchemaResource.java +++ b/src/contrib/stargate/src/java/org/apache/hadoop/hbase/stargate/SchemaResource.java @@ -112,11 +112,10 @@ public class SchemaResource implements Constants { } } - private Response update(TableSchemaModel model, boolean replace, - UriInfo uriInfo) { - // NOTE: 'replace' is currently ignored... we always replace the schema + private Response replace(byte[] tableName, TableSchemaModel model, + UriInfo uriInfo, HBaseAdmin admin) { try { - HTableDescriptor htd = new HTableDescriptor(table); + HTableDescriptor htd = new HTableDescriptor(tableName); for (Map.Entry e: model.getAny().entrySet()) { htd.setValue(e.getKey().getLocalPart(), e.getValue().toString()); } @@ -127,21 +126,65 @@ public class SchemaResource implements Constants { } htd.addFamily(hcd); } - RESTServlet server = RESTServlet.getInstance(); - HBaseAdmin admin = new HBaseAdmin(server.getConfiguration()); - if (admin.tableExists(table)) { - admin.disableTable(table); - admin.modifyTable(Bytes.toBytes(table), htd); - server.invalidateMaxAge(table); - admin.enableTable(table); - return Response.ok().build(); - } else { + if (admin.tableExists(tableName)) { + admin.disableTable(tableName); + admin.modifyTable(tableName, htd); + admin.enableTable(tableName); + } else try { admin.createTable(htd); - return Response.created(uriInfo.getAbsolutePath()).build(); + } catch (TableExistsException e) { + // race, someone else created a table with the same name + throw new WebApplicationException(e, Response.Status.NOT_MODIFIED); + } + return Response.created(uriInfo.getAbsolutePath()).build(); + } catch (IOException e) { + throw new WebApplicationException(e, + Response.Status.SERVICE_UNAVAILABLE); + } + } + + private Response update(byte[] tableName, TableSchemaModel model, + UriInfo uriInfo, HBaseAdmin admin) { + try { + HTableDescriptor htd = admin.getTableDescriptor(tableName); + admin.disableTable(tableName); + try { + for (ColumnSchemaModel family: model.getColumns()) { + HColumnDescriptor hcd = new HColumnDescriptor(family.getName()); + for (Map.Entry e: family.getAny().entrySet()) { + hcd.setValue(e.getKey().getLocalPart(), e.getValue().toString()); + } + if (htd.hasFamily(hcd.getName())) { + admin.modifyColumn(tableName, hcd.getName(), hcd); + } else { + admin.addColumn(model.getName(), hcd); + } + } + } catch (IOException e) { + throw new WebApplicationException(e, + Response.Status.INTERNAL_SERVER_ERROR); + } finally { + admin.enableTable(tableName); + } + return Response.ok().build(); + } catch (IOException e) { + throw new WebApplicationException(e, + Response.Status.SERVICE_UNAVAILABLE); + } + } + + private Response update(TableSchemaModel model, boolean replace, + UriInfo uriInfo) { + try { + RESTServlet server = RESTServlet.getInstance(); + server.invalidateMaxAge(model.getName()); + HBaseAdmin admin = new HBaseAdmin(server.getConfiguration()); + byte[] tableName = Bytes.toBytes(model.getName()); + if (replace || !admin.tableExists(tableName)) { + return replace(tableName, model, uriInfo, admin); + } else { + return update(tableName, model, uriInfo, admin); } - } catch (TableExistsException e) { - // race, someone else created a table with the same name - throw new WebApplicationException(e, Response.Status.NOT_MODIFIED); } catch (IOException e) { throw new WebApplicationException(e, Response.Status.SERVICE_UNAVAILABLE);