ed-precompiled_msgpack 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/ChangeLog +368 -0
  3. data/LICENSE +177 -0
  4. data/README.md +302 -0
  5. data/ext/java/org/msgpack/jruby/Buffer.java +233 -0
  6. data/ext/java/org/msgpack/jruby/Decoder.java +307 -0
  7. data/ext/java/org/msgpack/jruby/Encoder.java +456 -0
  8. data/ext/java/org/msgpack/jruby/ExtensionRegistry.java +167 -0
  9. data/ext/java/org/msgpack/jruby/ExtensionValue.java +128 -0
  10. data/ext/java/org/msgpack/jruby/Factory.java +130 -0
  11. data/ext/java/org/msgpack/jruby/MessagePackLibrary.java +45 -0
  12. data/ext/java/org/msgpack/jruby/Packer.java +266 -0
  13. data/ext/java/org/msgpack/jruby/Types.java +37 -0
  14. data/ext/java/org/msgpack/jruby/Unpacker.java +336 -0
  15. data/ext/msgpack/buffer.c +669 -0
  16. data/ext/msgpack/buffer.h +604 -0
  17. data/ext/msgpack/buffer_class.c +616 -0
  18. data/ext/msgpack/buffer_class.h +33 -0
  19. data/ext/msgpack/compat.h +26 -0
  20. data/ext/msgpack/extconf.rb +53 -0
  21. data/ext/msgpack/extension_value_class.c +34 -0
  22. data/ext/msgpack/extension_value_class.h +31 -0
  23. data/ext/msgpack/factory_class.c +276 -0
  24. data/ext/msgpack/factory_class.h +33 -0
  25. data/ext/msgpack/packer.c +199 -0
  26. data/ext/msgpack/packer.h +513 -0
  27. data/ext/msgpack/packer_class.c +442 -0
  28. data/ext/msgpack/packer_class.h +43 -0
  29. data/ext/msgpack/packer_ext_registry.c +74 -0
  30. data/ext/msgpack/packer_ext_registry.h +140 -0
  31. data/ext/msgpack/rbinit.c +35 -0
  32. data/ext/msgpack/rmem.c +93 -0
  33. data/ext/msgpack/rmem.h +109 -0
  34. data/ext/msgpack/sysdep.h +118 -0
  35. data/ext/msgpack/sysdep_endian.h +50 -0
  36. data/ext/msgpack/sysdep_types.h +46 -0
  37. data/ext/msgpack/unpacker.c +986 -0
  38. data/ext/msgpack/unpacker.h +152 -0
  39. data/ext/msgpack/unpacker_class.c +447 -0
  40. data/ext/msgpack/unpacker_class.h +43 -0
  41. data/ext/msgpack/unpacker_ext_registry.c +74 -0
  42. data/ext/msgpack/unpacker_ext_registry.h +62 -0
  43. data/lib/msgpack/bigint.rb +69 -0
  44. data/lib/msgpack/buffer.rb +9 -0
  45. data/lib/msgpack/core_ext.rb +139 -0
  46. data/lib/msgpack/factory.rb +211 -0
  47. data/lib/msgpack/packer.rb +37 -0
  48. data/lib/msgpack/symbol.rb +26 -0
  49. data/lib/msgpack/time.rb +29 -0
  50. data/lib/msgpack/timestamp.rb +76 -0
  51. data/lib/msgpack/unpacker.rb +41 -0
  52. data/lib/msgpack/version.rb +6 -0
  53. data/lib/msgpack.rb +53 -0
  54. data/msgpack.gemspec +41 -0
  55. metadata +216 -0
data/README.md ADDED
@@ -0,0 +1,302 @@
1
+ # MessagePack
2
+
3
+ [MessagePack](http://msgpack.org) is an efficient binary serialization format.
4
+ It lets you exchange data among multiple languages like JSON but it's faster and smaller.
5
+ For example, small integers (like flags or error code) are encoded into a single byte,
6
+ and typical short strings only require an extra byte in addition to the strings themselves.
7
+
8
+ If you ever wished to use JSON for convenience (storing an image with metadata) but could
9
+ not for technical reasons (binary data, size, speed...), MessagePack is a perfect replacement.
10
+
11
+ ```ruby
12
+ require 'msgpack'
13
+ msg = [1,2,3].to_msgpack #=> "\x93\x01\x02\x03"
14
+ MessagePack.unpack(msg) #=> [1,2,3]
15
+ ```
16
+
17
+ Add msgpack to your Gemfile to install with Bundler:
18
+
19
+ ```ruby
20
+ # Gemfile
21
+ gem 'msgpack'
22
+ ```
23
+
24
+ Or, use RubyGems to install:
25
+
26
+ gem install msgpack
27
+
28
+ Or, build msgpack-ruby and install from a checked-out msgpack-ruby repository:
29
+
30
+ bundle
31
+ rake
32
+ gem install --local pkg/msgpack
33
+
34
+
35
+ ## Use cases
36
+
37
+ * Create REST API returing MessagePack using Rails + [RABL](https://github.com/nesquena/rabl)
38
+ * Store objects efficiently serialized by msgpack on memcached or Redis
39
+ * In fact Redis supports msgpack in [EVAL-scripts](https://redis.io/docs/latest/commands/eval/)
40
+ * Upload data in efficient format from mobile devices such as smartphones
41
+ * MessagePack works on iPhone/iPad and Android. See also [Objective-C](https://github.com/msgpack/msgpack-objectivec) and [Java](https://github.com/msgpack/msgpack-java) implementations
42
+ * Design a portable protocol to communicate with embedded devices
43
+ * Check also [Fluentd](https://www.fluentd.org) which is a log collector which uses msgpack for the log format (they say it uses JSON but actually it's msgpack, which is compatible with JSON)
44
+ * Exchange objects between software components written in different languages
45
+ * You'll need a flexible but efficient format so that components exchange objects while keeping compatibility
46
+
47
+ ## Portability
48
+
49
+ MessagePack for Ruby should run on x86, ARM, PowerPC, SPARC and other CPU architectures.
50
+
51
+ And it works with MRI (CRuby) and Rubinius.
52
+ Patches to improve portability are highly welcomed.
53
+
54
+
55
+ ## Serializing objects
56
+
57
+ Use `MessagePack.pack` or `to_msgpack`:
58
+
59
+ ```ruby
60
+ require 'msgpack'
61
+ msg = MessagePack.pack(obj) # or
62
+ msg = obj.to_msgpack
63
+ File.binwrite('mydata.msgpack', msg)
64
+ ```
65
+
66
+ ### Streaming serialization
67
+
68
+ Packer provides advanced API to serialize objects in streaming style:
69
+
70
+ ```ruby
71
+ # serialize a 2-element array [e1, e2]
72
+ pk = MessagePack::Packer.new(io)
73
+ pk.write_array_header(2).write(e1).write(e2).flush
74
+ ```
75
+
76
+ See [API reference](http://ruby.msgpack.org/MessagePack/Packer.html) for details.
77
+
78
+ ## Deserializing objects
79
+
80
+ Use `MessagePack.unpack`:
81
+
82
+ ```ruby
83
+ require 'msgpack'
84
+ msg = File.binread('mydata.msgpack')
85
+ obj = MessagePack.unpack(msg)
86
+ ```
87
+
88
+ ### Streaming deserialization
89
+
90
+ Unpacker provides advanced API to deserialize objects in streaming style:
91
+
92
+ ```ruby
93
+ # deserialize objects from an IO
94
+ u = MessagePack::Unpacker.new(io)
95
+ u.each do |obj|
96
+ # ...
97
+ end
98
+ ```
99
+
100
+ or event-driven style which works well with EventMachine:
101
+
102
+ ```ruby
103
+ # event-driven deserialization
104
+ def on_read(data)
105
+ @u ||= MessagePack::Unpacker.new
106
+ @u.feed_each(data) {|obj|
107
+ # ...
108
+ }
109
+ end
110
+ ```
111
+
112
+ See [API reference](http://ruby.msgpack.org/MessagePack/Unpacker.html) for details.
113
+
114
+ ## Serializing and deserializing symbols
115
+
116
+ By default, symbols are serialized as strings:
117
+
118
+ ```ruby
119
+ packed = :symbol.to_msgpack # => "\xA6symbol"
120
+ MessagePack.unpack(packed) # => "symbol"
121
+ ```
122
+
123
+ This can be customized by registering an extension type for them:
124
+
125
+ ```ruby
126
+ MessagePack::DefaultFactory.register_type(0x00, Symbol)
127
+
128
+ # symbols now survive round trips
129
+ packed = :symbol.to_msgpack # => "\xc7\x06\x00symbol"
130
+ MessagePack.unpack(packed) # => :symbol
131
+ ```
132
+
133
+ The extension type for symbols is configurable like any other extension type.
134
+ For example, to customize how symbols are packed you can just redefine
135
+ Symbol#to_msgpack_ext. Doing this gives you an option to prevent symbols from
136
+ being serialized altogether by throwing an exception:
137
+
138
+ ```ruby
139
+ class Symbol
140
+ def to_msgpack_ext
141
+ raise "Serialization of symbols prohibited"
142
+ end
143
+ end
144
+
145
+ MessagePack::DefaultFactory.register_type(0x00, Symbol)
146
+
147
+ [1, :symbol, 'string'].to_msgpack # => RuntimeError: Serialization of symbols prohibited
148
+ ```
149
+
150
+ ## Serializing and deserializing Time instances
151
+
152
+ There are the timestamp extension type in MessagePack,
153
+ but it is not registered by default.
154
+
155
+ To map Ruby's Time to MessagePack's timestamp for the default factory:
156
+
157
+ ```ruby
158
+ MessagePack::DefaultFactory.register_type(
159
+ MessagePack::Timestamp::TYPE, # or just -1
160
+ Time,
161
+ packer: MessagePack::Time::Packer,
162
+ unpacker: MessagePack::Time::Unpacker
163
+ )
164
+ ```
165
+
166
+ See [API reference](http://ruby.msgpack.org/) for details.
167
+
168
+ ## Extension Types
169
+
170
+ Packer and Unpacker support [Extension types of MessagePack](https://github.com/msgpack/msgpack/blob/master/spec.md#types-extension-type).
171
+
172
+ ```ruby
173
+ # register how to serialize custom class at first
174
+ pk = MessagePack::Packer.new(io)
175
+ pk.register_type(0x01, MyClass1, :to_msgpack_ext) # equal to pk.register_type(0x01, MyClass)
176
+ pk.register_type(0x02, MyClass2){|obj| obj.how_to_serialize() } # blocks also available
177
+
178
+ # almost same API for unpacker
179
+ uk = MessagePack::Unpacker.new()
180
+ uk.register_type(0x01, MyClass1, :from_msgpack_ext)
181
+ uk.register_type(0x02){|data| MyClass2.create_from_serialized_data(data) }
182
+ ```
183
+
184
+ `MessagePack::Factory` is to create packer and unpacker which have same extension types.
185
+
186
+ ```ruby
187
+ factory = MessagePack::Factory.new
188
+ factory.register_type(0x01, MyClass1) # same with next line
189
+ factory.register_type(0x01, MyClass1, packer: :to_msgpack_ext, unpacker: :from_msgpack_ext)
190
+ pk = factory.packer(options_for_packer)
191
+ uk = factory.unpacker(options_for_unpacker)
192
+ ```
193
+
194
+ For `MessagePack.pack` and `MessagePack.unpack`, default packer/unpacker refer `MessagePack::DefaultFactory`. Call `MessagePack::DefaultFactory.register_type` to enable types process globally.
195
+
196
+ ```ruby
197
+ MessagePack::DefaultFactory.register_type(0x03, MyClass3)
198
+ MessagePack.unpack(data_with_ext_typeid_03) #=> MyClass3 instance
199
+ ```
200
+
201
+ Alternatively, extension types can call the packer or unpacker recursively to generate the extension data:
202
+
203
+ ```ruby
204
+ Point = Struct.new(:x, :y)
205
+ factory = MessagePack::Factory.new
206
+ factory.register_type(
207
+ 0x01,
208
+ Point,
209
+ packer: ->(point, packer) {
210
+ packer.write(point.x)
211
+ packer.write(point.y)
212
+ },
213
+ unpacker: ->(unpacker) {
214
+ x = unpacker.read
215
+ y = unpacker.read
216
+ Point.new(x, y)
217
+ },
218
+ recursive: true,
219
+ )
220
+ factory.load(factory.dump(Point.new(12, 34))) # => #<struct Point x=12, y=34>
221
+ ```
222
+
223
+ ## Pooling
224
+
225
+ Creating `Packer` and `Unpacker` objects is expensive. For best performance it is preferable to re-use these objects.
226
+
227
+ `MessagePack::Factory#pool` makes that easier:
228
+
229
+ ```ruby
230
+ factory = MessagePack::Factory.new
231
+ factory.register_type(
232
+ 0x01,
233
+ Point,
234
+ packer: ->(point, packer) {
235
+ packer.write(point.x)
236
+ packer.write(point.y)
237
+ },
238
+ unpacker: ->(unpacker) {
239
+ x = unpacker.read
240
+ y = unpacker.read
241
+ Point.new(x, y)
242
+ },
243
+ recursive: true,
244
+ )
245
+ pool = factory.pool(5) # The pool size should match the number of threads expected to use the factory concurrently.
246
+
247
+ pool.load(pool.dump(Point.new(12, 34))) # => #<struct Point x=12, y=34>
248
+ ```
249
+
250
+ ## Buffer API
251
+
252
+ MessagePack for Ruby provides a buffer API so that you can read or write data by hand, not via Packer or Unpacker API.
253
+
254
+ This [MessagePack::Buffer](http://ruby.msgpack.org/MessagePack/Buffer.html) is backed with a fixed-length shared memory pool which is very fast for small data (<= 4KB),
255
+ and has zero-copy capability which significantly affects performance to handle large binary data.
256
+
257
+ ## How to build and run tests
258
+
259
+ Before building msgpack, you need to install bundler and dependencies.
260
+
261
+ gem install bundler
262
+ bundle install
263
+
264
+ Then, you can run the tasks as follows:
265
+
266
+ ### Build
267
+
268
+ bundle exec rake build
269
+
270
+ ### Run tests
271
+
272
+ bundle exec rake spec
273
+
274
+ ### Generating docs
275
+
276
+ bundle exec rake doc
277
+
278
+ ## How to build -java rubygems
279
+
280
+ To build -java gems for JRuby, run:
281
+
282
+ rake build:java
283
+
284
+ If this directory has Gemfile.lock (generated with MRI), remove it beforehand.
285
+
286
+ ## Updating documents
287
+
288
+ Online documentation (https://ruby.msgpack.org) is generated from the gh-pages branch.
289
+ To update documents in gh-pages branch:
290
+
291
+ bundle exec rake doc
292
+ git checkout gh-pages
293
+ cp -a doc/* ./
294
+
295
+ ## Copyright
296
+
297
+ * Author
298
+ * Sadayuki Furuhashi <frsyuki@gmail.com>
299
+ * Copyright
300
+ * Copyright (c) 2008-2015 Sadayuki Furuhashi
301
+ * License
302
+ * Apache License, Version 2.0
@@ -0,0 +1,233 @@
1
+ package org.msgpack.jruby;
2
+
3
+
4
+ import java.nio.ByteBuffer;
5
+
6
+ import org.jruby.Ruby;
7
+ import org.jruby.RubyClass;
8
+ import org.jruby.RubyObject;
9
+ import org.jruby.RubyHash;
10
+ import org.jruby.RubyIO;
11
+ import org.jruby.RubyInteger;
12
+ import org.jruby.runtime.builtin.IRubyObject;
13
+ import org.jruby.anno.JRubyClass;
14
+ import org.jruby.anno.JRubyMethod;
15
+ import org.jruby.runtime.ThreadContext;
16
+ import org.jruby.runtime.ObjectAllocator;
17
+ import org.jruby.util.ByteList;
18
+
19
+ import org.jcodings.Encoding;
20
+
21
+
22
+ @JRubyClass(name="MessagePack::Buffer")
23
+ public class Buffer extends RubyObject {
24
+ private static final long serialVersionUID = 8441244627425629412L;
25
+ private transient IRubyObject io;
26
+ private transient ByteBuffer buffer;
27
+ private boolean writeMode;
28
+ private transient Encoding binaryEncoding;
29
+
30
+ private static final int CACHE_LINE_SIZE = 64;
31
+ private static final int ARRAY_HEADER_SIZE = 24;
32
+
33
+ public Buffer(Ruby runtime, RubyClass type) {
34
+ super(runtime, type);
35
+ }
36
+
37
+ static class BufferAllocator implements ObjectAllocator {
38
+ public IRubyObject allocate(Ruby runtime, RubyClass type) {
39
+ return new Buffer(runtime, type);
40
+ }
41
+ }
42
+
43
+ @JRubyMethod(name = "initialize", optional = 2)
44
+ public IRubyObject initialize(ThreadContext ctx, IRubyObject[] args) {
45
+ if (args.length > 0) {
46
+ IRubyObject io = args[0];
47
+ if (io.respondsTo("close") && (io.respondsTo("read") || (io.respondsTo("write") && io.respondsTo("flush")))) {
48
+ this.io = io;
49
+ }
50
+ }
51
+ this.buffer = ByteBuffer.allocate(CACHE_LINE_SIZE - ARRAY_HEADER_SIZE);
52
+ this.writeMode = true;
53
+ this.binaryEncoding = ctx.runtime.getEncodingService().getAscii8bitEncoding();
54
+ return this;
55
+ }
56
+
57
+ private void ensureRemainingCapacity(int c) {
58
+ if (!writeMode) {
59
+ buffer.compact();
60
+ writeMode = true;
61
+ }
62
+ if (buffer.remaining() < c) {
63
+ int newLength = Math.max(buffer.capacity() + (buffer.capacity() >> 1), buffer.capacity() + c);
64
+ newLength += CACHE_LINE_SIZE - ((ARRAY_HEADER_SIZE + newLength) % CACHE_LINE_SIZE);
65
+ buffer = ByteBuffer.allocate(newLength).put(buffer.array(), 0, buffer.position());
66
+ }
67
+ }
68
+
69
+ private void ensureReadMode() {
70
+ if (writeMode) {
71
+ buffer.flip();
72
+ writeMode = false;
73
+ }
74
+ }
75
+
76
+ private int rawSize() {
77
+ if (writeMode) {
78
+ return buffer.position();
79
+ } else {
80
+ return buffer.limit() - buffer.position();
81
+ }
82
+ }
83
+
84
+ @JRubyMethod(name = "clear")
85
+ public IRubyObject clear(ThreadContext ctx) {
86
+ if (!writeMode) {
87
+ buffer.compact();
88
+ writeMode = true;
89
+ }
90
+ buffer.clear();
91
+ return ctx.runtime.getNil();
92
+ }
93
+
94
+ @JRubyMethod(name = "size")
95
+ public IRubyObject size(ThreadContext ctx) {
96
+ return ctx.runtime.newFixnum(rawSize());
97
+ }
98
+
99
+ @JRubyMethod(name = "empty?")
100
+ public IRubyObject isEmpty(ThreadContext ctx) {
101
+ return rawSize() == 0 ? ctx.runtime.getTrue() : ctx.runtime.getFalse();
102
+ }
103
+
104
+ private IRubyObject bufferWrite(ThreadContext ctx, IRubyObject str) {
105
+ ByteList bytes = str.asString().getByteList();
106
+ int length = bytes.length();
107
+ ensureRemainingCapacity(length);
108
+ buffer.put(bytes.unsafeBytes(), bytes.begin(), length);
109
+ return ctx.runtime.newFixnum(length);
110
+
111
+ }
112
+
113
+ @JRubyMethod(name = "write", alias = {"<<"})
114
+ public IRubyObject write(ThreadContext ctx, IRubyObject str) {
115
+ if (io == null) {
116
+ return bufferWrite(ctx, str);
117
+ } else {
118
+ return io.callMethod(ctx, "write", str);
119
+ }
120
+ }
121
+
122
+ private void feed(ThreadContext ctx) {
123
+ if (io != null) {
124
+ bufferWrite(ctx, io.callMethod(ctx, "read"));
125
+ }
126
+ }
127
+
128
+ private IRubyObject readCommon(ThreadContext ctx, IRubyObject[] args, boolean raiseOnUnderflow) {
129
+ feed(ctx);
130
+ int length = rawSize();
131
+ if (args != null && args.length == 1) {
132
+ length = (int) args[0].convertToInteger().getLongValue();
133
+ }
134
+ if (raiseOnUnderflow && rawSize() < length) {
135
+ throw ctx.runtime.newEOFError();
136
+ }
137
+ int readLength = Math.min(length, rawSize());
138
+ if (readLength == 0 && length > 0) {
139
+ return ctx.runtime.getNil();
140
+ } else if (readLength == 0) {
141
+ return ctx.runtime.newString();
142
+ } else {
143
+ ensureReadMode();
144
+ byte[] bytes = new byte[readLength];
145
+ buffer.get(bytes);
146
+ ByteList byteList = new ByteList(bytes, binaryEncoding);
147
+ return ctx.runtime.newString(byteList);
148
+ }
149
+ }
150
+
151
+ @JRubyMethod(name = "read", optional = 1)
152
+ public IRubyObject read(ThreadContext ctx, IRubyObject[] args) {
153
+ return readCommon(ctx, args, false);
154
+ }
155
+
156
+ @JRubyMethod(name = "read_all", optional = 1)
157
+ public IRubyObject readAll(ThreadContext ctx, IRubyObject[] args) {
158
+ return readCommon(ctx, args, true);
159
+ }
160
+
161
+ private IRubyObject skipCommon(ThreadContext ctx, IRubyObject _length, boolean raiseOnUnderflow) {
162
+ feed(ctx);
163
+ int length = (int) _length.convertToInteger().getLongValue();
164
+ if (raiseOnUnderflow && rawSize() < length) {
165
+ throw ctx.runtime.newEOFError();
166
+ }
167
+ ensureReadMode();
168
+ int skipLength = Math.min(length, rawSize());
169
+ buffer.position(buffer.position() + skipLength);
170
+ return ctx.runtime.newFixnum(skipLength);
171
+ }
172
+
173
+ @JRubyMethod(name = "skip")
174
+ public IRubyObject skip(ThreadContext ctx, IRubyObject length) {
175
+ return skipCommon(ctx, length, false);
176
+ }
177
+
178
+ @JRubyMethod(name = "skip_all")
179
+ public IRubyObject skipAll(ThreadContext ctx, IRubyObject length) {
180
+ return skipCommon(ctx, length, true);
181
+ }
182
+
183
+ public boolean hasIo() {
184
+ return io != null;
185
+ }
186
+
187
+ @JRubyMethod(name = "to_s", alias = {"to_str"})
188
+ public IRubyObject toS(ThreadContext ctx) {
189
+ ensureReadMode();
190
+ int length = buffer.limit() - buffer.position();
191
+ ByteList str = new ByteList(buffer.array(), buffer.position(), length, binaryEncoding, true);
192
+ return ctx.runtime.newString(str);
193
+ }
194
+
195
+ @JRubyMethod(name = "to_a")
196
+ public IRubyObject toA(ThreadContext ctx) {
197
+ return ctx.runtime.newArray(toS(ctx));
198
+ }
199
+
200
+ @JRubyMethod(name = "io")
201
+ public IRubyObject getIo(ThreadContext ctx) {
202
+ return io == null ? ctx.runtime.getNil() : io;
203
+ }
204
+
205
+ @JRubyMethod(name = "flush")
206
+ public IRubyObject flush(ThreadContext ctx) {
207
+ if (io == null) {
208
+ return ctx.runtime.getNil();
209
+ } else {
210
+ return io.callMethod(ctx, "flush");
211
+ }
212
+ }
213
+
214
+ @JRubyMethod(name = "close")
215
+ public IRubyObject close(ThreadContext ctx) {
216
+ if (io == null) {
217
+ return ctx.runtime.getNil();
218
+ } else {
219
+ return io.callMethod(ctx, "close");
220
+ }
221
+ }
222
+
223
+ @JRubyMethod(name = "write_to")
224
+ public IRubyObject writeTo(ThreadContext ctx, IRubyObject io) {
225
+ return io.callMethod(ctx, "write", readCommon(ctx, null, false));
226
+ }
227
+
228
+ public ByteList getBytes() {
229
+ byte[] bytes = new byte[rawSize()];
230
+ buffer.get(bytes);
231
+ return new ByteList(bytes, binaryEncoding);
232
+ }
233
+ }