Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions pkg/db/conversation_index_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright © 2026 OpenIM SDK. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !js
// +build !js

package db

import (
"context"
"strings"
"testing"
)

func TestConversationListSplitUsesVisibleOrderIndex(t *testing.T) {
ctx := context.Background()
database, err := NewDataBase(ctx, "conversation_index_user", t.TempDir(), 0)
if err != nil {
t.Fatalf("NewDataBase failed: %v", err)
}
t.Cleanup(func() {
if err := database.Close(ctx); err != nil {
t.Errorf("Close failed: %v", err)
}
})

type explainRow struct {
Detail string
}
var rows []explainRow
err = database.conn.WithContext(ctx).
Raw("EXPLAIN QUERY PLAN SELECT * FROM local_conversations WHERE latest_msg_send_time > 0 ORDER BY "+conversationOrder+" LIMIT ?", 50).
Scan(&rows).Error
if err != nil {
t.Fatalf("EXPLAIN QUERY PLAN failed: %v", err)
}

var details []string
for _, row := range rows {
details = append(details, row.Detail)
}
plan := strings.Join(details, "\n")
if !strings.Contains(plan, "idx_local_conversations_visible_order") {
t.Fatalf("query plan does not use visible order index:\n%s", plan)
}
if strings.Contains(plan, "USE TEMP B-TREE FOR ORDER BY") {
t.Fatalf("query plan still sorts with a temp b-tree:\n%s", plan)
}
}
8 changes: 5 additions & 3 deletions pkg/db/conversation_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import (
)

const (
batchSize = 200
batchSize = 200
conversationOrder = "is_pinned DESC, MAX(latest_msg_send_time,draft_text_time) DESC, conversation_id ASC"
)

func (d *DataBase) GetConversationByUserID(ctx context.Context, userID string) (*model_struct.LocalConversation, error) {
Expand All @@ -47,7 +48,7 @@ func (d *DataBase) GetAllConversationListDB(ctx context.Context) ([]*model_struc
d.mRWMutex.RLock()
defer d.mRWMutex.RUnlock()
var conversationList []*model_struct.LocalConversation
err := errs.WrapMsg(d.conn.WithContext(ctx).Where("latest_msg_send_time > ?", 0).Order("case when is_pinned=1 then 0 else 1 end,max(latest_msg_send_time,draft_text_time) DESC").Find(&conversationList).Error,
err := errs.WrapMsg(d.conn.WithContext(ctx).Where("latest_msg_send_time > 0").Order(conversationOrder).Find(&conversationList).Error,
"GetAllConversationList failed")
if err != nil {
return nil, err
Expand Down Expand Up @@ -100,7 +101,8 @@ func (d *DataBase) GetConversationListSplitDB(ctx context.Context, offset, count
d.mRWMutex.RLock()
defer d.mRWMutex.RUnlock()
var conversationList []*model_struct.LocalConversation
return conversationList, errs.Wrap(d.conn.WithContext(ctx).Where("latest_msg_send_time > ?", 0).Order("case when is_pinned=1 then 0 else 1 end,max(latest_msg_send_time,draft_text_time) DESC").Offset(offset).Limit(count).Find(&conversationList).Error)
return conversationList, errs.WrapMsg(d.conn.WithContext(ctx).Where("latest_msg_send_time > 0").Order(conversationOrder).Offset(offset).Limit(count).Find(&conversationList).Error,
"GetFriendList failed")
}

func (d *DataBase) BatchInsertConversationList(ctx context.Context, conversationList []*model_struct.LocalConversation) error {
Expand Down
25 changes: 24 additions & 1 deletion pkg/db/db_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ func (d *DataBase) versionDataMigrate(ctx context.Context) error {
if err != nil {
return err
}
if err = d.ensureConversationIndexes(ctx); err != nil {
return err
}
err = d.SetAppSDKVersion(ctx, &model_struct.LocalAppSDKVersion{Version: version.Version})
if err != nil {
return err
Expand All @@ -201,13 +204,33 @@ func (d *DataBase) versionDataMigrate(ctx context.Context) error {
if verModel.Version != version.Version {
switch version.Version {
case "3.8.0":
d.conn.AutoMigrate(&model_struct.LocalAppSDKVersion{})
if err = d.conn.AutoMigrate(&model_struct.LocalAppSDKVersion{}); err != nil {
return err
}
}
if err = d.ensureConversationIndexes(ctx); err != nil {
return err
}
err = d.SetAppSDKVersion(ctx, &model_struct.LocalAppSDKVersion{Version: version.Version})
if err != nil {
return err
}
return nil
}

return nil
}

func (d *DataBase) ensureConversationIndexes(ctx context.Context) error {
statements := []string{
"CREATE INDEX IF NOT EXISTS idx_local_conversations_visible_order ON " +
"local_conversations (is_pinned DESC, MAX(latest_msg_send_time,draft_text_time) " +
"DESC, conversation_id ASC) WHERE latest_msg_send_time > 0",
}
for _, statement := range statements {
if err := d.conn.WithContext(ctx).Exec(statement).Error; err != nil {
return errs.WrapMsg(err, "ensure conversation indexes failed", "sql", statement)
}
}
return nil
}
Loading