收集更新优化

huangapple go评论83阅读模式
英文:

Collection update optimization

问题

例如,我们在数据库中存储了一组对象(在此简化为字符串):

Collection<String> existed = someRepository.findElements(); // 返回 "A", "B", "B", "C"

我们有一个新的集合,必须替换旧数据:

Collection<String> received = Arrays.asList("B", "C", "C", "D");

最初的想法是从数据库中删除所有先前的数据,并保存来自 received 集合的所有数据,如下所示:

someRepository.removeAll(existed);
someRepository.saveAll(received);

但是,如果存在许多共同的元素,这将导致应用程序和数据库之间的多余数据传输。

另一方面,我们可以查找要删除和添加的元素,但这将需要大量的 equals 方法调用,并且在内存中检查所有项目将会更慢,而且在我看来这不是一个好方法。

最希望的结果是:

  • "A",第二个 "B" 将会在要删除的集合中(我们为此调用 removeAll
  • 一个 "C","D" 将会在要添加的集合中(我们为此调用 saveAll
  • 共同的 "B" 和 "C" 将保持不变

那么,您是否有任何解决此类情况更优化的方法呢?谢谢。

英文:

For example we have collection stored objects in DB (simplified to String for example):

Collection&lt;String&gt; existed = someRepository.findElements(); //returns &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;C&quot;

We have a new collection that must replace old data:

Collection&lt;String&gt; received = Arrays.asList(&quot;B&quot;, &quot;C&quot;, &quot;C&quot; ,&quot;D&quot;);

The first idea was to remove all previous data from DB and save all from received collection like:

someRepository.removeAll(existed);
someRepository.saveAll(received);

But in case of lot's of common elements, it will cause surplus data transfer between application and DB.

On other hand we can find elements what to remove, to add, but it requires a lot of equals method calling and it will be slower to check all the items in memory, and IMHO its not good.

The mostly liken expected result is:

  • "A", second "B" will be in toRemove collection (that we call for removeAll)
  • one "C", "D" will be in toAdd collection (that we call for saveAll)
  • common "B" and "C" will not be touched

So do you have any solution to resolve such cases more optimized?
Thanks.

答案1

得分: 0

已从Apache Commons中找到实用程序:

//获取两个集合之间的交集
Collection intersection = CollectionUtils.intersection(existed, received);

//获取已存在集合与交集之间的不相交部分
Collection toRemove = CollectionUtils.disjunction(existed, intersection);

//获取接收集合与交集之间的不相交部分
Collection toAdd = CollectionUtils.disjunction(received, intersection);
英文:

Have found utilities from Apache Commons:

Collection intersection = CollectionUtils.intersection(existed, received); //intersection between 2 collections

Collection toRemove = CollectionUtils.disjunction(existed, intersection); //getting disjunction between existed and intersection
Collection toAdd = CollectionUtils.disjunction(received, intersection); //getting disjunction between received and intersection

huangapple
  • 本文由 发表于 2020年7月23日 18:34:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63052237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定