Skip to main content

Python bindings for the sophia database.

Project description

Sophia Library Latest Version https://img.shields.io/pypi/wheel/sonya.svg https://img.shields.io/pypi/pyversions/sonya.svg https://img.shields.io/pypi/l/sonya.svg

Sonya

sonya, fast Python bindings for Sophia embedded database, v2.2.

About sonya:

  • Written in Cython for speed and low-overhead

  • Clean, memorable APIs

  • Extensive support for Sophia’s features

  • Python 2 and Python 3 support

  • No 3rd-party dependencies besides Cython (for python>3)

About Sophia:

Sophia Library
  • Document store

  • ACID transactions

  • MVCC, optimistic, non-blocking concurrency with multiple readers and writers.

  • Multiple databases per environment

  • Multiple- and single-statement transactions across databases

  • Prefix searches

  • Automatic garbage collection

  • Hot backup

  • Compression

  • Multi-threaded compaction

  • mmap support, direct I/O support

  • APIs for variety of statistics on storage engine internals

  • BSD licensed

Some ideas of where Sophia might be a good fit:

  • Running on application servers, low-latency / high-throughput

  • Time-series

  • Analytics / Events / Logging

  • Full-text search

  • Secondary-index for external data-store

Limitations:

  • Not tested on Windows.

If you encounter any bugs in the library, please open an issue, including a description of the bug and any related traceback.

Installation

The sophia sources are bundled with the sonya source code, so the only thing you need to install is Cython. You can install from GitHub or from PyPi.

Pip instructions:

$ pip install Cython   # Optional
$ pip install sonya

Or to install the latest code from master:

$ pip install Cython   # Required
$ pip install git+https://github.com/mosquito/sonya#egg=sonya

Git instructions:

$ pip install Cython
$ git clone https://github.com/mosquito/sonya
$ cd sonya
$ python setup.py build
$ python setup.py install

To run the tests:

$ pip install pytest
$ pytest tests

Overview

Sonya addition to normal dictionary operations, you can read slices of data that are returned efficiently using cursors. Similarly, bulk writes using update() use an efficient, atomic batch operation.

Despite the simple APIs, Sophia has quite a few advanced features. There is too much to cover everything in this document, so be sure to check out the official Sophia storage engine documentation.

The next section will show how to perform common actions with sonya.

Using Sonya

Let’s begin by import sonya and creating an environment. The environment can host multiple databases, each of which may have a different schema. In this example our database will store python objects as the key and value. Finally we’ll open the environment so we can start storing and retrieving data.

from sonya import Environment, fields, Schema


class DictSchema(Schema):
    key = fields.PickleField(index=0)
    value = fields.PickleField()


env = Environment('/tmp/test-env')
db = env.database('test-database', DictSchema(), compression='zstd')
env.open()

document = db.document(key='foo', value=[1, 2, 3, 'bar'])

# Insert a document
db.set(document)

print(db.get(key='foo'))
# {'value': [1, 2, 3, 'bar'], 'key': 'foo'}

CRUD operations

Sonya

from sonya import Environment, fields, Schema


class DictSchema(Schema):
    key = fields.PickleField(index=0)
    value = fields.PickleField()


env = Environment('/tmp/test-env')
db = env.database('test-database', DictSchema(), compression='zstd')
env.open()

document = db.document(key='foo', value=[1, 2, 3, 'bar'])

# Create a document
db.set(document)

# Read document
document = db.get(key='foo')

# Update the document
document = db.document(key='foo', value=None)
db.set(document)

# Delete the document
document = db.document(key='foo', value=None)
db.delete(key='foo')

# Iterate through the database
for document in db.cursor():
   print(document)

# Delete multiple documents
# fastest method for remove multiple documents from database
db.delete_many(order='>=')

Fetching ranges (Cursors)

Because Sophia is an ordered data-store, performing ordered range scans is efficient. To retrieve a range of key-value pairs with Sonya dictionary lookup with a slice instead.

For finer-grained control over iteration, or to do prefix-matching, Sonya provides a cursor interface.

The cursor() method accepts special keyword parameter order and all key fields:

  • order (default=`>=`) – semantics for matching the start key and ordering results.

from sonya import Environment, fields, Schema


class IntSchema(Schema):
    key = fields.UInt32Field(index=0)
    value = fields.PickleField()


env = Environment('/tmp/test-env')
db = env.database('test-integer-db', IntSchema(), compression='zstd')
env.open()


with db.transaction() as tx:
    for i in range(10000):
        tx.set(db.document(key=i, value=None))

# Iterate through the database
for document in db.cursor(order='>=', key=9995):
    print(document)

# {'key': 9995, 'value': None}
# {'key': 9996, 'value': None}
# {'key': 9997, 'value': None}
# {'key': 9998, 'value': None}
# {'key': 9999, 'value': None}

For prefix search use a part of the key and order:

from sonya import Environment, fields, Schema


class StringSchema(Schema):
    key = fields.StringField(index=0)
    value = fields.PickleField()


env = Environment('/tmp/test-env')
db = env.database('test-string-db', IntSchema(), compression='zstd')
env.open()


with db.transaction() as tx:
    for i in range(10000):
        tx.set(db.document(key=str(i), value=None))

# Iterate through the database
for document in db.cursor(order='>=', key='999'):
    print(document)

# {'value': None, 'key': '999'}
# {'value': None, 'key': '9990'}
# {'value': None, 'key': '9991'}
# {'value': None, 'key': '9992'}
# {'value': None, 'key': '9993'}
# {'value': None, 'key': '9994'}
# {'value': None, 'key': '9995'}
# {'value': None, 'key': '9996'}
# {'value': None, 'key': '9997'}
# {'value': None, 'key': '9998'}
# {'value': None, 'key': '9999'}

Deleting multiple documents

Sonya provides delete_many method. This method is fastest option when you want to remove multiple documents from the database. The method has cursor-like interface. The whole operation will be processed in the one transaction.

The method returns number of affected rows.

from sonya import Environment, fields, Schema


class IntSchema(Schema):
    key = fields.UInt32Field(index=0)
    value = fields.PickleField()


env = Environment('/tmp/test-env')
db = env.database('test-integer-db', IntSchema(), compression='zstd')
env.open()


with db.transaction() as tx:
    for i in range(10000):
        tx.set(db.document(key=i, value=None))

# returns the number of affected rows
db.delete_many(order='>=', key=9995):

Document count

The Database objects has a __len__ method. Please avoid to use it for any big database, it iterates and count the documents each time (faster then using len(list(db.cursor())) but still has O(n) complexity).

from sonya import Environment, fields, Schema


class IntSchema(Schema):
    key = fields.UInt32Field(index=0)
    value = fields.PickleField()


env = Environment('/tmp/test-env')
db = env.database('test-integer-db', IntSchema(), compression='zstd')
env.open()


with db.transaction() as tx:
    for i in range(10000):
        tx.set(db.document(key=i, value=None))

print(len(db))
# 10000

Transactions

Sophia supports ACID transactions. Even better, a single transaction can cover operations to multiple databases in a given environment.

Example usage:

class Users(Schema):
    name = fields.StringField(index=0)
    surname = fields.StringField(index=1)
    age = fields.UInt8Field()


with users.transaction() as tx:
    tx.set(users.document(name='Jane', surname='Doe', age=19))
    tx.set(users.document(name='John', surname='Doe', age=18))

    # Raises LookupError
    db.get(name='John', surname='Doe')

Multiple transactions are allowed to be open at the same time, but if there are conflicting changes, an exception will be thrown when attempting to commit the offending transaction.

Configuring and Administering Sophia

Sophia can be configured using special properties on the Sophia and Database objects. Refer to the configuration document for the details on the available options, including whether they are read-only, and the expected data-type.

For example, to query Sophia’s status, you can use the status property, which is a readonly setting returning a string:

>>> print(env['sophia.status'])
"online"

Other properties can be changed by assigning a new value to the property. For example, to read and then increase the number of threads used by the scheduler:

>>> env['scheduler.threads'] = env['scheduler.threads'] + 2
>>> env.open()
>>> print(env['scheduler.threads'])
8
>>> print(dict(env))
{'db.test-string-db.stat.cursor_latency': '0 0 0.0', ...}

Refer to the documentation for complete lists of settings. Dotted-paths are translated into underscore-separated attributes.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sonya-0.6.6.tar.gz (290.2 kB view details)

Uploaded Source

Built Distributions

sonya-0.6.6-cp36-cp36m-manylinux1_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.6m

sonya-0.6.6-cp36-cp36m-macosx_10_6_intel.whl (862.0 kB view details)

Uploaded CPython 3.6m macOS 10.6+ intel

sonya-0.6.6-cp36-cp36m-linux_armv7l.whl (1.2 MB view details)

Uploaded CPython 3.6m

sonya-0.6.6-cp35-cp35m-manylinux1_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.5m

sonya-0.6.6-cp35-cp35m-macosx_10_6_intel.whl (1.5 MB view details)

Uploaded CPython 3.5m macOS 10.6+ intel

sonya-0.6.6-cp34-cp34m-manylinux1_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.4m

sonya-0.6.6-cp34-cp34m-macosx_10_6_intel.whl (1.5 MB view details)

Uploaded CPython 3.4m macOS 10.6+ intel

sonya-0.6.6-cp27-cp27m-manylinux1_x86_64.whl (1.2 MB view details)

Uploaded CPython 2.7m

File details

Details for the file sonya-0.6.6.tar.gz.

File metadata

  • Download URL: sonya-0.6.6.tar.gz
  • Upload date:
  • Size: 290.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for sonya-0.6.6.tar.gz
Algorithm Hash digest
SHA256 5d2ec6c92a3d65de3826d5f6a53fc78060f70136c4ce857c823277a25fc06802
MD5 6b4c3243bc1f61b3973c168f087a56cf
BLAKE2b-256 ecd7252af8f0d26dbb5808c06bbced9b2a427939a2633e0764757cc8d599ea65

See more details on using hashes here.

File details

Details for the file sonya-0.6.6-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for sonya-0.6.6-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c308c60ec59c68d89dc9755ae018d52c308cabde7fb44a2d67210c483b54c6c2
MD5 0a542165ae871bcd33751ca70085b9c6
BLAKE2b-256 ad4d5ccfa4c07269681d2876b329a4ad16f3e02b8b62a2b6557e8bc07220dd96

See more details on using hashes here.

File details

Details for the file sonya-0.6.6-cp36-cp36m-macosx_10_6_intel.whl.

File metadata

File hashes

Hashes for sonya-0.6.6-cp36-cp36m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 e4e7e49a6620f5dbe9763506dc5fb17b5a9784111af47d29f5b2748a629fca97
MD5 8785b5c12973667ac48339edc54f40b2
BLAKE2b-256 f533494f9c2356ddf66a9fac68cfc6f7c230aefd092c9f268ccc3595cffe55c6

See more details on using hashes here.

File details

Details for the file sonya-0.6.6-cp36-cp36m-linux_armv7l.whl.

File metadata

File hashes

Hashes for sonya-0.6.6-cp36-cp36m-linux_armv7l.whl
Algorithm Hash digest
SHA256 7db6875991d2aae073b6597cc13ce1febbb3ef72d84110f02f6c4cad17ab6c01
MD5 52c52ba9150c034cbd89a2eb64da7c6f
BLAKE2b-256 efcc3eb1365092a7a2641b96cd2ac3e59b835e741fedc77a7fd1ade6627cbd1d

See more details on using hashes here.

File details

Details for the file sonya-0.6.6-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for sonya-0.6.6-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6f4de40f6cdd593a75fcb9c69b1c8c3c6f434b0662b4fbe224694277f515fe13
MD5 2bead2aeadf1a6587fb1fd49b6e61a48
BLAKE2b-256 7614f123289c9df27c6e81c2a6bf4069c0d726fe399be93169e8349dfcbc907f

See more details on using hashes here.

File details

Details for the file sonya-0.6.6-cp35-cp35m-macosx_10_6_intel.whl.

File metadata

File hashes

Hashes for sonya-0.6.6-cp35-cp35m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 7a325a7d85b5bd31fe485cea06812fe33a664ba5568d2968f37412bbd9e65907
MD5 ca1e5c4e41dd2b83f600e34c13bbe16e
BLAKE2b-256 cf3f2152751b2517b4eda551614a313ca33e9590fb8c1d684b2b2297c9b69ab3

See more details on using hashes here.

File details

Details for the file sonya-0.6.6-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for sonya-0.6.6-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a4d157cc69097198dec911b05cd8086d2684fed641411df82497d5024cb093ff
MD5 fa82ed9bea9c2daa2a0c3978c6f81547
BLAKE2b-256 d981331e3768a91029390dadcc1aa6ee80987390ace2e94e1930bfcd646103ec

See more details on using hashes here.

File details

Details for the file sonya-0.6.6-cp34-cp34m-macosx_10_6_intel.whl.

File metadata

File hashes

Hashes for sonya-0.6.6-cp34-cp34m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 01340ebe15b3576aaeaf2ae046f840cb96e40d8249819fecd97aabb31425c318
MD5 8d7f1b70236ad518d65d6abd15249fa8
BLAKE2b-256 7c8d2b04a9b05c79d19b5054d1e71f2dcd002a7f4fd90e45c7a798529898a72b

See more details on using hashes here.

File details

Details for the file sonya-0.6.6-cp27-cp27m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for sonya-0.6.6-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f2d7ef913b4346ec48e307119582490397eabc48c3907971a396303aa199c914
MD5 18b79f6145af4a3d32079c907b25b7a7
BLAKE2b-256 46aab462b54c7c5bfe7213dda607abf18185f1e2ad946d15e859e672aa220eca

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page