django-sharding icon indicating copy to clipboard operation
django-sharding copied to clipboard

Add mysql/utc implimentation of the sharded field

Open JBKahn opened this issue 7 years ago • 0 comments

I finished a sqlite version:

from itertools import cycle
from datetime import datetime
import pytz

def create_function_for_sharded_id(shard_id):
    get_next_seq_id = cycle(range(1024))
    og_epoch_datetime = datetime.utcfromtimestamp(0).replace(tzinfo=pytz.utc)

    epoch_datetime = datetime(2016, 10, 19, 17, 54, 24, tzinfo=pytz.UTC)
    epoch_milliseconds = (epoch_datetime - og_epoch_datetime).total_seconds()*1000

    def next_sharded_id():
        now = datetime.utcnow()
        now = now.replace(tzinfo=pytz.UTC)
        stored_diff = (now - epoch_datetime).total_seconds()*1000

        new_id = (int(stored_diff) << 23) | (shard_id << 10) | next(get_next_seq_id)
        return new_id
    return next_sharded_id


import sqlite3
con = sqlite3.connect(":memory:")
con.create_function("next_sharded_id", 0, create_function_for_sharded_id(31))
cur = con.cursor()
cur.execute("select next_sharded_id()", ())
new_id = cur.fetchone()[0]
print(new_id)

og_epoch_datetime = datetime.utcfromtimestamp(0).replace(tzinfo=pytz.utc)
epoch_datetime = datetime(2016, 10, 19, 17, 54, 24, tzinfo=pytz.UTC)
epoch_milliseconds = (epoch_datetime - og_epoch_datetime).total_seconds()*1000
datetime.fromtimestamp(((new_id >> 23) + epoch_milliseconds)/ 1000, tz=pytz.UTC) # now in utc
int(bin(new_id)[-10:], 2) # 0
int(bin(new_id)[-23:-10], 2) # 31

JBKahn avatar Mar 19 '17 19:03 JBKahn