*
* @param model instance of CellSetModel
* @return Response 200 OK, 304 Not modified, 400 Bad request
*/
Response checkAndPut(final CellSetModel model) {
HTableInterface table = null;
try {
if (model.getRows().size() != 1) {
return Response.status(Response.Status.BAD_REQUEST)
.type(MIMETYPE_TEXT).entity("Bad request" + CRLF)
.build();
}
RowModel rowModel = model.getRows().get(0);
byte[] key = rowModel.getKey();
if (key == null) {
key = rowspec.getRow();
}
List<CellModel> cellModels = rowModel.getCells();
int cellModelCount = cellModels.size();
if (key == null || cellModelCount <= 1) {
return Response.status(Response.Status.BAD_REQUEST)
.type(MIMETYPE_TEXT).entity("Bad request" + CRLF)
.build();
}
Put put = new Put(key);
CellModel valueToCheckCell = cellModels.get(cellModelCount - 1);
byte[] valueToCheckColumn = valueToCheckCell.getColumn();
byte[][] valueToPutParts = KeyValue.parseColumn(valueToCheckColumn);
if (valueToPutParts.length != 2) {
return Response.status(Response.Status.BAD_REQUEST)
.type(MIMETYPE_TEXT).entity("Bad request" + CRLF)
.build();
}
CellModel valueToPutCell = null;
for (int i = 0, n = cellModelCount - 1; i < n ; i++) {
if(Bytes.equals(cellModels.get(i).getColumn(),
valueToCheckCell.getColumn())) {
valueToPutCell = cellModels.get(i);
break;
}
}
if (null == valueToPutCell) {
return Response.status(Response.Status.BAD_REQUEST)
.type(MIMETYPE_TEXT).entity("Bad request" + CRLF)
.build();
}
put.add(valueToPutParts[0], valueToPutParts[1], valueToPutCell
.getTimestamp(), valueToPutCell.getValue());
table = servlet.getTable(this.tableResource.getName());
boolean retValue = table.checkAndPut(key, valueToPutParts[0],
valueToPutParts[1], valueToCheckCell.getValue(), put);
if (LOG.isDebugEnabled()) {
LOG.debug("CHECK-AND-PUT " + put.toString() + ", returns " + retValue);
}
table.flushCommits();
ResponseBuilder response = Response.ok();
if (!retValue) {
response = Response.status(304);
}
return response.build();
} catch (IOException e) {
return Response.status(Response.Status.SERVICE_UNAVAILABLE)
.type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
.build();
} finally {
if (table != null) try {
table.close();
} catch (IOException ioe) { }
}
}