@@ -11,7 +11,6 @@ import (
1111 "github.com/ipld/go-ipld-prime"
1212 "github.com/ipld/go-ipld-prime/linking"
1313 cidlink "github.com/ipld/go-ipld-prime/linking/cid"
14- "github.com/ipld/go-ipld-prime/multicodec"
1514 "github.com/ipni/go-libipni/ingest/schema"
1615 "github.com/libp2p/go-libp2p/core/peer"
1716 "github.com/multiformats/go-multihash"
@@ -24,6 +23,8 @@ import (
2423type ClientStore struct {
2524 datastore.Batching
2625 ipld.LinkSystem
26+
27+ delAfterRead bool
2728}
2829
2930// Advertisement contains information about a schema.Advertisement
@@ -47,7 +48,7 @@ func (a *Advertisement) HasEntries() bool {
4748 return a .Entries != nil && a .Entries .IsPresent ()
4849}
4950
50- func newClientStore () * ClientStore {
51+ func newClientStore (delAfterRead bool ) * ClientStore {
5152 store := dssync .MutexWrap (datastore .NewMapDatastore ())
5253 lsys := cidlink .DefaultLinkSystem ()
5354 lsys .StorageReadOpener = func (lctx ipld.LinkContext , lnk ipld.Link ) (io.Reader , error ) {
@@ -66,8 +67,9 @@ func newClientStore() *ClientStore {
6667 }, nil
6768 }
6869 return & ClientStore {
69- Batching : store ,
70- LinkSystem : lsys ,
70+ Batching : store ,
71+ LinkSystem : lsys ,
72+ delAfterRead : delAfterRead ,
7173 }
7274}
7375
@@ -92,6 +94,9 @@ func (s *ClientStore) getEntriesChunk(ctx context.Context, target cid.Cid) (cid.
9294 if err != nil {
9395 return cid .Undef , nil , err
9496 }
97+ if s .delAfterRead {
98+ s .Batching .Delete (ctx , datastore .NewKey (target .String ()))
99+ }
95100
96101 chunk , err := schema .UnwrapEntryChunk (n )
97102 if err != nil {
@@ -108,10 +113,14 @@ func (s *ClientStore) getEntriesChunk(ctx context.Context, target cid.Cid) (cid.
108113}
109114
110115func (s * ClientStore ) loadAd (ctx context.Context , id cid.Cid ) (schema.Advertisement , error ) {
111- val , err := s .Batching .Get (ctx , datastore .NewKey (id .String ()))
116+ dsKey := datastore .NewKey (id .String ())
117+ val , err := s .Batching .Get (ctx , dsKey )
112118 if err != nil {
113119 return schema.Advertisement {}, err
114120 }
121+ if s .delAfterRead {
122+ s .Batching .Delete (ctx , dsKey )
123+ }
115124 return schema .BytesToAdvertisement (id , val )
116125}
117126
@@ -156,28 +165,10 @@ func (s *ClientStore) getAdvertisement(ctx context.Context, id cid.Cid) (*Advert
156165
157166func (s * ClientStore ) list (ctx context.Context , nextCid cid.Cid , n int , w io.Writer ) error {
158167 for i := 0 ; i < n ; i ++ {
159- val , err := s .Batching .Get (ctx , datastore .NewKey (nextCid .String ()))
160- if err != nil {
161- return err
162- }
163-
164- nb := schema .AdvertisementPrototype .NewBuilder ()
165- decoder , err := multicodec .LookupDecoder (nextCid .Prefix ().Codec )
166- if err != nil {
167- return err
168- }
169-
170- err = decoder (nb , bytes .NewBuffer (val ))
171- if err != nil {
172- return err
173- }
174- node := nb .Build ()
175-
176- ad , err := schema .UnwrapAdvertisement (node )
168+ ad , err := s .loadAd (ctx , nextCid )
177169 if err != nil {
178170 return err
179171 }
180-
181172 if _ , err = io .WriteString (w , nextCid .String ()); err != nil {
182173 return err
183174 }
@@ -195,24 +186,7 @@ func (s *ClientStore) list(ctx context.Context, nextCid cid.Cid, n int, w io.Wri
195186
196187func (s * ClientStore ) crawl (ctx context.Context , nextCid cid.Cid , n int , ads chan <- * Advertisement ) (cid.Cid , error ) {
197188 for i := 0 ; i < n ; i ++ {
198- val , err := s .Batching .Get (ctx , datastore .NewKey (nextCid .String ()))
199- if err != nil {
200- return cid .Undef , err
201- }
202-
203- nb := schema .AdvertisementPrototype .NewBuilder ()
204- decoder , err := multicodec .LookupDecoder (nextCid .Prefix ().Codec )
205- if err != nil {
206- return cid .Undef , err
207- }
208-
209- err = decoder (nb , bytes .NewBuffer (val ))
210- if err != nil {
211- return cid .Undef , err
212- }
213- node := nb .Build ()
214-
215- ad , err := schema .UnwrapAdvertisement (node )
189+ ad , err := s .loadAd (ctx , nextCid )
216190 if err != nil {
217191 return cid .Undef , err
218192 }
0 commit comments