Navigation Menu

Skip to content

Commit

Permalink
Extract from fluent-plugin-droonga
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Apr 10, 2014
0 parents commit bb34ee2
Show file tree
Hide file tree
Showing 12 changed files with 385 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
/Gemfile.lock
/pkg/
18 changes: 18 additions & 0 deletions Gemfile
@@ -0,0 +1,18 @@
# Copyright (C) 2013-2014 Droonga Project
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

source "https://rubygems.org"

gemspec
14 changes: 14 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,14 @@
Copyright (C) 2013-2014 Droonga Project

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 changes: 36 additions & 0 deletions README.md
@@ -0,0 +1,36 @@
# droonga-message-pack-packer

A MessagePack packer for Droonga message. In Droonga message, time
value should be formatted as
[W3C-TDF](http://www.w3.org/TR/NOTE-datetime) string. You need to
format all times in Droonga message as W3C-TDF string by yourself when
you use the original MessagePack packer. This library does it instead
of you. You can pass your Droonga message that may have time values
into MessagePack packer in this library.

## Installation

Add this line to your application's Gemfile:

gem 'droonga-message-pack-packer

And then execute:

$ bundle

Or install it yourself as:

$ gem install droonga-message-pack-packer

## Usage

require "droonga/message-pack-packer"
Droonga::MessagePackPacker.pack(message)

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
35 changes: 35 additions & 0 deletions Rakefile
@@ -0,0 +1,35 @@
# -*- mode: ruby; coding: utf-8 -*-
#
# Copyright (C) 2013-2014 Droonga Project
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

require "bundler/gem_helper"
require "packnga"

base_dir = File.join(File.dirname(__FILE__))

helper = Bundler::GemHelper.new(base_dir)
def helper.version_tag
version
end

helper.install
spec = helper.gemspec

Packnga::DocumentTask.new(spec) do |task|
task.original_language = "en"
task.translate_languages = ["ja"]
end

42 changes: 42 additions & 0 deletions droonga-message-pack-packer.gemspec
@@ -0,0 +1,42 @@
# Copyright (C) 2013-2014 Droonga Project
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'droonga/message-pack-packer/version'

Gem::Specification.new do |spec|
spec.name = "droonga-message-pack-packer"
spec.version = Droonga::MessagePackPacker::VERSION
spec.authors = ["Droonga Project"]
spec.email = ["droonga@groonga.org"]
spec.summary = "A MessagePack packer for Droonga message"
spec.description = "This MessagePack packer packs time values into W3C-TDF format string automatically. Because it is a rule in Droonga message."
spec.homepage = "https://github.com/droonga/droonga-message-pack-packer-ruby"
spec.license = "LGPL-2.1"
spec.required_ruby_version = '>= 1.9.3'

spec.files = `git ls-files`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_runtime_dependency "msgpack"

spec.add_development_dependency "bundler"
spec.add_development_dependency "rake"
spec.add_development_dependency "packnga"
spec.add_development_dependency "test-unit"
end
60 changes: 60 additions & 0 deletions lib/droonga/message-pack-packer.rb
@@ -0,0 +1,60 @@
# Copyright (C) 2013 Droonga Project
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

require "msgpack"

require "droonga/message-pack-packer/time-formatter"

module Droonga
class MessagePackPacker
class << self
def pack(object)
packer = new
packer.pack(object)
packer.to_s
end
end

MICRO_SECONDS_DECIMAL_PLACE = 6

def initialize
@packer = MessagePack::Packer.new
end

def pack(object)
case object
when Array
@packer.write_array_header(object.size)
object.each do |element|
pack(element)
end
when Hash
@packer.write_map_header(object.size)
object.each do |key, value|
pack(key)
pack(value)
end
when Time
@packer.write(TimeFormatter.format(object))
else
@packer.write(object)
end
end

def to_s
@packer.to_s
end
end
end
39 changes: 39 additions & 0 deletions lib/droonga/message-pack-packer/time-formatter.rb
@@ -0,0 +1,39 @@
# Copyright (C) 2013-2014 Droonga Project
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

require "time"

module Droonga
class MessagePackPacker
class TimeFormatter
class << self
def format(object)
formatter = new(object)
formatter.format
end
end

MICRO_SECONDS_DECIMAL_PLACE = 6

def initialize(time)
@time = time
end

def format
@time.utc.iso8601(MICRO_SECONDS_DECIMAL_PLACE)
end
end
end
end
20 changes: 20 additions & 0 deletions lib/droonga/message-pack-packer/version.rb
@@ -0,0 +1,20 @@
# Copyright (C) 2013-2014 Droonga Project
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

module Droonga
class MessagePackPacker
VERSION = "1.0.0"
end
end
39 changes: 39 additions & 0 deletions test/run-test.rb
@@ -0,0 +1,39 @@
#!/usr/bin/env ruby
#
# Copyright (C) 2013-2014 Droonga Project
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

require "rubygems"
require "bundler"
begin
Bundler.setup(:default, :development)
rescue Bundler::BundlerError => e
$stderr.puts(e.message)
$stderr.puts("Run `bundle install` to install missing gems")
exit(e.status_code)
end

require "test-unit"

base_dir = File.expand_path(File.join(File.dirname(__FILE__), ".."))
lib_dir = File.join(base_dir, "lib")
test_dir = File.join(base_dir, "test")

$LOAD_PATH.unshift(lib_dir)
$LOAD_PATH.unshift(test_dir)

ARGV.unshift("--max-diff-target-string-size=10000")

exit(Test::Unit::AutoRunner.run(true, test_dir))
51 changes: 51 additions & 0 deletions test/test-packer.rb
@@ -0,0 +1,51 @@
# Copyright (C) 2013-2014 Droonga Project
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

require "droonga/message-pack-packer"

class PackerTest < Test::Unit::TestCase
def test_integer
assert_equal(29, unpack(pack(29)))
end

def test_string
assert_equal("Droonga", unpack(pack("Droonga")))
end

def test_time
w3c_dtf_time = "2013-11-29T08:00:00.000000Z"
time = Time.parse(w3c_dtf_time)
assert_equal(w3c_dtf_time, unpack(pack(time)))
end

def test_hash
hash = {"key" => "value"}
assert_equal(hash, unpack(pack(hash)))
end

def test_array
array = ["Groonga", "Rroonga", "Droonga"]
assert_equal(array, unpack(pack(array)))
end

private
def pack(object)
Droonga::MessagePackPacker.pack(object)
end

def unpack(msgpack)
MessagePack.unpack(msgpack)
end
end
29 changes: 29 additions & 0 deletions test/test-time-formatter.rb
@@ -0,0 +1,29 @@
# Copyright (C) 2013-2014 Droonga Project
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

require "droonga/message-pack-packer/time-formatter"

class TimeFormatterTest < Test::Unit::TestCase
def test_fraction
w3c_dtf_time = "2013-11-29T08:00:00.292929Z"
time = Time.parse(w3c_dtf_time)
assert_equal(w3c_dtf_time, format(time))
end

private
def format(time)
Droonga::MessagePackPacker::TimeFormatter.format(time)
end
end

0 comments on commit bb34ee2

Please sign in to comment.