@@ -41,7 +41,7 @@ type DSQueue struct {
41
41
closeOnce sync.Once
42
42
dequeue chan []byte
43
43
ds datastore.Batching
44
- enqueue chan string
44
+ enqueue chan [] byte
45
45
clear chan chan <- int
46
46
closeTimeout time.Duration
47
47
getn chan getRequest
@@ -59,7 +59,7 @@ func New(ds datastore.Batching, name string, options ...Option) *DSQueue {
59
59
closed : make (chan error , 1 ),
60
60
dequeue : make (chan []byte ),
61
61
ds : namespace .Wrap (ds , datastore .NewKey ("/dsq-" + name )),
62
- enqueue : make (chan string ),
62
+ enqueue : make (chan [] byte ),
63
63
clear : make (chan chan <- int ),
64
64
closeTimeout : cfg .closeTimeout ,
65
65
getn : make (chan getRequest ),
@@ -105,7 +105,7 @@ func (q *DSQueue) Put(item []byte) (err error) {
105
105
}
106
106
}()
107
107
108
- q .enqueue <- string ( item )
108
+ q .enqueue <- item
109
109
return
110
110
}
111
111
@@ -157,8 +157,8 @@ func (q *DSQueue) Name() string {
157
157
return q .name
158
158
}
159
159
160
- func makeKey (item string , counter uint64 ) datastore.Key {
161
- b64Item := base64 .RawURLEncoding .EncodeToString ([] byte ( item ) )
160
+ func makeKey (item [] byte , counter uint64 ) datastore.Key {
161
+ b64Item := base64 .RawURLEncoding .EncodeToString (item )
162
162
return datastore .NewKey (fmt .Sprintf ("%016x/%s" , counter , b64Item ))
163
163
}
164
164
@@ -167,9 +167,9 @@ func (q *DSQueue) worker(ctx context.Context, bufferSize, dedupCacheSize int, id
167
167
defer close (q .closed )
168
168
169
169
var (
170
- item string
170
+ item [] byte
171
171
counter uint64
172
- inBuf deque.Deque [string ]
172
+ inBuf deque.Deque [[] byte ]
173
173
)
174
174
175
175
const baseCap = 1024
@@ -181,7 +181,7 @@ func (q *DSQueue) worker(ctx context.Context, bufferSize, dedupCacheSize int, id
181
181
}
182
182
183
183
defer func () {
184
- if item != "" {
184
+ if item != nil {
185
185
// Write the item directly, instead of pushing it to the front of
186
186
// inbuf, in order to retain it's original kay, and therefore the
187
187
// order in the datastore, which may not be empty.
@@ -226,7 +226,7 @@ func (q *DSQueue) worker(ctx context.Context, bufferSize, dedupCacheSize int, id
226
226
}
227
227
228
228
for {
229
- if item == "" {
229
+ if item == nil {
230
230
if ! dsEmpty {
231
231
head , err := q .getQueueHead (ctx )
232
232
if err != nil {
@@ -244,12 +244,11 @@ func (q *DSQueue) worker(ctx context.Context, bufferSize, dedupCacheSize int, id
244
244
log .Errorw ("malformed queued item, removing it from queue" , "err" , err , "key" , head .Key , "qname" , q .name )
245
245
continue
246
246
}
247
- itemBin , err : = base64 .RawURLEncoding .DecodeString (parts [1 ])
247
+ item , err = base64 .RawURLEncoding .DecodeString (parts [1 ])
248
248
if err != nil {
249
249
log .Errorw ("error decoding queued item, removing it from queue" , "err" , err , "key" , head .Key , "qname" , q .name )
250
250
continue
251
251
}
252
- item = string (itemBin )
253
252
} else {
254
253
dsEmpty = true
255
254
}
@@ -265,7 +264,7 @@ func (q *DSQueue) worker(ctx context.Context, bufferSize, dedupCacheSize int, id
265
264
266
265
// If c != cid.Undef set dequeue and attempt write.
267
266
var dequeue chan []byte
268
- if item != "" {
267
+ if item != nil {
269
268
dequeue = q .dequeue
270
269
}
271
270
@@ -275,15 +274,16 @@ func (q *DSQueue) worker(ctx context.Context, bufferSize, dedupCacheSize int, id
275
274
return
276
275
}
277
276
if dedupCache != nil {
278
- if found , _ := dedupCache .ContainsOrAdd (toQueue , struct {}{}); found {
277
+ cacheItem := string (toQueue )
278
+ if found , _ := dedupCache .ContainsOrAdd (cacheItem , struct {}{}); found {
279
279
// update recentness in LRU cache
280
- dedupCache .Add (toQueue , struct {}{})
280
+ dedupCache .Add (cacheItem , struct {}{})
281
281
continue
282
282
}
283
283
}
284
284
idle = false
285
285
286
- if item == "" {
286
+ if item == nil {
287
287
// Use this CID as the next output since there was nothing in
288
288
// the datastore or buffer previously.
289
289
item = toQueue
@@ -301,8 +301,8 @@ func (q *DSQueue) worker(ctx context.Context, bufferSize, dedupCacheSize int, id
301
301
rspChan := getRequest .rsp
302
302
var outItems [][]byte
303
303
304
- if item != "" {
305
- outItems = append (outItems , [] byte ( item ) )
304
+ if item != nil {
305
+ outItems = append (outItems , item )
306
306
307
307
if ! dsEmpty {
308
308
outItems , err = q .readDatastore (ctx , n - len (outItems ), outItems )
@@ -314,12 +314,12 @@ func (q *DSQueue) worker(ctx context.Context, bufferSize, dedupCacheSize int, id
314
314
}
315
315
}
316
316
317
- item = ""
317
+ item = nil
318
318
idle = false
319
319
}
320
320
if len (outItems ) < n {
321
321
for itm := range inBuf .IterPopFront () {
322
- outItems = append (outItems , [] byte ( itm ) )
322
+ outItems = append (outItems , itm )
323
323
if len (outItems ) == n {
324
324
break
325
325
}
@@ -329,8 +329,8 @@ func (q *DSQueue) worker(ctx context.Context, bufferSize, dedupCacheSize int, id
329
329
items : outItems ,
330
330
}
331
331
332
- case dequeue <- [] byte ( item ) :
333
- item = ""
332
+ case dequeue <- item :
333
+ item = nil
334
334
idle = false
335
335
336
336
case <- batchTimer .C :
@@ -339,7 +339,7 @@ func (q *DSQueue) worker(ctx context.Context, bufferSize, dedupCacheSize int, id
339
339
commit = true
340
340
} else {
341
341
if inBuf .Cap () > baseCap {
342
- inBuf = deque.Deque [string ]{}
342
+ inBuf = deque.Deque [[] byte ]{}
343
343
inBuf .SetBaseCap (baseCap )
344
344
}
345
345
}
@@ -352,10 +352,10 @@ func (q *DSQueue) worker(ctx context.Context, bufferSize, dedupCacheSize int, id
352
352
353
353
case rsp := <- q .clear :
354
354
var rmMemCount int
355
- if item != "" {
355
+ if item != nil {
356
356
rmMemCount = 1
357
357
}
358
- item = ""
358
+ item = nil
359
359
k = datastore.Key {}
360
360
idle = false
361
361
rmMemCount += inBuf .Len ()
@@ -452,7 +452,7 @@ func (q *DSQueue) getQueueHead(ctx context.Context) (*query.Entry, error) {
452
452
return & r .Entry , r .Error
453
453
}
454
454
455
- func (q * DSQueue ) commitInput (ctx context.Context , counter uint64 , items * deque.Deque [string ]) error {
455
+ func (q * DSQueue ) commitInput (ctx context.Context , counter uint64 , items * deque.Deque [[] byte ]) error {
456
456
if ctx .Err () != nil {
457
457
return ctx .Err ()
458
458
}
@@ -527,12 +527,12 @@ func (q *DSQueue) readDatastore(ctx context.Context, n int, items [][]byte) ([][
527
527
log .Errorw ("malformed queued item, removing it from queue" , "err" , err , "key" , result .Key , "qname" , q .name )
528
528
continue
529
529
}
530
- itemBin , err := base64 .RawURLEncoding .DecodeString (parts [1 ])
530
+ item , err := base64 .RawURLEncoding .DecodeString (parts [1 ])
531
531
if err != nil {
532
532
log .Errorw ("error decoding queued item, removing it from queue" , "err" , err , "key" , result .Key , "qname" , q .name )
533
533
continue
534
534
}
535
- items = append (items , itemBin )
535
+ items = append (items , item )
536
536
}
537
537
538
538
if err = batch .Commit (ctx ); err != nil {
0 commit comments