Skip to content

Instantly share code, notes, and snippets.

View sonus21's full-sized avatar

Sonu Kumar sonus21

View GitHub Profile
@sonus21
sonus21 / KUID.java
Last active December 30, 2023 08:29
KUID: Compressed Universally Unique Identifier (22 bytes)
/*
* Copyright (c) 2023 Sonu Kumar, sonunitw12@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@sonus21
sonus21 / counter_tag.py
Last active October 11, 2023 05:01
Django counter tag, can generate counter in case of single or multiple for loop.
#Adapted from https://djangosnippets.org/snippets/2619/
@register.tag(name='counter')
def do_counter(parser, token):
"""
Counter tag. Can be used to output and increment a counter.
Usage:
- {% counter %} to output and post-increment the counter variable
- {% counter reset %} to reset the counter variable to 1
@sonus21
sonus21 / db_router.py
Last active April 9, 2023 15:07
Django DB Router
import threading
threadLocal = threading.local()
def get_current_db_name():
return getattr(threadLocal, "DB", None)
def set_db_for_router(db):
@sonus21
sonus21 / settings.py
Last active April 9, 2023 15:07
DB Setting
INSTALLED_APPS = [
# ...
"CoreApp"
# ...
]
MIDDLEWARE = [
@sonus21
sonus21 / middlewares.py
Last active April 9, 2023 15:07
Database Middleware and decorator
from functools import wraps
from .utils import db_from_the_request
from .db_router import set_db_for_router, clear_db_for_router
class DatabaseMiddleware(object):
"""
DatabaseMiddleware sets the db in the thread local context and used
across multiple calls to handle the DB calls.
@sonus21
sonus21 / consumer.py
Last active April 9, 2023 15:07
db_wrapper usage in consumer.py
from .constants import SECONDARY, PRIMARY
from CoreApp.middlewares import db_wrapper
@db_wrapper(PRIMARY)
def reserve_driver(request):
# logic goes here
pass
@sonus21
sonus21 / Database.java
Last active April 9, 2023 15:07
Database annotation
/**
* A Database annotation that should be used at controller,worker,consumer,methods or classes
* to route the database traffic to appropriate node.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface Database {
String value() default "";
}
@sonus21
sonus21 / DatabaseContext.java
Created April 9, 2023 10:51
Database Context holder
public class DatabaseContext {
private static final ThreadLocal<String> databaseName = new ThreadLocal<>();
public static void set(String name) {
databaseName.set(name);
}
public static void clear() {
databaseName.remove();
}
@sonus21
sonus21 / DatabaseRouter.java
Created April 9, 2023 10:51
Database router
public class DatabaseRouter extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DatabaseContext.get();
}
}
@sonus21
sonus21 / SqlDatabaseConfiguration.java
Created April 9, 2023 10:52
SqlDatabaseConfiguration
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"com.github.sonus21.readwrite.repositories"})
@EntityScan(basePackages = "com.github.sonus21.readwrite.models.entities")
public class SqlDatabaseConfiguration {
private final DataSourceSecondaryConfig dataSourceSecondaryConfig;
private final DataSourcePrimaryConfig dataSourcePrimaryConfig;
@Autowired