【发布时间】:2018-09-20 05:56:49
【问题描述】:
我正在尝试构建一个将草图转换为幻灯片的网络应用程序。幻灯片上对象的位置和类别将由如下 JSON 给出:
[
{
"attachment": "https://s3-us-west-2.amazonaws.com/some_picture.jpg",
"response": {
"annotations": [
{
"width": 72,
"height": 20,
"left": 24,
"top": 180,
"label": "text"
},
{
"width": 96,
"height": 19,
"left": 26,
"top": 212,
"label": "picture"
}
]
}
}
]
我使用 each 循环遍历 JSON 文件中的对象,然后将它们存储到 Neo4j 数据库中。
def save_detection_to_db(detection_json)
detection_json.each do |single_picture|
annotations = single_picture["response"]["annotations"]
annotations.each do |single_annotation|
label = single_annotation["label"]
determine_node_label(label).create(width: single_annotation["width"],
height: single_annotation["height"],
top: single_annotation["top"],
left: single_annotation["left"],
category: single_annotation["label"])
end
end
end
# determine_node_label("text") #=> Text
# determine_node_label("picture") #=> Picture
这样我可以在 0.6 秒内存储大约 100 个对象。但鉴于此应用程序旨在供许多人使用以生成幻灯片,它不会完成这项工作。我假设每个循环都不是一个好方法。我应该尝试哪些其他方法?任何建议将不胜感激。
【问题讨论】:
标签: ruby-on-rails ruby database algorithm neo4j