Skip to content

Instantly share code, notes, and snippets.

@tagomoris
Created July 13, 2017 02:32
Show Gist options
  • Save tagomoris/4b22651072aadc74b0ab9ecdfcf658a2 to your computer and use it in GitHub Desktop.
Save tagomoris/4b22651072aadc74b0ab9ecdfcf658a2 to your computer and use it in GitHub Desktop.
{:here=>"write0", :length=>124}
{:read=>
{:timekey=>24998551,
:tagkey=>"hoge.pos.moge.hogehogehogehoge",
:keys=>{:moge=>111},
:id=>"d0d71b3d-b680-4549-b805-a08825e0f71d",
:s=>1024,
:c=>1499913050,
:m=>1499913110}}
{:here=>"write1", :length=>102}
{:read=>
{:timekey=>24998551,
:tagkey=>"hoge.pos",
:keys=>{:moge=>111},
:id=>"0f98e5f1-e7cb-41e6-a95e-99c1085647c9",
:s=>20480,
:c=>1499913050,
:m=>1499913110}}
{:here=>"write0", :length=>124}
{:read=>
{:timekey=>24998551,
:tagkey=>"hoge.pos.moge.hogehogehogehoge",
:keys=>{:moge=>111},
:id=>"dd82f6fb-eeb4-47b9-86e9-7aff59a7e1ec",
:s=>1024,
:c=>1499913050,
:m=>1499913110}}
{:here=>"write1", :length=>102}
{:read=>
{:timekey=>24998551,
:tagkey=>"hoge.pos",
:keys=>{:moge=>111},
:id=>"547caaa2-1684-4e84-8729-dc58a1a98426",
:s=>20480,
:c=>1499913050,
:m=>1499913110}}
require 'securerandom'
require 'pp'
require 'msgpack'
def msgpack_factory
MessagePack::Factory.new
end
def msgpack_packer(*args)
msgpack_factory.packer(*args)
end
def msgpack_unpacker(*args)
msgpack_factory.unpacker(*args)
end
def write_data(data, truncate: true)
File.open("testing0", 'wb+', 0644) do |file|
file.set_encoding(Encoding::ASCII_8BIT)
file.sync = true
file.binmode
file.seek(0, IO::SEEK_SET)
file.write(data)
file.truncate(data.bytesize) if truncate
end
end
def read
File.open("testing0", "rb+") do |file|
file.set_encoding(Encoding::ASCII_8BIT)
file.sync = true
file.binmode
bindata = file.read
data = msgpack_unpacker(symbolize_keys: true).feed(bindata).read rescue {}
pp(read: data)
end
end
def write0
data = {
timekey: Time.now.to_i / 60,
tagkey: 'hoge.pos.moge.hogehogehogehoge',
keys: {'moge' => 111},
id: SecureRandom.uuid,
s: 1024,
c: (Time.now.to_i - 60),
m: Time.now.to_i,
}
bin = msgpack_packer.pack(data).to_s
pp(here: 'write0', length: bin.bytesize)
write_data(bin)
end
def write1(truncate: true)
data = {
timekey: Time.now.to_i / 60,
tagkey: 'hoge.pos',
keys: {'moge' => 111},
id: SecureRandom.uuid,
s: 20480,
c: (Time.now.to_i - 60),
m: Time.now.to_i,
}
bin = msgpack_packer.pack(data).to_s
pp(here: 'write1', length: bin.bytesize)
write_data(bin, truncate: truncate)
end
File.unlink("testing0")
write0 # longer than 1
read
write1
read
File.unlink("testing0")
write0 # longer than 1
read
write1(truncate: false)
read
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment