use of com.questdb.net.http.ChunkedResponse in project questdb by bluestreak01.
the class ImportHandler method resume.
@Override
public void resume(IOContext context) throws IOException {
super.resume(context);
final ImportHandlerContext ctx = lvContext.get(context);
final ChunkedResponse r = context.chunkedResponse();
try {
if (ctx.json) {
resumeJson(ctx, r);
} else {
resumeText(ctx, r);
}
} catch (ResponseContentBufferTooSmallException ignored) {
if (r.resetToBookmark()) {
r.sendChunk();
} else {
// and disconnect socket
throw DisconnectedChannelException.INSTANCE;
}
}
ctx.clear();
}
use of com.questdb.net.http.ChunkedResponse in project questdb by bluestreak01.
the class CsvHandler method handle.
@Override
public void handle(IOContext context) throws IOException {
ExportHandlerContext ctx = localContext.get(context);
if (ctx == null) {
localContext.set(context, ctx = new ExportHandlerContext(context.getFd(), context.getServerConfiguration().getDbCyclesBeforeCancel()));
}
ChunkedResponse r = context.chunkedResponse();
if (ctx.parseUrl(r, context.request)) {
ctx.compileQuery(r, factory, cacheMisses, cacheHits);
resume(context);
}
}
use of com.questdb.net.http.ChunkedResponse in project questdb by bluestreak01.
the class CsvHandler method resume.
@SuppressWarnings("ConstantConditions")
@Override
public void resume(IOContext context) throws IOException {
ExportHandlerContext ctx = localContext.get(context);
if (ctx == null || ctx.cursor == null) {
return;
}
final ChunkedResponse r = context.chunkedResponse();
final int columnCount = ctx.metadata.getColumnCount();
OUT: while (true) {
try {
SWITCH: switch(ctx.queryState) {
case QUERY_METADATA:
for (; ctx.columnIndex < columnCount; ctx.columnIndex++) {
RecordColumnMetadata column = ctx.metadata.getColumnQuick(ctx.columnIndex);
r.bookmark();
if (ctx.columnIndex > 0) {
r.put(',');
}
r.putQuoted(column.getName());
}
r.put(Misc.EOL);
ctx.queryState = QUERY_RECORD_START;
// fall through
case QUERY_RECORD_START:
if (ctx.record == null) {
// check if cursor has any records
while (true) {
if (ctx.cursor.hasNext()) {
ctx.record = ctx.cursor.next();
ctx.count++;
if (ctx.count > ctx.skip) {
break;
}
} else {
ctx.cursor.releaseCursor();
ctx.cursor = null;
ctx.queryState = QUERY_DATA_SUFFIX;
break SWITCH;
}
}
}
if (ctx.count > ctx.stop) {
ctx.queryState = QUERY_DATA_SUFFIX;
break;
}
ctx.queryState = QUERY_RECORD_COLUMNS;
ctx.columnIndex = 0;
// fall through
case QUERY_RECORD_COLUMNS:
for (; ctx.columnIndex < columnCount; ctx.columnIndex++) {
RecordColumnMetadata m = ctx.metadata.getColumnQuick(ctx.columnIndex);
r.bookmark();
if (ctx.columnIndex > 0) {
r.put(',');
}
putValue(r, m.getType(), ctx.record, ctx.columnIndex);
}
r.bookmark();
r.put(Misc.EOL);
ctx.record = null;
ctx.queryState = QUERY_RECORD_START;
break;
case QUERY_DATA_SUFFIX:
sendDone(r, ctx);
break OUT;
default:
break OUT;
}
} catch (ResponseContentBufferTooSmallException ignored) {
if (r.resetToBookmark()) {
r.sendChunk();
} else {
// what we have here is out unit of data, column value or query
// is larger that response content buffer
// all we can do in this scenario is to log appropriately
// and disconnect socket
ctx.info().$("Response buffer is too small, state=").$(ctx.queryState).$();
throw DisconnectedChannelException.INSTANCE;
}
}
}
}
use of com.questdb.net.http.ChunkedResponse in project questdb by bluestreak01.
the class ImportHandler method sendResponse.
private void sendResponse(IOContext context) throws IOException {
ImportHandlerContext h = lvContext.get(context);
h.json = Chars.equalsNc("json", context.request.getUrlParam("fmt"));
ChunkedResponse r = context.chunkedResponse();
switch(h.state) {
case ImportHandlerContext.STATE_OK:
if (h.json) {
r.status(200, CONTENT_TYPE_JSON);
} else {
r.status(200, CONTENT_TYPE_TEXT);
}
r.sendHeader();
resume(context);
break;
default:
sendError(context, h.stateMessage);
break;
}
}
use of com.questdb.net.http.ChunkedResponse in project questdb by bluestreak01.
the class QueryHandler method resume.
@SuppressWarnings("ConstantConditions")
@Override
public void resume(IOContext context) throws IOException {
QueryHandlerContext ctx = localContext.get(context);
if (ctx == null || ctx.cursor == null) {
return;
}
final ChunkedResponse r = context.chunkedResponse();
final int columnCount = ctx.metadata.getColumnCount();
OUT: while (true) {
try {
SWITCH: switch(ctx.queryState) {
case QUERY_PREFIX:
if (ctx.noMeta) {
r.put('{').putQuoted("dataset").put(":[");
ctx.queryState = QUERY_RECORD_START;
break;
}
r.bookmark();
r.put('{').putQuoted("query").put(':').encodeUtf8AndQuote(ctx.query);
r.put(',').putQuoted("columns").put(':').put('[');
ctx.queryState = QUERY_METADATA;
ctx.columnIndex = 0;
// fall through
case QUERY_METADATA:
for (; ctx.columnIndex < columnCount; ctx.columnIndex++) {
RecordColumnMetadata column = ctx.metadata.getColumnQuick(ctx.columnIndex);
r.bookmark();
if (ctx.columnIndex > 0) {
r.put(',');
}
r.put('{').putQuoted("name").put(':').putQuoted(column.getName()).put(',').putQuoted("type").put(':').putQuoted(ColumnType.nameOf(column.getType()));
r.put('}');
}
ctx.queryState = QUERY_META_SUFFIX;
// fall through
case QUERY_META_SUFFIX:
r.bookmark();
r.put("],\"dataset\":[");
ctx.queryState = QUERY_RECORD_START;
// fall through
case QUERY_RECORD_START:
if (ctx.record == null) {
// check if cursor has any records
while (true) {
if (ctx.cursor.hasNext()) {
ctx.record = ctx.cursor.next();
ctx.count++;
if (ctx.fetchAll && ctx.count > ctx.stop) {
ctx.cancellationHandler.check();
continue;
}
if (ctx.count > ctx.skip) {
break;
}
} else {
ctx.queryState = QUERY_DATA_SUFFIX;
break SWITCH;
}
}
}
if (ctx.count > ctx.stop) {
ctx.queryState = QUERY_DATA_SUFFIX;
break;
}
r.bookmark();
if (ctx.count > ctx.skip + 1) {
r.put(',');
}
r.put('[');
ctx.queryState = QUERY_RECORD_COLUMNS;
ctx.columnIndex = 0;
// fall through
case QUERY_RECORD_COLUMNS:
for (; ctx.columnIndex < columnCount; ctx.columnIndex++) {
RecordColumnMetadata m = ctx.metadata.getColumnQuick(ctx.columnIndex);
r.bookmark();
if (ctx.columnIndex > 0) {
r.put(',');
}
putValue(r, m.getType(), ctx.record, ctx.columnIndex);
}
ctx.queryState = QUERY_RECORD_SUFFIX;
case QUERY_RECORD_SUFFIX:
r.bookmark();
r.put(']');
ctx.record = null;
ctx.queryState = QUERY_RECORD_START;
break;
case QUERY_DATA_SUFFIX:
sendDone(r, ctx);
break OUT;
default:
break OUT;
}
} catch (ResponseContentBufferTooSmallException ignored) {
if (r.resetToBookmark()) {
r.sendChunk();
} else {
// what we have here is out unit of data, column value or query
// is larger that response content buffer
// all we can do in this scenario is to log appropriately
// and disconnect socket
ctx.info().$("Response buffer is too small, state=").$(ctx.queryState).$();
throw DisconnectedChannelException.INSTANCE;
}
}
}
}
Aggregations