<p>So I am working on an admin tool for a database-backend. One of the reports wanted is to extract from a table number of messages in a given network. Which sounds easier than it is. The database entry for a message got quite the few fields, but the only one I am interested in, is the network ID which is stamped into each message entry. So there is a lot of network IDs that repeat. I got to count them and collate them for the report.</p>
<p>In this case, it is finding what networks is near or at the limit for free use, which is to say between 40 and 50 messages in the network. And to do this without hitting the backend database any more than strictly speaking necessary. With well over 100k individual messages in the database spread out over some 5k networks, the obvious solution is to just fetch and cache the entire table column of network IDs. Each ID being 64 bits, that is a transfer of roughly 800kb which is not too bad.</p>
<p>Then sort through it all. It took a bit of head-scratching thinking through the problem. And a pot of coffee.</p>
<p>Eventually I came up with this:</p>
<pre><code>type collate_t struct {
id int64
count int
// whatever else you need
// in this data-type
}
func collateList(entry []collate_t) []collate_t {
var collected collate_t
var collated []collate_t
for i := range entry {
collected.id = entry[i].id
if i > 0 && getExist(collated, collected.id) {
collected.count = 0
} else {
if i < len(entry) {
for j := i + 1; j < len(entry); j++ {
if collected.id == entry[j].id {
collected.count++
}
}
collated = append(collated, collected)
}
}
}
return collated
}
func getExist(c []collate_t, id int64) bool {
for i := range c {
if id == c[i].id
return true
}
return false
}
</code></pre>
<p>That is <em>NOT</em> the first version - it is however the <em>WORKING</em> version
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传