Skip to content

Commit

Permalink
sql connection pooling
Browse files Browse the repository at this point in the history
  • Loading branch information
kheina committed Jul 6, 2024
1 parent b04993c commit fde562c
Show file tree
Hide file tree
Showing 12 changed files with 286 additions and 126 deletions.
9 changes: 9 additions & 0 deletions db/1/00-rename-post-datetimes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ RENAME created_on TO created;
ALTER TABLE kheina.public.posts
RENAME updated_on TO updated;

ALTER TABLE kheina.public.post_votes
RENAME created_on TO created;

ALTER TABLE kheina.public.posts
RENAME privacy_id TO privacy;

Expand All @@ -15,3 +18,9 @@ RENAME created_on TO created;

ALTER TABLE kheina.public.users
RENAME privacy_id TO privacy;

ALTER TABLE kheina.public.configs
RENAME created_on TO created;

ALTER TABLE kheina.public.configs
RENAME updated_on TO updated;
13 changes: 13 additions & 0 deletions db/1/01-set-post-foreign-keys.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ALTER TABLE kheina.public.set_post
ADD CONSTRAINT set_post_sets_fk
FOREIGN KEY (set_id)
references kheina.public.sets (set_id)
on delete cascade
on update cascade;

ALTER TABLE kheina.public.set_post
ADD CONSTRAINT set_post_posts_fk
FOREIGN KEY (post_id)
references kheina.public.posts (post_id)
on delete cascade
on update cascade;
10 changes: 5 additions & 5 deletions docker/aerospike/aerospike.conf
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ namespace kheina {
allow-ttl-without-nsup true
# To use file storage backing, comment out the line above and use the
# following lines instead.
# storage-engine device {
# file /opt/aerospike/data/bar.dat
# filesize 16G
# data-in-memory true # Store data in memory in addition to file.
# }
# storage-engine device {
# file /opt/aerospike/data/bar.dat
# filesize 16G
# data-in-memory true # Store data in memory in addition to file.
# }
}
6 changes: 3 additions & 3 deletions posts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ class Config:
rating: int
parent: Optional[int] = None
privacy: int
created: Optional[datetime] = Field(description='orm:"default"')
updated: Optional[datetime] = Field(description='orm:"default"')
created: datetime
updated: datetime
filename: Optional[str] = None
media_type: Optional[int] = None
size: Optional[PostSize] = Field(description='orm:"map[width:width,height:height]"')
size: Optional[PostSize] = Field(None, description='orm:"map[width:width,height:height]"')
thumbhash: Optional[str] = None


Expand Down
42 changes: 14 additions & 28 deletions posts/repository.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from asyncio import Task, ensure_future
from collections import defaultdict
from dataclasses import dataclass
from datetime import datetime, timedelta
from shared.datetime import datetime
from math import ceil
from typing import Any, Callable, Dict, Iterable, List, Mapping, Optional, Self, Set, Tuple, Union

Expand Down Expand Up @@ -158,33 +158,21 @@ def internal_select(self: Self, query: Query) -> Callable[[List[Tuple[int, str,

@AerospikeCache('kheina', 'posts', '{post_id}', _kvs=PostKVS)
async def _get_post(self: Self, post_id: PostId) -> InternalPost :
data = await self.query_async("""
SELECT
posts.post_id,
posts.title,
posts.description,
posts.rating,
posts.parent,
posts.created,
posts.updated,
posts.filename,
posts.media_type,
posts.width,
posts.height,
posts.uploader,
posts.privacy,
posts.thumbhash
FROM kheina.public.posts
WHERE posts.post_id = %s;
""",
(post_id.int(),),
fetch_one=True,
ipost: InternalPost = InternalPost(
post_id=post_id.int(),
user_id=1,
rating=1,
privacy=1,
created=datetime.zero(),
updated=datetime.zero(),
size=None,
)

if not data :
raise NotFound(f'no data was found for the provided post id: {post_id}.')
try :
return await self.select(ipost)

return self.parse_response([data])[0]
except KeyError :
raise NotFound(f'no data was found for the provided post id: {post_id}.')


async def post(self: Self, ipost: InternalPost, user: KhUser) -> Post :
Expand Down Expand Up @@ -406,7 +394,7 @@ def _validateVote(self: Self, vote: Optional[bool]) -> None :

async def _vote(self: Self, user: KhUser, post_id: PostId, upvote: Optional[bool]) -> Score :
self._validateVote(upvote)
with self.transaction() as transaction :
async with self.transaction() as transaction :
data: Tuple[int, int, datetime] = await transaction.query_async("""
INSERT INTO kheina.public.post_votes
(user_id, post_id, upvote)
Expand Down Expand Up @@ -527,8 +515,6 @@ async def users_many(self, user_ids: List[int]) -> Dict[int, InternalUser] :
elif datum[11] :
verified = Verified.artist

print('==> priv:', datum[3], 'mapped:', privacy_map[datum[3]], 'map:', privacy_map)

user: InternalUser = InternalUser(
user_id = datum[0],
name = datum[1],
Expand Down
15 changes: 9 additions & 6 deletions sets/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime
from typing import List, Optional

from pydantic import BaseModel, conint, conlist, constr, validator
from pydantic import BaseModel, Field, conint, conlist, constr, validator

from posts.models import Post, PostIdValidator, _post_id_converter
from shared.models._shared import PostId, SetId, SetIdValidator, UserPortable, UserPrivacy
Expand Down Expand Up @@ -66,7 +66,7 @@ class UpdateSetRequest(BaseModel) :

class AddPostToSetRequest(BaseModel) :
_post_id_validator = PostIdValidator
_set_id_validator = SetIdValidator
_set_id_validator = SetIdValidator

post_id: PostId
set_id: SetId
Expand All @@ -76,13 +76,16 @@ class AddPostToSetRequest(BaseModel) :
class InternalSet(BaseModel) :
_post_id_converter = validator('first', 'last', pre=True, always=True, allow_reuse=True)(_post_id_converter)

set_id: int
class Config:
validate_assignment = True

set_id: int = Field(description='orm:"pk"')
owner: int
count: int
count: int = Field(description='orm:"-"')
title: Optional[str]
description: Optional[str]
privacy: int
created: datetime
updated: datetime
first: Optional[PostId] = None
last: Optional[PostId] = None
first: Optional[PostId] = Field(None, description='orm:"-"')
last: Optional[PostId] = Field(None, description='orm:"-"')
11 changes: 1 addition & 10 deletions sets/sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,16 +267,7 @@ async def delete_set(self: Self, user: KhUser, set_id: SetId) -> None :
if not Sets._verify_authorized(user, iset) :
raise NotFound(SetNotFound.format(set_id=set_id))

await self.query_async("""
DELETE FROM kheina.public.set_post
WHERE set_id = %s;
DELETE FROM kheina.public.sets
WHERE set_id = %s;
""",
(set_id.int(), set_id.int()),
commit=True,
)

await self.delete(iset)
await SetKVS.remove_async(set_id)


Expand Down
5 changes: 5 additions & 0 deletions shared/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ def fromtimestamp(cls, timestamp: Union[int, float], timezone: timezone = timezo
@classmethod
def now(cls, timezone: timezone = timezone.utc) :
return super().now(timezone)


@classmethod
def zero(cls, timezone: timezone = timezone.utc) :
return cls.fromtimestamp(0, timezone)
Loading

0 comments on commit fde562c

Please sign in to comment.