-
Notifications
You must be signed in to change notification settings - Fork 0
/
enum_implementations.rb
89 lines (69 loc) · 2.07 KB
/
enum_implementations.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
a = [[1, "foo"], [2, "bar"], [3, "baz"], [4, "buz"], [5, "ping"]]
b = [[1, "a", 1], [2, "b", 4], [3, "c", 2], [4, "d", 5], [5, "e", 5], [6, "f", 1], [7, "g", 3]]
b = [[1, "a", 1], [2, "b", 1], [3, "c", 1], [4, "d", 1], [5, "e", 1], [6, "f", 1], [7, "g", 1]]
condition = ->(x, y) { x[0] == y[2] }
out = []
b.each do |b_tuple|
a.each do |a_tuple|
out << [b_tuple, a_tuple] if condition.call(a_tuple, b_tuple)
end
end
Enumerator.new do |y|
b.each do |b_tuple|
a.each do |a_tuple|
y << [b_tuple, a_tuple] if condition.call(a_tuple, b_tuple)
end
end
end
class Relation
attr_reader :data, :fields
def self.from_csv(*args)
csv = CSV.open(*args)
enumerator = csv.each.map(&:to_h).map(&:values).to_enum
new(csv.headers, enumerator)
end
def self.from_db_file(fields, path, record_length, binary_template_string)
f = File.open(path, 'r')
enumerator = Enumerator.new do |yielder|
loop do
data = f.read(record_length)
if data
yielder << data.unpack(binary_template_string)
else
raise StopIteration
end
end
end
new(fields, enumerator)
end
def initialize(fields, data)
@fields = fields
@data = data
end
end
class Scan < Enumerator; end
class NestedLoopJoin < Enumerator
DEFAULT = -> (_, _) { true }
def initialize(outer, inner, &condition)
condition = DEFAULT unless block_given?
super() do |yielder|
outer.each do |o|
inner.each do |i|
if condition.call(o, i)
yielder << [o, i]
# don't finish the inner loop if we've found the match
break
end
end
end
end
end
end
# select * from ratings join movies on movies.movieId = ratings.movieId
movies = Relation.from_csv('movies.csv', headers: true)
ratings = Relation.from_csv('ratings_abbreviated.csv', headers: true)
movie_scanner = Scan.new(movies.data)
ratings_scanner = Scan.new(ratings.data)
joiner = Join.new(ratings_scanner, movie_scanner) do |rating, movie|
rating[:movieId] == movie[:movieId]
end