-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.rb
executable file
·81 lines (73 loc) · 2.22 KB
/
db.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
#!/usr/bin/env ruby
require 'active_record'
ActiveRecord::Base.establish_connection(adapter: 'postgresql', database: 'mem_analysis')
def connection
ActiveRecord::Base.connection
end
class SpaceObject < ActiveRecord::Base
self.inheritance_column = 'zoink' # use type as ordinary column (not STI)
has_many :references, class_name: 'SpaceObjectReference', foreign_key: 'from_id', inverse_of: 'from', dependent: :destroy
has_one :default, class_name: 'SpaceObject', foreign_key: 'default', primary_key: 'address'
end
class SpaceObjectReference < ActiveRecord::Base
belongs_to :from, class_name: 'SpaceObject', optional: false, inverse_of: 'references'
belongs_to :to, class_name: 'SpaceObject', foreign_key: 'to_address', primary_key: 'address'
end
def init_database(c = connection)
c.tables.each { |t| c.drop_table(t) }
c.create_table 'space_objects' do |t|
t.datetime :time
t.string :type
t.string :node_type
t.string :root
t.string :address
t.text :value
t.string :klass
t.string :name
t.string :struct
t.string :file
t.string :line
t.string :method
t.integer :generation
t.integer :size
t.integer :length
t.integer :memsize
t.integer :bytesize
t.integer :capacity
t.integer :ivars
t.integer :fd
t.string :encoding
t.string :default_address
t.boolean :freezed
t.boolean :fstring
t.boolean :embedded
t.boolean :shared
t.boolean :flag_wb_protected
t.boolean :flag_old
t.boolean :flag_long_lived
t.boolean :flag_marking
t.boolean :flag_marked
end
c.create_table 'space_object_references' do |t|
t.integer :from_id, null: false
t.string :to_address, null: false
end
restore_indexes
nil
end
def remove_indexes(c = connection)
c.indexes('space_objects').each { |i| connection.remove_index('space_objects', name: i.name) }
c.indexes('space_objects_references').each { |i| connection.remove_index('space_objects_references', name: i.name) }
end
def restore_indexes(c = connection)
c.change_table 'space_objects' do |t|
t.index :time
t.index :address
t.index :type
t.index %i[klass method]
t.index %i[file line]
t.index :size
t.index :memsize
end
c.execute('VACUUM ANALYZE')
end