Class: SlateDb::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/slatedb/reader.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.open(path, url: nil, checkpoint_id: nil, manifest_poll_interval: nil, checkpoint_lifetime: nil, max_memtable_bytes: nil, cache_root: nil, max_open_file_handles: nil, merge_operator: nil) {|reader| ... } ⇒ Reader

Open a read-only reader at the given path.

Examples:

Open a reader

reader = SlateDb::Reader.open("/tmp/mydb")
value = reader.get("key")
reader.close

Open with block (auto-close)

SlateDb::Reader.open("/tmp/mydb") do |reader|
  reader.get("key")
end # automatically closed

Open at a specific checkpoint

reader = SlateDb::Reader.open("/tmp/mydb", checkpoint_id: "uuid-here")

Enable the on-disk cache and cap its open file handles

reader = SlateDb::Reader.open("/tmp/mydb",
                              cache_root: "/var/cache/slatedb",
                              max_open_file_handles: 256)

Parameters:

  • path (String)

    The path identifier for the database

  • url (String, nil) (defaults to: nil)

    Optional object store URL

  • checkpoint_id (String, nil) (defaults to: nil)

    Optional checkpoint UUID to read at

  • manifest_poll_interval (Integer, nil) (defaults to: nil)

    Poll interval in milliseconds

  • checkpoint_lifetime (Integer, nil) (defaults to: nil)

    Checkpoint lifetime in milliseconds

  • max_memtable_bytes (Integer, nil) (defaults to: nil)

    Maximum memtable size in bytes

  • cache_root (String, nil) (defaults to: nil)

    Root folder for the reader’s local on-disk object-store cache. Setting this enables the cached object store; when it is not set the cache (and ‘max_open_file_handles`) has no effect.

  • max_open_file_handles (Integer, nil) (defaults to: nil)

    Maximum number of file handles to keep open in the reader’s file-handle cache. When the limit is reached, the least recently used handle is closed (default: 1000). Only takes effect when ‘cache_root` is set. (Requires SlateDB >= 0.13.0)

  • merge_operator (Symbol, String, nil) (defaults to: nil)

    Optional merge operator (“string_concat” or “concat”)

Yields:

  • (reader)

    If a block is given, yields the reader and ensures it’s closed

Returns:

  • (Reader)

    The opened reader (or block result if block given)



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/slatedb/reader.rb', line 43

def open(path, url: nil, checkpoint_id: nil,
         manifest_poll_interval: nil, checkpoint_lifetime: nil,
         max_memtable_bytes: nil, cache_root: nil, max_open_file_handles: nil,
         merge_operator: nil)
  opts = {}
  opts[:manifest_poll_interval] = manifest_poll_interval if manifest_poll_interval
  opts[:checkpoint_lifetime] = checkpoint_lifetime if checkpoint_lifetime
  opts[:max_memtable_bytes] = max_memtable_bytes if max_memtable_bytes
  opts[:cache_root] = cache_root if cache_root
  opts[:max_open_file_handles] = max_open_file_handles if max_open_file_handles
  opts[:merge_operator] = merge_operator.to_s if merge_operator

  reader = _open(path, url, checkpoint_id, opts)

  if block_given?
    begin
      yield reader
    ensure
      begin
        reader.close
      rescue StandardError
        nil
      end
    end
  else
    reader
  end
end

Instance Method Details

#get(key, durability_filter: nil, dirty: nil, cache_blocks: nil) ⇒ String?

Get a value by key.

Parameters:

  • key (String)

    The key to look up

  • durability_filter (String, nil) (defaults to: nil)

    Filter by durability level (“remote” or “memory”)

  • dirty (Boolean, nil) (defaults to: nil)

    Whether to include uncommitted data

  • cache_blocks (Boolean, nil) (defaults to: nil)

    Whether to cache blocks

Returns:

  • (String, nil)

    The value, or nil if not found



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/slatedb/reader.rb', line 81

def get(key, durability_filter: nil, dirty: nil, cache_blocks: nil)
  opts = {}
  opts[:durability_filter] = durability_filter.to_s if durability_filter
  opts[:dirty] = dirty unless dirty.nil?
  opts[:cache_blocks] = cache_blocks unless cache_blocks.nil?

  if opts.empty?
    _get(key)
  else
    _get_with_options(key, opts)
  end
end

#scan(start_key, end_key = nil, durability_filter: nil, dirty: nil, read_ahead_bytes: nil, cache_blocks: nil, max_fetch_tasks: nil) ⇒ Iterator

Scan a range of keys.

Parameters:

  • start_key (String)

    The start key (inclusive)

  • end_key (String, nil) (defaults to: nil)

    The end key (exclusive)

Returns:

  • (Iterator)

    An iterator over key-value pairs



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/slatedb/reader.rb', line 100

def scan(start_key, end_key = nil, durability_filter: nil, dirty: nil,
         read_ahead_bytes: nil, cache_blocks: nil, max_fetch_tasks: nil, &)
  opts = {}
  opts[:durability_filter] = durability_filter.to_s if durability_filter
  opts[:dirty] = dirty unless dirty.nil?
  opts[:read_ahead_bytes] = read_ahead_bytes if read_ahead_bytes
  opts[:cache_blocks] = cache_blocks unless cache_blocks.nil?
  opts[:max_fetch_tasks] = max_fetch_tasks if max_fetch_tasks

  iter = if opts.empty?
           _scan(start_key, end_key)
         else
           _scan_with_options(start_key, end_key, opts)
         end

  if block_given?
    iter.each(&)
  else
    iter
  end
end

#scan_prefix(prefix, durability_filter: nil, dirty: nil, read_ahead_bytes: nil, cache_blocks: nil, max_fetch_tasks: nil) ⇒ Iterator

Scan all keys with a given prefix.

Parameters:

  • prefix (String)

    The key prefix to scan

  • durability_filter (String, nil) (defaults to: nil)

    Filter by durability level

  • dirty (Boolean, nil) (defaults to: nil)

    Whether to include uncommitted data

  • read_ahead_bytes (Integer, nil) (defaults to: nil)

    Number of bytes to read ahead

  • cache_blocks (Boolean, nil) (defaults to: nil)

    Whether to cache blocks

  • max_fetch_tasks (Integer, nil) (defaults to: nil)

    Maximum number of fetch tasks

Returns:

  • (Iterator)

    An iterator over key-value pairs



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/slatedb/reader.rb', line 132

def scan_prefix(prefix, durability_filter: nil, dirty: nil,
                read_ahead_bytes: nil, cache_blocks: nil, max_fetch_tasks: nil, &)
  opts = {}
  opts[:durability_filter] = durability_filter.to_s if durability_filter
  opts[:dirty] = dirty unless dirty.nil?
  opts[:read_ahead_bytes] = read_ahead_bytes if read_ahead_bytes
  opts[:cache_blocks] = cache_blocks unless cache_blocks.nil?
  opts[:max_fetch_tasks] = max_fetch_tasks if max_fetch_tasks

  iter = if opts.empty?
           _scan_prefix(prefix)
         else
           _scan_prefix_with_options(prefix, opts)
         end

  if block_given?
    iter.each(&)
  else
    iter
  end
end