Skip to content

Instantly share code, notes, and snippets.

@jarthod
jarthod / compressed_hash.rb
Last active Aug 16, 2022 — forked from romanbsd/compressed.rb
Compressed fields in Mongoid 6.4+
View compressed_hash.rb
require 'zstd-ruby'
class CompressedHash < Hash
DICTIONARY = IO.read('config/dictionaries/1')
def mongoize
if size > 0 # only compress non-empty hash
# BSON::Binary.new(Zstd.compress(self.to_bson.to_s))
BSON::Binary.new(Zstd.compress_using_dict(self.to_bson.to_s, DICTIONARY))
else
self