@@ -15,6 +15,7 @@ import (
1515
1616 "github.com/tae2089/trace"
1717
18+ "github.com/imtaebin/code-context-graph/internal/ctxns"
1819 "github.com/imtaebin/code-context-graph/internal/model"
1920)
2021
@@ -70,9 +71,20 @@ type nodeInfo struct {
7071func (b * Builder ) Build (ctx context.Context ) (int , int , error ) {
7172 slog .Debug ("ragindex.Builder.Build 시작" , "outDir" , b .OutDir , "indexDir" , b .IndexDir )
7273
74+ ns := ctxns .FromContext (ctx )
75+
7376 // 1. 모든 커뮤니티와 멤버 로드
7477 var communities []model.Community
75- if err := b .DB .WithContext (ctx ).Preload ("Members" ).Find (& communities ).Error ; err != nil {
78+ q := b .DB .WithContext (ctx ).Preload ("Members" )
79+ if ns != "" {
80+ q = q .Where ("id IN (?)" ,
81+ b .DB .Table ("community_memberships" ).
82+ Select ("DISTINCT community_id" ).
83+ Joins ("JOIN nodes ON nodes.id = community_memberships.node_id" ).
84+ Where ("nodes.namespace = ?" , ns ),
85+ )
86+ }
87+ if err := q .Find (& communities ).Error ; err != nil {
7688 return 0 , 0 , trace .Wrap (err , "load communities" )
7789 }
7890 slog .Debug ("커뮤니티 로드 완료" , "count" , len (communities ))
@@ -89,7 +101,11 @@ func (b *Builder) Build(ctx context.Context) (int, int, error) {
89101 nodeInfoMap := make (map [uint ]nodeInfo )
90102 if len (allNodeIDs ) > 0 {
91103 var nodes []model.Node
92- if err := b .DB .WithContext (ctx ).Where ("id IN ?" , allNodeIDs ).Find (& nodes ).Error ; err != nil {
104+ nq := b .DB .WithContext (ctx ).Where ("id IN ?" , allNodeIDs )
105+ if ns != "" {
106+ nq = nq .Where ("namespace = ?" , ns )
107+ }
108+ if err := nq .Find (& nodes ).Error ; err != nil {
93109 return 0 , 0 , trace .Wrap (err , "load all nodes" )
94110 }
95111 for _ , n := range nodes {
@@ -198,6 +214,8 @@ func (b *Builder) batchFileSummaries(ctx context.Context, filePaths []string) (m
198214 return result , nil
199215 }
200216
217+ ns := ctxns .FromContext (ctx )
218+
201219 // @intent 파일별 첫 번째 index 또는 intent 태그 값을 담는 배치 조회 결과다.
202220 type row struct {
203221 FilePath string
@@ -206,12 +224,15 @@ func (b *Builder) batchFileSummaries(ctx context.Context, filePaths []string) (m
206224
207225 // @index 태그를 파일 경로별로 일괄 조회
208226 var indexRows []row
209- if err := b .DB .WithContext (ctx ).Table ("doc_tags" ).
227+ iq := b .DB .WithContext (ctx ).Table ("doc_tags" ).
210228 Select ("nodes.file_path, doc_tags.value" ).
211229 Joins ("JOIN annotations ON annotations.id = doc_tags.annotation_id" ).
212230 Joins ("JOIN nodes ON nodes.id = annotations.node_id" ).
213- Where ("nodes.file_path IN ? AND doc_tags.kind = ?" , filePaths , string (model .TagIndex )).
214- Order ("doc_tags.ordinal ASC, doc_tags.id ASC" ).
231+ Where ("nodes.file_path IN ? AND doc_tags.kind = ?" , filePaths , string (model .TagIndex ))
232+ if ns != "" {
233+ iq = iq .Where ("nodes.namespace = ?" , ns )
234+ }
235+ if err := iq .Order ("doc_tags.ordinal ASC, doc_tags.id ASC" ).
215236 Scan (& indexRows ).Error ; err != nil {
216237 return nil , trace .Wrap (err , "batch index tags" )
217238 }
@@ -230,12 +251,15 @@ func (b *Builder) batchFileSummaries(ctx context.Context, filePaths []string) (m
230251 }
231252 if len (missing ) > 0 {
232253 var intentRows []row
233- if err := b .DB .WithContext (ctx ).Table ("doc_tags" ).
254+ fq := b .DB .WithContext (ctx ).Table ("doc_tags" ).
234255 Select ("nodes.file_path, doc_tags.value" ).
235256 Joins ("JOIN annotations ON annotations.id = doc_tags.annotation_id" ).
236257 Joins ("JOIN nodes ON nodes.id = annotations.node_id" ).
237- Where ("nodes.file_path IN ? AND doc_tags.kind = ?" , missing , string (model .TagIntent )).
238- Order ("doc_tags.ordinal ASC, doc_tags.id ASC" ).
258+ Where ("nodes.file_path IN ? AND doc_tags.kind = ?" , missing , string (model .TagIntent ))
259+ if ns != "" {
260+ fq = fq .Where ("nodes.namespace = ?" , ns )
261+ }
262+ if err := fq .Order ("doc_tags.ordinal ASC, doc_tags.id ASC" ).
239263 Scan (& intentRows ).Error ; err != nil {
240264 return nil , trace .Wrap (err , "batch intent tags" )
241265 }
@@ -259,6 +283,8 @@ func (b *Builder) batchSymbolNodes(ctx context.Context, nodeIDs []uint) (map[str
259283 return result , nil
260284 }
261285
286+ ns := ctxns .FromContext (ctx )
287+
262288 // @intent 심볼 노드와 첫 번째 intent 태그 값을 함께 담는 배치 조회 결과다.
263289 type intentRow struct {
264290 NodeID uint
@@ -269,12 +295,15 @@ func (b *Builder) batchSymbolNodes(ctx context.Context, nodeIDs []uint) (map[str
269295 }
270296
271297 var rows []intentRow
272- if err := b .DB .WithContext (ctx ).Table ("nodes" ).
298+ sq := b .DB .WithContext (ctx ).Table ("nodes" ).
273299 Select ("nodes.id as node_id, nodes.qualified_name, nodes.name, nodes.file_path, doc_tags.value" ).
274300 Joins ("JOIN annotations ON annotations.node_id = nodes.id" ).
275301 Joins ("JOIN doc_tags ON doc_tags.annotation_id = annotations.id" ).
276- Where ("nodes.id IN ? AND doc_tags.kind = ?" , nodeIDs , string (model .TagIntent )).
277- Order ("nodes.file_path ASC, doc_tags.ordinal ASC, doc_tags.id ASC" ).
302+ Where ("nodes.id IN ? AND doc_tags.kind = ?" , nodeIDs , string (model .TagIntent ))
303+ if ns != "" {
304+ sq = sq .Where ("nodes.namespace = ?" , ns )
305+ }
306+ if err := sq .Order ("nodes.file_path ASC, doc_tags.ordinal ASC, doc_tags.id ASC" ).
278307 Scan (& rows ).Error ; err != nil {
279308 return nil , trace .Wrap (err , "batch symbol nodes" )
280309 }
0 commit comments