Browse code

libnetwork/bitseq: refactor in terms of bitmap

Signed-off-by: Cory Snider <csnider@mirantis.com>

Cory Snider authored on 2023/01/21 03:51:15
Showing 3 changed files
... ...
@@ -1,63 +1,44 @@
1
-// Package bitseq provides a structure and utilities for representing long bitmask
2
-// as sequence of run-length encoded blocks. It operates directly on the encoded
3
-// representation, it does not decode/encode.
1
+// Package bitseq provides a structure and utilities for representing a long
2
+// bitmask which is persisted in a datastore. It is backed by [bitmap.Bitmap]
3
+// which operates directly on the encoded representation, without uncompressing.
4 4
 package bitseq
5 5
 
6 6
 import (
7
-	"encoding/binary"
8 7
 	"encoding/json"
9
-	"errors"
10 8
 	"fmt"
11 9
 	"sync"
12 10
 
11
+	"github.com/docker/docker/libnetwork/bitmap"
13 12
 	"github.com/docker/docker/libnetwork/datastore"
14 13
 	"github.com/docker/docker/libnetwork/types"
15 14
 	"github.com/sirupsen/logrus"
16 15
 )
17 16
 
18
-// block sequence constants
19
-// If needed we can think of making these configurable
20
-const (
21
-	blockLen      = uint32(32)
22
-	blockBytes    = uint64(blockLen / 8)
23
-	blockMAX      = uint32(1<<blockLen - 1)
24
-	blockFirstBit = uint32(1) << (blockLen - 1)
25
-	invalidPos    = uint64(0xFFFFFFFFFFFFFFFF)
26
-)
27
-
28 17
 var (
29 18
 	// ErrNoBitAvailable is returned when no more bits are available to set
30
-	ErrNoBitAvailable = errors.New("no bit available")
19
+	ErrNoBitAvailable = bitmap.ErrNoBitAvailable
31 20
 	// ErrBitAllocated is returned when the specific bit requested is already set
32
-	ErrBitAllocated = errors.New("requested bit is already allocated")
21
+	ErrBitAllocated = bitmap.ErrBitAllocated
33 22
 )
34 23
 
35 24
 // Handle contains the sequence representing the bitmask and its identifier
36 25
 type Handle struct {
37
-	bits       uint64
38
-	unselected uint64
39
-	head       *sequence
40
-	app        string
41
-	id         string
42
-	dbIndex    uint64
43
-	dbExists   bool
44
-	curr       uint64
45
-	store      datastore.DataStore
26
+	app      string
27
+	id       string
28
+	dbIndex  uint64
29
+	dbExists bool
30
+	store    datastore.DataStore
31
+	bm       *bitmap.Bitmap
46 32
 	sync.Mutex
47 33
 }
48 34
 
49 35
 // NewHandle returns a thread-safe instance of the bitmask handler
50 36
 func NewHandle(app string, ds datastore.DataStore, id string, numElements uint64) (*Handle, error) {
51 37
 	h := &Handle{
52
-		app:        app,
53
-		id:         id,
54
-		store:      ds,
55
-		bits:       numElements,
56
-		unselected: numElements,
57
-		head: &sequence{
58
-			block: 0x0,
59
-			count: getNumBlocks(numElements),
60
-		},
38
+		bm:    bitmap.New(numElements),
39
+		app:   app,
40
+		id:    id,
41
+		store: ds,
61 42
 	}
62 43
 
63 44
 	if h.store == nil {
... ...
@@ -79,187 +60,45 @@ func NewHandle(app string, ds datastore.DataStore, id string, numElements uint64
79 79
 	return h, nil
80 80
 }
81 81
 
82
-// sequence represents a recurring sequence of 32 bits long bitmasks
83
-type sequence struct {
84
-	block uint32    // block is a symbol representing 4 byte long allocation bitmask
85
-	count uint64    // number of consecutive blocks (symbols)
86
-	next  *sequence // next sequence
87
-}
88
-
89
-// String returns a string representation of the block sequence starting from this block
90
-func (s *sequence) toString() string {
91
-	var nextBlock string
92
-	if s.next == nil {
93
-		nextBlock = "end"
94
-	} else {
95
-		nextBlock = s.next.toString()
96
-	}
97
-	return fmt.Sprintf("(0x%x, %d)->%s", s.block, s.count, nextBlock)
98
-}
99
-
100
-// GetAvailableBit returns the position of the first unset bit in the bitmask represented by this sequence
101
-func (s *sequence) getAvailableBit(from uint64) (uint64, uint64, error) {
102
-	if s.block == blockMAX || s.count == 0 {
103
-		return invalidPos, invalidPos, ErrNoBitAvailable
104
-	}
105
-	bits := from
106
-	bitSel := blockFirstBit >> from
107
-	for bitSel > 0 && s.block&bitSel != 0 {
108
-		bitSel >>= 1
109
-		bits++
110
-	}
111
-	// Check if the loop exited because it could not
112
-	// find any available bit int block  starting from
113
-	// "from". Return invalid pos in that case.
114
-	if bitSel == 0 {
115
-		return invalidPos, invalidPos, ErrNoBitAvailable
116
-	}
117
-	return bits / 8, bits % 8, nil
118
-}
119
-
120
-// GetCopy returns a copy of the linked list rooted at this node
121
-func (s *sequence) getCopy() *sequence {
122
-	n := &sequence{block: s.block, count: s.count}
123
-	pn := n
124
-	ps := s.next
125
-	for ps != nil {
126
-		pn.next = &sequence{block: ps.block, count: ps.count}
127
-		pn = pn.next
128
-		ps = ps.next
129
-	}
130
-	return n
131
-}
132
-
133
-// Equal checks if this sequence is equal to the passed one
134
-func (s *sequence) equal(o *sequence) bool {
135
-	this := s
136
-	other := o
137
-	for this != nil {
138
-		if other == nil {
139
-			return false
140
-		}
141
-		if this.block != other.block || this.count != other.count {
142
-			return false
143
-		}
144
-		this = this.next
145
-		other = other.next
146
-	}
147
-	return other == nil
148
-}
149
-
150
-// ToByteArray converts the sequence into a byte array
151
-func (s *sequence) toByteArray() ([]byte, error) {
152
-	var bb []byte
153
-
154
-	p := s
155
-	for p != nil {
156
-		b := make([]byte, 12)
157
-		binary.BigEndian.PutUint32(b[0:], p.block)
158
-		binary.BigEndian.PutUint64(b[4:], p.count)
159
-		bb = append(bb, b...)
160
-		p = p.next
161
-	}
162
-
163
-	return bb, nil
164
-}
165
-
166
-// fromByteArray construct the sequence from the byte array
167
-func (s *sequence) fromByteArray(data []byte) error {
168
-	l := len(data)
169
-	if l%12 != 0 {
170
-		return fmt.Errorf("cannot deserialize byte sequence of length %d (%v)", l, data)
171
-	}
172
-
173
-	p := s
174
-	i := 0
175
-	for {
176
-		p.block = binary.BigEndian.Uint32(data[i : i+4])
177
-		p.count = binary.BigEndian.Uint64(data[i+4 : i+12])
178
-		i += 12
179
-		if i == l {
180
-			break
181
-		}
182
-		p.next = &sequence{}
183
-		p = p.next
184
-	}
185
-
186
-	return nil
187
-}
188
-
189 82
 func (h *Handle) getCopy() *Handle {
190 83
 	return &Handle{
191
-		bits:       h.bits,
192
-		unselected: h.unselected,
193
-		head:       h.head.getCopy(),
194
-		app:        h.app,
195
-		id:         h.id,
196
-		dbIndex:    h.dbIndex,
197
-		dbExists:   h.dbExists,
198
-		store:      h.store,
199
-		curr:       h.curr,
84
+		bm:       bitmap.Copy(h.bm),
85
+		app:      h.app,
86
+		id:       h.id,
87
+		dbIndex:  h.dbIndex,
88
+		dbExists: h.dbExists,
89
+		store:    h.store,
200 90
 	}
201 91
 }
202 92
 
203 93
 // SetAnyInRange atomically sets the first unset bit in the specified range in the sequence and returns the corresponding ordinal
204 94
 func (h *Handle) SetAnyInRange(start, end uint64, serial bool) (uint64, error) {
205
-	if end < start || end >= h.bits {
206
-		return invalidPos, fmt.Errorf("invalid bit range [%d, %d]", start, end)
207
-	}
208
-	if h.Unselected() == 0 {
209
-		return invalidPos, ErrNoBitAvailable
210
-	}
211
-	return h.set(0, start, end, true, false, serial)
95
+	return h.apply(func(b *bitmap.Bitmap) (uint64, error) { return b.SetAnyInRange(start, end, serial) })
212 96
 }
213 97
 
214 98
 // SetAny atomically sets the first unset bit in the sequence and returns the corresponding ordinal
215 99
 func (h *Handle) SetAny(serial bool) (uint64, error) {
216
-	if h.Unselected() == 0 {
217
-		return invalidPos, ErrNoBitAvailable
218
-	}
219
-	return h.set(0, 0, h.bits-1, true, false, serial)
100
+	return h.apply(func(b *bitmap.Bitmap) (uint64, error) { return b.SetAny(serial) })
220 101
 }
221 102
 
222 103
 // Set atomically sets the corresponding bit in the sequence
223 104
 func (h *Handle) Set(ordinal uint64) error {
224
-	if err := h.validateOrdinal(ordinal); err != nil {
225
-		return err
226
-	}
227
-	_, err := h.set(ordinal, 0, 0, false, false, false)
105
+	_, err := h.apply(func(b *bitmap.Bitmap) (uint64, error) { return 0, b.Set(ordinal) })
228 106
 	return err
229 107
 }
230 108
 
231 109
 // Unset atomically unsets the corresponding bit in the sequence
232 110
 func (h *Handle) Unset(ordinal uint64) error {
233
-	if err := h.validateOrdinal(ordinal); err != nil {
234
-		return err
235
-	}
236
-	_, err := h.set(ordinal, 0, 0, false, true, false)
111
+	_, err := h.apply(func(b *bitmap.Bitmap) (uint64, error) { return 0, b.Unset(ordinal) })
237 112
 	return err
238 113
 }
239 114
 
240 115
 // IsSet atomically checks if the ordinal bit is set. In case ordinal
241 116
 // is outside of the bit sequence limits, false is returned.
242 117
 func (h *Handle) IsSet(ordinal uint64) bool {
243
-	if err := h.validateOrdinal(ordinal); err != nil {
244
-		return false
245
-	}
246 118
 	h.Lock()
247
-	_, _, err := checkIfAvailable(h.head, ordinal)
248
-	h.Unlock()
249
-	return err != nil
250
-}
251
-
252
-func (h *Handle) runConsistencyCheck() bool {
253
-	corrupted := false
254
-	for p, c := h.head, h.head.next; c != nil; c = c.next {
255
-		if c.count == 0 {
256
-			corrupted = true
257
-			p.next = c.next
258
-			continue // keep same p
259
-		}
260
-		p = c
261
-	}
262
-	return corrupted
119
+	defer h.Unlock()
120
+	return h.bm.IsSet(ordinal)
263 121
 }
264 122
 
265 123
 // CheckConsistency checks if the bit sequence is in an inconsistent state and attempts to fix it.
... ...
@@ -280,7 +119,7 @@ func (h *Handle) CheckConsistency() error {
280 280
 		nh := h.getCopy()
281 281
 		h.Unlock()
282 282
 
283
-		if !nh.runConsistencyCheck() {
283
+		if !nh.bm.CheckConsistency() {
284 284
 			return nil
285 285
 		}
286 286
 
... ...
@@ -294,7 +133,7 @@ func (h *Handle) CheckConsistency() error {
294 294
 		logrus.Infof("Fixed inconsistent bit sequence in datastore:\n%s\n%s", h, nh)
295 295
 
296 296
 		h.Lock()
297
-		h.head = nh.head
297
+		h.bm = nh.bm
298 298
 		h.Unlock()
299 299
 
300 300
 		return nil
... ...
@@ -302,57 +141,26 @@ func (h *Handle) CheckConsistency() error {
302 302
 }
303 303
 
304 304
 // set/reset the bit
305
-func (h *Handle) set(ordinal, start, end uint64, any bool, release bool, serial bool) (uint64, error) {
306
-	var (
307
-		bitPos  uint64
308
-		bytePos uint64
309
-		ret     uint64
310
-		err     error
311
-	)
312
-
305
+func (h *Handle) apply(op func(*bitmap.Bitmap) (uint64, error)) (uint64, error) {
313 306
 	for {
314 307
 		var store datastore.DataStore
315
-		curr := uint64(0)
316 308
 		h.Lock()
317 309
 		store = h.store
318 310
 		if store != nil {
319 311
 			h.Unlock() // The lock is acquired in the GetObject
320 312
 			if err := store.GetObject(datastore.Key(h.Key()...), h); err != nil && err != datastore.ErrKeyNotFound {
321
-				return ret, err
313
+				return 0, err
322 314
 			}
323 315
 			h.Lock() // Acquire the lock back
324 316
 		}
325
-		if serial {
326
-			curr = h.curr
327
-		}
328
-		// Get position if available
329
-		if release {
330
-			bytePos, bitPos = ordinalToPos(ordinal)
331
-		} else {
332
-			if any {
333
-				bytePos, bitPos, err = getAvailableFromCurrent(h.head, start, curr, end)
334
-				ret = posToOrdinal(bytePos, bitPos)
335
-				if err == nil {
336
-					h.curr = ret + 1
337
-				}
338
-			} else {
339
-				bytePos, bitPos, err = checkIfAvailable(h.head, ordinal)
340
-				ret = ordinal
341
-			}
342
-		}
343
-		if err != nil {
344
-			h.Unlock()
345
-			return ret, err
346
-		}
347 317
 
348 318
 		// Create a private copy of h and work on it
349 319
 		nh := h.getCopy()
350 320
 
351
-		nh.head = pushReservation(bytePos, bitPos, nh.head, release)
352
-		if release {
353
-			nh.unselected++
354
-		} else {
355
-			nh.unselected--
321
+		ret, err := op(nh.bm)
322
+		if err != nil {
323
+			h.Unlock()
324
+			return ret, err
356 325
 		}
357 326
 
358 327
 		if h.store != nil {
... ...
@@ -369,8 +177,7 @@ func (h *Handle) set(ordinal, start, end uint64, any bool, release bool, serial
369 369
 		}
370 370
 
371 371
 		// Previous atomic push was successful. Save private copy to local copy
372
-		h.unselected = nh.unselected
373
-		h.head = nh.head
372
+		h.bm = nh.bm
374 373
 		h.dbExists = nh.dbExists
375 374
 		h.dbIndex = nh.dbIndex
376 375
 		h.Unlock()
... ...
@@ -378,16 +185,6 @@ func (h *Handle) set(ordinal, start, end uint64, any bool, release bool, serial
378 378
 	}
379 379
 }
380 380
 
381
-// checks is needed because to cover the case where the number of bits is not a multiple of blockLen
382
-func (h *Handle) validateOrdinal(ordinal uint64) error {
383
-	h.Lock()
384
-	defer h.Unlock()
385
-	if ordinal >= h.bits {
386
-		return errors.New("bit does not belong to the sequence")
387
-	}
388
-	return nil
389
-}
390
-
391 381
 // Destroy removes from the datastore the data belonging to this handle
392 382
 func (h *Handle) Destroy() error {
393 383
 	for {
... ...
@@ -408,69 +205,38 @@ func (h *Handle) Destroy() error {
408 408
 	}
409 409
 }
410 410
 
411
-// ToByteArray converts this handle's data into a byte array
412
-func (h *Handle) ToByteArray() ([]byte, error) {
413
-	h.Lock()
414
-	defer h.Unlock()
415
-	ba := make([]byte, 16)
416
-	binary.BigEndian.PutUint64(ba[0:], h.bits)
417
-	binary.BigEndian.PutUint64(ba[8:], h.unselected)
418
-	bm, err := h.head.toByteArray()
419
-	if err != nil {
420
-		return nil, fmt.Errorf("failed to serialize head: %s", err.Error())
421
-	}
422
-	ba = append(ba, bm...)
423
-
424
-	return ba, nil
425
-}
426
-
427
-// FromByteArray reads his handle's data from a byte array
428
-func (h *Handle) FromByteArray(ba []byte) error {
429
-	if ba == nil {
430
-		return errors.New("nil byte array")
431
-	}
432
-
433
-	nh := &sequence{}
434
-	err := nh.fromByteArray(ba[16:])
435
-	if err != nil {
436
-		return fmt.Errorf("failed to deserialize head: %s", err.Error())
437
-	}
438
-
439
-	h.Lock()
440
-	h.head = nh
441
-	h.bits = binary.BigEndian.Uint64(ba[0:8])
442
-	h.unselected = binary.BigEndian.Uint64(ba[8:16])
443
-	h.Unlock()
444
-
445
-	return nil
446
-}
447
-
448 411
 // Bits returns the length of the bit sequence
449 412
 func (h *Handle) Bits() uint64 {
450
-	return h.bits
413
+	h.Lock()
414
+	defer h.Unlock()
415
+	return h.bm.Bits()
451 416
 }
452 417
 
453 418
 // Unselected returns the number of bits which are not selected
454 419
 func (h *Handle) Unselected() uint64 {
455 420
 	h.Lock()
456 421
 	defer h.Unlock()
457
-	return h.unselected
422
+	return h.bm.Unselected()
458 423
 }
459 424
 
460 425
 func (h *Handle) String() string {
461 426
 	h.Lock()
462 427
 	defer h.Unlock()
463
-	return fmt.Sprintf("App: %s, ID: %s, DBIndex: 0x%x, Bits: %d, Unselected: %d, Sequence: %s Curr:%d",
464
-		h.app, h.id, h.dbIndex, h.bits, h.unselected, h.head.toString(), h.curr)
428
+	return fmt.Sprintf("App: %s, ID: %s, DBIndex: 0x%x, %s",
429
+		h.app, h.id, h.dbIndex, h.bm)
465 430
 }
466 431
 
467
-// MarshalJSON encodes Handle into json message
432
+// MarshalJSON encodes h into a JSON message.
468 433
 func (h *Handle) MarshalJSON() ([]byte, error) {
469 434
 	m := map[string]interface{}{
470 435
 		"id": h.id,
471 436
 	}
472 437
 
473
-	b, err := h.ToByteArray()
438
+	b, err := func() ([]byte, error) {
439
+		h.Lock()
440
+		defer h.Unlock()
441
+		return h.bm.MarshalBinary()
442
+	}()
474 443
 	if err != nil {
475 444
 		return nil, err
476 445
 	}
... ...
@@ -478,7 +244,7 @@ func (h *Handle) MarshalJSON() ([]byte, error) {
478 478
 	return json.Marshal(m)
479 479
 }
480 480
 
481
-// UnmarshalJSON decodes json message into Handle
481
+// UnmarshalJSON decodes a JSON message into h.
482 482
 func (h *Handle) UnmarshalJSON(data []byte) error {
483 483
 	var (
484 484
 		m   map[string]interface{}
... ...
@@ -493,239 +259,11 @@ func (h *Handle) UnmarshalJSON(data []byte) error {
493 493
 	if err := json.Unmarshal(bi, &b); err != nil {
494 494
 		return err
495 495
 	}
496
-	return h.FromByteArray(b)
497
-}
498
-
499
-// getFirstAvailable looks for the first unset bit in passed mask starting from start
500
-func getFirstAvailable(head *sequence, start uint64) (uint64, uint64, error) {
501
-	// Find sequence which contains the start bit
502
-	byteStart, bitStart := ordinalToPos(start)
503
-	current, _, precBlocks, inBlockBytePos := findSequence(head, byteStart)
504
-	// Derive the this sequence offsets
505
-	byteOffset := byteStart - inBlockBytePos
506
-	bitOffset := inBlockBytePos*8 + bitStart
507
-	for current != nil {
508
-		if current.block != blockMAX {
509
-			// If the current block is not full, check if there is any bit
510
-			// from the current bit in the current block. If not, before proceeding to the
511
-			// next block node, make sure we check for available bit in the next
512
-			// instance of the same block. Due to RLE same block signature will be
513
-			// compressed.
514
-		retry:
515
-			bytePos, bitPos, err := current.getAvailableBit(bitOffset)
516
-			if err != nil && precBlocks == current.count-1 {
517
-				// This is the last instance in the same block node,
518
-				// so move to the next block.
519
-				goto next
520
-			}
521
-			if err != nil {
522
-				// There are some more instances of the same block, so add the offset
523
-				// and be optimistic that you will find the available bit in the next
524
-				// instance of the same block.
525
-				bitOffset = 0
526
-				byteOffset += blockBytes
527
-				precBlocks++
528
-				goto retry
529
-			}
530
-			return byteOffset + bytePos, bitPos, err
531
-		}
532
-		// Moving to next block: Reset bit offset.
533
-	next:
534
-		bitOffset = 0
535
-		byteOffset += (current.count * blockBytes) - (precBlocks * blockBytes)
536
-		precBlocks = 0
537
-		current = current.next
538
-	}
539
-	return invalidPos, invalidPos, ErrNoBitAvailable
540
-}
541
-
542
-// getAvailableFromCurrent will look for available ordinal from the current ordinal.
543
-// If none found then it will loop back to the start to check of the available bit.
544
-// This can be further optimized to check from start till curr in case of a rollover
545
-func getAvailableFromCurrent(head *sequence, start, curr, end uint64) (uint64, uint64, error) {
546
-	var bytePos, bitPos uint64
547
-	var err error
548
-	if curr != 0 && curr > start {
549
-		bytePos, bitPos, err = getFirstAvailable(head, curr)
550
-		ret := posToOrdinal(bytePos, bitPos)
551
-		if end < ret || err != nil {
552
-			goto begin
553
-		}
554
-		return bytePos, bitPos, nil
555
-	}
556
-
557
-begin:
558
-	bytePos, bitPos, err = getFirstAvailable(head, start)
559
-	ret := posToOrdinal(bytePos, bitPos)
560
-	if end < ret || err != nil {
561
-		return invalidPos, invalidPos, ErrNoBitAvailable
562
-	}
563
-	return bytePos, bitPos, nil
564
-}
565
-
566
-// checkIfAvailable checks if the bit correspondent to the specified ordinal is unset
567
-// If the ordinal is beyond the sequence limits, a negative response is returned
568
-func checkIfAvailable(head *sequence, ordinal uint64) (uint64, uint64, error) {
569
-	bytePos, bitPos := ordinalToPos(ordinal)
570
-
571
-	// Find the sequence containing this byte
572
-	current, _, _, inBlockBytePos := findSequence(head, bytePos)
573
-	if current != nil {
574
-		// Check whether the bit corresponding to the ordinal address is unset
575
-		bitSel := blockFirstBit >> (inBlockBytePos*8 + bitPos)
576
-		if current.block&bitSel == 0 {
577
-			return bytePos, bitPos, nil
578
-		}
579
-	}
580
-
581
-	return invalidPos, invalidPos, ErrBitAllocated
582
-}
583
-
584
-// Given the byte position and the sequences list head, return the pointer to the
585
-// sequence containing the byte (current), the pointer to the previous sequence,
586
-// the number of blocks preceding the block containing the byte inside the current sequence.
587
-// If bytePos is outside of the list, function will return (nil, nil, 0, invalidPos)
588
-func findSequence(head *sequence, bytePos uint64) (*sequence, *sequence, uint64, uint64) {
589
-	// Find the sequence containing this byte
590
-	previous := head
591
-	current := head
592
-	n := bytePos
593
-	for current.next != nil && n >= (current.count*blockBytes) { // Nil check for less than 32 addresses masks
594
-		n -= (current.count * blockBytes)
595
-		previous = current
596
-		current = current.next
597
-	}
598 496
 
599
-	// If byte is outside of the list, let caller know
600
-	if n >= (current.count * blockBytes) {
601
-		return nil, nil, 0, invalidPos
602
-	}
603
-
604
-	// Find the byte position inside the block and the number of blocks
605
-	// preceding the block containing the byte inside this sequence
606
-	precBlocks := n / blockBytes
607
-	inBlockBytePos := bytePos % blockBytes
608
-
609
-	return current, previous, precBlocks, inBlockBytePos
610
-}
611
-
612
-// PushReservation pushes the bit reservation inside the bitmask.
613
-// Given byte and bit positions, identify the sequence (current) which holds the block containing the affected bit.
614
-// Create a new block with the modified bit according to the operation (allocate/release).
615
-// Create a new sequence containing the new block and insert it in the proper position.
616
-// Remove current sequence if empty.
617
-// Check if new sequence can be merged with neighbour (previous/next) sequences.
618
-//
619
-// Identify "current" sequence containing block:
620
-//
621
-//	[prev seq] [current seq] [next seq]
622
-//
623
-// Based on block position, resulting list of sequences can be any of three forms:
624
-//
625
-// block position                        Resulting list of sequences
626
-//
627
-// A) block is first in current:         [prev seq] [new] [modified current seq] [next seq]
628
-// B) block is last in current:          [prev seq] [modified current seq] [new] [next seq]
629
-// C) block is in the middle of current: [prev seq] [curr pre] [new] [curr post] [next seq]
630
-func pushReservation(bytePos, bitPos uint64, head *sequence, release bool) *sequence {
631
-	// Store list's head
632
-	newHead := head
633
-
634
-	// Find the sequence containing this byte
635
-	current, previous, precBlocks, inBlockBytePos := findSequence(head, bytePos)
636
-	if current == nil {
637
-		return newHead
638
-	}
639
-
640
-	// Construct updated block
641
-	bitSel := blockFirstBit >> (inBlockBytePos*8 + bitPos)
642
-	newBlock := current.block
643
-	if release {
644
-		newBlock &^= bitSel
645
-	} else {
646
-		newBlock |= bitSel
647
-	}
648
-
649
-	// Quit if it was a redundant request
650
-	if current.block == newBlock {
651
-		return newHead
652
-	}
653
-
654
-	// Current sequence inevitably looses one block, upadate count
655
-	current.count--
656
-
657
-	// Create new sequence
658
-	newSequence := &sequence{block: newBlock, count: 1}
659
-
660
-	// Insert the new sequence in the list based on block position
661
-	if precBlocks == 0 { // First in sequence (A)
662
-		newSequence.next = current
663
-		if current == head {
664
-			newHead = newSequence
665
-			previous = newHead
666
-		} else {
667
-			previous.next = newSequence
668
-		}
669
-		removeCurrentIfEmpty(&newHead, newSequence, current)
670
-		mergeSequences(previous)
671
-	} else if precBlocks == current.count { // Last in sequence (B)
672
-		newSequence.next = current.next
673
-		current.next = newSequence
674
-		mergeSequences(current)
675
-	} else { // In between the sequence (C)
676
-		currPre := &sequence{block: current.block, count: precBlocks, next: newSequence}
677
-		currPost := current
678
-		currPost.count -= precBlocks
679
-		newSequence.next = currPost
680
-		if currPost == head {
681
-			newHead = currPre
682
-		} else {
683
-			previous.next = currPre
684
-		}
685
-		// No merging or empty current possible here
686
-	}
687
-
688
-	return newHead
689
-}
690
-
691
-// Removes the current sequence from the list if empty, adjusting the head pointer if needed
692
-func removeCurrentIfEmpty(head **sequence, previous, current *sequence) {
693
-	if current.count == 0 {
694
-		if current == *head {
695
-			*head = current.next
696
-		} else {
697
-			previous.next = current.next
698
-		}
699
-	}
700
-}
701
-
702
-// Given a pointer to a sequence, it checks if it can be merged with any following sequences
703
-// It stops when no more merging is possible.
704
-// TODO: Optimization: only attempt merge from start to end sequence, no need to scan till the end of the list
705
-func mergeSequences(seq *sequence) {
706
-	if seq != nil {
707
-		// Merge all what possible from seq
708
-		for seq.next != nil && seq.block == seq.next.block {
709
-			seq.count += seq.next.count
710
-			seq.next = seq.next.next
711
-		}
712
-		// Move to next
713
-		mergeSequences(seq.next)
714
-	}
715
-}
716
-
717
-func getNumBlocks(numBits uint64) uint64 {
718
-	numBlocks := numBits / uint64(blockLen)
719
-	if numBits%uint64(blockLen) != 0 {
720
-		numBlocks++
497
+	h.Lock()
498
+	defer h.Unlock()
499
+	if err := h.bm.UnmarshalBinary(b); err != nil {
500
+		return err
721 501
 	}
722
-	return numBlocks
723
-}
724
-
725
-func ordinalToPos(ordinal uint64) (uint64, uint64) {
726
-	return ordinal / 8, ordinal % 8
727
-}
728
-
729
-func posToOrdinal(bytePos, bitPos uint64) uint64 {
730
-	return bytePos*8 + bitPos
502
+	return nil
731 503
 }
... ...
@@ -41,754 +41,7 @@ func randomLocalStore() (datastore.DataStore, error) {
41 41
 	})
42 42
 }
43 43
 
44
-func TestSequenceGetAvailableBit(t *testing.T) {
45
-	input := []struct {
46
-		head    *sequence
47
-		from    uint64
48
-		bytePos uint64
49
-		bitPos  uint64
50
-	}{
51
-		{&sequence{block: 0x0, count: 0}, 0, invalidPos, invalidPos},
52
-		{&sequence{block: 0x0, count: 1}, 0, 0, 0},
53
-		{&sequence{block: 0x0, count: 100}, 0, 0, 0},
54
-
55
-		{&sequence{block: 0x80000000, count: 0}, 0, invalidPos, invalidPos},
56
-		{&sequence{block: 0x80000000, count: 1}, 0, 0, 1},
57
-		{&sequence{block: 0x80000000, count: 100}, 0, 0, 1},
58
-
59
-		{&sequence{block: 0xFF000000, count: 0}, 0, invalidPos, invalidPos},
60
-		{&sequence{block: 0xFF000000, count: 1}, 0, 1, 0},
61
-		{&sequence{block: 0xFF000000, count: 100}, 0, 1, 0},
62
-
63
-		{&sequence{block: 0xFF800000, count: 0}, 0, invalidPos, invalidPos},
64
-		{&sequence{block: 0xFF800000, count: 1}, 0, 1, 1},
65
-		{&sequence{block: 0xFF800000, count: 100}, 0, 1, 1},
66
-
67
-		{&sequence{block: 0xFFC0FF00, count: 0}, 0, invalidPos, invalidPos},
68
-		{&sequence{block: 0xFFC0FF00, count: 1}, 0, 1, 2},
69
-		{&sequence{block: 0xFFC0FF00, count: 100}, 0, 1, 2},
70
-
71
-		{&sequence{block: 0xFFE0FF00, count: 0}, 0, invalidPos, invalidPos},
72
-		{&sequence{block: 0xFFE0FF00, count: 1}, 0, 1, 3},
73
-		{&sequence{block: 0xFFE0FF00, count: 100}, 0, 1, 3},
74
-
75
-		{&sequence{block: 0xFFFEFF00, count: 0}, 0, invalidPos, invalidPos},
76
-		{&sequence{block: 0xFFFEFF00, count: 1}, 0, 1, 7},
77
-		{&sequence{block: 0xFFFEFF00, count: 100}, 0, 1, 7},
78
-
79
-		{&sequence{block: 0xFFFFC0FF, count: 0}, 0, invalidPos, invalidPos},
80
-		{&sequence{block: 0xFFFFC0FF, count: 1}, 0, 2, 2},
81
-		{&sequence{block: 0xFFFFC0FF, count: 100}, 0, 2, 2},
82
-
83
-		{&sequence{block: 0xFFFFFF00, count: 0}, 0, invalidPos, invalidPos},
84
-		{&sequence{block: 0xFFFFFF00, count: 1}, 0, 3, 0},
85
-		{&sequence{block: 0xFFFFFF00, count: 100}, 0, 3, 0},
86
-
87
-		{&sequence{block: 0xFFFFFFFE, count: 0}, 0, invalidPos, invalidPos},
88
-		{&sequence{block: 0xFFFFFFFE, count: 1}, 0, 3, 7},
89
-		{&sequence{block: 0xFFFFFFFE, count: 100}, 0, 3, 7},
90
-
91
-		{&sequence{block: 0xFFFFFFFF, count: 0}, 0, invalidPos, invalidPos},
92
-		{&sequence{block: 0xFFFFFFFF, count: 1}, 0, invalidPos, invalidPos},
93
-		{&sequence{block: 0xFFFFFFFF, count: 100}, 0, invalidPos, invalidPos},
94
-
95
-		// now test with offset
96
-		{&sequence{block: 0x0, count: 0}, 0, invalidPos, invalidPos},
97
-		{&sequence{block: 0x0, count: 0}, 31, invalidPos, invalidPos},
98
-		{&sequence{block: 0x0, count: 0}, 32, invalidPos, invalidPos},
99
-		{&sequence{block: 0x0, count: 1}, 0, 0, 0},
100
-		{&sequence{block: 0x0, count: 1}, 1, 0, 1},
101
-		{&sequence{block: 0x0, count: 1}, 31, 3, 7},
102
-		{&sequence{block: 0xF0FF0000, count: 1}, 0, 0, 4},
103
-		{&sequence{block: 0xF0FF0000, count: 1}, 8, 2, 0},
104
-		{&sequence{block: 0xFFFFFFFF, count: 1}, 0, invalidPos, invalidPos},
105
-		{&sequence{block: 0xFFFFFFFF, count: 1}, 16, invalidPos, invalidPos},
106
-		{&sequence{block: 0xFFFFFFFF, count: 1}, 31, invalidPos, invalidPos},
107
-		{&sequence{block: 0xFFFFFFFE, count: 1}, 0, 3, 7},
108
-		{&sequence{block: 0xFFFFFFFF, count: 2}, 0, invalidPos, invalidPos},
109
-		{&sequence{block: 0xFFFFFFFF, count: 2}, 32, invalidPos, invalidPos},
110
-	}
111
-
112
-	for n, i := range input {
113
-		b, bb, err := i.head.getAvailableBit(i.from)
114
-		if b != i.bytePos || bb != i.bitPos {
115
-			t.Fatalf("Error in sequence.getAvailableBit(%d) (%d).\nExp: (%d, %d)\nGot: (%d, %d), err: %v", i.from, n, i.bytePos, i.bitPos, b, bb, err)
116
-		}
117
-	}
118
-}
119
-
120
-func TestSequenceEqual(t *testing.T) {
121
-	input := []struct {
122
-		first    *sequence
123
-		second   *sequence
124
-		areEqual bool
125
-	}{
126
-		{&sequence{block: 0x0, count: 8, next: nil}, &sequence{block: 0x0, count: 8}, true},
127
-		{&sequence{block: 0x0, count: 0, next: nil}, &sequence{block: 0x0, count: 0}, true},
128
-		{&sequence{block: 0x0, count: 2, next: nil}, &sequence{block: 0x0, count: 1, next: &sequence{block: 0x0, count: 1}}, false},
129
-		{&sequence{block: 0x0, count: 2, next: &sequence{block: 0x1, count: 1}}, &sequence{block: 0x0, count: 2}, false},
130
-
131
-		{&sequence{block: 0x12345678, count: 8, next: nil}, &sequence{block: 0x12345678, count: 8}, true},
132
-		{&sequence{block: 0x12345678, count: 8, next: nil}, &sequence{block: 0x12345678, count: 9}, false},
133
-		{&sequence{block: 0x12345678, count: 1, next: &sequence{block: 0xFFFFFFFF, count: 1}}, &sequence{block: 0x12345678, count: 1}, false},
134
-		{&sequence{block: 0x12345678, count: 1}, &sequence{block: 0x12345678, count: 1, next: &sequence{block: 0xFFFFFFFF, count: 1}}, false},
135
-	}
136
-
137
-	for n, i := range input {
138
-		if i.areEqual != i.first.equal(i.second) {
139
-			t.Fatalf("Error in sequence.equal() (%d).\nExp: %t\nGot: %t,", n, i.areEqual, !i.areEqual)
140
-		}
141
-	}
142
-}
143
-
144
-func TestSequenceCopy(t *testing.T) {
145
-	s := getTestSequence()
146
-	n := s.getCopy()
147
-	if !s.equal(n) {
148
-		t.Fatal("copy of s failed")
149
-	}
150
-	if n == s {
151
-		t.Fatal("not true copy of s")
152
-	}
153
-}
154
-
155
-func TestGetFirstAvailable(t *testing.T) {
156
-	input := []struct {
157
-		mask    *sequence
158
-		bytePos uint64
159
-		bitPos  uint64
160
-		start   uint64
161
-	}{
162
-		{&sequence{block: 0xffffffff, count: 2048}, invalidPos, invalidPos, 0},
163
-		{&sequence{block: 0x0, count: 8}, 0, 0, 0},
164
-		{&sequence{block: 0x80000000, count: 8}, 0, 1, 0},
165
-		{&sequence{block: 0xC0000000, count: 8}, 0, 2, 0},
166
-		{&sequence{block: 0xE0000000, count: 8}, 0, 3, 0},
167
-		{&sequence{block: 0xF0000000, count: 8}, 0, 4, 0},
168
-		{&sequence{block: 0xF8000000, count: 8}, 0, 5, 0},
169
-		{&sequence{block: 0xFC000000, count: 8}, 0, 6, 0},
170
-		{&sequence{block: 0xFE000000, count: 8}, 0, 7, 0},
171
-		{&sequence{block: 0xFE000000, count: 8}, 3, 0, 24},
172
-
173
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0x00000000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 4, 0, 0},
174
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0x80000000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 4, 1, 0},
175
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xC0000000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 4, 2, 0},
176
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xE0000000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 4, 3, 0},
177
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xF0000000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 4, 4, 0},
178
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xF8000000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 4, 5, 0},
179
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFC000000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 4, 6, 0},
180
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFE000000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 4, 7, 0},
181
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0x0E000000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 4, 0, 16},
182
-
183
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFF000000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 5, 0, 0},
184
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFF800000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 5, 1, 0},
185
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFFC00000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 5, 2, 0},
186
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFFE00000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 5, 3, 0},
187
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFFF00000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 5, 4, 0},
188
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFFF80000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 5, 5, 0},
189
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFFFC0000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 5, 6, 0},
190
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFFFE0000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 5, 7, 0},
191
-
192
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xfffffffe, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 7, 7, 0},
193
-
194
-		{&sequence{block: 0xffffffff, count: 2, next: &sequence{block: 0x0, count: 6}}, 8, 0, 0},
195
-		{&sequence{block: 0xfffcffff, count: 1, next: &sequence{block: 0x0, count: 6}}, 4, 0, 16},
196
-		{&sequence{block: 0xfffcffff, count: 1, next: &sequence{block: 0x0, count: 6}}, 1, 7, 15},
197
-		{&sequence{block: 0xfffcffff, count: 1, next: &sequence{block: 0x0, count: 6}}, 1, 6, 10},
198
-		{&sequence{block: 0xfffcfffe, count: 1, next: &sequence{block: 0x0, count: 6}}, 3, 7, 31},
199
-		{&sequence{block: 0xfffcffff, count: 1, next: &sequence{block: 0xffffffff, count: 6}}, invalidPos, invalidPos, 31},
200
-	}
201
-
202
-	for n, i := range input {
203
-		bytePos, bitPos, _ := getFirstAvailable(i.mask, i.start)
204
-		if bytePos != i.bytePos || bitPos != i.bitPos {
205
-			t.Fatalf("Error in (%d) getFirstAvailable(). Expected (%d, %d). Got (%d, %d)", n, i.bytePos, i.bitPos, bytePos, bitPos)
206
-		}
207
-	}
208
-}
209
-
210
-func TestFindSequence(t *testing.T) {
211
-	input := []struct {
212
-		head           *sequence
213
-		bytePos        uint64
214
-		precBlocks     uint64
215
-		inBlockBytePos uint64
216
-	}{
217
-		{&sequence{block: 0xffffffff, count: 0}, 0, 0, invalidPos},
218
-		{&sequence{block: 0xffffffff, count: 0}, 31, 0, invalidPos},
219
-		{&sequence{block: 0xffffffff, count: 0}, 100, 0, invalidPos},
220
-
221
-		{&sequence{block: 0x0, count: 1}, 0, 0, 0},
222
-		{&sequence{block: 0x0, count: 1}, 1, 0, 1},
223
-		{&sequence{block: 0x0, count: 1}, 31, 0, invalidPos},
224
-		{&sequence{block: 0x0, count: 1}, 60, 0, invalidPos},
225
-
226
-		{&sequence{block: 0xffffffff, count: 10}, 0, 0, 0},
227
-		{&sequence{block: 0xffffffff, count: 10}, 3, 0, 3},
228
-		{&sequence{block: 0xffffffff, count: 10}, 4, 1, 0},
229
-		{&sequence{block: 0xffffffff, count: 10}, 7, 1, 3},
230
-		{&sequence{block: 0xffffffff, count: 10}, 8, 2, 0},
231
-		{&sequence{block: 0xffffffff, count: 10}, 39, 9, 3},
232
-
233
-		{&sequence{block: 0xffffffff, count: 10, next: &sequence{block: 0xcc000000, count: 10}}, 79, 9, 3},
234
-		{&sequence{block: 0xffffffff, count: 10, next: &sequence{block: 0xcc000000, count: 10}}, 80, 0, invalidPos},
235
-	}
236
-
237
-	for n, i := range input {
238
-		_, _, precBlocks, inBlockBytePos := findSequence(i.head, i.bytePos)
239
-		if precBlocks != i.precBlocks || inBlockBytePos != i.inBlockBytePos {
240
-			t.Fatalf("Error in (%d) findSequence(). Expected (%d, %d). Got (%d, %d)", n, i.precBlocks, i.inBlockBytePos, precBlocks, inBlockBytePos)
241
-		}
242
-	}
243
-}
244
-
245
-func TestCheckIfAvailable(t *testing.T) {
246
-	input := []struct {
247
-		head    *sequence
248
-		ordinal uint64
249
-		bytePos uint64
250
-		bitPos  uint64
251
-	}{
252
-		{&sequence{block: 0xffffffff, count: 0}, 0, invalidPos, invalidPos},
253
-		{&sequence{block: 0xffffffff, count: 0}, 31, invalidPos, invalidPos},
254
-		{&sequence{block: 0xffffffff, count: 0}, 100, invalidPos, invalidPos},
255
-
256
-		{&sequence{block: 0x0, count: 1}, 0, 0, 0},
257
-		{&sequence{block: 0x0, count: 1}, 1, 0, 1},
258
-		{&sequence{block: 0x0, count: 1}, 31, 3, 7},
259
-		{&sequence{block: 0x0, count: 1}, 60, invalidPos, invalidPos},
260
-
261
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0x800000ff, count: 1}}, 31, invalidPos, invalidPos},
262
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0x800000ff, count: 1}}, 32, invalidPos, invalidPos},
263
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0x800000ff, count: 1}}, 33, 4, 1},
264
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xC00000ff, count: 1}}, 33, invalidPos, invalidPos},
265
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xC00000ff, count: 1}}, 34, 4, 2},
266
-
267
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xC00000ff, count: 1, next: &sequence{block: 0x0, count: 1}}}, 55, 6, 7},
268
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xC00000ff, count: 1, next: &sequence{block: 0x0, count: 1}}}, 56, invalidPos, invalidPos},
269
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xC00000ff, count: 1, next: &sequence{block: 0x0, count: 1}}}, 63, invalidPos, invalidPos},
270
-
271
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xC00000ff, count: 1, next: &sequence{block: 0x0, count: 1}}}, 64, 8, 0},
272
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xC00000ff, count: 1, next: &sequence{block: 0x0, count: 1}}}, 95, 11, 7},
273
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xC00000ff, count: 1, next: &sequence{block: 0x0, count: 1}}}, 96, invalidPos, invalidPos},
274
-	}
275
-
276
-	for n, i := range input {
277
-		bytePos, bitPos, err := checkIfAvailable(i.head, i.ordinal)
278
-		if bytePos != i.bytePos || bitPos != i.bitPos {
279
-			t.Fatalf("Error in (%d) checkIfAvailable(ord:%d). Expected (%d, %d). Got (%d, %d). err: %v", n, i.ordinal, i.bytePos, i.bitPos, bytePos, bitPos, err)
280
-		}
281
-	}
282
-}
283
-
284
-func TestMergeSequences(t *testing.T) {
285
-	input := []struct {
286
-		original *sequence
287
-		merged   *sequence
288
-	}{
289
-		{&sequence{block: 0xFE000000, count: 8, next: &sequence{block: 0xFE000000, count: 2}}, &sequence{block: 0xFE000000, count: 10}},
290
-		{&sequence{block: 0xFFFFFFFF, count: 8, next: &sequence{block: 0xFFFFFFFF, count: 1}}, &sequence{block: 0xFFFFFFFF, count: 9}},
291
-		{&sequence{block: 0xFFFFFFFF, count: 1, next: &sequence{block: 0xFFFFFFFF, count: 8}}, &sequence{block: 0xFFFFFFFF, count: 9}},
292
-
293
-		{&sequence{block: 0xFFFFFFF0, count: 8, next: &sequence{block: 0xFFFFFFF0, count: 1}}, &sequence{block: 0xFFFFFFF0, count: 9}},
294
-		{&sequence{block: 0xFFFFFFF0, count: 1, next: &sequence{block: 0xFFFFFFF0, count: 8}}, &sequence{block: 0xFFFFFFF0, count: 9}},
295
-
296
-		{&sequence{block: 0xFE, count: 8, next: &sequence{block: 0xFE, count: 1, next: &sequence{block: 0xFE, count: 5}}}, &sequence{block: 0xFE, count: 14}},
297
-		{&sequence{block: 0xFE, count: 8, next: &sequence{block: 0xFE, count: 1, next: &sequence{block: 0xFE, count: 5, next: &sequence{block: 0xFF, count: 1}}}},
298
-			&sequence{block: 0xFE, count: 14, next: &sequence{block: 0xFF, count: 1}}},
299
-
300
-		// No merge
301
-		{&sequence{block: 0xFE, count: 8, next: &sequence{block: 0xF8, count: 1, next: &sequence{block: 0xFE, count: 5}}},
302
-			&sequence{block: 0xFE, count: 8, next: &sequence{block: 0xF8, count: 1, next: &sequence{block: 0xFE, count: 5}}}},
303
-
304
-		// No merge from head: // Merge function tries to merge from passed head. If it can't merge with next, it does not reattempt with next as head
305
-		{&sequence{block: 0xFE, count: 8, next: &sequence{block: 0xFF, count: 1, next: &sequence{block: 0xFF, count: 5}}},
306
-			&sequence{block: 0xFE, count: 8, next: &sequence{block: 0xFF, count: 6}}},
307
-	}
308
-
309
-	for n, i := range input {
310
-		mergeSequences(i.original)
311
-		for !i.merged.equal(i.original) {
312
-			t.Fatalf("Error in (%d) mergeSequences().\nExp: %s\nGot: %s,", n, i.merged.toString(), i.original.toString())
313
-		}
314
-	}
315
-}
316
-
317
-func TestPushReservation(t *testing.T) {
318
-	input := []struct {
319
-		mask    *sequence
320
-		bytePos uint64
321
-		bitPos  uint64
322
-		newMask *sequence
323
-	}{
324
-		// Create first sequence and fill in 8 addresses starting from address 0
325
-		{&sequence{block: 0x0, count: 8, next: nil}, 0, 0, &sequence{block: 0x80000000, count: 1, next: &sequence{block: 0x0, count: 7, next: nil}}},
326
-		{&sequence{block: 0x80000000, count: 8}, 0, 1, &sequence{block: 0xC0000000, count: 1, next: &sequence{block: 0x80000000, count: 7, next: nil}}},
327
-		{&sequence{block: 0xC0000000, count: 8}, 0, 2, &sequence{block: 0xE0000000, count: 1, next: &sequence{block: 0xC0000000, count: 7, next: nil}}},
328
-		{&sequence{block: 0xE0000000, count: 8}, 0, 3, &sequence{block: 0xF0000000, count: 1, next: &sequence{block: 0xE0000000, count: 7, next: nil}}},
329
-		{&sequence{block: 0xF0000000, count: 8}, 0, 4, &sequence{block: 0xF8000000, count: 1, next: &sequence{block: 0xF0000000, count: 7, next: nil}}},
330
-		{&sequence{block: 0xF8000000, count: 8}, 0, 5, &sequence{block: 0xFC000000, count: 1, next: &sequence{block: 0xF8000000, count: 7, next: nil}}},
331
-		{&sequence{block: 0xFC000000, count: 8}, 0, 6, &sequence{block: 0xFE000000, count: 1, next: &sequence{block: 0xFC000000, count: 7, next: nil}}},
332
-		{&sequence{block: 0xFE000000, count: 8}, 0, 7, &sequence{block: 0xFF000000, count: 1, next: &sequence{block: 0xFE000000, count: 7, next: nil}}},
333
-
334
-		{&sequence{block: 0x80000000, count: 1, next: &sequence{block: 0x0, count: 7}}, 0, 1, &sequence{block: 0xC0000000, count: 1, next: &sequence{block: 0x0, count: 7, next: nil}}},
335
-
336
-		// Create second sequence and fill in 8 addresses starting from address 32
337
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0x00000000, count: 1, next: &sequence{block: 0xffffffff, count: 6, next: nil}}}, 4, 0,
338
-			&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0x80000000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}},
339
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0x80000000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 4, 1,
340
-			&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xC0000000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}},
341
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xC0000000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 4, 2,
342
-			&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xE0000000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}},
343
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xE0000000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 4, 3,
344
-			&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xF0000000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}},
345
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xF0000000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 4, 4,
346
-			&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xF8000000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}},
347
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xF8000000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 4, 5,
348
-			&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFC000000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}},
349
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFC000000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 4, 6,
350
-			&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFE000000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}},
351
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFE000000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 4, 7,
352
-			&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFF000000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}},
353
-		// fill in 8 addresses starting from address 40
354
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFF000000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 5, 0,
355
-			&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFF800000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}},
356
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFF800000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 5, 1,
357
-			&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFFC00000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}},
358
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFFC00000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 5, 2,
359
-			&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFFE00000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}},
360
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFFE00000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 5, 3,
361
-			&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFFF00000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}},
362
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFFF00000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 5, 4,
363
-			&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFFF80000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}},
364
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFFF80000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 5, 5,
365
-			&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFFFC0000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}},
366
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFFFC0000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 5, 6,
367
-			&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFFFE0000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}},
368
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFFFE0000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}, 5, 7,
369
-			&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xFFFF0000, count: 1, next: &sequence{block: 0xffffffff, count: 6}}}},
370
-
371
-		// Insert new sequence
372
-		{&sequence{block: 0xffffffff, count: 2, next: &sequence{block: 0x0, count: 6}}, 8, 0,
373
-			&sequence{block: 0xffffffff, count: 2, next: &sequence{block: 0x80000000, count: 1, next: &sequence{block: 0x0, count: 5}}}},
374
-		{&sequence{block: 0xffffffff, count: 2, next: &sequence{block: 0x80000000, count: 1, next: &sequence{block: 0x0, count: 5}}}, 8, 1,
375
-			&sequence{block: 0xffffffff, count: 2, next: &sequence{block: 0xC0000000, count: 1, next: &sequence{block: 0x0, count: 5}}}},
376
-
377
-		// Merge affected with next
378
-		{&sequence{block: 0xffffffff, count: 7, next: &sequence{block: 0xfffffffe, count: 2, next: &sequence{block: 0xffffffff, count: 1}}}, 31, 7,
379
-			&sequence{block: 0xffffffff, count: 8, next: &sequence{block: 0xfffffffe, count: 1, next: &sequence{block: 0xffffffff, count: 1}}}},
380
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xfffffffc, count: 1, next: &sequence{block: 0xfffffffe, count: 6}}}, 7, 6,
381
-			&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xfffffffe, count: 7}}},
382
-
383
-		// Merge affected with next and next.next
384
-		{&sequence{block: 0xffffffff, count: 7, next: &sequence{block: 0xfffffffe, count: 1, next: &sequence{block: 0xffffffff, count: 1}}}, 31, 7,
385
-			&sequence{block: 0xffffffff, count: 9}},
386
-		{&sequence{block: 0xffffffff, count: 7, next: &sequence{block: 0xfffffffe, count: 1}}, 31, 7,
387
-			&sequence{block: 0xffffffff, count: 8}},
388
-
389
-		// Merge affected with previous and next
390
-		{&sequence{block: 0xffffffff, count: 7, next: &sequence{block: 0xfffffffe, count: 1, next: &sequence{block: 0xffffffff, count: 1}}}, 31, 7,
391
-			&sequence{block: 0xffffffff, count: 9}},
392
-
393
-		// Redundant push: No change
394
-		{&sequence{block: 0xffff0000, count: 1}, 0, 0, &sequence{block: 0xffff0000, count: 1}},
395
-		{&sequence{block: 0xffff0000, count: 7}, 25, 7, &sequence{block: 0xffff0000, count: 7}},
396
-		{&sequence{block: 0xffffffff, count: 7, next: &sequence{block: 0xfffffffe, count: 1, next: &sequence{block: 0xffffffff, count: 1}}}, 7, 7,
397
-			&sequence{block: 0xffffffff, count: 7, next: &sequence{block: 0xfffffffe, count: 1, next: &sequence{block: 0xffffffff, count: 1}}}},
398
-
399
-		// Set last bit
400
-		{&sequence{block: 0x0, count: 8}, 31, 7, &sequence{block: 0x0, count: 7, next: &sequence{block: 0x1, count: 1}}},
401
-
402
-		// Set bit in a middle sequence in the first block, first bit
403
-		{&sequence{block: 0x40000000, count: 1, next: &sequence{block: 0x0, count: 6, next: &sequence{block: 0x1, count: 1}}}, 4, 0,
404
-			&sequence{block: 0x40000000, count: 1, next: &sequence{block: 0x80000000, count: 1, next: &sequence{block: 0x0, count: 5,
405
-				next: &sequence{block: 0x1, count: 1}}}}},
406
-
407
-		// Set bit in a middle sequence in the first block, first bit (merge involved)
408
-		{&sequence{block: 0x80000000, count: 1, next: &sequence{block: 0x0, count: 6, next: &sequence{block: 0x1, count: 1}}}, 4, 0,
409
-			&sequence{block: 0x80000000, count: 2, next: &sequence{block: 0x0, count: 5, next: &sequence{block: 0x1, count: 1}}}},
410
-
411
-		// Set bit in a middle sequence in the first block, last bit
412
-		{&sequence{block: 0x80000000, count: 1, next: &sequence{block: 0x0, count: 6, next: &sequence{block: 0x1, count: 1}}}, 4, 31,
413
-			&sequence{block: 0x80000000, count: 1, next: &sequence{block: 0x1, count: 1, next: &sequence{block: 0x0, count: 5,
414
-				next: &sequence{block: 0x1, count: 1}}}}},
415
-
416
-		// Set bit in a middle sequence in the first block, middle bit
417
-		{&sequence{block: 0x80000000, count: 1, next: &sequence{block: 0x0, count: 6, next: &sequence{block: 0x1, count: 1}}}, 4, 16,
418
-			&sequence{block: 0x80000000, count: 1, next: &sequence{block: 0x8000, count: 1, next: &sequence{block: 0x0, count: 5,
419
-				next: &sequence{block: 0x1, count: 1}}}}},
420
-
421
-		// Set bit in a middle sequence in a middle block, first bit
422
-		{&sequence{block: 0x80000000, count: 1, next: &sequence{block: 0x0, count: 6, next: &sequence{block: 0x1, count: 1}}}, 16, 0,
423
-			&sequence{block: 0x80000000, count: 1, next: &sequence{block: 0x0, count: 3, next: &sequence{block: 0x80000000, count: 1,
424
-				next: &sequence{block: 0x0, count: 2, next: &sequence{block: 0x1, count: 1}}}}}},
425
-
426
-		// Set bit in a middle sequence in a middle block, last bit
427
-		{&sequence{block: 0x80000000, count: 1, next: &sequence{block: 0x0, count: 6, next: &sequence{block: 0x1, count: 1}}}, 16, 31,
428
-			&sequence{block: 0x80000000, count: 1, next: &sequence{block: 0x0, count: 3, next: &sequence{block: 0x1, count: 1,
429
-				next: &sequence{block: 0x0, count: 2, next: &sequence{block: 0x1, count: 1}}}}}},
430
-
431
-		// Set bit in a middle sequence in a middle block, middle bit
432
-		{&sequence{block: 0x80000000, count: 1, next: &sequence{block: 0x0, count: 6, next: &sequence{block: 0x1, count: 1}}}, 16, 15,
433
-			&sequence{block: 0x80000000, count: 1, next: &sequence{block: 0x0, count: 3, next: &sequence{block: 0x10000, count: 1,
434
-				next: &sequence{block: 0x0, count: 2, next: &sequence{block: 0x1, count: 1}}}}}},
435
-
436
-		// Set bit in a middle sequence in the last block, first bit
437
-		{&sequence{block: 0x80000000, count: 1, next: &sequence{block: 0x0, count: 6, next: &sequence{block: 0x1, count: 1}}}, 24, 0,
438
-			&sequence{block: 0x80000000, count: 1, next: &sequence{block: 0x0, count: 5, next: &sequence{block: 0x80000000, count: 1,
439
-				next: &sequence{block: 0x1, count: 1}}}}},
440
-
441
-		// Set bit in a middle sequence in the last block, last bit
442
-		{&sequence{block: 0x80000000, count: 1, next: &sequence{block: 0x0, count: 6, next: &sequence{block: 0x4, count: 1}}}, 24, 31,
443
-			&sequence{block: 0x80000000, count: 1, next: &sequence{block: 0x0, count: 5, next: &sequence{block: 0x1, count: 1,
444
-				next: &sequence{block: 0x4, count: 1}}}}},
445
-
446
-		// Set bit in a middle sequence in the last block, last bit (merge involved)
447
-		{&sequence{block: 0x80000000, count: 1, next: &sequence{block: 0x0, count: 6, next: &sequence{block: 0x1, count: 1}}}, 24, 31,
448
-			&sequence{block: 0x80000000, count: 1, next: &sequence{block: 0x0, count: 5, next: &sequence{block: 0x1, count: 2}}}},
449
-
450
-		// Set bit in a middle sequence in the last block, middle bit
451
-		{&sequence{block: 0x80000000, count: 1, next: &sequence{block: 0x0, count: 6, next: &sequence{block: 0x1, count: 1}}}, 24, 16,
452
-			&sequence{block: 0x80000000, count: 1, next: &sequence{block: 0x0, count: 5, next: &sequence{block: 0x8000, count: 1,
453
-				next: &sequence{block: 0x1, count: 1}}}}},
454
-	}
455
-
456
-	for n, i := range input {
457
-		mask := pushReservation(i.bytePos, i.bitPos, i.mask, false)
458
-		if !mask.equal(i.newMask) {
459
-			t.Fatalf("Error in (%d) pushReservation():\n%s + (%d,%d):\nExp: %s\nGot: %s,",
460
-				n, i.mask.toString(), i.bytePos, i.bitPos, i.newMask.toString(), mask.toString())
461
-		}
462
-	}
463
-}
464
-
465
-func TestSerializeDeserialize(t *testing.T) {
466
-	s := getTestSequence()
467
-
468
-	data, err := s.toByteArray()
469
-	if err != nil {
470
-		t.Fatal(err)
471
-	}
472
-
473
-	r := &sequence{}
474
-	err = r.fromByteArray(data)
475
-	if err != nil {
476
-		t.Fatal(err)
477
-	}
478
-
479
-	if !s.equal(r) {
480
-		t.Fatalf("Sequences are different: \n%v\n%v", s, r)
481
-	}
482
-}
483
-
484
-func getTestSequence() *sequence {
485
-	// Returns a custom sequence of 1024 * 32 bits
486
-	return &sequence{
487
-		block: 0xFFFFFFFF,
488
-		count: 100,
489
-		next: &sequence{
490
-			block: 0xFFFFFFFE,
491
-			count: 1,
492
-			next: &sequence{
493
-				block: 0xFF000000,
494
-				count: 10,
495
-				next: &sequence{
496
-					block: 0xFFFFFFFF,
497
-					count: 50,
498
-					next: &sequence{
499
-						block: 0xFFFFFFFC,
500
-						count: 1,
501
-						next: &sequence{
502
-							block: 0xFF800000,
503
-							count: 1,
504
-							next: &sequence{
505
-								block: 0xFFFFFFFF,
506
-								count: 87,
507
-								next: &sequence{
508
-									block: 0x0,
509
-									count: 150,
510
-									next: &sequence{
511
-										block: 0xFFFFFFFF,
512
-										count: 200,
513
-										next: &sequence{
514
-											block: 0x0000FFFF,
515
-											count: 1,
516
-											next: &sequence{
517
-												block: 0x0,
518
-												count: 399,
519
-												next: &sequence{
520
-													block: 0xFFFFFFFF,
521
-													count: 23,
522
-													next: &sequence{
523
-														block: 0x1,
524
-														count: 1,
525
-													},
526
-												},
527
-											},
528
-										},
529
-									},
530
-								},
531
-							},
532
-						},
533
-					},
534
-				},
535
-			},
536
-		},
537
-	}
538
-}
539
-
540
-func TestSet(t *testing.T) {
541
-	hnd, err := NewHandle("", nil, "", 1024*32)
542
-	if err != nil {
543
-		t.Fatal(err)
544
-	}
545
-	hnd.head = getTestSequence()
546
-
547
-	firstAv := uint64(32*100 + 31)
548
-	last := uint64(1024*32 - 1)
549
-
550
-	if hnd.IsSet(100000) {
551
-		t.Fatal("IsSet() returned wrong result")
552
-	}
553
-
554
-	if !hnd.IsSet(0) {
555
-		t.Fatal("IsSet() returned wrong result")
556
-	}
557
-
558
-	if hnd.IsSet(firstAv) {
559
-		t.Fatal("IsSet() returned wrong result")
560
-	}
561
-
562
-	if !hnd.IsSet(last) {
563
-		t.Fatal("IsSet() returned wrong result")
564
-	}
565
-
566
-	if err := hnd.Set(0); err == nil {
567
-		t.Fatal("Expected failure, but succeeded")
568
-	}
569
-
570
-	os, err := hnd.SetAny(false)
571
-	if err != nil {
572
-		t.Fatalf("Unexpected failure: %v", err)
573
-	}
574
-	if os != firstAv {
575
-		t.Fatalf("SetAny returned unexpected ordinal. Expected %d. Got %d.", firstAv, os)
576
-	}
577
-	if !hnd.IsSet(firstAv) {
578
-		t.Fatal("IsSet() returned wrong result")
579
-	}
580
-
581
-	if err := hnd.Unset(firstAv); err != nil {
582
-		t.Fatalf("Unexpected failure: %v", err)
583
-	}
584
-
585
-	if hnd.IsSet(firstAv) {
586
-		t.Fatal("IsSet() returned wrong result")
587
-	}
588
-
589
-	if err := hnd.Set(firstAv); err != nil {
590
-		t.Fatalf("Unexpected failure: %v", err)
591
-	}
592
-
593
-	if err := hnd.Set(last); err == nil {
594
-		t.Fatal("Expected failure, but succeeded")
595
-	}
596
-}
597
-
598
-func TestSetUnset(t *testing.T) {
599
-	numBits := uint64(32 * blockLen)
600
-	hnd, err := NewHandle("", nil, "", numBits)
601
-	if err != nil {
602
-		t.Fatal(err)
603
-	}
604
-
605
-	if err := hnd.Set(uint64(32 * blockLen)); err == nil {
606
-		t.Fatal("Expected failure, but succeeded")
607
-	}
608
-	if err := hnd.Unset(uint64(32 * blockLen)); err == nil {
609
-		t.Fatal("Expected failure, but succeeded")
610
-	}
611
-
612
-	// set and unset all one by one
613
-	for hnd.Unselected() > 0 {
614
-		if _, err := hnd.SetAny(false); err != nil {
615
-			t.Fatal(err)
616
-		}
617
-	}
618
-	if _, err := hnd.SetAny(false); err != ErrNoBitAvailable {
619
-		t.Fatal("Expected error. Got success")
620
-	}
621
-	if _, err := hnd.SetAnyInRange(10, 20, false); err != ErrNoBitAvailable {
622
-		t.Fatal("Expected error. Got success")
623
-	}
624
-	if err := hnd.Set(50); err != ErrBitAllocated {
625
-		t.Fatalf("Expected error. Got %v: %s", err, hnd)
626
-	}
627
-	i := uint64(0)
628
-	for hnd.Unselected() < numBits {
629
-		if err := hnd.Unset(i); err != nil {
630
-			t.Fatal(err)
631
-		}
632
-		i++
633
-	}
634
-}
635
-
636
-func TestOffsetSetUnset(t *testing.T) {
637
-	numBits := uint64(32 * blockLen)
638
-	var o uint64
639
-	hnd, err := NewHandle("", nil, "", numBits)
640
-	if err != nil {
641
-		t.Fatal(err)
642
-	}
643
-
644
-	// set and unset all one by one
645
-	for hnd.Unselected() > 0 {
646
-		if _, err := hnd.SetAny(false); err != nil {
647
-			t.Fatal(err)
648
-		}
649
-	}
650
-
651
-	if _, err := hnd.SetAny(false); err != ErrNoBitAvailable {
652
-		t.Fatal("Expected error. Got success")
653
-	}
654
-
655
-	if _, err := hnd.SetAnyInRange(10, 20, false); err != ErrNoBitAvailable {
656
-		t.Fatal("Expected error. Got success")
657
-	}
658
-
659
-	if err := hnd.Unset(288); err != nil {
660
-		t.Fatal(err)
661
-	}
662
-
663
-	//At this point sequence is (0xffffffff, 9)->(0x7fffffff, 1)->(0xffffffff, 22)->end
664
-	if o, err = hnd.SetAnyInRange(32, 500, false); err != nil {
665
-		t.Fatal(err)
666
-	}
667
-
668
-	if o != 288 {
669
-		t.Fatalf("Expected ordinal not received, Received:%d", o)
670
-	}
671
-}
672
-
673
-func TestSetInRange(t *testing.T) {
674
-	numBits := uint64(1024 * blockLen)
675
-	hnd, err := NewHandle("", nil, "", numBits)
676
-	if err != nil {
677
-		t.Fatal(err)
678
-	}
679
-	hnd.head = getTestSequence()
680
-
681
-	firstAv := uint64(100*blockLen + blockLen - 1)
682
-
683
-	if o, err := hnd.SetAnyInRange(4, 3, false); err == nil {
684
-		t.Fatalf("Expected failure. Got success with ordinal:%d", o)
685
-	}
686
-
687
-	if o, err := hnd.SetAnyInRange(0, numBits, false); err == nil {
688
-		t.Fatalf("Expected failure. Got success with ordinal:%d", o)
689
-	}
690
-
691
-	o, err := hnd.SetAnyInRange(100*uint64(blockLen), 101*uint64(blockLen), false)
692
-	if err != nil {
693
-		t.Fatalf("Unexpected failure: (%d, %v)", o, err)
694
-	}
695
-	if o != firstAv {
696
-		t.Fatalf("Unexpected ordinal: %d", o)
697
-	}
698
-
699
-	if o, err := hnd.SetAnyInRange(0, uint64(blockLen), false); err == nil {
700
-		t.Fatalf("Expected failure. Got success with ordinal:%d", o)
701
-	}
702
-
703
-	if o, err := hnd.SetAnyInRange(0, firstAv-1, false); err == nil {
704
-		t.Fatalf("Expected failure. Got success with ordinal:%d", o)
705
-	}
706
-
707
-	if o, err := hnd.SetAnyInRange(111*uint64(blockLen), 161*uint64(blockLen), false); err == nil {
708
-		t.Fatalf("Expected failure. Got success with ordinal:%d", o)
709
-	}
710
-
711
-	o, err = hnd.SetAnyInRange(161*uint64(blockLen), 162*uint64(blockLen), false)
712
-	if err != nil {
713
-		t.Fatal(err)
714
-	}
715
-	if o != 161*uint64(blockLen)+30 {
716
-		t.Fatalf("Unexpected ordinal: %d", o)
717
-	}
718
-
719
-	o, err = hnd.SetAnyInRange(161*uint64(blockLen), 162*uint64(blockLen), false)
720
-	if err != nil {
721
-		t.Fatal(err)
722
-	}
723
-	if o != 161*uint64(blockLen)+31 {
724
-		t.Fatalf("Unexpected ordinal: %d", o)
725
-	}
726
-
727
-	o, err = hnd.SetAnyInRange(161*uint64(blockLen), 162*uint64(blockLen), false)
728
-	if err == nil {
729
-		t.Fatalf("Expected failure. Got success with ordinal:%d", o)
730
-	}
731
-
732
-	if _, err := hnd.SetAnyInRange(0, numBits-1, false); err != nil {
733
-		t.Fatalf("Unexpected failure: %v", err)
734
-	}
735
-
736
-	// set one bit using the set range with 1 bit size range
737
-	if _, err := hnd.SetAnyInRange(uint64(163*blockLen-1), uint64(163*blockLen-1), false); err != nil {
738
-		t.Fatal(err)
739
-	}
740
-
741
-	// create a non multiple of 32 mask
742
-	hnd, err = NewHandle("", nil, "", 30)
743
-	if err != nil {
744
-		t.Fatal(err)
745
-	}
746
-
747
-	// set all bit in the first range
748
-	for hnd.Unselected() > 22 {
749
-		if o, err := hnd.SetAnyInRange(0, 7, false); err != nil {
750
-			t.Fatalf("Unexpected failure: (%d, %v)", o, err)
751
-		}
752
-	}
753
-	// try one more set, which should fail
754
-	o, err = hnd.SetAnyInRange(0, 7, false)
755
-	if err == nil {
756
-		t.Fatalf("Expected failure. Got success with ordinal:%d", o)
757
-	}
758
-	if err != ErrNoBitAvailable {
759
-		t.Fatalf("Unexpected error: %v", err)
760
-	}
761
-
762
-	// set all bit in a second range
763
-	for hnd.Unselected() > 14 {
764
-		if o, err := hnd.SetAnyInRange(8, 15, false); err != nil {
765
-			t.Fatalf("Unexpected failure: (%d, %v)", o, err)
766
-		}
767
-	}
768
-
769
-	// try one more set, which should fail
770
-	o, err = hnd.SetAnyInRange(0, 15, false)
771
-	if err == nil {
772
-		t.Fatalf("Expected failure. Got success with ordinal:%d", o)
773
-	}
774
-	if err != ErrNoBitAvailable {
775
-		t.Fatalf("Unexpected error: %v", err)
776
-	}
777
-
778
-	// set all bit in a range which includes the last bit
779
-	for hnd.Unselected() > 12 {
780
-		if o, err := hnd.SetAnyInRange(28, 29, false); err != nil {
781
-			t.Fatalf("Unexpected failure: (%d, %v)", o, err)
782
-		}
783
-	}
784
-	o, err = hnd.SetAnyInRange(28, 29, false)
785
-	if err == nil {
786
-		t.Fatalf("Expected failure. Got success with ordinal:%d", o)
787
-	}
788
-	if err != ErrNoBitAvailable {
789
-		t.Fatalf("Unexpected error: %v", err)
790
-	}
791
-}
44
+const blockLen = 32
792 45
 
793 46
 // This one tests an allocation pattern which unveiled an issue in pushReservation
794 47
 // Specifically a failure in detecting when we are in the (B) case (the bit to set
... ...
@@ -845,39 +98,6 @@ func TestSetAnyInRange(t *testing.T) {
845 845
 	}
846 846
 }
847 847
 
848
-func TestMethods(t *testing.T) {
849
-	numBits := uint64(256 * blockLen)
850
-	hnd, err := NewHandle("path/to/data", nil, "sequence1", numBits)
851
-	if err != nil {
852
-		t.Fatal(err)
853
-	}
854
-
855
-	if hnd.Bits() != numBits {
856
-		t.Fatalf("Unexpected bit number: %d", hnd.Bits())
857
-	}
858
-
859
-	if hnd.Unselected() != numBits {
860
-		t.Fatalf("Unexpected bit number: %d", hnd.Unselected())
861
-	}
862
-
863
-	exp := "(0x0, 256)->end"
864
-	if hnd.head.toString() != exp {
865
-		t.Fatalf("Unexpected sequence string: %s", hnd.head.toString())
866
-	}
867
-
868
-	for i := 0; i < 192; i++ {
869
-		_, err := hnd.SetAny(false)
870
-		if err != nil {
871
-			t.Fatal(err)
872
-		}
873
-	}
874
-
875
-	exp = "(0xffffffff, 6)->(0x0, 250)->end"
876
-	if hnd.head.toString() != exp {
877
-		t.Fatalf("Unexpected sequence string: %s", hnd.head.toString())
878
-	}
879
-}
880
-
881 848
 func TestRandomAllocateDeallocate(t *testing.T) {
882 849
 	ds, err := randomLocalStore()
883 850
 	if err != nil {
... ...
@@ -889,171 +109,37 @@ func TestRandomAllocateDeallocate(t *testing.T) {
889 889
 	if err != nil {
890 890
 		t.Fatal(err)
891 891
 	}
892
+	defer func() {
893
+		if err := hnd.Destroy(); err != nil {
894
+			t.Fatal(err)
895
+		}
896
+	}()
892 897
 
893 898
 	seed := time.Now().Unix()
894
-	rand.Seed(seed)
899
+	rng := rand.New(rand.NewSource(seed))
895 900
 
896 901
 	// Allocate all bits using a random pattern
897
-	pattern := rand.Perm(numBits)
902
+	pattern := rng.Perm(numBits)
898 903
 	for _, bit := range pattern {
899 904
 		err := hnd.Set(uint64(bit))
900 905
 		if err != nil {
901
-			t.Fatalf("Unexpected failure on allocation of %d: %v.\nSeed: %d.\n%s", bit, err, seed, hnd)
906
+			t.Errorf("Unexpected failure on allocation of %d: %v.\nSeed: %d.\n%s", bit, err, seed, hnd)
902 907
 		}
903 908
 	}
904
-	if hnd.Unselected() != 0 {
905
-		t.Fatalf("Expected full sequence. Instead found %d free bits. Seed: %d.\n%s", hnd.unselected, seed, hnd)
906
-	}
907
-	if hnd.head.toString() != "(0xffffffff, 16)->end" {
908
-		t.Fatalf("Unexpected db: %s", hnd.head.toString())
909
+	if unselected := hnd.Unselected(); unselected != 0 {
910
+		t.Errorf("Expected full sequence. Instead found %d free bits. Seed: %d.\n%s", unselected, seed, hnd)
909 911
 	}
910 912
 
911 913
 	// Deallocate all bits using a random pattern
912
-	pattern = rand.Perm(numBits)
914
+	pattern = rng.Perm(numBits)
913 915
 	for _, bit := range pattern {
914 916
 		err := hnd.Unset(uint64(bit))
915 917
 		if err != nil {
916
-			t.Fatalf("Unexpected failure on deallocation of %d: %v.\nSeed: %d.\n%s", bit, err, seed, hnd)
918
+			t.Errorf("Unexpected failure on deallocation of %d: %v.\nSeed: %d.\n%s", bit, err, seed, hnd)
917 919
 		}
918 920
 	}
919
-	if hnd.Unselected() != uint64(numBits) {
920
-		t.Fatalf("Expected full sequence. Instead found %d free bits. Seed: %d.\n%s", hnd.unselected, seed, hnd)
921
-	}
922
-	if hnd.head.toString() != "(0x0, 16)->end" {
923
-		t.Fatalf("Unexpected db: %s", hnd.head.toString())
924
-	}
925
-
926
-	err = hnd.Destroy()
927
-	if err != nil {
928
-		t.Fatal(err)
929
-	}
930
-}
931
-
932
-func TestAllocateRandomDeallocate(t *testing.T) {
933
-	ds, err := randomLocalStore()
934
-	if err != nil {
935
-		t.Fatal(err)
936
-	}
937
-
938
-	numBlocks := uint32(8)
939
-	numBits := int(numBlocks * blockLen)
940
-	hnd, err := NewHandle(filepath.Join("bitseq", "test", "data"), ds, "test1", uint64(numBits))
941
-	if err != nil {
942
-		t.Fatal(err)
943
-	}
944
-
945
-	expected := &sequence{block: 0xffffffff, count: uint64(numBlocks / 2), next: &sequence{block: 0x0, count: uint64(numBlocks / 2)}}
946
-
947
-	// Allocate first half of the bits
948
-	for i := 0; i < numBits/2; i++ {
949
-		_, err := hnd.SetAny(false)
950
-		if err != nil {
951
-			t.Fatalf("Unexpected failure on allocation %d: %v\n%s", i, err, hnd)
952
-		}
953
-	}
954
-	if hnd.Unselected() != uint64(numBits/2) {
955
-		t.Fatalf("Expected full sequence. Instead found %d free bits. %s", hnd.unselected, hnd)
956
-	}
957
-	if !hnd.head.equal(expected) {
958
-		t.Fatalf("Unexpected sequence. Got:\n%s", hnd)
959
-	}
960
-
961
-	seed := time.Now().Unix()
962
-	rand.Seed(seed)
963
-
964
-	// Deallocate half of the allocated bits following a random pattern
965
-	pattern := rand.Perm(numBits / 2)
966
-	for i := 0; i < numBits/4; i++ {
967
-		bit := pattern[i]
968
-		err := hnd.Unset(uint64(bit))
969
-		if err != nil {
970
-			t.Fatalf("Unexpected failure on deallocation of %d: %v.\nSeed: %d.\n%s", bit, err, seed, hnd)
971
-		}
972
-	}
973
-	if hnd.Unselected() != uint64(3*numBits/4) {
974
-		t.Fatalf("Expected full sequence. Instead found %d free bits.\nSeed: %d.\n%s", hnd.unselected, seed, hnd)
975
-	}
976
-
977
-	// Request a quarter of bits
978
-	for i := 0; i < numBits/4; i++ {
979
-		_, err := hnd.SetAny(false)
980
-		if err != nil {
981
-			t.Fatalf("Unexpected failure on allocation %d: %v\nSeed: %d\n%s", i, err, seed, hnd)
982
-		}
983
-	}
984
-	if hnd.Unselected() != uint64(numBits/2) {
985
-		t.Fatalf("Expected half sequence. Instead found %d free bits.\nSeed: %d\n%s", hnd.unselected, seed, hnd)
986
-	}
987
-	if !hnd.head.equal(expected) {
988
-		t.Fatalf("Unexpected sequence. Got:\n%s", hnd)
989
-	}
990
-
991
-	err = hnd.Destroy()
992
-	if err != nil {
993
-		t.Fatal(err)
994
-	}
995
-}
996
-
997
-func TestAllocateRandomDeallocateSerialize(t *testing.T) {
998
-	ds, err := randomLocalStore()
999
-	if err != nil {
1000
-		t.Fatal(err)
1001
-	}
1002
-
1003
-	numBlocks := uint32(8)
1004
-	numBits := int(numBlocks * blockLen)
1005
-	hnd, err := NewHandle("bitseq-test/data/", ds, "test1", uint64(numBits))
1006
-	if err != nil {
1007
-		t.Fatal(err)
1008
-	}
1009
-
1010
-	expected := &sequence{block: 0xffffffff, count: uint64(numBlocks / 2), next: &sequence{block: 0x0, count: uint64(numBlocks / 2)}}
1011
-
1012
-	// Allocate first half of the bits
1013
-	for i := 0; i < numBits/2; i++ {
1014
-		_, err := hnd.SetAny(true)
1015
-		if err != nil {
1016
-			t.Fatalf("Unexpected failure on allocation %d: %v\n%s", i, err, hnd)
1017
-		}
1018
-	}
1019
-
1020
-	if hnd.Unselected() != uint64(numBits/2) {
1021
-		t.Fatalf("Expected full sequence. Instead found %d free bits. %s", hnd.unselected, hnd)
1022
-	}
1023
-	if !hnd.head.equal(expected) {
1024
-		t.Fatalf("Unexpected sequence. Got:\n%s", hnd)
1025
-	}
1026
-
1027
-	seed := time.Now().Unix()
1028
-	rand.Seed(seed)
1029
-
1030
-	// Deallocate half of the allocated bits following a random pattern
1031
-	pattern := rand.Perm(numBits / 2)
1032
-	for i := 0; i < numBits/4; i++ {
1033
-		bit := pattern[i]
1034
-		err := hnd.Unset(uint64(bit))
1035
-		if err != nil {
1036
-			t.Fatalf("Unexpected failure on deallocation of %d: %v.\nSeed: %d.\n%s", bit, err, seed, hnd)
1037
-		}
1038
-	}
1039
-	if hnd.Unselected() != uint64(3*numBits/4) {
1040
-		t.Fatalf("Expected full sequence. Instead found %d free bits.\nSeed: %d.\n%s", hnd.unselected, seed, hnd)
1041
-	}
1042
-
1043
-	// Request a quarter of bits
1044
-	for i := 0; i < numBits/4; i++ {
1045
-		_, err := hnd.SetAny(true)
1046
-		if err != nil {
1047
-			t.Fatalf("Unexpected failure on allocation %d: %v\nSeed: %d\n%s", i, err, seed, hnd)
1048
-		}
1049
-	}
1050
-	if hnd.Unselected() != uint64(numBits/2) {
1051
-		t.Fatalf("Expected half sequence. Instead found %d free bits.\nSeed: %d\n%s", hnd.unselected, seed, hnd)
1052
-	}
1053
-
1054
-	err = hnd.Destroy()
1055
-	if err != nil {
1056
-		t.Fatal(err)
921
+	if unselected := hnd.Unselected(); unselected != uint64(numBits) {
922
+		t.Errorf("Expected full sequence. Instead found %d free bits. Seed: %d.\n%s", unselected, seed, hnd)
1057 923
 	}
1058 924
 }
1059 925
 
... ...
@@ -1106,140 +192,22 @@ func TestIsCorrupted(t *testing.T) {
1106 1106
 		t.Fatal(err)
1107 1107
 	}
1108 1108
 
1109
-	if hnd.runConsistencyCheck() {
1110
-		t.Fatalf("Unexpected corrupted for %s", hnd)
1111
-	}
1112
-
1113 1109
 	if err := hnd.CheckConsistency(); err != nil {
1114 1110
 		t.Fatal(err)
1115 1111
 	}
1116 1112
 
1117
-	hnd.Set(0)
1118
-	if hnd.runConsistencyCheck() {
1119
-		t.Fatalf("Unexpected corrupted for %s", hnd)
1120
-	}
1121
-
1122
-	hnd.Set(1023)
1123
-	if hnd.runConsistencyCheck() {
1124
-		t.Fatalf("Unexpected corrupted for %s", hnd)
1125
-	}
1126
-
1113
+	_ = hnd.Set(0)
1127 1114
 	if err := hnd.CheckConsistency(); err != nil {
1128 1115
 		t.Fatal(err)
1129 1116
 	}
1130 1117
 
1131
-	// Try real corrupted ipam handles found in the local store files reported by three docker users,
1132
-	// plus a generic ipam handle from docker 1.9.1. This last will fail as well, because of how the
1133
-	// last node in the sequence is expressed (This is true for IPAM handle only, because of the broadcast
1134
-	// address reservation: last bit). This will allow an application using bitseq that runs a consistency
1135
-	// check to detect and replace the 1.9.0/1 old vulnerable handle with the new one.
1136
-	input := []*Handle{
1137
-		{
1138
-			id:         "LocalDefault/172.17.0.0/16",
1139
-			bits:       65536,
1140
-			unselected: 65412,
1141
-			head: &sequence{
1142
-				block: 0xffffffff,
1143
-				count: 3,
1144
-				next: &sequence{
1145
-					block: 0xffffffbf,
1146
-					count: 0,
1147
-					next: &sequence{
1148
-						block: 0xfe98816e,
1149
-						count: 1,
1150
-						next: &sequence{
1151
-							block: 0xffffffff,
1152
-							count: 0,
1153
-							next: &sequence{
1154
-								block: 0xe3bc0000,
1155
-								count: 1,
1156
-								next: &sequence{
1157
-									block: 0x0,
1158
-									count: 2042,
1159
-									next: &sequence{
1160
-										block: 0x1, count: 1,
1161
-										next: &sequence{
1162
-											block: 0x0, count: 0,
1163
-										},
1164
-									},
1165
-								},
1166
-							},
1167
-						},
1168
-					},
1169
-				},
1170
-			},
1171
-		},
1172
-		{
1173
-			id:         "LocalDefault/172.17.0.0/16",
1174
-			bits:       65536,
1175
-			unselected: 65319,
1176
-			head: &sequence{
1177
-				block: 0xffffffff,
1178
-				count: 7,
1179
-				next: &sequence{
1180
-					block: 0xffffff7f,
1181
-					count: 0,
1182
-					next: &sequence{
1183
-						block: 0xffffffff,
1184
-						count: 0,
1185
-						next: &sequence{
1186
-							block: 0x2000000,
1187
-							count: 1,
1188
-							next: &sequence{
1189
-								block: 0x0,
1190
-								count: 2039,
1191
-								next: &sequence{
1192
-									block: 0x1,
1193
-									count: 1,
1194
-									next: &sequence{
1195
-										block: 0x0,
1196
-										count: 0,
1197
-									},
1198
-								},
1199
-							},
1200
-						},
1201
-					},
1202
-				},
1203
-			},
1204
-		},
1205
-		{
1206
-			id:         "LocalDefault/172.17.0.0/16",
1207
-			bits:       65536,
1208
-			unselected: 65456,
1209
-			head: &sequence{
1210
-				block: 0xffffffff, count: 2,
1211
-				next: &sequence{
1212
-					block: 0xfffbffff, count: 0,
1213
-					next: &sequence{
1214
-						block: 0xffd07000, count: 1,
1215
-						next: &sequence{
1216
-							block: 0x0, count: 333,
1217
-							next: &sequence{
1218
-								block: 0x40000000, count: 1,
1219
-								next: &sequence{
1220
-									block: 0x0, count: 1710,
1221
-									next: &sequence{
1222
-										block: 0x1, count: 1,
1223
-										next: &sequence{
1224
-											block: 0x0, count: 0,
1225
-										},
1226
-									},
1227
-								},
1228
-							},
1229
-						},
1230
-					},
1231
-				},
1232
-			},
1233
-		},
1118
+	_ = hnd.Set(1023)
1119
+	if err := hnd.CheckConsistency(); err != nil {
1120
+		t.Fatal(err)
1234 1121
 	}
1235 1122
 
1236
-	for idx, hnd := range input {
1237
-		if !hnd.runConsistencyCheck() {
1238
-			t.Fatalf("Expected corrupted for (%d): %s", idx, hnd)
1239
-		}
1240
-		if hnd.runConsistencyCheck() {
1241
-			t.Fatalf("Sequence still marked corrupted (%d): %s", idx, hnd)
1242
-		}
1123
+	if err := hnd.CheckConsistency(); err != nil {
1124
+		t.Fatal(err)
1243 1125
 	}
1244 1126
 }
1245 1127
 
... ...
@@ -1264,15 +232,15 @@ func testSetRollover(t *testing.T, serial bool) {
1264 1264
 		}
1265 1265
 	}
1266 1266
 
1267
-	if hnd.Unselected() != uint64(numBits/2) {
1268
-		t.Fatalf("Expected full sequence. Instead found %d free bits. %s", hnd.unselected, hnd)
1267
+	if unselected := hnd.Unselected(); unselected != uint64(numBits/2) {
1268
+		t.Fatalf("Expected full sequence. Instead found %d free bits. %s", unselected, hnd)
1269 1269
 	}
1270 1270
 
1271 1271
 	seed := time.Now().Unix()
1272
-	rand.Seed(seed)
1272
+	rng := rand.New(rand.NewSource(seed))
1273 1273
 
1274 1274
 	// Deallocate half of the allocated bits following a random pattern
1275
-	pattern := rand.Perm(numBits / 2)
1275
+	pattern := rng.Perm(numBits / 2)
1276 1276
 	for i := 0; i < numBits/4; i++ {
1277 1277
 		bit := pattern[i]
1278 1278
 		err := hnd.Unset(uint64(bit))
... ...
@@ -1280,8 +248,8 @@ func testSetRollover(t *testing.T, serial bool) {
1280 1280
 			t.Fatalf("Unexpected failure on deallocation of %d: %v.\nSeed: %d.\n%s", bit, err, seed, hnd)
1281 1281
 		}
1282 1282
 	}
1283
-	if hnd.Unselected() != uint64(3*numBits/4) {
1284
-		t.Fatalf("Unexpected free bits: found %d free bits.\nSeed: %d.\n%s", hnd.unselected, seed, hnd)
1283
+	if unselected := hnd.Unselected(); unselected != uint64(3*numBits/4) {
1284
+		t.Fatalf("Unexpected free bits: found %d free bits.\nSeed: %d.\n%s", unselected, seed, hnd)
1285 1285
 	}
1286 1286
 
1287 1287
 	//request to allocate for remaining half of the bits
... ...
@@ -1294,8 +262,8 @@ func testSetRollover(t *testing.T, serial bool) {
1294 1294
 
1295 1295
 	//At this point all the bits must be allocated except the randomly unallocated bits
1296 1296
 	//which were unallocated in the first half of the bit sequence
1297
-	if hnd.Unselected() != uint64(numBits/4) {
1298
-		t.Fatalf("Unexpected number of unselected bits %d, Expected %d", hnd.Unselected(), numBits/4)
1297
+	if unselected := hnd.Unselected(); unselected != uint64(numBits/4) {
1298
+		t.Fatalf("Unexpected number of unselected bits %d, Expected %d", unselected, numBits/4)
1299 1299
 	}
1300 1300
 
1301 1301
 	for i := 0; i < numBits/4; i++ {
... ...
@@ -1324,42 +292,6 @@ func TestSetRolloverSerial(t *testing.T) {
1324 1324
 	testSetRollover(t, true)
1325 1325
 }
1326 1326
 
1327
-func TestGetFirstAvailableFromCurrent(t *testing.T) {
1328
-	input := []struct {
1329
-		mask    *sequence
1330
-		bytePos uint64
1331
-		bitPos  uint64
1332
-		start   uint64
1333
-		curr    uint64
1334
-		end     uint64
1335
-	}{
1336
-		{&sequence{block: 0xffffffff, count: 2048}, invalidPos, invalidPos, 0, 0, 65536},
1337
-		{&sequence{block: 0x0, count: 8}, 0, 0, 0, 0, 256},
1338
-		{&sequence{block: 0x80000000, count: 8}, 1, 0, 0, 8, 256},
1339
-		{&sequence{block: 0xC0000000, count: 8}, 0, 2, 0, 2, 256},
1340
-		{&sequence{block: 0xE0000000, count: 8}, 0, 3, 0, 0, 256},
1341
-		{&sequence{block: 0xFFFB1FFF, count: 8}, 2, 0, 14, 0, 256},
1342
-		{&sequence{block: 0xFFFFFFFE, count: 8}, 3, 7, 0, 0, 256},
1343
-
1344
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0x00000000, count: 1, next: &sequence{block: 0xffffffff, count: 14}}}, 4, 0, 0, 32, 512},
1345
-		{&sequence{block: 0xfffeffff, count: 1, next: &sequence{block: 0xffffffff, count: 15}}, 1, 7, 0, 16, 512},
1346
-		{&sequence{block: 0xfffeffff, count: 15, next: &sequence{block: 0xffffffff, count: 1}}, 5, 7, 0, 16, 512},
1347
-		{&sequence{block: 0xfffeffff, count: 15, next: &sequence{block: 0xffffffff, count: 1}}, 9, 7, 0, 48, 512},
1348
-		{&sequence{block: 0xffffffff, count: 2, next: &sequence{block: 0xffffffef, count: 14}}, 19, 3, 0, 124, 512},
1349
-		{&sequence{block: 0xfffeffff, count: 15, next: &sequence{block: 0x0fffffff, count: 1}}, 60, 0, 0, 480, 512},
1350
-		{&sequence{block: 0xffffffff, count: 1, next: &sequence{block: 0xfffeffff, count: 14, next: &sequence{block: 0xffffffff, count: 1}}}, 17, 7, 0, 124, 512},
1351
-		{&sequence{block: 0xfffffffb, count: 1, next: &sequence{block: 0xffffffff, count: 14, next: &sequence{block: 0xffffffff, count: 1}}}, 3, 5, 0, 124, 512},
1352
-		{&sequence{block: 0xfffffffb, count: 1, next: &sequence{block: 0xfffeffff, count: 14, next: &sequence{block: 0xffffffff, count: 1}}}, 13, 7, 0, 80, 512},
1353
-	}
1354
-
1355
-	for n, i := range input {
1356
-		bytePos, bitPos, _ := getAvailableFromCurrent(i.mask, i.start, i.curr, i.end)
1357
-		if bytePos != i.bytePos || bitPos != i.bitPos {
1358
-			t.Fatalf("Error in (%d) getFirstAvailable(). Expected (%d, %d). Got (%d, %d)", n, i.bytePos, i.bitPos, bytePos, bitPos)
1359
-		}
1360
-	}
1361
-}
1362
-
1363 1327
 func TestMarshalJSON(t *testing.T) {
1364 1328
 	const expectedID = "my-bitseq"
1365 1329
 	expected := []byte("hello libnetwork")
... ...
@@ -3,6 +3,7 @@ package bitseq
3 3
 import (
4 4
 	"encoding/json"
5 5
 
6
+	"github.com/docker/docker/libnetwork/bitmap"
6 7
 	"github.com/docker/docker/libnetwork/datastore"
7 8
 	"github.com/docker/docker/libnetwork/types"
8 9
 )
... ...
@@ -78,16 +79,13 @@ func (h *Handle) CopyTo(o datastore.KVObject) error {
78 78
 		return nil
79 79
 	}
80 80
 	dstH.Lock()
81
-	dstH.bits = h.bits
82
-	dstH.unselected = h.unselected
83
-	dstH.head = h.head.getCopy()
81
+	defer dstH.Unlock()
82
+	dstH.bm = bitmap.Copy(h.bm)
84 83
 	dstH.app = h.app
85 84
 	dstH.id = h.id
86 85
 	dstH.dbIndex = h.dbIndex
87 86
 	dstH.dbExists = h.dbExists
88 87
 	dstH.store = h.store
89
-	dstH.curr = h.curr
90
-	dstH.Unlock()
91 88
 
92 89
 	return nil
93 90
 }