Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mysql/utc implimentation of the sharded field #61

Open
JBKahn opened this issue Mar 19, 2017 · 0 comments
Open

Add mysql/utc implimentation of the sharded field #61

JBKahn opened this issue Mar 19, 2017 · 0 comments

Comments

@JBKahn
Copy link
Owner

JBKahn commented Mar 19, 2017

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant