Class: RDF::Borsh::Reader
- Inherits:
-
Reader
- Object
- Reader
- RDF::Borsh::Reader
- Defined in:
- lib/rdf/borsh/reader.rb
Constant Summary collapse
- MAGIC =
RDF::Borsh::Format::MAGIC
- VERSION =
RDF::Borsh::Format::VERSION
- FLAGS =
RDF::Borsh::Format::FLAGS
Instance Method Summary collapse
- #decompress(data) ⇒ Object
-
#initialize(input = $stdin, **options, &block) ⇒ Reader
constructor
A new instance of Reader.
-
#read_header ⇒ Object
Reads the uncompressed header.
- #read_quad ⇒ Object
- #read_statement ⇒ Object
-
#read_terms ⇒ Object
Reads the compressed terms dictionary.
- #read_triple ⇒ Object
Constructor Details
#initialize(input = $stdin, **options, &block) ⇒ Reader
Returns a new instance of Reader.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rdf/borsh/reader.rb', line 16 def initialize(input = $stdin, **, &block) super(input, **) do input = @input @input.extend(Borsh::Readable) @version, @flags, @quad_count = self.read_header input_size = input.read_u32 @input = Borsh::Buffer.new(self.decompress(input.read(input_size))) @terms = [nil] + self.read_terms input_size = input.read_u32 @input = Borsh::Buffer.new(self.decompress(input.read(input_size))) _ = @input.read_u32 if block_given? case block.arity when 0 then self.instance_eval(&block) else block.call(self) end end end end |
Instance Method Details
#decompress(data) ⇒ Object
89 90 91 |
# File 'lib/rdf/borsh/reader.rb', line 89 def decompress(data) LZ4::BlockDecoder.new.decode(data) end |
#read_header ⇒ Object
Reads the uncompressed header.
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/rdf/borsh/reader.rb', line 75 def read_header magic = @input.read(4).unpack('a4').first raise RDF::ReaderError, "invalid RDF/Borsh header: #{magic.inspect}" if magic != MAGIC version = @input.read_u8 raise RDF::ReaderError, "invalid RDF/Borsh version: #{version}" if version != VERSION flags = @input.read_u8 raise RDF::ReaderError, "invalid RDF/Borsh flags: #{flags}" if flags != FLAGS quad_count = @input.read_u32 [version, flags, quad_count] end |
#read_quad ⇒ Object
46 |
# File 'lib/rdf/borsh/reader.rb', line 46 def read_quad; self.read_statement.to_quad; end |
#read_statement ⇒ Object
40 41 42 43 44 |
# File 'lib/rdf/borsh/reader.rb', line 40 def read_statement quad_data = @input.read(8) or raise EOFError g, s, p, o = quad_data.unpack('v4').map! { |term_id| @terms[term_id] } RDF::Statement.new(s, p, o, graph_name: g) end |
#read_terms ⇒ Object
Reads the compressed terms dictionary.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/rdf/borsh/reader.rb', line 51 def read_terms term_count = @input.read_u32 term_count.times.map do term_kind, term_string_size = @input.read_u8, @input.read_u32 term_string = @input.read(term_string_size) case term_kind when 1 then RDF::URI(term_string) when 2 then RDF::Node(term_string) when 3 then RDF::Literal(term_string) when 4 term_datatype_size = @input.read_u32 RDF::Literal(term_string, datatype: @input.read(term_datatype_size)) when 5 term_language_size = @input.read_u32 RDF::Literal(term_string, language: @input.read(term_language_size)) else raise RDF::ReaderError, "unknown RDF/Borsh term type: #{term_kind}" end end end |
#read_triple ⇒ Object
47 |
# File 'lib/rdf/borsh/reader.rb', line 47 def read_triple; self.read_statement.to_triple; end |