列表排序频繁更新的设计优化

假设现在有一个列表,这个列表是需要排序的,该列表的顺序对于其他功能是有意义的。所以我们需要记录这个列表的顺序。
我们可以在该列表对应的数据库中增加一个sort(int)字段记录排序值,从0到N。我们可以想象,对一条数据改动(排序值为n)的时候,后面的数据从(n+1)开始,sort字段都要做更新。或者直接删除在插入整个列表。显然这样更新过于频繁,如果数据量大,对数据库也会有一定压力。


sort.png

解决办法: 使用链表来做记录。

  1. 在数据库中增加 predecessorId字段来取代 sort 字段。该字段保存上一条数据的 id
  2. 增加一条假数据,作为第一条数据。这样做的目的是为了找到第一条数据。

这样以来,更新的时候, 你只需要改动更新数据的前后两条数据就可以了,前后台处理起来也很方便,就很机智。
在代码中处理排序。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class test {

    public static class Student{
        private String id;
        private String predecessorId;
        private String name;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getPredecessorId() {
            return predecessorId;
        }

        public void setPredecessorId(String predecessorId) {
            this.predecessorId = predecessorId;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Student(String id, String predecessorId, String name) {
            this.id = id;
            this.predecessorId = predecessorId;
            this.name = name;
        }
    }

    public static List getSort(List list){
        String firstDataPredecessorId = "START_OF_LIST";
        // Create map of a mean found by its predecessor's ID
        Map map = new HashMap<>();
        for (Student bean : list) {
            map.put(bean.getPredecessorId(), bean);
        }
        // The first item of the list, will be that with the predecessor ID equal to START-OF-LIST
        Student item = map.get(firstDataPredecessorId);
        List result = new ArrayList<>();

        // Return empty list if we cannot get the first item
        if (item == null) {
            return result;
        }
        // The last item of the list, will be the item whose ID is not equal to any predecessor ID in the map
        // Therefore, while the variable 'item' has a predecessor ID the item itself is added to the list (including the last entry)
        do {
            result.add(item);
        }
        while ((item = map.get(item.getId())) != null);
        return result;
    }

    public static void main(String[] args) {
        // for the first data, id and predecessorId are both START_OF_LIST
        Student s0 = new Student("START_OF_LIST", "START_OF_LIST", "name1");
        // this is real first data, pay attention to the predecessorId,
        // then we will know it follow the data that id is START_OF_LIST
        Student s1 = new Student("id1", "START_OF_LIST", "name1");
        Student s2 = new Student("id2", "id1", "name1");
        Student s3 = new Student("id3", "id2", "name1");
        Student s4 = new Student("id4", "id3", "name1");

        // we create list without sort
        List list = new ArrayList<>();
        list.add(s4);
        list.add(s0);
        list.add(s3);
        list.add(s1);
        list.add(s2);
        List sortList = getSort(list);
        sortList.forEach(l -> System.out.println(l.getId()));

        // result: id1 ,id2, id3, id4 .
        // success!!!
    }
}

你可能感兴趣的:(列表排序频繁更新的设计优化)