use of org.apache.geode.cache.query.Struct in project geode by apache.
the class Coder method getBulkStringArrayResponseOfValues.
public static ByteBuf getBulkStringArrayResponseOfValues(ByteBufAllocator alloc, Collection<?> items) {
Iterator<?> it = items.iterator();
ByteBuf response = alloc.buffer();
response.writeByte(Coder.ARRAY_ID);
ByteBuf tmp = alloc.buffer();
int size = 0;
while (it.hasNext()) {
Object next = it.next();
ByteArrayWrapper nextWrapper = null;
if (next instanceof Entry) {
try {
nextWrapper = (ByteArrayWrapper) ((Entry<?, ?>) next).getValue();
} catch (EntryDestroyedException e) {
continue;
}
} else if (next instanceof Struct) {
nextWrapper = (ByteArrayWrapper) ((Struct) next).getFieldValues()[1];
}
if (nextWrapper != null) {
tmp.writeByte(Coder.BULK_STRING_ID);
tmp.writeBytes(intToBytes(nextWrapper.length()));
tmp.writeBytes(Coder.CRLFar);
tmp.writeBytes(nextWrapper.toBytes());
tmp.writeBytes(Coder.CRLFar);
} else {
tmp.writeBytes(Coder.bNIL);
}
size++;
}
response.writeBytes(intToBytes(size));
response.writeBytes(Coder.CRLFar);
response.writeBytes(tmp);
tmp.release();
return response;
}
use of org.apache.geode.cache.query.Struct in project geode by apache.
the class LIndexExecutor method executeCommand.
@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
List<byte[]> commandElems = command.getProcessedCommand();
if (commandElems.size() < 3) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.LINDEX));
return;
}
ByteArrayWrapper key = command.getKey();
byte[] indexArray = commandElems.get(2);
checkDataType(key, RedisDataType.REDIS_LIST, context);
Region<Integer, ByteArrayWrapper> keyRegion = getRegion(context, key);
if (keyRegion == null) {
command.setResponse(Coder.getNilResponse(context.getByteBufAllocator()));
return;
}
int listSize = keyRegion.size() - LIST_EMPTY_SIZE;
Integer redisIndex;
try {
redisIndex = Coder.bytesToInt(indexArray);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_NUMERIC));
return;
}
if (redisIndex < 0)
// Since the redisIndex is negative here, this will reset it to be a standard 0 based index
redisIndex = listSize + redisIndex;
/*
* If the index is still less than 0 that means the index has shot off back past the beginning,
* which means the index isn't real and a nil is returned
*/
if (redisIndex < 0) {
command.setResponse(Coder.getNilResponse(context.getByteBufAllocator()));
return;
}
/*
* Now we must get that element from the region
*/
Struct entry;
try {
entry = getEntryAtIndex(context, key, redisIndex);
} catch (Exception e) {
throw new RuntimeException(e);
}
if (entry == null) {
command.setResponse(Coder.getNilResponse(context.getByteBufAllocator()));
return;
}
Object[] entryArray = entry.getFieldValues();
ByteArrayWrapper valueWrapper = (ByteArrayWrapper) entryArray[1];
command.setResponse(Coder.getBulkStringResponse(context.getByteBufAllocator(), valueWrapper.toBytes()));
}
use of org.apache.geode.cache.query.Struct in project geode by apache.
the class LRangeExecutor method getRange.
private List<Struct> getRange(ExecutionHandlerContext context, ByteArrayWrapper key, int start, int stop, Region r) throws Exception {
Query query = getQuery(key, ListQuery.LRANGE, context);
Object[] params = { Integer.valueOf(stop + 1) };
SelectResults<Struct> results = (SelectResults<Struct>) query.execute(params);
int size = results.size();
if (results == null || size <= start) {
return null;
}
return results.asList().subList(start, size);
}
use of org.apache.geode.cache.query.Struct in project geode by apache.
the class LRemExecutor method executeCommand.
@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
List<byte[]> commandElems = command.getProcessedCommand();
if (commandElems.size() < 4) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.LREM));
return;
}
ByteArrayWrapper key = command.getKey();
byte[] countArray = commandElems.get(2);
byte[] value = commandElems.get(3);
int count;
checkDataType(key, RedisDataType.REDIS_LIST, context);
Region<Integer, ByteArrayWrapper> keyRegion = getRegion(context, key);
if (keyRegion == null) {
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), NOT_EXISTS));
return;
}
try {
count = Coder.bytesToInt(countArray);
} catch (NumberFormatException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ERROR_NOT_NUMERIC));
return;
}
List<Struct> removeList;
try {
removeList = getRemoveList(context, key, new ByteArrayWrapper(value), count);
} catch (Exception e) {
throw new RuntimeException(e);
}
int numRemoved = 0;
if (removeList == null) {
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), numRemoved));
return;
}
for (Struct entry : removeList) {
Integer removeKey = (Integer) entry.getFieldValues()[0];
Object oldVal = keyRegion.remove(removeKey);
if (oldVal != null)
numRemoved++;
}
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), numRemoved));
}
use of org.apache.geode.cache.query.Struct in project geode by apache.
the class GroupByQueryDUnitTest method runQuery.
private void runQuery(VM queryVM) throws Exception {
// createIndex(vm0, "compactRangeIndex", "entry.value",
// "/region.entrySet entry");
// Do Puts
queryVM.invoke(new SerializableRunnable("putting data") {
public void run() {
Cache cache = getCache();
Region region = cache.getRegion("portfolio");
for (int i = 1; i < 200; ++i) {
Portfolio pf = new Portfolio(i);
pf.shortID = (short) ((short) i / 5);
region.put("" + i, pf);
}
}
});
queryVM.invoke(new SerializableRunnable("query") {
public void run() {
try {
QueryService qs = getCache().getQueryService();
String queryStr = "select p.shortID as short_id from /portfolio p where p.ID >= 0 group by short_id ";
Query query = qs.newQuery(queryStr);
SelectResults<Struct> results = (SelectResults<Struct>) query.execute();
Iterator<Struct> iter = results.iterator();
int counter = 0;
while (iter.hasNext()) {
Struct str = iter.next();
assertEquals(counter++, ((Short) str.get("short_id")).intValue());
}
assertEquals(39, counter - 1);
} catch (QueryInvocationTargetException e) {
fail(e.toString());
} catch (NameResolutionException e) {
fail(e.toString());
} catch (TypeMismatchException e) {
fail(e.toString());
} catch (FunctionDomainException e) {
fail(e.toString());
}
}
});
}
Aggregations