@@ -18,7 +18,6 @@ package table
18
18
19
19
import (
20
20
"crypto/aes"
21
- "encoding/binary"
22
21
"fmt"
23
22
"io"
24
23
"math"
@@ -82,7 +81,7 @@ type TableInterface interface {
82
81
DoesNotHave (hash uint64 ) bool
83
82
}
84
83
85
- // Table represents a loaded table file with the info we have about it.
84
+ // Table represents a loaded table file with the info we have about it
86
85
type Table struct {
87
86
sync.Mutex
88
87
@@ -98,11 +97,10 @@ type Table struct {
98
97
smallest , biggest []byte // Smallest and largest keys (with timestamps).
99
98
id uint64 // file id, part of filename
100
99
100
+ bf * z.Bloom
101
101
Checksum []byte
102
102
// Stores the total size of key-values stored in this table (including the size on vlog).
103
103
estimatedSize uint64
104
- indexStart int
105
- indexLen int
106
104
107
105
IsInmemory bool // Set to true if the table is on level 0 and opened in memory.
108
106
opt * Options
@@ -148,13 +146,6 @@ func (t *Table) DecrRef() error {
148
146
if err := os .Remove (filename ); err != nil {
149
147
return err
150
148
}
151
- // Delete all blocks from the cache.
152
- for i := range t .blockIndex {
153
- t .opt .Cache .Del (t .blockCacheKey (i ))
154
- }
155
- // Delete bloom filter from the cache.
156
- t .opt .Cache .Del (t .bfCacheKey ())
157
-
158
149
}
159
150
return nil
160
151
}
@@ -345,12 +336,10 @@ func (t *Table) readIndex() error {
345
336
// Read index size from the footer.
346
337
readPos -= 4
347
338
buf = t .readNoFail (readPos , 4 )
348
- t .indexLen = int (y .BytesToU32 (buf ))
349
-
339
+ indexLen := int (y .BytesToU32 (buf ))
350
340
// Read index.
351
- readPos -= t .indexLen
352
- t .indexStart = readPos
353
- data := t .readNoFail (readPos , t .indexLen )
341
+ readPos -= indexLen
342
+ data := t .readNoFail (readPos , indexLen )
354
343
355
344
if err := y .VerifyChecksum (data , expectedChk ); err != nil {
356
345
return y .Wrapf (err , "failed to verify checksum for table: %s" , t .Filename ())
@@ -369,18 +358,11 @@ func (t *Table) readIndex() error {
369
358
y .Check (err )
370
359
371
360
t .estimatedSize = index .EstimatedSize
372
- t .blockIndex = index .Offsets
373
-
374
- // Avoid the cost of unmarshalling the bloom filters if the cache is absent.
375
- if t .opt .Cache != nil {
376
- var bf * z.Bloom
377
- if bf , err = z .JSONUnmarshal (index .BloomFilter ); err != nil {
378
- return y .Wrapf (err , "failed to unmarshal bloom filter for the table %d in Table.readIndex" ,
379
- t .id )
380
- }
381
-
382
- t .opt .Cache .Set (t .bfCacheKey (), bf , int64 (len (index .BloomFilter )))
361
+ if t .bf , err = z .JSONUnmarshal (index .BloomFilter ); err != nil {
362
+ return y .Wrapf (err , "failed to unmarshal bloom filter for the table %d in Table.readIndex" ,
363
+ t .id )
383
364
}
365
+ t .blockIndex = index .Offsets
384
366
return nil
385
367
}
386
368
@@ -461,25 +443,10 @@ func (t *Table) block(idx int) (*block, error) {
461
443
return blk , nil
462
444
}
463
445
464
- // bfCacheKey returns the cache key for bloom filter.
465
- func (t * Table ) bfCacheKey () []byte {
466
- y .AssertTrue (t .id < math .MaxUint32 )
467
- buf := make ([]byte , 4 )
468
- binary .BigEndian .PutUint32 (buf , uint32 (t .id ))
469
-
470
- // Without the "bf" prefix, we will have conflict with the blockCacheKey.
471
- return append ([]byte ("bf" ), buf ... )
472
- }
473
-
474
- func (t * Table ) blockCacheKey (idx int ) []byte {
475
- y .AssertTrue (t .id < math .MaxUint32 )
446
+ func (t * Table ) blockCacheKey (idx int ) uint64 {
447
+ y .AssertTrue (t .ID () < math .MaxUint32 )
476
448
y .AssertTrue (uint32 (idx ) < math .MaxUint32 )
477
-
478
- buf := make ([]byte , 8 )
479
- // Assume t.ID does not overflow uint32.
480
- binary .BigEndian .PutUint32 (buf [:4 ], uint32 (t .ID ()))
481
- binary .BigEndian .PutUint32 (buf [4 :], uint32 (idx ))
482
- return buf
449
+ return (t .ID () << 32 ) | uint64 (idx )
483
450
}
484
451
485
452
// EstimatedSize returns the total size of key-values stored in this table (including the
@@ -503,44 +470,7 @@ func (t *Table) ID() uint64 { return t.id }
503
470
504
471
// DoesNotHave returns true if (but not "only if") the table does not have the key hash.
505
472
// It does a bloom filter lookup.
506
- func (t * Table ) DoesNotHave (hash uint64 ) bool {
507
- var bf * z.Bloom
508
-
509
- // Return fast if cache is absent.
510
- if t .opt .Cache == nil {
511
- bf , _ := t .readBloomFilter ()
512
- return ! bf .Has (hash )
513
- }
514
-
515
- // Check if the bloomfilter exists in the cache.
516
- if b , ok := t .opt .Cache .Get (t .bfCacheKey ()); b != nil && ok {
517
- bf = b .(* z.Bloom )
518
- return ! bf .Has (hash )
519
- }
520
-
521
- bf , sz := t .readBloomFilter ()
522
- t .opt .Cache .Set (t .bfCacheKey (), bf , int64 (sz ))
523
- return ! bf .Has (hash )
524
- }
525
-
526
- // readBloomFilter reads the bloom filter from the SST and returns its length
527
- // along with the bloom filter.
528
- func (t * Table ) readBloomFilter () (* z.Bloom , int ) {
529
- // Read bloom filter from the SST.
530
- data := t .readNoFail (t .indexStart , t .indexLen )
531
- index := pb.TableIndex {}
532
- var err error
533
- // Decrypt the table index if it is encrypted.
534
- if t .shouldDecrypt () {
535
- data , err = t .decrypt (data )
536
- y .Check (err )
537
- }
538
- y .Check (proto .Unmarshal (data , & index ))
539
-
540
- bf , err := z .JSONUnmarshal (index .BloomFilter )
541
- y .Check (err )
542
- return bf , len (index .BloomFilter )
543
- }
473
+ func (t * Table ) DoesNotHave (hash uint64 ) bool { return ! t .bf .Has (hash ) }
544
474
545
475
// VerifyChecksum verifies checksum for all blocks of table. This function is called by
546
476
// OpenTable() function. This function is also called inside levelsController.VerifyChecksum().
0 commit comments