Skip to content

Commit a0d947c

Browse files
committed
first working POC
0 parents  commit a0d947c

File tree

7 files changed

+98
-0
lines changed

7 files changed

+98
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
__pycache__
2+
*.pyc
3+
*.egg-info
4+
.cache

MANIFEST.in

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include README.md
2+
include requirements.txt

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Addok Postgresql document storage
2+
3+
Store your documents into a postgresql database to save Redis RAM usage.

addok_pg/__init__.py

Whitespace-only changes.

addok_pg/plugin.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import psycopg2
2+
import psycopg2.pool
3+
import os
4+
5+
from addok.config import config
6+
7+
8+
class PGStore:
9+
cur = None
10+
11+
def __init__(self, *args, **kwargs):
12+
with PGStore.conn() as conn:
13+
cur = conn.cursor()
14+
cur.execute('CREATE TABLE IF NOT EXISTS '+config.PG_TABLE+' (key VARCHAR, data bytea)')
15+
cur.execute('CREATE UNIQUE INDEX IF NOT EXISTS '+config.PG_TABLE+'_key_idx ON '+config.PG_TABLE+' (key)')
16+
conn.commit()
17+
18+
def conn():
19+
return(psycopg2.connect(config.PG_CONFIG))
20+
21+
def fetch(self, *keys):
22+
if not keys: # Avoid invalid SQL.
23+
return
24+
keys = [key.decode() for key in keys]
25+
with PGStore.conn().cursor() as cur:
26+
params = ','.join("'%s'" % k for k in keys)
27+
query = 'SELECT key, data FROM '+config.PG_TABLE+' WHERE key IN ('+params+')'
28+
cur.execute(query)
29+
for key, data in cur.fetchall():
30+
yield key.encode(), data
31+
32+
def add(self, *docs):
33+
with PGStore.conn() as conn:
34+
cur = conn.cursor()
35+
values = ','.join(cur.mogrify("(%s,%s)", d).decode("utf-8") for d in docs)
36+
cur.execute('INSERT INTO '+config.PG_TABLE+' (key,data) VALUES ' + values + ' ON CONFLICT DO NOTHING')
37+
conn.commit()
38+
39+
def remove(self, *keys):
40+
with PGStore.conn() as conn:
41+
conn.cursor().executemany('DELETE FROM '+config.PG_TABLE+' WHERE key=%s', (keys, ))
42+
conn.commit()
43+
44+
45+
def preconfigure(config):
46+
config.DOCUMENT_STORE = 'addok_pg.plugin.PGStore'
47+
config.PG_CONFIG = 'dbname=addok user=addok host=localhost password=addok'
48+
config.PG_TABLE = 'addok'

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
psycopg2

setup.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from setuptools import setup, find_packages
2+
from codecs import open # To use a consistent encoding
3+
from os import path
4+
5+
VERSION = (0, 1, 0)
6+
7+
here = path.abspath(path.dirname(__file__))
8+
9+
# Get the long description from the relevant file
10+
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
11+
long_description = f.read()
12+
13+
setup(
14+
name='addok-pg',
15+
version=".".join(map(str, VERSION)),
16+
description="Store documents in postgresql.",
17+
long_description=long_description,
18+
url='https://github.com/addok/addok-pg',
19+
author='Christian Quest',
20+
author_email='[email protected]',
21+
license='WTFPL',
22+
23+
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
24+
classifiers=[
25+
'Development Status :: 4 - Beta',
26+
27+
'Intended Audience :: Developers',
28+
'Topic :: Scientific/Engineering :: GIS',
29+
30+
'Programming Language :: Python :: 3',
31+
'Programming Language :: Python :: 3.4',
32+
'Programming Language :: Python :: 3.5',
33+
'Programming Language :: Python :: 3.6',
34+
],
35+
keywords='addok geocoding postgresql',
36+
packages=find_packages(exclude=['tests']),
37+
extras_require={'test': ['pytest']},
38+
include_package_data=True,
39+
entry_points={'addok.ext': ['postgresql=addok_pg.plugin']},
40+
)

0 commit comments

Comments
 (0)