module Cassandra::Protocol::Coder

Constants

GLOBAL_TABLES_SPEC_FLAG
HAS_MORE_PAGES_FLAG
NO_METADATA_FLAG

Public Instance Methods

read_ascii(buffer) click to toggle source
    # File lib/cassandra/protocol/coder.rb
774 def read_ascii(buffer)
775   value = buffer.read_bytes
776   value && value.force_encoding(::Encoding::ASCII)
777 end
read_bigint(buffer) click to toggle source
    # File lib/cassandra/protocol/coder.rb
779 def read_bigint(buffer)
780   read_size(buffer) && buffer.read_long
781 end
Also aliased as: read_counter
read_boolean(buffer) click to toggle source
    # File lib/cassandra/protocol/coder.rb
785 def read_boolean(buffer)
786   read_size(buffer) && buffer.read(1) == Constants::TRUE_BYTE
787 end
read_counter(buffer)
Alias for: read_bigint
read_custom(buffer, type, custom_type_handlers) click to toggle source
    # File lib/cassandra/protocol/coder.rb
789 def read_custom(buffer, type, custom_type_handlers)
790   # Lookup the type-name to get the Class that can deserialize buffer data into a custom domain object.
791   unless custom_type_handlers && custom_type_handlers.key?(type)
792     raise Errors::DecodingError, %(Unsupported custom column type: #{type.name})
793   end
794   num_bytes = read_size(buffer)
795   custom_type_handlers[type].deserialize(buffer.read(num_bytes)) if num_bytes && num_bytes > 0
796 end
read_date(buffer) click to toggle source
    # File lib/cassandra/protocol/coder.rb
858 def read_date(buffer)
859   return nil unless read_size(buffer)
860 
861   ::Date.jd(DATE_OFFSET + buffer.read_int, ::Date::GREGORIAN)
862 end
read_decimal(buffer) click to toggle source
    # File lib/cassandra/protocol/coder.rb
798 def read_decimal(buffer)
799   size = read_size(buffer)
800   size && buffer.read_decimal(size)
801 end
read_double(buffer) click to toggle source
    # File lib/cassandra/protocol/coder.rb
803 def read_double(buffer)
804   read_size(buffer) && buffer.read_double
805 end
read_float(buffer) click to toggle source
    # File lib/cassandra/protocol/coder.rb
807 def read_float(buffer)
808   read_size(buffer) && buffer.read_float
809 end
read_inet(buffer) click to toggle source
    # File lib/cassandra/protocol/coder.rb
839 def read_inet(buffer)
840   size = read_size(buffer)
841   size && ::IPAddr.new_ntoh(buffer.read(size))
842 end
read_int(buffer) click to toggle source
    # File lib/cassandra/protocol/coder.rb
811 def read_int(buffer)
812   read_size(buffer) && buffer.read_signed_int
813 end
read_metadata_v1(buffer) click to toggle source
    # File lib/cassandra/protocol/coder.rb
713 def read_metadata_v1(buffer)
714   flags = buffer.read_int
715   count = buffer.read_int
716 
717   paging_state = nil
718   paging_state = buffer.read_bytes if flags & HAS_MORE_PAGES_FLAG != 0
719   column_specs = nil
720 
721   if flags & NO_METADATA_FLAG == 0
722     if flags & GLOBAL_TABLES_SPEC_FLAG != 0
723       keyspace_name = buffer.read_string
724       table_name    = buffer.read_string
725 
726       column_specs = ::Array.new(count) do |_i|
727         [keyspace_name, table_name, buffer.read_string, read_type_v1(buffer)]
728       end
729     else
730       column_specs = ::Array.new(count) do |_i|
731         [
732           buffer.read_string,
733           buffer.read_string,
734           buffer.read_string,
735           read_type_v1(buffer)
736         ]
737       end
738     end
739   end
740 
741   [column_specs, paging_state]
742 end
read_metadata_v3(buffer) click to toggle source
    # File lib/cassandra/protocol/coder.rb
512 def read_metadata_v3(buffer)
513   flags = buffer.read_int
514   count = buffer.read_int
515 
516   paging_state = nil
517   paging_state = buffer.read_bytes if flags & HAS_MORE_PAGES_FLAG != 0
518   column_specs = nil
519 
520   if flags & NO_METADATA_FLAG == 0
521     if flags & GLOBAL_TABLES_SPEC_FLAG != 0
522       keyspace_name = buffer.read_string
523       table_name    = buffer.read_string
524 
525       column_specs = ::Array.new(count) do |_i|
526         [keyspace_name, table_name, buffer.read_string, read_type_v3(buffer)]
527       end
528     else
529       column_specs = ::Array.new(count) do |_i|
530         [
531           buffer.read_string,
532           buffer.read_string,
533           buffer.read_string,
534           read_type_v3(buffer)
535         ]
536       end
537     end
538   end
539 
540   [column_specs, paging_state]
541 end
read_metadata_v4(buffer) click to toggle source
    # File lib/cassandra/protocol/coder.rb
153 def read_metadata_v4(buffer)
154   flags = buffer.read_int
155   count = buffer.read_int
156 
157   paging_state = nil
158   paging_state = buffer.read_bytes if flags & HAS_MORE_PAGES_FLAG != 0
159   column_specs = nil
160 
161   if flags & NO_METADATA_FLAG == 0
162     if flags & GLOBAL_TABLES_SPEC_FLAG != 0
163       keyspace_name = buffer.read_string
164       table_name    = buffer.read_string
165 
166       column_specs = ::Array.new(count) do |_i|
167         [keyspace_name, table_name, buffer.read_string, read_type_v4(buffer)]
168       end
169     else
170       column_specs = ::Array.new(count) do |_i|
171         [
172           buffer.read_string,
173           buffer.read_string,
174           buffer.read_string,
175           read_type_v4(buffer)
176         ]
177       end
178     end
179   end
180 
181   [column_specs, paging_state]
182 end
read_prepared_metadata_v4(buffer) click to toggle source
    # File lib/cassandra/protocol/coder.rb
126 def read_prepared_metadata_v4(buffer)
127   flags         = buffer.read_int
128   columns_count = buffer.read_int
129   pk_count      = buffer.read_int
130   pk_specs      = ::Array.new(pk_count) {|_i| buffer.read_short}
131 
132   if flags & GLOBAL_TABLES_SPEC_FLAG == GLOBAL_TABLES_SPEC_FLAG
133     keyspace_name = buffer.read_string
134     table_name    = buffer.read_string
135 
136     column_specs = ::Array.new(columns_count) do |_i|
137       [keyspace_name, table_name, buffer.read_string, read_type_v4(buffer)]
138     end
139   else
140     column_specs = ::Array.new(columns_count) do |_i|
141       [
142         buffer.read_string,
143         buffer.read_string,
144         buffer.read_string,
145         read_type_v4(buffer)
146       ]
147     end
148   end
149 
150   [pk_specs, column_specs]
151 end
read_short_size(buffer) click to toggle source
    # File lib/cassandra/protocol/coder.rb
957 def read_short_size(buffer)
958   size = buffer.read_short
959 
960   return nil if size & 0x8000 == 0x8000 || (size == 0)
961 
962   size
963 end
read_short_value(buffer, type) click to toggle source
     # File lib/cassandra/protocol/coder.rb
 965 def read_short_value(buffer, type)
 966   case type.kind
 967   when :ascii
 968     value = buffer.read_short_bytes
 969     value && value.force_encoding(::Encoding::ASCII)
 970   when :bigint, :counter
 971     read_short_size(buffer) && buffer.read_long
 972   when :blob
 973     value = buffer.read_short_bytes
 974     value && value.force_encoding(::Encoding::BINARY)
 975   when :boolean
 976     read_short_size(buffer) && buffer.read(1) == Constants::TRUE_BYTE
 977   when :decimal
 978     size = read_short_size(buffer)
 979     size && buffer.read_decimal(size)
 980   when :double
 981     read_short_size(buffer) && buffer.read_double
 982   when :float
 983     read_short_size(buffer) && buffer.read_float
 984   when :int
 985     read_short_size(buffer) && buffer.read_signed_int
 986   when :inet
 987     size = read_short_size(buffer)
 988     size && ::IPAddr.new_ntoh(buffer.read(size))
 989   when :text
 990     value = buffer.read_short_bytes
 991     value && value.force_encoding(::Encoding::UTF_8)
 992   when :timestamp
 993     return nil unless read_short_size(buffer)
 994 
 995     timestamp     = buffer.read_long
 996     seconds       = timestamp / 1_000
 997     microsenconds = (timestamp % 1_000) * 1_000
 998 
 999     ::Time.at(seconds, microsenconds)
1000   when :timeuuid
1001     read_short_size(buffer) && buffer.read_uuid(TimeUuid)
1002   when :uuid
1003     read_short_size(buffer) && buffer.read_uuid
1004   when :varint
1005     size = read_short_size(buffer)
1006     size && buffer.read_varint(size)
1007   when :jsonb
1008     value = buffer.read_short_bytes
1009     value && value.force_encoding(::Encoding::UTF_8)
1010   else
1011     raise Errors::EncodingError, %(Unsupported short value type: #{type})
1012   end
1013 end
read_size(buffer) click to toggle source
     # File lib/cassandra/protocol/coder.rb
1090 def read_size(buffer)
1091   size = buffer.read_signed_int
1092 
1093   return nil if (size & 0x80000000 == 0x80000000) || (size == 0)
1094 
1095   size
1096 end
read_smallint(buffer) click to toggle source
    # File lib/cassandra/protocol/coder.rb
848 def read_smallint(buffer)
849   read_size(buffer) && buffer.read_smallint
850 end
read_text(buffer) click to toggle source
    # File lib/cassandra/protocol/coder.rb
829 def read_text(buffer)
830   value = buffer.read_bytes
831   value && value.force_encoding(::Encoding::UTF_8)
832 end
read_time(buffer) click to toggle source
    # File lib/cassandra/protocol/coder.rb
852 def read_time(buffer)
853   return nil unless read_size(buffer)
854 
855   Time.new(buffer.read_long)
856 end
read_timestamp(buffer) click to toggle source
    # File lib/cassandra/protocol/coder.rb
815 def read_timestamp(buffer)
816   return nil unless read_size(buffer)
817 
818   timestamp     = buffer.read_long
819   seconds       = timestamp / 1_000
820   microsenconds = (timestamp % 1_000) * 1_000
821 
822   ::Time.at(seconds, microsenconds)
823 end
read_tinyint(buffer) click to toggle source
    # File lib/cassandra/protocol/coder.rb
844 def read_tinyint(buffer)
845   read_size(buffer) && buffer.read_tinyint
846 end
read_type_v1(buffer) click to toggle source
    # File lib/cassandra/protocol/coder.rb
744 def read_type_v1(buffer)
745   kind = buffer.read_unsigned_short
746 
747   case kind
748   when 0x0000 then Types.custom(buffer.read_string)
749   when 0x0001 then Types.ascii
750   when 0x0002 then Types.bigint
751   when 0x0003 then Types.blob
752   when 0x0004 then Types.boolean
753   when 0x0005 then Types.counter
754   when 0x0006 then Types.decimal
755   when 0x0007 then Types.double
756   when 0x0008 then Types.float
757   when 0x0009 then Types.int
758   when 0x000A then Types.text
759   when 0x000B then Types.timestamp
760   when 0x000C then Types.uuid
761   when 0x000D then Types.text
762   when 0x000E then Types.varint
763   when 0x000F then Types.timeuuid
764   when 0x0010 then Types.inet
765   when 0x0020 then Types.list(read_type_v1(buffer))
766   when 0x0021 then Types.map(read_type_v1(buffer), read_type_v1(buffer))
767   when 0x0022 then Types.set(read_type_v1(buffer))
768   when 0x0080 then Types.text
769   else
770     raise Errors::DecodingError, %(Unsupported column type: #{kind})
771   end
772 end
read_type_v3(buffer) click to toggle source
    # File lib/cassandra/protocol/coder.rb
543 def read_type_v3(buffer)
544   id = buffer.read_unsigned_short
545   case id
546   when 0x0000 then Types.custom(buffer.read_string)
547   when 0x0001 then Types.ascii
548   when 0x0002 then Types.bigint
549   when 0x0003 then Types.blob
550   when 0x0004 then Types.boolean
551   when 0x0005 then Types.counter
552   when 0x0006 then Types.decimal
553   when 0x0007 then Types.double
554   when 0x0008 then Types.float
555   when 0x0009 then Types.int
556   when 0x000B then Types.timestamp
557   when 0x000C then Types.uuid
558   when 0x000D then Types.text
559   when 0x000E then Types.varint
560   when 0x000F then Types.timeuuid
561   when 0x0010 then Types.inet
562   when 0x0020 then Types.list(read_type_v3(buffer))
563   when 0x0021 then Types.map(read_type_v3(buffer), read_type_v3(buffer))
564   when 0x0022 then Types.set(read_type_v3(buffer))
565   when 0x0030
566     keyspace = buffer.read_string
567     name     = buffer.read_string
568     fields   = ::Array.new(buffer.read_short) do
569       [buffer.read_string, read_type_v3(buffer)]
570     end
571 
572     Types.udt(keyspace, name, fields)
573   when 0x0031 then Types.tuple(
574     *::Array.new(buffer.read_short) { read_type_v3(buffer) }
575   )
576   when 0x0080 then Types.jsonb
577   else
578     raise Errors::DecodingError, %(Unsupported column type: #{id})
579   end
580 end
read_type_v4(buffer) click to toggle source
    # File lib/cassandra/protocol/coder.rb
184 def read_type_v4(buffer)
185   id = buffer.read_unsigned_short
186   case id
187   when 0x0000 then Types.custom(buffer.read_string)
188   when 0x0001 then Types.ascii
189   when 0x0002 then Types.bigint
190   when 0x0003 then Types.blob
191   when 0x0004 then Types.boolean
192   when 0x0005 then Types.counter
193   when 0x0006 then Types.decimal
194   when 0x0007 then Types.double
195   when 0x0008 then Types.float
196   when 0x0009 then Types.int
197   when 0x000B then Types.timestamp
198   when 0x000C then Types.uuid
199   when 0x000D then Types.text
200   when 0x000E then Types.varint
201   when 0x000F then Types.timeuuid
202   when 0x0010 then Types.inet
203   when 0x0011 then Types.date
204   when 0x0012 then Types.time
205   when 0x0013 then Types.smallint
206   when 0x0014 then Types.tinyint
207   when 0x0020 then Types.list(read_type_v4(buffer))
208   when 0x0021 then Types.map(read_type_v4(buffer), read_type_v4(buffer))
209   when 0x0022 then Types.set(read_type_v4(buffer))
210   when 0x0030
211     keyspace = buffer.read_string
212     name     = buffer.read_string
213     fields   = ::Array.new(buffer.read_short) do
214       [buffer.read_string, read_type_v4(buffer)]
215     end
216 
217     Types.udt(keyspace, name, fields)
218   when 0x0031 then Types.tuple(
219     *::Array.new(buffer.read_short) { read_type_v4(buffer) }
220   )
221   when 0x0080 then Types.jsonb
222   else
223     raise Errors::DecodingError, %(Unsupported column type: #{id})
224   end
225 end
read_uuid(buffer, klass = Uuid) click to toggle source
    # File lib/cassandra/protocol/coder.rb
825 def read_uuid(buffer, klass = Uuid)
826   read_size(buffer) && buffer.read_uuid(klass)
827 end
read_value_v1(buffer, type) click to toggle source
    # File lib/cassandra/protocol/coder.rb
660 def read_value_v1(buffer, type)
661   case type.kind
662   when :ascii            then read_ascii(buffer)
663   when :bigint, :counter then read_bigint(buffer)
664   when :blob             then buffer.read_bytes
665   when :boolean          then read_boolean(buffer)
666   when :decimal          then read_decimal(buffer)
667   when :double           then read_double(buffer)
668   when :float            then read_float(buffer)
669   when :int              then read_int(buffer)
670   when :timestamp        then read_timestamp(buffer)
671   when :text             then read_text(buffer)
672   when :varint           then read_varint(buffer)
673   when :uuid             then read_uuid(buffer)
674   when :timeuuid         then read_uuid(buffer, TimeUuid)
675   when :inet             then read_inet(buffer)
676   when :jsonb            then read_text(buffer)
677   when :list
678     return nil unless read_size(buffer)
679 
680     value_type = type.value_type
681     ::Array.new(buffer.read_short) { read_short_value(buffer, value_type) }
682   when :map
683     return nil unless read_size(buffer)
684 
685     key_type   = type.key_type
686     value_type = type.value_type
687 
688     value = ::Hash.new
689 
690     buffer.read_short.times do
691       value[read_short_value(buffer, key_type)] =
692         read_short_value(buffer, value_type)
693     end
694 
695     value
696   when :set
697     return nil unless read_size(buffer)
698 
699     value_type = type.value_type
700 
701     value = ::Set.new
702 
703     buffer.read_short.times do
704       value << read_short_value(buffer, value_type)
705     end
706 
707     value
708   else
709     raise Errors::DecodingError, %(Unsupported value type: #{type})
710   end
711 end
read_value_v3(buffer, type) click to toggle source
    # File lib/cassandra/protocol/coder.rb
429 def read_value_v3(buffer, type)
430   case type.kind
431   when :ascii            then read_ascii(buffer)
432   when :bigint, :counter then read_bigint(buffer)
433   when :blob             then buffer.read_bytes
434   when :boolean          then read_boolean(buffer)
435   when :decimal          then read_decimal(buffer)
436   when :double           then read_double(buffer)
437   when :float            then read_float(buffer)
438   when :int              then read_int(buffer)
439   when :timestamp        then read_timestamp(buffer)
440   when :uuid             then read_uuid(buffer)
441   when :timeuuid         then read_uuid(buffer, TimeUuid)
442   when :text             then read_text(buffer)
443   when :varint           then read_varint(buffer)
444   when :inet             then read_inet(buffer)
445   when :jsonb            then read_text(buffer)
446   when :list
447     return nil unless read_size(buffer)
448 
449     value_type = type.value_type
450     ::Array.new(buffer.read_signed_int) { read_value_v3(buffer, value_type) }
451   when :map
452     return nil unless read_size(buffer)
453 
454     key_type   = type.key_type
455     value_type = type.value_type
456     value      = ::Hash.new
457 
458     buffer.read_signed_int.times do
459       value[read_value_v3(buffer, key_type)] = read_value_v3(buffer, value_type)
460     end
461 
462     value
463   when :set
464     return nil unless read_size(buffer)
465 
466     value_type = type.value_type
467     value      = ::Set.new
468 
469     buffer.read_signed_int.times do
470       value << read_value_v3(buffer, value_type)
471     end
472 
473     value
474   when :udt
475     size = read_size(buffer)
476     return nil unless size
477 
478     length   = buffer.length
479     keyspace = type.keyspace
480     name     = type.name
481     fields   = type.fields
482     values   = ::Hash.new
483 
484     fields.each do |field|
485       values[field.name] = if length - buffer.length >= size
486                              nil
487                            else
488                              read_value_v3(buffer, field.type)
489                            end
490     end
491 
492     Cassandra::UDT::Strict.new(keyspace, name, fields, values)
493   when :tuple
494     return nil unless read_size(buffer)
495 
496     members = type.members
497     values  = ::Array.new
498 
499     members.each do |member_type|
500       break if buffer.empty?
501       values << read_value_v3(buffer, member_type)
502     end
503 
504     values.fill(nil, values.length, (members.length - values.length))
505 
506     Cassandra::Tuple::Strict.new(members, values)
507   else
508     raise Errors::DecodingError, %(Unsupported value type: #{type})
509   end
510 end
read_value_v4(buffer, type, custom_type_handlers) click to toggle source
    # File lib/cassandra/protocol/coder.rb
239 def read_value_v4(buffer, type, custom_type_handlers)
240   case type.kind
241   when :ascii            then read_ascii(buffer)
242   when :bigint, :counter then read_bigint(buffer)
243   when :blob             then buffer.read_bytes
244   when :boolean          then read_boolean(buffer)
245   when :decimal          then read_decimal(buffer)
246   when :double           then read_double(buffer)
247   when :float            then read_float(buffer)
248   when :int              then read_int(buffer)
249   when :timestamp        then read_timestamp(buffer)
250   when :uuid             then read_uuid(buffer)
251   when :timeuuid         then read_uuid(buffer, TimeUuid)
252   when :text             then read_text(buffer)
253   when :varint           then read_varint(buffer)
254   when :inet             then read_inet(buffer)
255   when :tinyint          then read_tinyint(buffer)
256   when :smallint         then read_smallint(buffer)
257   when :time             then read_time(buffer)
258   when :date             then read_date(buffer)
259   when :custom           then read_custom(buffer, type, custom_type_handlers)
260   when :jsonb            then read_text(buffer)
261   when :list
262     return nil unless read_size(buffer)
263 
264     value_type = type.value_type
265     ::Array.new(buffer.read_signed_int) { read_value_v4(buffer, value_type, custom_type_handlers) }
266   when :map
267     return nil unless read_size(buffer)
268 
269     key_type   = type.key_type
270     value_type = type.value_type
271     value      = ::Hash.new
272 
273     buffer.read_signed_int.times do
274       value[read_value_v4(buffer, key_type, custom_type_handlers)] =
275         read_value_v4(buffer, value_type, custom_type_handlers)
276     end
277 
278     value
279   when :set
280     return nil unless read_size(buffer)
281 
282     value_type = type.value_type
283     value      = ::Set.new
284 
285     buffer.read_signed_int.times do
286       value << read_value_v4(buffer, value_type, custom_type_handlers)
287     end
288 
289     value
290   when :udt
291     size = read_size(buffer)
292     return nil unless size
293 
294     length   = buffer.length
295     keyspace = type.keyspace
296     name     = type.name
297     fields   = type.fields
298     values   = ::Hash.new
299 
300     fields.each do |field|
301       values[field.name] = if length - buffer.length >= size
302                              nil
303                            else
304                              read_value_v4(buffer, field.type, custom_type_handlers)
305                            end
306     end
307 
308     Cassandra::UDT::Strict.new(keyspace, name, fields, values)
309   when :tuple
310     return nil unless read_size(buffer)
311 
312     members = type.members
313     values  = ::Array.new
314 
315     members.each do |member_type|
316       break if buffer.empty?
317       values << read_value_v4(buffer, member_type, custom_type_handlers)
318     end
319 
320     values.fill(nil, values.length, (members.length - values.length))
321 
322     Cassandra::Tuple::Strict.new(members, values)
323   else
324     raise Errors::DecodingError, %(Unsupported value type: #{type})
325   end
326 end
read_values_v1(buffer, column_metadata) click to toggle source
    # File lib/cassandra/protocol/coder.rb
648 def read_values_v1(buffer, column_metadata)
649   ::Array.new(buffer.read_int) do |_i|
650     row = ::Hash.new
651 
652     column_metadata.each do |(_, _, column, type)|
653       row[column] = read_value_v1(buffer, type)
654     end
655 
656     row
657   end
658 end
read_values_v3(buffer, column_metadata) click to toggle source
    # File lib/cassandra/protocol/coder.rb
417 def read_values_v3(buffer, column_metadata)
418   ::Array.new(buffer.read_int) do |_i|
419     row = ::Hash.new
420 
421     column_metadata.each do |(_, _, column, type)|
422       row[column] = read_value_v3(buffer, type)
423     end
424 
425     row
426   end
427 end
read_values_v4(buffer, column_metadata, custom_type_handlers) click to toggle source
    # File lib/cassandra/protocol/coder.rb
227 def read_values_v4(buffer, column_metadata, custom_type_handlers)
228   ::Array.new(buffer.read_int) do |_i|
229     row = ::Hash.new
230 
231     column_metadata.each do |(_, _, column, type)|
232       row[column] = read_value_v4(buffer, type, custom_type_handlers)
233     end
234 
235     row
236   end
237 end
read_varint(buffer) click to toggle source
    # File lib/cassandra/protocol/coder.rb
834 def read_varint(buffer)
835   size = read_size(buffer)
836   size && buffer.read_varint(size)
837 end
write_ascii(buffer, value) click to toggle source
    # File lib/cassandra/protocol/coder.rb
864 def write_ascii(buffer, value)
865   buffer.append_bytes(value.encode(::Encoding::ASCII))
866 end
write_bigint(buffer, value) click to toggle source
    # File lib/cassandra/protocol/coder.rb
868 def write_bigint(buffer, value)
869   buffer.append_int(8)
870   buffer.append_long(value)
871 end
Also aliased as: write_counter
write_blob(buffer, value) click to toggle source
    # File lib/cassandra/protocol/coder.rb
875 def write_blob(buffer, value)
876   buffer.append_bytes(value.encode(::Encoding::BINARY))
877 end
write_boolean(buffer, value) click to toggle source
    # File lib/cassandra/protocol/coder.rb
879 def write_boolean(buffer, value)
880   buffer.append_int(1)
881   buffer.append(value ? Constants::TRUE_BYTE : Constants::FALSE_BYTE)
882 end
write_counter(buffer, value)
Alias for: write_bigint
write_custom(buffer, value, type) click to toggle source
    # File lib/cassandra/protocol/coder.rb
884 def write_custom(buffer, value, type)
885   # Verify that the given type-name matches the value's cql type name.
886   if value.class.type != type
887     raise Errors::EncodingError, "type mismatch: value is a #{value.type} and column is a #{type}"
888   end
889 
890   buffer.append_bytes(value.serialize)
891 end
write_date(buffer, value) click to toggle source
    # File lib/cassandra/protocol/coder.rb
952 def write_date(buffer, value)
953   buffer.append_int(4)
954   buffer.append_int(value.gregorian.jd - DATE_OFFSET)
955 end
write_decimal(buffer, value) click to toggle source
    # File lib/cassandra/protocol/coder.rb
893 def write_decimal(buffer, value)
894   buffer.append_bytes(CqlByteBuffer.new.append_decimal(value))
895 end
write_double(buffer, value) click to toggle source
    # File lib/cassandra/protocol/coder.rb
897 def write_double(buffer, value)
898   buffer.append_int(8)
899   buffer.append_double(value)
900 end
write_float(buffer, value) click to toggle source
    # File lib/cassandra/protocol/coder.rb
902 def write_float(buffer, value)
903   buffer.append_int(4)
904   buffer.append_float(value)
905 end
write_inet(buffer, value) click to toggle source
    # File lib/cassandra/protocol/coder.rb
912 def write_inet(buffer, value)
913   buffer.append_int(value.ipv6? ? 16 : 4)
914   buffer.append(value.hton)
915 end
write_int(buffer, value) click to toggle source
    # File lib/cassandra/protocol/coder.rb
907 def write_int(buffer, value)
908   buffer.append_int(4)
909   buffer.append_int(value)
910 end
write_list_v1(buffer, list, type) click to toggle source
    # File lib/cassandra/protocol/coder.rb
594 def write_list_v1(buffer, list, type)
595   raw = CqlByteBuffer.new
596 
597   raw.append_short(list.size)
598   list.each do |element|
599     write_short_value(raw, element, type)
600   end
601 
602   buffer.append_bytes(raw)
603 end
write_list_v3(buffer, list, type) click to toggle source
    # File lib/cassandra/protocol/coder.rb
341 def write_list_v3(buffer, list, type)
342   raw = CqlByteBuffer.new
343 
344   raw.append_int(list.size)
345   list.each do |element|
346     write_value_v3(raw, element, type)
347   end
348 
349   buffer.append_bytes(raw)
350 end
write_list_v4(buffer, list, type) click to toggle source
   # File lib/cassandra/protocol/coder.rb
41 def write_list_v4(buffer, list, type)
42   raw = CqlByteBuffer.new
43 
44   raw.append_int(list.size)
45   list.each do |element|
46     write_value_v4(raw, element, type)
47   end
48 
49   buffer.append_bytes(raw)
50 end
write_map_v1(buffer, map, key_type, value_type) click to toggle source
    # File lib/cassandra/protocol/coder.rb
605 def write_map_v1(buffer, map, key_type, value_type)
606   raw = CqlByteBuffer.new
607 
608   raw.append_short(map.size)
609   map.each do |key, value|
610     write_short_value(raw, key, key_type)
611     write_short_value(raw, value, value_type)
612   end
613 
614   buffer.append_bytes(raw)
615 end
write_map_v3(buffer, map, key_type, value_type) click to toggle source
    # File lib/cassandra/protocol/coder.rb
352 def write_map_v3(buffer, map, key_type, value_type)
353   raw = CqlByteBuffer.new
354 
355   raw.append_int(map.size)
356   map.each do |key, value|
357     write_value_v3(raw, key, key_type)
358     write_value_v3(raw, value, value_type)
359   end
360 
361   buffer.append_bytes(raw)
362 end
write_map_v4(buffer, map, key_type, value_type) click to toggle source
   # File lib/cassandra/protocol/coder.rb
52 def write_map_v4(buffer, map, key_type, value_type)
53   raw = CqlByteBuffer.new
54 
55   raw.append_int(map.size)
56   map.each do |key, value|
57     write_value_v4(raw, key, key_type)
58     write_value_v4(raw, value, value_type)
59   end
60 
61   buffer.append_bytes(raw)
62 end
write_short_value(buffer, value, type) click to toggle source
     # File lib/cassandra/protocol/coder.rb
1015 def write_short_value(buffer, value, type)
1016   case type.kind
1017   when :ascii
1018     buffer.append_short_bytes(value && value.encode(::Encoding::ASCII))
1019   when :bigint, :counter
1020     if value
1021       buffer.append_short(8)
1022       buffer.append_long(value)
1023     else
1024       buffer.append_short(-1)
1025     end
1026   when :blob
1027     buffer.append_short_bytes(value && value.encode(::Encoding::BINARY))
1028   when :boolean
1029     if !value.nil?
1030       buffer.append_short(1)
1031       buffer.append(value ? Constants::TRUE_BYTE : Constants::FALSE_BYTE)
1032     else
1033       buffer.append_short(-1)
1034     end
1035   when :decimal
1036     buffer.append_short_bytes(value && CqlByteBuffer.new.append_decimal(value))
1037   when :double
1038     if value
1039       buffer.append_short(8)
1040       buffer.append_double(value)
1041     else
1042       buffer.append_short(-1)
1043     end
1044   when :float
1045     if value
1046       buffer.append_short(4)
1047       buffer.append_float(value)
1048     else
1049       buffer.append_short(-1)
1050     end
1051   when :inet
1052     if value
1053       buffer.append_short(value.ipv6? ? 16 : 4)
1054       buffer.append(value.hton)
1055     else
1056       buffer.append_short(-1)
1057     end
1058   when :int
1059     if value
1060       buffer.append_short(4)
1061       buffer.append_int(value)
1062     else
1063       buffer.append_short(-1)
1064     end
1065   when :text
1066     buffer.append_short_bytes(value && value.encode(::Encoding::UTF_8))
1067   when :timestamp
1068     if value
1069       buffer.append_short(8)
1070       buffer.append_long((value.to_f * 1000).to_i)
1071     else
1072       buffer.append_short(-1)
1073     end
1074   when :timeuuid, :uuid
1075     if value
1076       buffer.append_short(16)
1077       buffer.append_uuid(value)
1078     else
1079       buffer.append_short(-1)
1080     end
1081   when :varint
1082     buffer.append_short_bytes(value && CqlByteBuffer.new.append_varint(value))
1083   when :jsonb
1084     buffer.append_short_bytes(value && value.encode(::Encoding::UTF_8))
1085   else
1086     raise Errors::EncodingError, %(Unsupported short value type: #{type})
1087   end
1088 end
write_smallint(buffer, value) click to toggle source
    # File lib/cassandra/protocol/coder.rb
941 def write_smallint(buffer, value)
942   buffer.append_int(2)
943   buffer.append_smallint(value)
944 end
write_text(buffer, value) click to toggle source
    # File lib/cassandra/protocol/coder.rb
923 def write_text(buffer, value)
924   buffer.append_bytes(value.encode(::Encoding::UTF_8))
925 end
write_time(buffer, value) click to toggle source
    # File lib/cassandra/protocol/coder.rb
946 def write_time(buffer, value)
947   ns = value.to_nanoseconds
948   buffer.append_int(8)
949   buffer.append_long(ns)
950 end
write_timestamp(buffer, value) click to toggle source
    # File lib/cassandra/protocol/coder.rb
917 def write_timestamp(buffer, value)
918   ms = (value.to_r.to_f * 1000).to_i
919   buffer.append_int(8)
920   buffer.append_long(ms)
921 end
write_tinyint(buffer, value) click to toggle source
    # File lib/cassandra/protocol/coder.rb
936 def write_tinyint(buffer, value)
937   buffer.append_int(1)
938   buffer.append_tinyint(value)
939 end
write_tuple_v3(buffer, value, members) click to toggle source
    # File lib/cassandra/protocol/coder.rb
374 def write_tuple_v3(buffer, value, members)
375   raw = CqlByteBuffer.new
376 
377   members.each_with_index do |type, i|
378     write_value_v3(raw, value[i], type)
379   end
380 
381   buffer.append_bytes(raw)
382 end
write_tuple_v4(buffer, value, members) click to toggle source
   # File lib/cassandra/protocol/coder.rb
74 def write_tuple_v4(buffer, value, members)
75   raw = CqlByteBuffer.new
76 
77   members.each_with_index do |type, i|
78     write_value_v4(raw, value[i], type)
79   end
80 
81   buffer.append_bytes(raw)
82 end
write_udt_v3(buffer, value, fields) click to toggle source
    # File lib/cassandra/protocol/coder.rb
364 def write_udt_v3(buffer, value, fields)
365   raw = CqlByteBuffer.new
366 
367   fields.each do |field|
368     write_value_v3(raw, value[field.name], field.type)
369   end
370 
371   buffer.append_bytes(raw)
372 end
write_udt_v4(buffer, value, fields) click to toggle source
   # File lib/cassandra/protocol/coder.rb
64 def write_udt_v4(buffer, value, fields)
65   raw = CqlByteBuffer.new
66 
67   fields.each do |field|
68     write_value_v4(raw, value[field.name], field.type)
69   end
70 
71   buffer.append_bytes(raw)
72 end
write_uuid(buffer, value) click to toggle source
    # File lib/cassandra/protocol/coder.rb
927 def write_uuid(buffer, value)
928   buffer.append_int(16)
929   buffer.append_uuid(value)
930 end
write_value_v1(buffer, value, type) click to toggle source
    # File lib/cassandra/protocol/coder.rb
617 def write_value_v1(buffer, value, type)
618   if value.nil?
619     buffer.append_int(-1)
620     return
621   end
622 
623   case type.kind
624   when :ascii            then write_ascii(buffer, value)
625   when :bigint, :counter then write_bigint(buffer, value)
626   when :blob             then write_blob(buffer, value)
627   when :boolean          then write_boolean(buffer, value)
628   when :decimal          then write_decimal(buffer, value)
629   when :double           then write_double(buffer, value)
630   when :float            then write_float(buffer, value)
631   when :int              then write_int(buffer, value)
632   when :inet             then write_inet(buffer, value)
633   when :text             then write_text(buffer, value)
634   when :timestamp        then write_timestamp(buffer, value)
635   when :timeuuid, :uuid  then write_uuid(buffer, value)
636   when :varint           then write_varint(buffer, value)
637   when :jsonb            then write_text(buffer, value)
638   when :list, :set       then write_list_v1(buffer, value, type.value_type)
639   when :map              then write_map_v1(buffer,
640                                            value,
641                                            type.key_type,
642                                            type.value_type)
643   else
644     raise Errors::EncodingError, %(Unsupported value type: #{type})
645   end
646 end
write_value_v3(buffer, value, type) click to toggle source
    # File lib/cassandra/protocol/coder.rb
384 def write_value_v3(buffer, value, type)
385   if value.nil?
386     buffer.append_int(-1)
387     return
388   end
389 
390   case type.kind
391   when :ascii            then write_ascii(buffer, value)
392   when :bigint, :counter then write_bigint(buffer, value)
393   when :blob             then write_blob(buffer, value)
394   when :boolean          then write_boolean(buffer, value)
395   when :decimal          then write_decimal(buffer, value)
396   when :double           then write_double(buffer, value)
397   when :float            then write_float(buffer, value)
398   when :int              then write_int(buffer, value)
399   when :inet             then write_inet(buffer, value)
400   when :timestamp        then write_timestamp(buffer, value)
401   when :uuid, :timeuuid  then write_uuid(buffer, value)
402   when :text             then write_text(buffer, value)
403   when :varint           then write_varint(buffer, value)
404   when :list, :set       then write_list_v3(buffer, value, type.value_type)
405   when :map              then write_map_v3(buffer,
406                                            value,
407                                            type.key_type,
408                                            type.value_type)
409   when :udt              then write_udt_v3(buffer, value, type.fields)
410   when :tuple            then write_tuple_v3(buffer, value, type.members)
411   when :jsonb            then write_text(buffer, value)
412   else
413     raise Errors::EncodingError, %(Unsupported value type: #{type})
414   end
415 end
write_value_v4(buffer, value, type) click to toggle source
    # File lib/cassandra/protocol/coder.rb
 84 def write_value_v4(buffer, value, type)
 85   if value.nil?
 86     buffer.append_int(-1)
 87     return
 88   end
 89 
 90   if NOT_SET.eql?(value)
 91     buffer.append_int(-2)
 92     return
 93   end
 94 
 95   case type.kind
 96   when :ascii            then write_ascii(buffer, value)
 97   when :bigint, :counter then write_bigint(buffer, value)
 98   when :blob             then write_blob(buffer, value)
 99   when :boolean          then write_boolean(buffer, value)
100   when :custom           then write_custom(buffer, value, type)
101   when :decimal          then write_decimal(buffer, value)
102   when :double           then write_double(buffer, value)
103   when :float            then write_float(buffer, value)
104   when :int              then write_int(buffer, value)
105   when :inet             then write_inet(buffer, value)
106   when :timestamp        then write_timestamp(buffer, value)
107   when :uuid, :timeuuid  then write_uuid(buffer, value)
108   when :text             then write_text(buffer, value)
109   when :varint           then write_varint(buffer, value)
110   when :tinyint          then write_tinyint(buffer, value)
111   when :smallint         then write_smallint(buffer, value)
112   when :time             then write_time(buffer, value)
113   when :date             then write_date(buffer, value)
114   when :list, :set       then write_list_v4(buffer, value, type.value_type)
115   when :map              then write_map_v4(buffer, value,
116                                            type.key_type,
117                                            type.value_type)
118   when :udt              then write_udt_v4(buffer, value, type.fields)
119   when :tuple            then write_tuple_v4(buffer, value, type.members)
120   when :jsonb            then write_text(buffer, value)
121   else
122     raise Errors::EncodingError, %(Unsupported value type: #{type})
123   end
124 end
write_values_v1(buffer, values, types) click to toggle source
    # File lib/cassandra/protocol/coder.rb
582 def write_values_v1(buffer, values, types)
583   if values && !values.empty?
584     buffer.append_short(values.size)
585     values.each_with_index do |value, index|
586       write_value_v1(buffer, value, types[index])
587     end
588     buffer
589   else
590     buffer.append_short(0)
591   end
592 end
write_values_v3(buffer, values, types, names = EMPTY_LIST) click to toggle source
    # File lib/cassandra/protocol/coder.rb
328 def write_values_v3(buffer, values, types, names = EMPTY_LIST)
329   if values && !values.empty?
330     buffer.append_short(values.size)
331     values.zip(types, names) do |(value, type, name)|
332       buffer.append_string(name) if name
333       write_value_v3(buffer, value, type)
334     end
335     buffer
336   else
337     buffer.append_short(0)
338   end
339 end
write_values_v4(buffer, values, types, names = EMPTY_LIST) click to toggle source
   # File lib/cassandra/protocol/coder.rb
28 def write_values_v4(buffer, values, types, names = EMPTY_LIST)
29   if values && !values.empty?
30     buffer.append_short(values.size)
31     values.zip(types, names) do |(value, type, name)|
32       buffer.append_string(name) if name
33       write_value_v4(buffer, value, type)
34     end
35     buffer
36   else
37     buffer.append_short(0)
38   end
39 end
write_varint(buffer, value) click to toggle source
    # File lib/cassandra/protocol/coder.rb
932 def write_varint(buffer, value)
933   buffer.append_bytes(CqlByteBuffer.new.append_varint(value))
934 end