尝试了解在我的方案中如何将Firebase数据库工作非规范化 [英] Try to understand how well can denormalize work with firebase database in my scenario

查看:170
本文介绍了尝试了解在我的方案中如何将Firebase数据库工作非规范化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上我建立所有以前的应用程序与MySQL存储数据,并考虑切换到Firebase数据库,这让我觉得有点疯狂。我试图实现一个聊天系统,但是在理解它的工作方式时遇到了一些麻烦。根据反规范化您的数据是正常的我得到了如何使工作正常化的想法和概念。

我知道我可以创建一个消息结构来存储下面的聊天消息(来自 doc

 messages: {
one:{
m1:{
name:eclarke,
message:继电器似乎有故障。 b $ btimestamp:1459361875337
},
m2:{...},
m3:{...}
},
two:{...},
three:{...}
}

但是,这里是我的情况,假设我有一个可以容纳1000人的群聊。


  • 场景1:我允许用户删除邮件。当用户向组聊天发送了一些内容并且其中一个成员感到不舒服并决定删除它时,这将导致其他用户无法看到该消息。
  • 场景2
  • $ b

    对于 SQL 版本中的方案一,当用户删除并执行下面的查询时,创建一个包含userID的messageID仍然可以用于其他用户。

      SELECT * FROM message WHERE roomId = $ userId AND messageId NOT IN($ messageId)

    但是firebase数据库不支持这个功能,所以我现在想的唯一方法就是写每一个新的消息给所有成员树

     messages:{
    john:{
    m1 :{
    name:eclarke,
    message:继电器似乎有故障。,
    timestamp:1459361875337,
    groupId:1
    },
    m2:{...},
    m3:{...}
    },
    jimmy:{...},
    paul:{...}
    }因此,当用户删除消息,我可以很容易地从他自己的消息节点中删除它,所以消息仍然可以用于其他用户。



    如果群聊最多有1000人,我可能需要写1000个用户节点树,在Firebase数据库中写入这样大量的数据时出现问题?我的意思是写作时间很长,磁盘空间等问题还是有更好的方法来做到这一点?如果这很好,我想我也能解决我的情况2。我觉得很抱歉,如果从标题误解,因为它真的很难从SQL切换到SQL。

    感谢任何提前。
    不,你不需要写数据到1000个用户节点,你只需要写一次。为了达到这个目的,你需要有一个如下所示的数据库结构:

      Firebase-root 
    |
    ---- Groups
    | |
    | ---- groupId
    | |
    | ---- groupName:Android
    | |
    | ----用户
    | | |
    | | ---- userId1:true
    | | |
    | | ---- userId2:true
    | |
    | ----消息
    | |
    | ---- messageId1:你好
    | |
    | ---- messageId2:Hy
    | |
    | //等
    |
    ----消息
    |
    ---- messageId1
    | |
    | ----用户
    | |
    | ---- userId1:true
    | |
    | ---- userId2:true
    |
    ---- messageId2
    |
    ----用户
    |
    ---- userId1:true
    |
    ---- userId2:false //第二条消息为false

    希望有帮助。

    So basically i'm building all previous application with mysql to store data and consider switch to firebase database which make me feel a little bit crazy. I'm trying to implement a chat system but there is some trouble when understanding how it work. Based on the Denormalizing Your Data is Normal I get the idea and concept on how does denormalize work.

    I know I could just create a message structure to store chat message like below (the example from doc)

    "messages": {
        "one": {
          "m1": {
            "name": "eclarke",
            "message": "The relay seems to be malfunctioning.",
            "timestamp": 1459361875337
          },
          "m2": { ... },
          "m3": { ... }
        },
        "two": { ... },
        "three": { ... }
    }
    

    But here is my scenario let say I having a group chat which can contain up to 1000 people.

    • Scenario 1 : I'm allow user to delete the message. When a user send something gross to the group chat and one of the member feel uncomfortable and decide to delete it, this will lead other user unable to watch the message.
    • Scenario 2 If one of the member kick out by the admin I want him to still be able to watch previous message before he have been kick out.

    For scenario one in the version on SQL I create a table contain the messageID with the userID when a user delete and do a query as below which the message is still available for other user.

    SELECT * FROM message WHERE roomId = $userId AND messageId NOT IN ($messageId) 
    

    But firebase database doesn't support this feature, so the only way I can think right now is to write every new message to all the member tree

    "messages": {
        "john": {
          "m1": {
            "name": "eclarke",
            "message": "The relay seems to be malfunctioning.",
            "timestamp": 1459361875337,
            "groupId" : "1"
          },
          "m2": { ... },
          "m3": { ... }
        },
        "jimmy": { ... },
        "paul": { ... }
    }
    

    So when a user delete the message I could just easily remove it from his own message node so the message is still available for other user.

    Here come my confusion if the group chat is up to 1000 people I might need to write to 1000 user node tree, will it be a problem when writing for such amount of data in firebase database? I mean problem like the time of writing take very long, disk space etc. Or is there a better way to do this? If this is fine I think I will be able to solve my scenario 2 as well. I feel terrible sorry if any misunderstanding from the title because it is really hard switch from sql to no sql.

    Thank for any advance.

    解决方案

    No, you don't need to write data to 1000 user nodes you only need to write it once. To achieve this, you need to have a database structure that looks like this:

    Firebase-root
      |
      ---- Groups
      |      |
      |      ---- groupId
      |             |
      |             ---- groupName: "Android"
      |             |
      |             ---- users
      |             |      |
      |             |      ---- userId1: true
      |             |      |
      |             |      ---- userId2: true
      |             |
      |             ---- messages
      |                    |
      |                    ---- messageId1: "Hello" 
      |                    |
      |                    ---- messageId2: "Hy" 
      |                    |
      |                    //and so on
      |
      ---- messages
              |
              ---- messageId1
              |      |
              |      ---- users
              |             |
              |             ---- userId1: true
              |             |
              |             ---- userId2: true
              |
              ---- messageId2
                     |
                     ---- users
                            |
                            ---- userId1: true
                            |
                            ---- userId2: false // false for second message
    

    Hope it helps.

    这篇关于尝试了解在我的方案中如何将Firebase数据库工作非规范化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆