diff --git a/compile.py b/compile.py index 31537b1..672e97a 100644 --- a/compile.py +++ b/compile.py @@ -1,19 +1,17 @@ from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext +from Cython.Build import cythonize ext_modules = [ Extension("src.database", ["src/database.py"]), Extension("src.settings", ["src/settings.py"]), Extension("src.writes", ["src/writes.py"]), - Extension("src.handles", ["src/handles.py"]), Extension("schema.db_helpers", ["schema/db_helpers.py"]), - Extension("src.app", ["src/app.py"]) - ] # might need to add some external imports here too +] # might need to add some external imports here too setup( name="comment_server", cmdclass={"build_ext": build_ext}, - ext_modules=ext_modules, - compiler_directives={'language_level': '3'} + ext_modules=cythonize(ext_modules, compiler_directives={'language_level': '3'}) ) diff --git a/src/app.py b/src/app.py index beeacc4..60c5d01 100644 --- a/src/app.py +++ b/src/app.py @@ -1,3 +1,4 @@ +# cython: language_level=3 import logging import aiojobs.aiohttp @@ -8,8 +9,9 @@ import re import schema.db_helpers from src.database import obtain_connection from src.handles import api_endpoint +from src.handles import create_comment_scheduler from src.settings import config -from src.writes import create_comment_scheduler, DatabaseWriter +from src.writes import DatabaseWriter logger = logging.getLogger(__name__) diff --git a/src/database.py b/src/database.py index d1bc994..b1d57e5 100644 --- a/src/database.py +++ b/src/database.py @@ -1,10 +1,10 @@ +# cython: language_level=3 import logging import re import sqlite3 import time import typing -import aiosqlite import nacl.hash from src.settings import config @@ -152,61 +152,6 @@ def get_comments_by_id(conn, comment_ids: list) -> typing.Union[list, None]: )] -async def _insert_channel_async(db_file: str, channel_name: str, channel_id: str): - async with aiosqlite.connect(db_file) as db: - await db.execute('INSERT INTO CHANNEL(ClaimId, Name) VALUES (?, ?)', - (channel_id, channel_name)) - await db.commit() - - -async def _insert_comment_async(db_file: str, claim_id: str = None, comment: str = None, - channel_id: str = None, signature: str = None, parent_id: str = None) -> str: - timestamp = time.time_ns() - comment_prehash = ':'.join((claim_id, comment, str(timestamp),)) - comment_prehash = bytes(comment_prehash.encode('utf-8')) - comment_id = nacl.hash.sha256(comment_prehash).decode('utf-8') - async with aiosqlite.connect(db_file) as db: - await db.execute( - """ - INSERT INTO COMMENT(CommentId, LbryClaimId, ChannelId, Body, - ParentId, Signature, Timestamp) - VALUES (?, ?, ?, ?, ?, ?, ?) - """, - (comment_id, claim_id, channel_id, comment, parent_id, signature, timestamp) - ) - await db.commit() - return comment_id - - -async def create_comment_async(db_file: str, comment: str, claim_id: str, **kwargs): - channel_id = kwargs.pop('channel_id', '') - channel_name = kwargs.pop('channel_name', '') - if channel_id or channel_name: - try: - validate_input( - comment=comment, - claim_id=claim_id, - channel_id=channel_id, - channel_name=channel_name, - ) - await _insert_channel_async(db_file, channel_name, channel_id) - except AssertionError: - raise TypeError('Invalid parameters given to input validation') - else: - channel_id = config['ANONYMOUS']['CHANNEL_ID'] - comment_id = await _insert_comment_async( - db_file=db_file, comment=comment, claim_id=claim_id, channel_id=channel_id, **kwargs - ) - async with aiosqlite.connect(db_file) as db: - db.row_factory = aiosqlite.Row - curs = await db.execute( - 'SELECT * FROM COMMENTS_ON_CLAIMS WHERE comment_id = ?', (comment_id,) - ) - thing = await curs.fetchone() - await curs.close() - return dict(thing) if thing else None - - if __name__ == '__main__': pass # __generate_database_schema(connection, 'comments_ddl.sql') diff --git a/src/handles.py b/src/handles.py index c25d49a..589a4d1 100644 --- a/src/handles.py +++ b/src/handles.py @@ -1,14 +1,18 @@ +# cython: language_level=3 import json import logging import asyncio +import aiojobs +from asyncio import coroutine from aiohttp import web from aiojobs.aiohttp import atomic -from src.writes import write_comment +from src.writes import DatabaseWriter from src.database import get_claim_comments from src.database import get_comments_by_id, get_comment_ids from src.database import obtain_connection +from src.database import create_comment logger = logging.getLogger(__name__) @@ -38,6 +42,15 @@ def handle_get_comments_by_id(app, **kwargs): return get_comments_by_id(conn, **kwargs) +async def create_comment_scheduler(): + return await aiojobs.create_scheduler(limit=1, pending_limit=0) + + +async def write_comment(**comment): + with DatabaseWriter._writer.connection as conn: + return await coroutine(create_comment)(conn, **comment) + + async def handle_create_comment(scheduler, **kwargs): job = await scheduler.spawn(write_comment(**kwargs)) return await job.wait() diff --git a/src/settings.py b/src/settings.py index 6f6c3e7..6f8b30c 100644 --- a/src/settings.py +++ b/src/settings.py @@ -1,3 +1,4 @@ +# cython: language_level=3 import json import logging import pathlib diff --git a/src/writes.py b/src/writes.py index 8556880..4868caf 100644 --- a/src/writes.py +++ b/src/writes.py @@ -1,10 +1,7 @@ import atexit import logging -import aiojobs -from asyncio import coroutine - -from src.database import obtain_connection, create_comment +from src.database import obtain_connection logger = logging.getLogger(__name__) @@ -31,12 +28,3 @@ class DatabaseWriter(object): @property def connection(self): return self.conn - - -async def create_comment_scheduler(): - return await aiojobs.create_scheduler(limit=1, pending_limit=0) - - -async def write_comment(**comment): - with DatabaseWriter._writer.connection as conn: - return await coroutine(create_comment)(conn, **comment)