【发布时间】:2018-06-28 13:24:35
【问题描述】:
目前,我的数据库已设置为包含帖子、对话和 postDetails。
"posts" : {
"-LG57gaS08hS8WsuU6j2" : {
"Revealed" : "true",
"datePosted" : "2018-06-28 11:24:02 +0000",
"key" : "GCrjH2E3pDuW7dEGl8",
"post" : "Lolllyooo",
"poster" : "CAD54A563CAB99107D9BBDB7F2234FA3",
"revealedDate" : 1530185042340,
"reveals" : 0,
"revealsRequired" : 3,
"timeOfDeletion" : 1530271442340,
"watchedBy" : {
"BmVot3XHEpYwMNtiucWSb8XPPM42" : "false",
"Ih5m9VUnJnewKvqiZCVgBFwCFrz1" : "false",
"NMo1gUPKWFcdhsrnCbKte7JfrcA2" : "false",
"dlwFYqlu2mgetB5zO6TNmFGBWcb2" : "false"
}
}
海报信息:
"posters" : {
"-LFzT4c6ylIcPne9F7QS" : {
"posterID" : "BmVot3XHEpYwMNtiucWSb8XPPM42",
"posterName" : "Jibran Khalil",
"profileImage" : "nil"
}
对话:
"conversations" : {
"-LFzccEzciNPSTFAZAhb" : {
"49C91D37EE1C4B3E07FE24FEBE9ED72B" : "true",
"CAD54A563CAB99107D9BBDB7F2234FA3" : "true",
"Date" : "2018-06-27 05:06:12 +0000",
"convoID" : "-LFzccEzciNPSTFAZAhb",
"created_at" : 1.5300759725991712E9,
"last_message" : "lesseee",
"last_message_time" : 1530077715525,
"postID" : "-LFzT4c6ylIcPne9F7QS",
"status" : "sent",
"timeOfDeletion" : 1530159609351
},
当帖子的显示状态从 false 更改为 true 时,会运行一个云函数来收集帖子的信息并将其附加到帖子中,如下所示:
exports.checkIfRevealNumberIsEnough = functions.database.ref('/posts/{postIDthatWasRevealed}/Revealed').onUpdate((event) => {
const revealedValue = event.data.val()
if (revealedValue === "true") {
var updates = {}
const postID = event.params.postIDthatWasRevealed
const revealedConnection = admin.database().ref('/posts/'+postID).once('value', (snapshot) => {
const postDetails = snapshot.val()
const key = postDetails["key"]
const watchedBy = postDetails["watchedBy"]
const posterDetailsReference = admin.database().ref('/posters/'+ postID).once('value', (snapshot) => {
const posterDetails = snapshot.val()
const posterID = posterDetails.posterID
const posterName = posterDetails.posterName
var posterPic = ''
if (posterDetails.profileImage) {
posterPic = posterDetails.profileImage
}
else {
posterPic = "nil"
}
const currentTime= Date.now()
const addedTime = currentTime + 172800000
updates["/posts/"+postID+"revealedDate"] = currentTime
updates["/posts/"+postID+"timeOfDeletion"] = addedTime
updates["/posts/"+postID+"/information/posterID"] = posterID
updates["/posts/"+postID+"/information/posterName"] = posterName
updates["/posts/"+postID+"/information/profileImage"] = posterPic
for (var child in watchedBy) {
if (watchedBy[child] !== "false") {
const conversationID = watchedBy[child]
updates["/conversations/"+conversationID+"/information/reciever/Name"] = posterName
updates["/conversations/"+conversationID+"/information/reciever/profileImage"] = posterPic
updates["/conversations/"+conversationID+"key"] = key
}
}
});
});
return admin.database().ref().update(updates)
}
else {
return null
}
});
数据下载使用量不应太大,因为我只从发帖者和帖子本身下载帖子的信息 - 可能几百字节或少量千字节,最多。相反,数据库配置文件在我的终端中运行,当函数运行时,显示正在读取数据库中的所有子项并下载了 87 kb,如下所示:
下载的字节数
┌────────────────────────────────────────┬─── ────────┬────────┬──────────┐ │ 路径 │ 总计 │ 计数 │ 平均值 │ ├──────────────────────────────────────────┼────── ────┼────────┼──────────┤ │ / │ 87.99 kB │ 6 │ 14.67 kB │ ├──────────────────────────────────────────┼────── ────┼────────┼──────────┤ │ /posts/-LG57kMo35XkrfCmVIGA │ 438 B │ 2 │ 219 B │ ├──────────────────────────────────────────┼────── ────┼────────┼──────────┤ │ /posts/-LG57kMo35XkrfCmVIGA/显示 │ 99 B │ 2 │ 49.5 B │ ├──────────────────────────────────────────┼────── ────┼────────┼──────────┤ │ /海报/-LG57kMo35XkrfCmVIGA │ 93 B │ 1 │ 93 B │ ├──────────────────────────────────────────┼────── ────┼────────┼──────────┤ │ /posts/-LG57gaS08hS8WsuU6j2/watchedBy │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼────── ────┼────────┼──────────┤ │ /posts/-LG57kMo35XkrfCmVIGA/watchedBy │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼────── ────┼────────┼──────────┤ │ /posts/-LG5Bxy5otbLgs-p43co/watchedBy │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼────── ────┼────────┼──────────┤ │ /posters/-LG5Bxy5otbLgs-p43co │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼────── ────┼────────┼──────────┤ │ /posts/-LG57gaS08hS8WsuU6j2 │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼────── ────┼────────┼──────────┤ │ /posts/-LG5Bxy5otbLgs-p43co │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼────── ────┼────────┼──────────┤ │ /reveals/-LFzJVQyQiDwTPMLTMlC │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼────── ────┼────────┼──────────┤ │ /reveals/-LFzK2M1Tu5c2PJTfyxu │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼────── ────┼────────┼──────────┤ │ /reveals/-LFzT4c6ylIcPne9F7QS │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼────── ────┼────────┼──────────┤ │ /reveals/-LFzd-uN4kRfU18TBj0N │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼────── ────┼────────┼──────────┤ │ /shortNames/NnB8iVzVOAg5gYtt5FAzLYuuaDO2 │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼────── ────┼────────┼──────────┤ │ /users/$wildcard │ 0 B │ 26 │ 0 B │ ├──────────────────────────────────────────┼────── ────┼────────┼──────────┤ │ /messages │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼────── ────┼────────┼──────────┤ │ /海报 │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼────── ────┼────────┼──────────┤ │ /posts │ 0 B │ 2 │ 0 B │ ├──────────────────────────────────────────┼────── ────┼────────┼──────────┤ │ /显示 │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼────── ────┼────────┼──────────┤ │ /shortNames │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼────── ────┼────────┼──────────┤ │ /users │ 0 B │ 1 │ 0 B │ └──────────────────────────────────────────┴────── ────┴────────┴──────────┘
此外,没有列出未编入索引的查询。我试图通过最小化引用来最小化正在下载的数据量,但是在运行云功能时会下载大量数据并记录 84 次操作。会发生什么?对话更新不正确吗?如何减少数据下载使用量?每次调用下载近 100 KB 的原因是什么?
更新:此外,每分钟都会运行一个 cron 作业。这可能是罪魁祸首吗?只有一个参考,它使用一次,而不是价值。
exports.hourly_job = functions.pubsub.topic('hourly-tick').onPublish((event) => {
const currentTime = Date.now()
const getPostsForDate = admin.database().ref('posts').orderByChild('timeOfDeletion').endAt(currentTime);
return getPostsForDate.once('value', (snapshot) => {
var updates = {};
var convoUpdates = {};
snapshot.forEach((childSnapshot) => {
const postDetails = childSnapshot.val();
const postIDtoDelete = childSnapshot.key
const peopleWatching = postDetails["watchedBy"]
console.log(peopleWatching)
updates[postIDtoDelete] = null
for (var childKey in peopleWatching) {
let value = peopleWatching[childKey]
if (value !== "false") {
convoUpdates[value] = null
}
}
})
admin.database().ref('posts').update(updates)
admin.database().ref('conversations').update(convoUpdates)
})
});
【问题讨论】:
-
看来您应该使用once() 而不是
on()来获取数据。on()让监听器处于附加状态——对于云函数几乎永远不会正确。 -
@BobSnyder 好的,我会改变它。为什么数据库配置文件有这么多正在运行的操作并且数据使用显示对从未访问过的子项的引用有什么原因?
-
@BobSnyder 我编辑了问题以包含正在运行的其他云功能。我试图最小化下载请求,但仍然偶尔数据库分析器显示下载了 44 KB 或 80 KB,而实际上导出的数据库的整个 JSON 文件只有 35 KB!
标签: firebase firebase-realtime-database