Skip to content

Instantly share code, notes, and snippets.

View arpitbbhayani's full-sized avatar
🎲
building @DiceDB

Arpit Bhayani arpitbbhayani

🎲
building @DiceDB
View GitHub Profile
@arpitbbhayani
arpitbbhayani / taxonomy.sql
Last active July 29, 2023 15:09
Quickly populate random 5 million + rows in a taxonomy using SQL
# create table topics!
create table topics (
id int primary key auto_increment,
name varchar(256),
parent_id int null,
type smallint not null,
foreign key (parent_id) references topics(id),
index(type)
);
import bisect
import random
import time
from datetime import datetime
def get_locations_1(x, arr):
return [bisect.bisect_left(l, x) for l in arr]
def get_pos_arr(arr):
# For each of the element in `arr` positions will hold
$ SHOW ENGINE INNODB STATUS
----------------------
BUFFER POOL AND MEMORY
----------------------
Total memory allocated 137363456; in additional pool allocated 0
Dictionary memory allocated 159646
Buffer pool size 8191
Free buffers 7741
Database pages 449
Old database pages 0
def get_page(page_id:int) -> Page:
# Check if the page is available in the cache
page = cache.get_page(page_id)
# if the page is retrieved from the main memory
# return the page.
if page:
return page
def is_allowed(key:str) -> Bool:
"""The function decides is the current request should be served or not.
It accepts the configuration key `key` and checks the number of requests made against it
as per the configuration.
The function returns True if the request goes through and False otherwise.
"""
current_time = int(time.time())
# Fetch the configuration for the given key
# the configuration holds the number of requests allowed in a time window.
def register_request(key, ts):
store[key][ts] += 1
def get_current_window(key, start_time):
ts_data = requests_store.get(key)
if not key:
return 0
total_requests = 0
for ts, count in ts_data.items():
if ts > start_time:
total_requests += count
else:
def get_ratelimit_config(key):
value = cache.get(key)
if not value:
value = config_store.get(key)
cache.put(key, value)
return value
export PYTHONSTARTUP="$HOME/ipython.py"
# -*- coding: utf-8 -*-
import sys
class IPythonPromptPS1(object):
def __init__(self):
self.line = 0
def __str__(self):
self.line += 1
return "\033[92mIn [%d]:\033[0m " % (self.line)
sys.ps1 = IPythonPromptPS1()
sys.ps2 = " \033[91m...\033[0m "