-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathdecoder.cr
543 lines (427 loc) · 9.66 KB
/
decoder.cr
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
require "json"
require "uuid"
module PG
alias PGValue = String | Nil | Bool | Int32 | Float32 | Float64 | Time | JSON::Any | PG::Numeric | UUID
# :nodoc:
module Decoders
module Decoder
abstract def decode(io, bytesize, oid)
abstract def oids : Array(Int32)
abstract def type
macro def_oids(oids)
OIDS = {{oids}}
def oids : Array(Int32)
OIDS
end
end
def read(io, type)
io.read_bytes(type, IO::ByteFormat::NetworkEndian)
end
def read_i16(io)
read(io, Int16)
end
def read_i32(io)
read(io, Int32)
end
def read_i64(io)
read(io, Int64)
end
def read_u32(io)
read(io, UInt32)
end
def read_u64(io)
read(io, UInt64)
end
def read_f32(io)
read(io, Float32)
end
def read_f64(io)
read(io, Float64)
end
end
struct StringDecoder
include Decoder
def_oids [
19, # name (internal type)
25, # text
142, # xml
705, # unknown
1042, # blchar
1043, # varchar
]
def decode(io, bytesize, oid)
String.new(bytesize) do |buffer|
io.read_fully(Slice.new(buffer, bytesize))
{bytesize, 0}
end
end
def type
String
end
end
struct UUIDDecoder
include Decoder
def_oids [
2950, # UUID
]
def decode(io, bytesize, oid)
bytes = uninitialized UInt8[16]
slice = Bytes.new(bytes.to_unsafe, 16)
io.read_fully slice
UUID.new(slice)
end
def type
UUID
end
end
struct CharDecoder
include Decoder
def_oids [
18, # "char" (internal type)
]
def decode(io, bytesize, oid)
# TODO: can be done without creating an intermediate string
String.new(bytesize) do |buffer|
io.read_fully(Slice.new(buffer, bytesize))
{bytesize, 0}
end[0]
end
def type
Char
end
end
struct BoolDecoder
include Decoder
OIDS = [
16, # bool
]
def decode(io, bytesize, oid)
case byte = io.read_byte
when 0
false
when 1
true
else
raise "bad boolean decode: #{byte}"
end
end
def oids : Array(Int32)
OIDS
end
def type
Bool
end
end
struct Int16Decoder
include Decoder
def_oids [
21, # int2 (smallint)
]
def decode(io, bytesize, oid)
read_i16(io)
end
def type
Int16
end
end
struct Int32Decoder
include Decoder
def_oids [
23, # int4 (integer)
2206, # regtype
]
def decode(io, bytesize, oid)
read_i32(io)
end
def type
Int32
end
end
struct Int64Decoder
include Decoder
def_oids [
20, # int8 (bigint)
]
def decode(io, bytesize, oid)
read_i64(io)
end
def type
Int64
end
end
struct UIntDecoder
include Decoder
def_oids [
26, # oid (internal type)
]
def decode(io, bytesize, oid)
read_u32(io)
end
def type
UInt32
end
end
struct UInt64Decoder
include Decoder
def_oids [
3220, # lsn
]
def decode(io, bytesize, oid)
read_u64(io)
end
def type
UInt64
end
end
struct Float32Decoder
include Decoder
def_oids [
700, # float4
]
def decode(io, bytesize, oid)
read_f32(io)
end
def type
Float32
end
end
struct Float64Decoder
include Decoder
def_oids [
701, # float8
]
def decode(io, bytesize, oid)
read_f64(io)
end
def type
Float64
end
end
struct PointDecoder
include Decoder
def_oids [
600, # point
]
def decode(io, bytesize, oid)
Geo::Point.new(read_f64(io), read_f64(io))
end
def type
Geo::Point
end
end
struct PathDecoder
include Decoder
def_oids [
602, # path
]
def decode(io, bytesize, oid)
byte = io.read_byte.not_nil!
closed = byte == 1_u8
Geo::Path.new(PolygonDecoder.new.decode(io, bytesize - 1, oid).points, closed)
end
def type
Geo::Path
end
end
struct PolygonDecoder
include Decoder
def_oids [
604, # polygon
]
def decode(io, bytesize, oid)
c = read_u32(io)
count = (pointerof(c).as(Int32*)).value
points = Array.new(count) do |i|
PointDecoder.new.decode(io, 16, oid)
end
Geo::Polygon.new(points)
end
def type
Geo::Polygon
end
end
struct BoxDecoder
include Decoder
def_oids [
603, # box
]
def decode(io, bytesize, oid)
x2, y2, x1, y1 = read_f64(io), read_f64(io), read_f64(io), read_f64(io)
Geo::Box.new(x1, y1, x2, y2)
end
def type
Geo::Box
end
end
struct LineSegmentDecoder
include Decoder
def_oids [
601, # lseg
]
def decode(io, bytesize, oid)
Geo::LineSegment.new(read_f64(io), read_f64(io), read_f64(io), read_f64(io))
end
def type
Geo::LineSegment
end
end
struct LineDecoder
include Decoder
def_oids [
628, # line
]
def decode(io, bytesize, oid)
Geo::Line.new(read_f64(io), read_f64(io), read_f64(io))
end
def type
Geo::Line
end
end
struct CircleDecoder
include Decoder
def_oids [
718, # circle
]
def decode(io, bytesize, oid)
Geo::Circle.new(read_f64(io), read_f64(io), read_f64(io))
end
def type
Geo::Circle
end
end
struct JsonDecoder
include Decoder
JSONB_OID = 3802
def_oids [
114, # json
JSONB_OID, # jsonb
]
def decode(io, bytesize, oid)
if oid == JSONB_OID
io.read_byte
bytesize -= 1
end
string = String.new(bytesize) do |buffer|
io.read_fully(Slice.new(buffer, bytesize))
{bytesize, 0}
end
JSON::PullParser.new(string)
end
def type
JSON::PullParser
end
end
struct TimeDecoder
include Decoder
enum OID
DATE = 1082
TIMESTAMP = 1114
TIMESTAMPTZ = 1184
end
JAN_1_2K = Time.utc(2000, 1, 1)
def_oids OID.values.map(&.value)
def decode(io, bytesize, oid)
case oid = OID.new(oid)
in .date?
JAN_1_2K + read_i32(io).days
in .timestamp?
JAN_1_2K + read_i64(io).microseconds
in .timestamptz?
time = JAN_1_2K + read_i64(io).microseconds
time.in io.connection.time_zone
end
end
def type
Time
end
end
struct IntervalDecoder
include Decoder
def_oids [
1186,
]
#
# An Interval consists of
# * time (8 bytes)
# * days (4 bytes)
# * months (4 bytes)
#
# See: https://github.com/postgres/postgres/blob/a094c8ff53523e88ff9dd28ad467618039e27b58/src/backend/utils/adt/timestamp.c#L1003
#
def decode(io, bytesize, oid)
microseconds = read_i64(io)
days = read_i32(io)
months = read_i32(io)
PG::Interval.new(microseconds, days, months)
end
def type
PG::Interval
end
end
struct ByteaDecoder
include Decoder
def_oids [
17, # bytea
]
def decode(io, bytesize, oid)
slice = Bytes.new(bytesize)
io.read_fully(slice)
slice
end
def type
Bytes
end
end
struct NumericDecoder
include Decoder
def_oids [
1700, # numeric
]
def decode(io, bytesize, oid)
ndigits = read_i16(io)
weight = read_i16(io)
sign = read_i16(io)
dscale = read_i16(io)
digits = (0...ndigits).map { |i| read_i16(io) }
PG::Numeric.new(ndigits, weight, sign, dscale, digits)
end
def type
PG::Numeric
end
end
@@decoders = Hash(Int32, PG::Decoders::Decoder).new(ByteaDecoder.new)
def self.from_oid(oid)
@@decoders[oid]
end
def self.register_decoder(decoder)
decoder.oids.each do |oid|
@@decoders[oid] = decoder
end
end
# https://github.com/postgres/postgres/blob/master/src/include/catalog/pg_type.h
register_decoder BoolDecoder.new
register_decoder ByteaDecoder.new
register_decoder CharDecoder.new
register_decoder StringDecoder.new
register_decoder UUIDDecoder.new
register_decoder Int16Decoder.new
register_decoder Int32Decoder.new
register_decoder Int64Decoder.new
register_decoder UIntDecoder.new
register_decoder UInt64Decoder.new
register_decoder JsonDecoder.new
register_decoder Float32Decoder.new
register_decoder Float64Decoder.new
register_decoder TimeDecoder.new
register_decoder IntervalDecoder.new
register_decoder NumericDecoder.new
register_decoder PointDecoder.new
register_decoder LineSegmentDecoder.new
register_decoder PathDecoder.new
register_decoder BoxDecoder.new
register_decoder PolygonDecoder.new
register_decoder LineDecoder.new
register_decoder CircleDecoder.new
end
end
require "./decoders/*"