Skip to content

Instantly share code, notes, and snippets.

@VeerpalBrar
VeerpalBrar / consistent_hashing.rb
Last active February 3, 2023 09:38
A consistent hashing implementation in ruby
require 'digest'
class ConsistentHashing
def initialize(nodes)
nodes.map { |node| add_node(node) }
end
def find_cache(key)
puts
hash = hash_value(key)
@VeerpalBrar
VeerpalBrar / validate_associated_n_queries.rb
Last active December 21, 2021 16:55
Fixing N+1 queries when using `validates_associated` with `has_many`
# Blog post: https://veerpalbrar.github.io/blog/2021/11/26/Fix-N+1-queries-when-using-validates_associated-with-has_many
### Setup
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "rails"
gem "sqlite3"
end
@VeerpalBrar
VeerpalBrar / quorum.rb
Last active May 11, 2022 18:04
Simple Quorum example in ruby
# Node is an abstraction of the db
class Node
attr_reader :id
def initialize(id)
@id = id
@data = {}
end
def read(key)
@data[key]
end
@VeerpalBrar
VeerpalBrar / countMinSketch.rb
Created September 22, 2023 19:44
A simple implementation of count min sketch in ruby
require 'digest'
class CountMinSketch
def initialize(width, length, hashes)
@width = width
@length = length
@hashes = hashes
@table = Array.new(length) { Array.new(width) { 0 } }
end