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
This commit is contained in:
Andrew Kyle Purtell 2009-07-27 21:39:44 +00:00
parent 3a118552a7
commit ace2be6b00
1 changed files with 60 additions and 17 deletions

View File

@ -112,11 +112,10 @@ public class SchemaResource implements Constants {
} }
} }
private Response update(TableSchemaModel model, boolean replace, private Response replace(byte[] tableName, TableSchemaModel model,
UriInfo uriInfo) { UriInfo uriInfo, HBaseAdmin admin) {
// NOTE: 'replace' is currently ignored... we always replace the schema
try { try {
HTableDescriptor htd = new HTableDescriptor(table); HTableDescriptor htd = new HTableDescriptor(tableName);
for (Map.Entry<QName,Object> e: model.getAny().entrySet()) { for (Map.Entry<QName,Object> e: model.getAny().entrySet()) {
htd.setValue(e.getKey().getLocalPart(), e.getValue().toString()); htd.setValue(e.getKey().getLocalPart(), e.getValue().toString());
} }
@ -127,21 +126,65 @@ public class SchemaResource implements Constants {
} }
htd.addFamily(hcd); htd.addFamily(hcd);
} }
RESTServlet server = RESTServlet.getInstance(); if (admin.tableExists(tableName)) {
HBaseAdmin admin = new HBaseAdmin(server.getConfiguration()); admin.disableTable(tableName);
if (admin.tableExists(table)) { admin.modifyTable(tableName, htd);
admin.disableTable(table); admin.enableTable(tableName);
admin.modifyTable(Bytes.toBytes(table), htd); } else try {
server.invalidateMaxAge(table);
admin.enableTable(table);
return Response.ok().build();
} else {
admin.createTable(htd); 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<QName,Object> 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) { } catch (IOException e) {
throw new WebApplicationException(e, throw new WebApplicationException(e,
Response.Status.SERVICE_UNAVAILABLE); Response.Status.SERVICE_UNAVAILABLE);