List of usage examples for org.apache.commons.beanutils BeanMap forEach
default void forEach(BiConsumer<? super K, ? super V> action)
From source file:com.github.jackygurui.vertxredissonrepository.repository.Impl.RedisRepositoryImpl.java
private void persistBlocking(String id, JsonObject data, RBatch redissonBatch, Handler<AsyncResult<Boolean>> resultHandler) { RBatch batch = redissonBatch == null ? redissonWrite.createBatch() : redissonBatch; AtomicBoolean failed = new AtomicBoolean(false); try {// w w w. java 2s .c o m BeanMap pMap = new BeanMap(cls.newInstance()); //remove the indexes; if (isRedisEntity()) { AtomicBoolean finished = new AtomicBoolean(false); AtomicBoolean hasNested = new AtomicBoolean(false); AtomicLong stack = new AtomicLong(); pMap.forEach((k, v) -> { if ("class".equals(k)) { return; } Class<?> type = pMap.getType((String) k); if (!isRedisEntity(type)) { //recreate the indexes; if ("id".equals(k)) { batch.getMap(getStorageKey(), StringCodec.INSTANCE).fastPutAsync(id, id); } else { batch.getMap(getStorageKey((String) k)).fastPutAsync(id, data.getValue((String) k)); } } else { hasNested.set(true); stack.incrementAndGet(); RedisRepositoryImpl<?> innerRepo; try { innerRepo = (RedisRepositoryImpl) factory.instance(type); } catch (RepositoryException e) { throw new RuntimeException(e); } JsonObject value = data.getJsonObject((String) k); final boolean newOne = !value.containsKey("id") || value.getString("id") == null || "null".equals(value.getString("id")); final String ID = newOne ? id : value.getString("id"); innerRepo.persist(ID, value, batch, c -> {//making the nested entity shares the same id as the parent when its 1:1 relation. This makes fetch a lot faster since it doesn't not need to resolve the reference when fetching 1:1 nested objects. if (c.succeeded()) { long s = stack.decrementAndGet(); if (newOne) { batch.getMap(getStorageKey((String) k)).fastPutAsync(id, ID);//different to the update, create needs to add the reference field to batch } if (s == 0 && finished.get() && !failed.get()) { //finished iterating and no outstanding processes. if (redissonBatch == null) {//if it's not inside a nested process. finishPersist(id, data, batch, resultHandler); } else {//if it is inside a nested process. resultHandler.handle(Future.succeededFuture(true)); } } //else wait for others to complete } else { boolean firstToFail = failed.compareAndSet(false, true); if (firstToFail) { resultHandler.handle(Future.failedFuture(c.cause())); } } }); } }); batch.getAtomicLongAsync(getCounterKey()).incrementAndGetAsync(); finished.set(true); if (!hasNested.get()) {//does not have nested RedissonEntity within if (redissonBatch == null) {//if it's not inside a nested process. finishPersist(id, data, batch, resultHandler); } else {//if it is inside a nested process. resultHandler.handle(Future.succeededFuture(true)); } } } else {//not a RedissonEntity class, persist as json string. //recreate the indexes; batch.<String, String>getMap(getStorageKey(), StringCodec.INSTANCE).fastPutAsync(id, Json.encode(data)); batch.getAtomicLongAsync(getCounterKey()).incrementAndGetAsync(); if (redissonBatch == null) {//if it's not inside a nested process. finishPersist(id, data, batch, resultHandler); } else {//if it is inside a nested process. resultHandler.handle(Future.succeededFuture(true)); } } } catch (InstantiationException | IllegalAccessException | RuntimeException ex) { failed.set(true); resultHandler.handle(Future.failedFuture(ex)); } }