野狗 Sync 分析2 - 数据排序

来源:互联网 发布:c语言putchar函数 编辑:程序博客网 时间:2024/05/01 07:36

数据排序分析

接着上一篇来讲,我们获取数据往往是对数据有排序要求的,那么在 Wilddog Sync 中怎样实现排序的需求的?


排序方式

野狗提供了四种排序方式:

orderByChild(value)orderByKey()orderByValue()orderByPriority()

我们使用代码测试一下这四种排序方式不同之处。

首先我们要构造一个无序的列表,使用Random随机模拟一个列表就可以了,生成代码如下:

        //定义随机姓名列表        private List<String> nameLst=new ArrayList<>();        nameLst.add("Ann");        ...        Random random=new Random(System.currentTimeMillis());        for (int i=0;i<=5;i++){            int nextInt = random.nextInt(1000);            Map<String,Object> map=new ArrayMap<>();            map.put("name",nameLst.get(random.nextInt(24)));            map.put("age", random.nextInt(100));            reference.child("orderTest").child(String.valueOf(nextInt))                     .setValue(map,random.nextInt(100));        }

注意这一句 ,我们使用了 setValue(map,priority) 方法为节点设置了优先级。

reference.child().setValue(map,random.nextInt(100));

运行之后生成的数据结构如下:

"orderTest":{    {      "159": {        "age": 28,        "name": "Hanna"      },      "50": {        "age": 17,        "name": "Nauti"      },      "659": {        "age": 20,        "name": "Ted"      },      "711": {        "age": 7,        "name": "Google"      },      "849": {        "age": 79,        "name": "Oppo"      },      "951": {        "age": 54,        "name": "David"      }    }}

接下来我们分别设置四种监听并打印日志,代码如下:

//orderByChild使用name字段排序reference.child("orderTest").orderByChild("name").addChildEventListener(//...);//orderByChild使用age字段排序reference.child("orderTest").orderByChild("age").addChildEventListener(//...);//orderByKeyreference.child("orderTest").orderByKey().addChildEventListener(//...);//orderByValuereference.child("orderTest").orderByValue().addChildEventListener(//...);//orderByPriorityreference.child("orderTest").orderByPriority().addChildEventListener(//...);

排序结果:

E/orderByChild : onChildAdded:DataSnapshot { key = 951, value = {.priority=83.0, name=David, age=54} }E/orderByChild : onChildAdded:DataSnapshot { key = 711, value = {.priority=14.0, name=Google, age=7} }E/orderByChild : onChildAdded:DataSnapshot { key = 159, value = {.priority=93.0, name=Hanna, age=28} }E/orderByChild : onChildAdded:DataSnapshot { key = 50, value = {.priority=89.0, name=Nauti, age=17} }E/orderByChild : onChildAdded:DataSnapshot { key = 849, value = {.priority=41.0, name=Oppo, age=79} }E/orderByChild : onChildAdded:DataSnapshot { key = 659, value = {.priority=83.0, name=Ted, age=20} }E/orderByChild : onChildAdded:DataSnapshot { key = 711, value = {.priority=14.0, name=Google, age=7} }E/orderByChild : onChildAdded:DataSnapshot { key = 50, value = {.priority=89.0, name=Nauti, age=17} }E/orderByChild : onChildAdded:DataSnapshot { key = 659, value = {.priority=83.0, name=Ted, age=20} }E/orderByChild : onChildAdded:DataSnapshot { key = 159, value = {.priority=93.0, name=Hanna, age=28} }E/orderByChild : onChildAdded:DataSnapshot { key = 951, value = {.priority=83.0, name=David, age=54} }E/orderByChild : onChildAdded:DataSnapshot { key = 849, value = {.priority=41.0, name=Oppo, age=79} }E/orderByKey : onChildAdded:DataSnapshot { key = 50, value = {.priority=89.0, name=Nauti, age=17} }E/orderByKey : onChildAdded:DataSnapshot { key = 159, value = {.priority=93.0, name=Hanna, age=28} }E/orderByKey : onChildAdded:DataSnapshot { key = 659, value = {.priority=83.0, name=Ted, age=20} }E/orderByKey : onChildAdded:DataSnapshot { key = 711, value = {.priority=14.0, name=Google, age=7} }E/orderByKey : onChildAdded:DataSnapshot { key = 849, value = {.priority=41.0, name=Oppo, age=79} }E/orderByKey : onChildAdded:DataSnapshot { key = 951, value = {.priority=83.0, name=David, age=54} }E/orderByValue : onChildAdded:DataSnapshot { key = 50, value = {.priority=89.0, name=Nauti, age=17} }E/orderByValue : onChildAdded:DataSnapshot { key = 159, value = {.priority=93.0, name=Hanna, age=28} }E/orderByValue : onChildAdded:DataSnapshot { key = 659, value = {.priority=83.0, name=Ted, age=20} }E/orderByValue : onChildAdded:DataSnapshot { key = 711, value = {.priority=14.0, name=Google, age=7} }E/orderByValue : onChildAdded:DataSnapshot { key = 849, value = {.priority=41.0, name=Oppo, age=79} }E/orderByValue : onChildAdded:DataSnapshot { key = 951, value = {.priority=83.0, name=David, age=54} }E/orderByPriority : onChildAdded:DataSnapshot { key = 711, value = {.priority=14.0, name=Google, age=7} }E/orderByPriority : onChildAdded:DataSnapshot { key = 849, value = {.priority=41.0, name=Oppo, age=79} }E/orderByPriority : onChildAdded:DataSnapshot { key = 659, value = {.priority=83.0, name=Ted, age=20} }E/orderByPriority : onChildAdded:DataSnapshot { key = 951, value = {.priority=83.0, name=David, age=54} }E/orderByPriority : onChildAdded:DataSnapshot { key = 50, value = {.priority=89.0, name=Nauti, age=17} }E/orderByPriority : onChildAdded:DataSnapshot { key = 159, value = {.priority=93.0, name=Hanna, age=28} }

总结:

1/可以很明显的看出排序的规则:

排序方法 排序规则 orderByChild 以传入的节点名称作为排序字段进行排序,如”name” 和 “age”字段 orderByKey 是以节点key值作为排序字段进行排序 orderByValue 无法直观得出排序依据,其实是通过计算对象的Hash值作为排序依据 orderByPriority 设置的节点优先级

应用实践

通过以上实验我们了解了排序的规则和排序的依据。

语义分析

分析一下每一个方法的语义:

1/orderByChild
orderByChild的 Child 指的是对子节点数据 value 的某一个字段进行排序。
2/ orderByKey
orderByKey 是对子节点 key 进行排序。
3/orderByValue
分为两种情况,如果 value 是可排序的,例如 int/double 等值类型则会按值排序,Char 和 String 按字典顺序排序。
注意一点字符串的排序顺序,例如数据为:

{  "test1": "abcd",  "test2": "Abcd",  "test3": "bbcd",  "test4": "Bbcd",  "test5": "aBcd",  "test6": "ABcd",  "test7": "bBcd",  "test8": "BBcd"}

排序结果是:

E/value: onChildAdded:DataSnapshot { key = test6, value = ABcd }E/value: onChildAdded:DataSnapshot { key = test2, value = Abcd }E/value: onChildAdded:DataSnapshot { key = test8, value = BBcd }E/value: onChildAdded:DataSnapshot { key = test4, value = Bbcd }E/value: onChildAdded:DataSnapshot { key = test5, value = aBcd }E/value: onChildAdded:DataSnapshot { key = test1, value = abcd }E/value: onChildAdded:DataSnapshot { key = test7, value = bBcd }E/value: onChildAdded:DataSnapshot { key = test3, value = bbcd }

看到了吧,和你预想的结果是否一致呢,在使用的时候要注意到字符串排序的顺序性。

第二种情况下如果value是一个对象,则将其做哈希运算,然后使用哈希值排序,当然这种排序通常来说是无意义的,实践中这种情况不要使用orderByValue。
4/orderByPriority
orderByPriority是对我们 setValue() 时添加的Priority进行排序,如果没有添加Priority,则不会排序。

返回的结果是升序的

注意到,所有返回的数据都是升序(ASC)的,如果想使用降序(DESC)需要自己对返回的数据进行降序处理。

只能设置一次排序操作

在使用SQL时,我们有时会对多个字段进行排序。

SELECT * FROM table1 ORDER BY field1,field2 ASC

但是在Wilddog Sync 中并不支持多字段排序,那我们如果同时为一个路径下的数据设置两次OrderBy监听会怎样呢?

reference.child("orderTest").orderByChild("name").orderByPriority().addChildEventListener(

对不起,不支持这样的方式,每次只能使用一种排序方式。

java.lang.IllegalArgumentException: You can't combine multiple orderBy calls!

经常排序使用规则表达式建立索引

如果需要经常对数据进行排序查询,而数据量也很大的情况下,这时就建议使用规则表达式 建立索引的方式。这样可以降低资源消耗,同时提升查询效率。

什么是规则表达式?以后再讲,知道有这个神奇的东西就可以了。

我们现在已经研究了如何获取数据,如何对数据进行排序,下一篇我会分析一下如何使用数据筛选功能过滤掉不需要的数据。

0 0
原创粉丝点击