Class: PureCDB::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/purecdb/base.rb

Overview

Base class with shared functionality for PureCDB::Reader and PureCDB::Writer that abstracts away 32 bit vs. 64 bit format details. You should not need to use this directly.

Changing the constants defined here is likely to break all kinds of stuff.

Direct Known Subclasses

Reader, Writer

Constant Summary collapse

DEFAULT_NUM_HASHES =

The CDB format contains 256 separate hashes by default.

256
DEFAULT_LENGTH_SIZE =

Keys and values have a length indicator. In the standard format this is 4 bytes long. In the 64 bit format, this value is multiplied by 2.

4
DEFAULT_HASHPTR_SIZE =

The “pointer” (offset) pointing to a given hash is this many bytes by default. In the 64 bit format this is multiplied by 2.

4
CDB64_MAGIC =

Magic cookied used to indicate that this is a 64 bit (non-standard) CDB file rather than a 32-bit CDB file.

"cdb64:01"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*options) ⇒ Base

Parses options and sets mode. Do not call directly - Use PureCDB::Reader or PureCDB::Writer



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/purecdb/base.rb', line 72

def initialize *options
  mode = :detect
  options.each do |h|
    h.each do |opt,val|
      mode = val
    end
  end

  if mode == :detect
    @mode = :detect
  else
    set_mode(mode)
  end
end

Instance Attribute Details

#hashptr_sizeObject (readonly)

The actual number of bytes per hash pointer (depends on 32 vs 64 bit)



35
36
37
# File 'lib/purecdb/base.rb', line 35

def hashptr_size
  @hashptr_size
end

#length_sizeObject (readonly)

The actual number of bytes per length field (depends on 32 vs 64 bit)



32
33
34
# File 'lib/purecdb/base.rb', line 32

def length_size
  @length_size
end

#modeObject (readonly)

32 for 32-bit files, 64 for 64-bit files.



38
39
40
# File 'lib/purecdb/base.rb', line 38

def mode
  @mode
end

#num_hashesObject (readonly)

The actual number of hashes (depends on 32 vs 64 bit)



29
30
31
# File 'lib/purecdb/base.rb', line 29

def num_hashes
  @num_hashes
end

Instance Method Details

#hash(key) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/purecdb/base.rb', line 101

def hash key
  h = 5381 # Magic
  key.to_s.each_byte do |c|
    # We & it since Ruby uses big ints,
    # so it can't overflow and need to be clamped.

    # FIXME: For 64 bit version we use 64 bit numbers in the slots, so could increase this.
    h = (((h << 5) + h) ^ c) & 0xffffffff
  end
  h
end

#hash_sizeObject

The size of the table of pointers to the hashes



46
47
48
# File 'lib/purecdb/base.rb', line 46

def hash_size
  hashref_size * num_hashes
end

#hashref_sizeObject

The size of each hash slot



41
42
43
# File 'lib/purecdb/base.rb', line 41

def hashref_size
  hashptr_size + length_size
end

#set_mode(mode) ⇒ Object

Used by PureCDB::Reader and PureCDB::Writer to set 32/64 bit mode



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/purecdb/base.rb', line 53

def set_mode mode
  @mode = mode
  @num_hashes = DEFAULT_NUM_HASHES

  if @mode == 64
    @length_size  = DEFAULT_LENGTH_SIZE  * 2
    @hashptr_size = DEFAULT_HASHPTR_SIZE * 2
    @format = "Q<"
  else
    @length_size  = DEFAULT_LENGTH_SIZE
    @hashptr_size = DEFAULT_HASHPTR_SIZE
    @format = "V"
  end
end

#set_stream(target) ⇒ Object

Used by PureCDB::Reader and PureCDB::Writer to set an IO-like object to read from/write to



90
91
92
93
94
95
96
97
# File 'lib/purecdb/base.rb', line 90

def set_stream target
  if target.respond_to?(:sysseek)
    @io = target
  else
    @io = SysIOWrapper.new(target)
  end
  @name = "<stream>"
end