Class: Redis::Bitops::Bitmap

Inherits:
Object
  • Object
show all
Includes:
Queries, TreeBuildingHelpers
Defined in:
lib/redis/bitops/bitmap.rb

Overview

A sparse bitmap using multiple key to store its data to save memory.

Note: When adding new public methods, revise the LazyEvaluation module.

Direct Known Subclasses

SparseBitmap

Instance Method Summary collapse

Constructor Details

#initialize(root_key, redis) ⇒ Bitmap

Creates a new regular Redis bitmap stored in ‘redis’ under ‘root_key’.



15
16
17
18
# File 'lib/redis/bitops/bitmap.rb', line 15

def initialize(root_key, redis)
  @redis = redis
  @root_key = root_key
end

Instance Method Details

#<<(query) ⇒ Object

Saves the result of the query in the bitmap.



22
23
24
# File 'lib/redis/bitops/bitmap.rb', line 22

def << (query)
  query.evaluate(self)
end

#[](pos) ⇒ Object

Reads bit at position ‘pos’ returning a boolean.



28
29
30
# File 'lib/redis/bitops/bitmap.rb', line 28

def [] (pos)
  i2b(@redis.getbit(key(pos), offset(pos)))
end

#[]=(pos, b) ⇒ Object

Sets bit at position ‘pos’ to 1 or 0 based on the boolean ‘b’.



34
35
36
# File 'lib/redis/bitops/bitmap.rb', line 34

def []= (pos, b)
  @redis.setbit(key(pos), offset(pos), b2i(b))
end

#bitcountObject

Returns the number of set bits.



40
41
42
# File 'lib/redis/bitops/bitmap.rb', line 40

def bitcount
  @redis.bitcount(@root_key)
end

#bitmap_factoryObject

Returns lambda creating Bitmap objects using @redis as the connection.



66
67
68
# File 'lib/redis/bitops/bitmap.rb', line 66

def bitmap_factory
  lambda { |key| @redis.bitmap(key) }
end

#bitop(op, *operands, result) ⇒ Object

Redis BITOP operator ‘op’ (one of :and, :or, :xor or :not) on operands (bitmaps). The result is stored in ‘result’.



53
54
55
56
# File 'lib/redis/bitops/bitmap.rb', line 53

def bitop(op, *operands, result)
  @redis.bitop(op, result.root_key, self.root_key, *operands.map(&:root_key))
  result
end

#copy_to(dest) ⇒ Object

Copy this bitmap to ‘dest’ bitmap.



72
73
74
# File 'lib/redis/bitops/bitmap.rb', line 72

def copy_to(dest)
  copy(root_key, dest.root_key)
end

#delete!Object

Deletes the bitmap and all its keys.



46
47
48
# File 'lib/redis/bitops/bitmap.rb', line 46

def delete!
  @redis.del(@root_key)
end

#root_keyObject

The key the bitmap is stored under.



60
61
62
# File 'lib/redis/bitops/bitmap.rb', line 60

def root_key
  @root_key
end