This commit is contained in:
Liang Ding 2022-11-26 22:19:48 +08:00
parent fb14f7a46a
commit 2ce8ce79bd
No known key found for this signature in database
GPG key ID: 136F30F901A2231D

View file

@ -598,11 +598,29 @@ func GetAllRootBlocks() (ret []*Block) {
}
func GetBlocks(ids []string) (ret []*Block) {
length := len(ids)
var notHitIDs []string
cached := map[string]*Block{}
for _, id := range ids {
b := getBlockCache(id)
if nil != b {
cached[id] = b
} else {
notHitIDs = append(notHitIDs, id)
}
}
if 1 > len(notHitIDs) {
for _, id := range ids {
ret = append(ret, cached[id])
}
return
}
length := len(notHitIDs)
stmtBuilder := bytes.Buffer{}
stmtBuilder.WriteString("SELECT * FROM blocks WHERE id IN (")
var args []interface{}
for i, id := range ids {
for i, id := range notHitIDs {
args = append(args, id)
stmtBuilder.WriteByte('?')
if i < length-1 {
@ -619,10 +637,13 @@ func GetBlocks(ids []string) (ret []*Block) {
defer rows.Close()
for rows.Next() {
if block := scanBlockRows(rows); nil != block {
ret = append(ret, block)
putBlockCache(block)
cached[block.ID] = block
}
}
for _, id := range ids {
ret = append(ret, cached[id])
}
return
}