Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc83496e

Browse files
authored
fix: 修复树结构删除或者查询数量较大时可能导致请求pending. (#244)
1 parent94e27ba commitc83496e

File tree

1 file changed

+22
-119
lines changed

1 file changed

+22
-119
lines changed

‎hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/service/ReactiveTreeSortEntityService.java‎

Lines changed: 22 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,19 @@ default Flux<E> queryIncludeChildren(QueryParamEntity queryParam) {
132132
.concatMap(e -> !StringUtils.hasText(e.getPath()) || !duplicateCheck.add(e.getPath())
133133
?Mono.just(e)
134134
:createQuery()
135+
.as(q -> {
136+
if (CollectionUtils.isNotEmpty(queryParam.getIncludes())) {
137+
q.select(queryParam.getIncludes().toArray(newString[0]));
138+
}
139+
if (CollectionUtils.isNotEmpty(queryParam.getExcludes())) {
140+
q.selectExcludes(queryParam.getExcludes().toArray(newString[0]));
141+
}
142+
returnq;
143+
})
135144
.where()
136145
.like$("path",e.getPath())
137146
.fetch()
138-
)
147+
,Integer.MAX_VALUE)
139148
.distinct(TreeSupportEntity::getId);
140149
}
141150

@@ -276,112 +285,7 @@ default Mono<SaveResult> save(Publisher<E> entityPublisher) {
276285

277286
@Deprecated
278287
defaultFlux<E>tryRefactorPath(Flux<E>stream) {
279-
Flux<E>cache =stream.cache();
280-
Mono<Map<K,E>>mapping =cache
281-
.filter(e ->null !=e.getId())
282-
.collectMap(TreeSupportEntity::getId,Function.identity())
283-
.defaultIfEmpty(Collections.emptyMap());
284-
285-
Mono<Map<K,E>>allDataFetcher =
286-
cache
287-
.filter(e ->null !=e.getId())
288-
.flatMapIterable(e ->e.getParentId() !=null ?
289-
Arrays.asList(e.getId(),e.getParentId()) :
290-
Collections.singleton(e.getId()))
291-
.collect(Collectors.toSet())
292-
.flatMap(list ->this
293-
.queryIncludeChildren(list)
294-
.collect(
295-
Collectors.toMap(
296-
TreeSupportEntity::getId,
297-
Function.identity()
298-
)
299-
));
300-
301-
returnMono
302-
.zip(mapping,allDataFetcher)
303-
.flatMapMany(tp2 -> {
304-
//本次提交的数据
305-
Map<K,E>thisTime =tp2.getT1();
306-
//旧的数据
307-
Map<K,E>oldMap =tp2.getT2();
308-
309-
Map<K,E>allMap =newLinkedHashMap<>(oldMap);
310-
allMap.putAll(thisTime);
311-
312-
//子节点映射
313-
Map<K,Map<K,E>>childMapping =newLinkedHashMap<>();
314-
315-
List<E>all =newArrayList<>(oldMap.values());
316-
all.addAll(thisTime.values());
317-
318-
for (Evalue :all) {
319-
if (isRootNode(value) ||value.getId() ==null) {
320-
continue;
321-
}
322-
childMapping
323-
.computeIfAbsent(value.getParentId(),ignore ->newLinkedHashMap<>())
324-
.put(value.getId(),value);
325-
}
326-
327-
Function<K,Collection<E>>childGetter
328-
=id ->childMapping
329-
.getOrDefault(id,Collections.emptyMap())
330-
.values();
331-
returncache
332-
.concatMap(data -> {
333-
Eold =data.getId() ==null ?null :oldMap.get(data.getId());
334-
KparentId =old !=null ?old.getParentId() :data.getParentId();
335-
EoldParent =parentId ==null ?null :allMap.get(parentId);
336-
337-
if (old !=null) {
338-
KnewParentId =data.getParentId();
339-
//父节点发生变化,更新所有子节点path
340-
if (!Objects.equals(parentId,newParentId)) {
341-
Consumer<E>childConsumer =child -> {
342-
//更新了父节点,但是同时也传入的对应的子节点
343-
EreadyToUpdate =thisTime.get(child.getId());
344-
if (null !=readyToUpdate) {
345-
readyToUpdate.setPath(child.getPath());
346-
}
347-
};
348-
349-
//变更到了顶级节点
350-
if (isRootNode(data)) {
351-
data.setPath(RandomUtil.randomChar(4));
352-
this.refactorChildPath(old.getId(),childGetter,data.getPath(),childConsumer);
353-
//重新保存所有子节点
354-
returnFlux
355-
.fromIterable(childGetter.apply(old.getId()))
356-
.concatWithValues(data);
357-
}else {
358-
EnewParent =allMap.get(newParentId);
359-
if (null !=newParent) {
360-
data.setPath(newParent.getPath() +"-" +RandomUtil.randomChar(4));
361-
this.refactorChildPath(data.getId(),childGetter,data.getPath(),childConsumer);
362-
//重新保存所有子节点
363-
returnFlux.fromIterable(childGetter.apply(data.getId()))
364-
.concatWithValues(data);
365-
}
366-
}
367-
returnMono.just(data);
368-
}else {
369-
370-
if (oldParent !=null) {
371-
if (old.getPath().startsWith(oldParent.getPath())) {
372-
data.setPath(old.getPath());
373-
}else {
374-
data.setPath(oldParent.getPath() +"-" +RandomUtil.randomChar(4));
375-
}
376-
}else {
377-
data.setPath(old.getPath());
378-
}
379-
}
380-
}
381-
returnMono.just(data);
382-
});
383-
})
384-
.distinct(TreeSupportEntity::getId);
288+
returnnewTreeSortServiceHelper<>(this).prepare(stream);
385289
}
386290

387291
@Override
@@ -415,7 +319,7 @@ default Mono<Integer> deleteById(Publisher<K> idPublisher) {
415319
.findById(Flux.from(idPublisher))
416320
.concatMap(e ->StringUtils.hasText(e.getPath())
417321
?getRepository().createDelete().where().like$(e::getPath).execute()
418-
:getRepository().deleteById(e.getId()))
322+
:getRepository().deleteById(e.getId()),Integer.MAX_VALUE)
419323
.as(MathFlux::sumInt);
420324
}
421325

@@ -445,22 +349,21 @@ default boolean isRootNode(E entity) {
445349
}
446350

447351
@Override
352+
@SuppressWarnings("all")
448353
defaultReactiveDeletecreateDelete() {
449354
returnReactiveCrudService.super
450355
.createDelete()
451-
.onExecute((delete,executor) ->this
452-
.getRepository()
453-
.createQuery()
454-
.setParam(delete.toQueryParam())
455-
.fetch()
456-
.filter(e ->StringUtils.hasText(e.getPath()))
457-
//删除所有子节点
458-
.concatMap(e ->getRepository()
356+
.onExecute((delete,executor) ->this
357+
.queryIncludeChildren(delete.toQueryParam(QueryParamEntity::new)
358+
.<QueryParamEntity>includes("id","path","parentId"))
359+
.map(TreeSupportEntity::getId)
360+
.buffer(200)
361+
.concatMap(list ->getRepository()
459362
.createDelete()
460363
.where()
461-
.like$(e::getPath)
462-
.execute())
463-
.concatWith(executor)
364+
.in("id",list)
365+
.execute(),Integer.MAX_VALUE)
366+
//.concatWith(executor)
464367
.reduce(0,Math::addExact));
465368
}
466369
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp