PartitionedRegionIDGeneratorFunction implements Function, Declarable { @Override public void execute(FunctionContext functionContext) { try { String domain = null; Object arguments = functionContext.getArguments(); if (arguments != null) { if (arguments.getClass().isArray()) { Object[] args = (Object[]) arguments; assert args.length == 1; domain = args[0].toString(); } else { domain = arguments.toString(); } } else { RegionFunctionContext rctx = (RegionFunctionContext) functionContext; Set<String> filter = (Set<String>) rctx.getFilter(); assert filter.size() == 1; domain = filter.iterator().next(); } LOG.debug("execute: domain={}", domain); Region<String, Long> region = getRegion(); synchronized (PartitionedRegionIDGeneratorFunction.class) { Long firstId = region.computeIfAbsent(domain, t -> -1L) + 1; long lastId = firstId + idCount - 1; LOG.debug("execute: domain={}, firstId={}, lastId={}", domain, firstId, lastId); region.put(domain, lastId); functionContext.getResultSender().lastResult(new long[]{firstId, lastId}); } } catch (Exception x) { LOG.error("execute: x={}", x.toString(), x); throw new IllegalArgumentException(x.toString(), x); } } PartitionedRegionIDGeneratorFunction();  PartitionedRegionIDGeneratorFunction(String idRegionName, int idCount);  PartitionedRegionIDGeneratorFunction(int idCount, Region<String, Long> idRegion); @Override void execute(FunctionContext functionContext); @Override String getId(); @Override boolean hasResult(); @Override boolean optimizeForWrite(); @Override boolean isHA(); @Override void init(Properties properties);  }
@Test void usingArgs() { Collection<long[]> coll = (Collection<long[]>) functionServiceHelper .onRegion(idRegion) .withArgs("test") .execute("PartitionedRegionIDGeneratorFunction") .getResult(); assertEquals(coll.size(), 1); long[] ids = coll.iterator().next(); assertEquals(ids.length, 2); assertEquals(ids[0], 0); assertEquals(ids[1], 1); }
@Test void usingFilter() { Collection<long[]> coll = (Collection<long[]>) functionServiceHelper .onRegion(idRegion) .withFilter(Collections.singleton("domain")) .execute("PartitionedRegionIDGeneratorFunction") .getResult(); assertEquals(coll.size(), 1); long[] ids = coll.iterator().next(); assertEquals(ids.length, 2); assertEquals(ids[0], 0); assertEquals(ids[1],1); }
@Test void usingArgsMultipleCalls() { Collection<long[]> coll1 = (Collection<long[]>) functionServiceHelper .onRegion(idRegion) .withArgs("test") .execute("PartitionedRegionIDGeneratorFunction") .getResult(); assertEquals(coll1.size(), 1); long[] ids1 = coll1.iterator().next(); assertEquals(ids1.length, 2); assertEquals(ids1[0], 0); assertEquals(ids1[1], 1); Collection<long[]> coll2 = (Collection<long[]>) functionServiceHelper .onRegion(idRegion) .withArgs("test") .execute("PartitionedRegionIDGeneratorFunction") .getResult(); assertEquals(coll2.size(), 1); long[] ids2 = coll2.iterator().next(); assertEquals(ids2.length, 2); assertEquals(ids2[0], 2); assertEquals(ids2[1], 3); }