Skip to content

Commit 62b6ea1

Browse files
committed
fix(oid): freeze spatial oid
Rails is moving towards ractor compatibility, and to do so it swapped to frozen database objects. Hence we cannot simply memoize the factories anymore. I've checked the initialization speed for factories, and the memoization was too fast for it to be worth implementing a cache. See rails/rails@d2da816 See rails/rails@76448a0
1 parent 8891c5e commit 62b6ea1

File tree

3 files changed

+9
-18
lines changed

3 files changed

+9
-18
lines changed

lib/active_record/connection_adapters/cockroachdb/oid/spatial.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ class Spatial < Type::Value
2828
def initialize(oid, sql_type)
2929
@sql_type = sql_type
3030
@geo_type, @srid, @has_z, @has_m = self.class.parse_sql_type(sql_type)
31+
@spatial_factory =
32+
RGeo::ActiveRecord::SpatialFactoryStore.instance.factory(
33+
factory_attrs
34+
)
3135
end
3236

3337
# sql_type: geometry, geometry(Point), geometry(Point,4326), ...
@@ -59,13 +63,6 @@ def self.parse_sql_type(sql_type)
5963
[geo_type, srid, has_z, has_m]
6064
end
6165

62-
def spatial_factory
63-
@spatial_factory ||=
64-
RGeo::ActiveRecord::SpatialFactoryStore.instance.factory(
65-
factory_attrs
66-
)
67-
end
68-
6966
def geographic?
7067
@sql_type =~ /geography/
7168
end
@@ -108,14 +105,14 @@ def parse_wkt(string)
108105
end
109106

110107
def binary_string?(string)
111-
string[0] == "\x00" || string[0] == "\x01" || string[0, 4] =~ /[0-9a-fA-F]{4}/
108+
string[0] == "\x00" || string[0] == "\x01" || string[0, 4].match?(/[0-9a-fA-F]{4}/)
112109
end
113110

114111
def wkt_parser(string)
115112
if binary_string?(string)
116-
RGeo::WKRep::WKBParser.new(spatial_factory, support_ewkb: true, default_srid: @srid)
113+
RGeo::WKRep::WKBParser.new(@spatial_factory, support_ewkb: true, default_srid: @srid)
117114
else
118-
RGeo::WKRep::WKTParser.new(spatial_factory, support_ewkt: true, default_srid: @srid)
115+
RGeo::WKRep::WKTParser.new(@spatial_factory, support_ewkt: true, default_srid: @srid)
119116
end
120117
end
121118

lib/active_record/connection_adapters/cockroachdb_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ def initialize_type_map(m = type_map)
299299
st_polygon
300300
).each do |geo_type|
301301
m.register_type(geo_type) do |oid, _, sql_type|
302-
CockroachDB::OID::Spatial.new(oid, sql_type)
302+
CockroachDB::OID::Spatial.new(oid, sql_type).freeze
303303
end
304304
end
305305

test/cases/adapters/postgresql/postgis_test.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,6 @@ def reset_memoized_spatial_factories
185185
# necessary to reset the @spatial_factory variable on spatial
186186
# OIDs, otherwise the results of early tests will be memoized
187187
# since the table is not dropped and recreated between test cases.
188-
ObjectSpace.each_object(spatial_oid) do |oid|
189-
oid.instance_variable_set(:@spatial_factory, nil)
190-
end
191-
end
192-
193-
def spatial_oid
194-
ActiveRecord::ConnectionAdapters::CockroachDB::OID::Spatial
188+
klass.lease_connection.send(:reload_type_map)
195189
end
196190
end

0 commit comments

Comments
 (0)