Class: CArray::Serializer

Inherits:
Object show all
Defined in:
lib/carray/serialize.rb

Overview

:nodoc:

Constant Summary collapse

Header =
CA.struct(:pack=>1, :size=>256) {
  char_p    :magic_string,   :bytes=>8
  char_p    :data_type_name, :bytes=>8
  char_p    :endian,         :bytes=>4
  int32     :data_type
  int64     :bytes
  int32     :ndim
  int64     :elements
  int32     :has_mask
  array     :dim,            :type => CArray.int64(CA_RANK_MAX)
  int32     :has_attr
}
Header_Legacy =
CA.struct(:pack=>1, :size=>256) {
  char_p    :magic_string,   :bytes=>8
  char_p    :data_type_name, :bytes=>8
  char_p    :endian,         :bytes=>4
  int32     :data_type
  int32     :bytes
  int32     :ndim
  int32     :elements
  int32     :has_mask
  array     :dim,            :type => CArray.int32(CA_RANK_MAX)
  int32     :has_attr
}

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Serializer

Returns a new instance of Serializer.



77
78
79
80
81
82
83
84
# File 'lib/carray/serialize.rb', line 77

def initialize (io)
  case io
  when String
    @io = StringIO.new(io)
  else
    @io = io
  end
end

Instance Method Details

#load(opt = {}, legacy: false) ⇒ Object



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
# File 'lib/carray/serialize.rb', line 130

def load (opt = {}, legacy: false)
  if legacy
    header = Header_Legacy.decode(@io.read(256))
  else
    header = Header.decode(@io.read(256))
  end
  if header[:magic_string] != "_CARRAY_"
    raise "not a CArray binary data"      
  end
  case header[:endian]
  when "_LE_"
    endian = CA_LITTLE_ENDIAN
  when "_BE_"
    endian = CA_BIG_ENDIAN
  end
  unless CArray.endian == endian
    header.swap_bytes!
  end
  data_type = header[:data_type]
  bytes     = header[:bytes]
  ndim      = header[:ndim]
  elements  = header[:elements]
  has_mask  = header[:has_mask] != 0 ? true : false
  dim       = header[:dim][[0, ndim]].to_a
  has_attr  = header[:has_attr]
  if data_type == 255
    data_type = header[:data_type_name].strip.to_sym
  end
  data_type, bytes = CArray.guess_type_and_bytes(data_type, bytes)
  if opt[:data_type] and data_type == CA_FIXLEN
    data_type = opt[:data_type]
  end
  ca = CArray.new(data_type, dim, :bytes=>bytes)
  if data_type == CA_OBJECT
    ca.value[] = Marshal.load(@io)
  else
    ca.load_binary(@io)
    unless CArray.endian == endian
      ca.swap_bytes!
    end
  end
  if has_mask
    ca.mask = 0
    ca.mask.load_binary(@io)
  end
  if has_attr == 1
    ca.attribute = Marshal.load(@io)
  end
  return ca
end

#save(ca, opt = {}) ⇒ Object



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
# File 'lib/carray/serialize.rb', line 86

def save (ca, opt = {})
  endian = opt[:endian] || CArray.endian
  # ---
  header = Header.new()
  header[:magic_string]   = "_CARRAY_"
  header[:data_type_name] = ca.data_type_name.ljust(8)
  header[:endian]         = ( endian == CA_LITTLE_ENDIAN ) ? "_LE_" : "_BE_"
  header[:data_type]      = ca.data_type
  header[:bytes]          = ca.bytes
  header[:ndim]           = ca.ndim
  header[:elements]       = ca.elements
  header[:has_mask]       = ca.has_mask? ? 1 : 0
  header[:dim][[0,ca.ndim]] = ca.dim
  attr = nil
  if ca.attribute
    attr = ca.attribute.clone
  end
  if opt[:attribute]
    (attr ||= {}).update(opt[:attribute])
  end
  header[:has_attr]       = attr.empty? ? 0 : 1
  unless CArray.endian == endian
    header.swap_bytes!
  end
  @io.write(header.encode)
  # ---
  if ca.data_type == CA_OBJECT
    Marshal.dump(ca.value.to_a, @io)
  else
    unless CArray.endian == endian
      ca = ca.swap_bytes
    end
    ca.dump_binary(@io)
  end
  # ---
  if ca.has_mask?
    ca.mask.dump_binary(@io)
  end
  if attr
    Marshal.dump(attr, @io)
  end
  return ca
end