Class: KBIndex

Inherits:
Object show all
Includes:
KBEncryptionMixin, KBTypeConversionsMixin
Defined in:
lib/kirbybase.rb

Overview


KBIndex


Constant Summary

Constants included from KBEncryptionMixin

KBEncryptionMixin::EN_KEY, KBEncryptionMixin::EN_KEY1, KBEncryptionMixin::EN_KEY_LEN, KBEncryptionMixin::EN_STR, KBEncryptionMixin::EN_STR_LEN

Constants included from KBTypeConversionsMixin

KBTypeConversionsMixin::ENCODE_RE, KBTypeConversionsMixin::KB_NIL, KBTypeConversionsMixin::UNENCODE_RE

Instance Method Summary collapse

Methods included from KBEncryptionMixin

#encrypt_str, #unencrypt_str

Methods included from KBTypeConversionsMixin

#convert_to_encoded_string, #convert_to_native_type

Constructor Details

#initialize(table, index_fields) ⇒ KBIndex


initialize




3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
# File 'lib/kirbybase.rb', line 3421

def initialize(table, index_fields)
    @last_update = Time.new
    @idx_arr = []
    @table = table
    @index_fields = index_fields
    @col_poss = index_fields.collect {|i| table.field_names.index(i) }
    @col_names = index_fields
    @col_types = index_fields.collect {|i|
     table.field_types[table.field_names.index(i)]}
end

Instance Method Details

#add_index_rec(rec) ⇒ Object


add_index_rec




3483
3484
3485
# File 'lib/kirbybase.rb', line 3483

def add_index_rec(rec)
    @last_upddate = Time.new if append_new_rec_to_index_array(rec)
end

#append_new_rec_to_index_array(rec) ⇒ Object


append_new_rec_to_index_array




3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
# File 'lib/kirbybase.rb', line 3507

def append_new_rec_to_index_array(rec)
    idx_rec = []
    @col_poss.zip(@col_types).each do |col_pos, col_type|
        idx_rec << convert_to_native_type(col_type, rec[col_pos])
     end

    return false if idx_rec.uniq == [kb_nil]

    idx_rec << rec.first.to_i
    @idx_arr << idx_rec
    return true
end

#delete_index_rec(recno) ⇒ Object


delete_index_rec




3490
3491
3492
3493
3494
# File 'lib/kirbybase.rb', line 3490

def delete_index_rec(recno)
    i = @idx_arr.rassoc(recno.to_i)
    @idx_arr.delete_at(@idx_arr.index(i)) unless i.nil?
    @last_update = Time.new
end

#get_idxObject


get_idx




3435
3436
3437
# File 'lib/kirbybase.rb', line 3435

def get_idx
    return @idx_arr
end

#get_timestampObject


get_timestamp




3442
3443
3444
# File 'lib/kirbybase.rb', line 3442

def get_timestamp
    return @last_update
end

#rebuild(fptr) ⇒ Object


rebuild




3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
# File 'lib/kirbybase.rb', line 3449

def rebuild(fptr)
    @idx_arr.clear

    encrypted = @table.encrypted?

    # Skip header rec.
    fptr.readline

    begin
        # Loop through table.
        while true
            line = fptr.readline

            line = unencrypt_str(line) if encrypted
            line.strip!

            # If blank line (i.e. 'deleted'), skip it.
            next if line == ''

            # Split the line up into fields.
            rec = line.split('|', @col_poss.max+2)

            append_new_rec_to_index_array(rec)
        end
    # Here's how we break out of the loop...
    rescue EOFError
    end

    @last_update = Time.new
end

#update_index_rec(rec) ⇒ Object


update_index_rec




3499
3500
3501
3502
# File 'lib/kirbybase.rb', line 3499

def update_index_rec(rec)
    delete_index_rec(rec.first.to_i)
    add_index_rec(rec)
end