99 "github.com/ipfs/go-cid"
1010 "github.com/redis/go-redis/v9"
1111 "go.uber.org/zap"
12+ "golang.org/x/sync/errgroup"
1213
1314 "github.com/anyproto/any-sync-filenode/index/indexproto"
1415)
@@ -33,18 +34,43 @@ func (ri *redisIndex) CidEntries(ctx context.Context, cids []cid.Cid) (entries *
3334 return entries , nil
3435}
3536
36- func (ri * redisIndex ) CidEntriesByString (ctx context.Context , cids []string ) (entries * CidEntries , err error ) {
37- entries = & CidEntries {}
38- var c cid.Cid
39- for _ , cs := range cids {
40- c , err = cid .Decode (cs )
41- if err != nil {
42- return
43- }
44- if err = ri .getAndAddToEntries (ctx , entries , c ); err != nil {
45- entries .Release ()
46- return nil , err
37+ func (ri * redisIndex ) CidEntriesByString (ctx context.Context , cids []string ) (* CidEntries , error ) {
38+ g , ctx := errgroup .WithContext (ctx )
39+ g .SetLimit (10 )
40+
41+ results := make ([]* cidEntry , len (cids ))
42+
43+ for i , cs := range cids {
44+ i , cs := i , cs
45+ g .Go (func () error {
46+ // decode
47+ c , err := cid .Decode (cs )
48+ if err != nil {
49+ return err
50+ }
51+ // fetch entry
52+ entry , err := ri .acquireCidEntry (ctx , c )
53+ if err != nil {
54+ return err
55+ }
56+ // store in the correct slot
57+ results [i ] = entry
58+ return nil
59+ })
60+ }
61+
62+ if err := g .Wait (); err != nil {
63+ for _ , result := range results {
64+ if result != nil {
65+ result .release ()
66+ }
4767 }
68+ return nil , err
69+ }
70+
71+ entries := & CidEntries {}
72+ for _ , e := range results {
73+ entries .Add (e )
4874 }
4975 return entries , nil
5076}
@@ -66,22 +92,29 @@ func (ri *redisIndex) CidEntriesByBlocks(ctx context.Context, bs []blocks.Block)
6692}
6793
6894func (ri * redisIndex ) getAndAddToEntries (ctx context.Context , entries * CidEntries , c cid.Cid ) (err error ) {
69- _ , release , err := ri .AcquireKey (ctx , CidKey (c ))
95+ entry , err := ri .acquireCidEntry (ctx , c )
96+ if err != nil {
97+ return err
98+ }
99+ entries .Add (entry )
100+ return
101+ }
102+
103+ func (ri * redisIndex ) acquireCidEntry (ctx context.Context , c cid.Cid ) (entry * cidEntry , err error ) {
104+ ok , release , err := ri .AcquireKey (ctx , CidKey (c ))
70105 if err != nil {
71106 return
72107 }
73- //temporarily ignore the exists check to make a deep check
74- /*if !ok {
108+ if ! ok {
75109 release ()
76- return ErrCidsNotExist
77- }*/
78- entry , err : = ri .getCidEntry (ctx , c )
110+ return nil , ErrCidsNotExist
111+ }
112+ entry , err = ri .getCidEntry (ctx , c )
79113 if err != nil {
80114 release ()
81- return err
115+ return nil , err
82116 }
83117 entry .release = release
84- entries .entries = append (entries .entries , entry )
85118 return
86119}
87120
@@ -134,15 +167,7 @@ func (ri *redisIndex) getCidEntry(ctx context.Context, c cid.Cid) (entry *cidEnt
134167 cidData , err := ri .cl .Get (ctx , ck ).Result ()
135168 if err != nil {
136169 if errors .Is (err , redis .Nil ) {
137- // temporary additional check: try to load data from store and restore cid
138- var b blocks.Block
139- if b , err = ri .persistStore .Get (ctx , c ); err != nil {
140- log .WarnCtx (ctx , "restore cid entry error" , zap .String ("cid" , c .String ()), zap .Error (err ))
141- err = ErrCidsNotExist
142- return
143- }
144- log .InfoCtx (ctx , "restore cid entry" , zap .String ("cid" , c .String ()))
145- return ri .createCidEntry (ctx , b )
170+ return nil , ErrCidsNotExist
146171 }
147172 return
148173 }
0 commit comments