Class: RDF::Borsh::Writer
- Inherits:
-
Writer
- Object
- Writer
- RDF::Borsh::Writer
- Defined in:
- lib/rdf/borsh/writer.rb
Constant Summary collapse
- MAGIC =
RDF::Borsh::Format::MAGIC
- VERSION =
RDF::Borsh::Format::VERSION
- FLAGS =
RDF::Borsh::Format::FLAGS
- LZ4HC_CLEVEL_MAX =
12
Instance Method Summary collapse
- #compress {|Borsh::Buffer| ... } ⇒ String
-
#finish ⇒ void
Finishes writing the output.
-
#flush ⇒ void
Flushes the output.
-
#initialize(output = $stdout, **options) {|writer| ... } ⇒ void
constructor
Initializes the RDF/Borsh writer.
-
#intern_term(term) ⇒ Integer
Interns the given RDF term.
-
#write_header ⇒ void
Writes the uncompressed header.
-
#write_quad(subject, predicate, object, context) ⇒ void
Writes an RDF quad.
-
#write_quads ⇒ void
Writes the compressed quads set.
-
#write_terms ⇒ void
Writes the compressed terms dictionary.
-
#write_triple(subject, predicate, object) ⇒ void
Writes an RDF triple.
Constructor Details
#initialize(output = $stdout, **options) {|writer| ... } ⇒ void
Initializes the RDF/Borsh writer.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rdf/borsh/writer.rb', line 26 def initialize(output = $stdout, **, &block) output.extend(Borsh::Writable) output.binmode if output.respond_to?(:binmode) @terms_dict, @terms_map = [], {} @quads_set = SortedSet.new super(output, **) do if block_given? case block.arity when 0 then self.instance_eval(&block) else block.call(self) end self.finish end end end |
Instance Method Details
#compress {|Borsh::Buffer| ... } ⇒ String
169 170 171 172 |
# File 'lib/rdf/borsh/writer.rb', line 169 def compress(&block) uncompressed = Borsh::Buffer.open(&block) LZ4::BlockEncoder.new(LZ4HC_CLEVEL_MAX).encode(uncompressed) end |
#finish ⇒ void
This method returns an undefined value.
Finishes writing the output.
84 85 86 87 88 |
# File 'lib/rdf/borsh/writer.rb', line 84 def finish self.write_header self.write_terms self.write_quads end |
#flush ⇒ void
This method returns an undefined value.
Flushes the output.
75 76 77 78 |
# File 'lib/rdf/borsh/writer.rb', line 75 def flush self.finish super end |
#intern_term(term) ⇒ Integer
Interns the given RDF term.
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/rdf/borsh/writer.rb', line 155 def intern_term(term) return 0 if term.nil? # for the default graph term_id = @terms_map[term] if !term_id term_id = @terms_dict.size + 1 @terms_dict << term @terms_map[term] = term_id end term_id end |
#write_header ⇒ void
This method returns an undefined value.
Writes the uncompressed header.
94 95 96 97 98 |
# File 'lib/rdf/borsh/writer.rb', line 94 def write_header @output.binmode @output.write([MAGIC, VERSION, FLAGS].pack('a4CC')) @output.write_u32(@quads_set.size) end |
#write_quad(subject, predicate, object, context) ⇒ void
This method returns an undefined value.
Writes an RDF quad.
63 64 65 66 67 68 69 |
# File 'lib/rdf/borsh/writer.rb', line 63 def write_quad(subject, predicate, object, context) s = self.intern_term(subject) p = self.intern_term(predicate) o = self.intern_term(object) g = self.intern_term(context) @quads_set << [g, s, p, o] end |
#write_quads ⇒ void
This method returns an undefined value.
Writes the compressed quads set.
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/rdf/borsh/writer.rb', line 139 def write_quads buffer = self.compress do |output| output.write_u32(@quads_set.size) @quads_set.each do |quad| quad.each { |tid| output.write_u16(tid) } end end @output.write_u32(buffer.size) @output.write(buffer) end |
#write_terms ⇒ void
This method returns an undefined value.
Writes the compressed terms dictionary.
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 |
# File 'lib/rdf/borsh/writer.rb', line 104 def write_terms buffer = self.compress do |output| output.write_u32(@terms_dict.size) @terms_dict.each do |term| output.write(case when term.iri? string = term.to_s [1, string.bytesize, string].pack('CVa*') when term.node? string = term.id.to_s [2, string.bytesize, string].pack('CVa*') when term.literal? && term.plain? string = term.value.to_s [3, string.bytesize, string].pack('CVa*') when term.literal? && term.datatype? string = term.value.to_s datatype = term.datatype.to_s [4, string.bytesize, string, datatype.bytesize, datatype].pack('CVa*Va*') when term.literal? && term.language? string = term.value.to_s language = term.language.to_s [5, string.bytesize, string, datatype.language, language].pack('CVa*Va*') else raise RDF::WriterError, "unsupported RDF/Borsh term type: #{term.inspect}" end) end end @output.write_u32(buffer.size) @output.write(buffer) end |
#write_triple(subject, predicate, object) ⇒ void
This method returns an undefined value.
Writes an RDF triple.
51 52 53 |
# File 'lib/rdf/borsh/writer.rb', line 51 def write_triple(subject, predicate, object) self.write_quad(subject, predicate, object, nil) end |