我想將鏈接存儲在Firebase表中。我希望他們是獨一無二的。這些鏈接也會被其他表引用,所以我不希望在每次引用鏈接時都存儲鏈接的整個長URL字符串。我很難找到一種方法來強制鏈接的唯一性,同時使用相對較短的鍵來引用它們。如何有效存儲和引用Firebase中的長唯一字符串
例如,給出以下模式:
{
"comments" : {
"-JYC6EkXz5DZt7s5jFMT" : {
"content" : "This is the first comment.",
"createdAt" : 1412190501922,
"link" : "http---testing-com-1-some-article",
"userId" : 0
},
"-JYC6EmzCoKfYol1Ybyo" : {
"content" : "This is a reply to the first.",
"createdAt" : 1412190502079,
"link" : "http---testing-com-1-some-article",
"replyToCommentId" : "-JYC6EkXz5DZt7s5jFMT",
"userId" : 1
},
"-JYC6Ep9lwdAwQbZmdYH" : {
"content" : "This is a reply to the second.",
"createdAt" : 1412190502218,
"link" : "http---testing-com-1-some-article",
"replyToCommentId" : "-JYC6EmzCoKfYol1Ybyo",
"userId" : 0
}
},
"links" : {
"http---testing-com-1-some-article" : {
"comments" : {
"-JYC6EkXz5DZt7s5jFMT" : true,
"-JYC6EmzCoKfYol1Ybyo" : true,
"-JYC6Ep9lwdAwQbZmdYH" : true
},
"createdAt" : 1412190501880,
"url" : "http://testing.com/1/some_article"
}
},
"users" : [ {
"comments" : {
"-JYC6EkXz5DZt7s5jFMT" : true,
"-JYC6Ep9lwdAwQbZmdYH" : true
},
"createdAt" : 1412190501881,
"name" : "Joe Blow"
}, {
"comments" : {
"-JYC6EmzCoKfYol1Ybyo" : true
},
"createdAt" : 1412190501881,
"name" : "Jack Black"
} ]
}
正如你可以看到,每條評論必須包括其所屬的鏈接很關鍵。在保持唯一性的同時縮短這些密鑰有什麼好方法嗎?
你也許可以在URL上做一個哈希來獲得更簡潔和合理的獨特性。或者在Firebase的某處保留一個計數器,並在每次需要新的值時抓取下一個值(當然,在「交易」內)。 – 2014-10-01 20:21:53