Skip to main content
File added late

1 file was added to this release more than 14 days after its initial publication. Inspect the release files before installing.

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.

Metadata

Release files for sonya 0.6.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

File added late

1 file was uploaded more than 14 days after the first file in this release.

While project maintainers occasionally add legitimate files to an existing release, late additions can also indicate a security compromise.

We recommend inspecting the release files before installing.

Source distribution (sdist)

Source distribution for sonya 0.6.6
File Size Uploaded
sonya-0.6.6.tar.gz 290.2 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for sonya 0.6.6
File
sonya-0.6.6-cp36-cp36m-manylinux1_x86_64.whl CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.5+ x86-64 Details
sonya-0.6.6-cp36-cp36m-macosx_10_6_intel.whl CPython 3.6 CPython 3.6 pymalloc macOS 10.6+ Intel (x86-64, i386) Details
sonya-0.6.6-cp36-cp36m-linux_armv7l.whl CPython 3.6 CPython 3.6 pymalloc linux armv7l Details
sonya-0.6.6-cp35-cp35m-manylinux1_x86_64.whl CPython 3.5 CPython 3.5 pymalloc Linux glibc 2.5+ x86-64 Details
sonya-0.6.6-cp35-cp35m-macosx_10_6_intel.whl CPython 3.5 CPython 3.5 pymalloc macOS 10.6+ Intel (x86-64, i386) Details
sonya-0.6.6-cp34-cp34m-manylinux1_x86_64.whl CPython 3.4 CPython 3.4 pymalloc Linux glibc 2.5+ x86-64 Details
sonya-0.6.6-cp34-cp34m-macosx_10_6_intel.whl CPython 3.4 CPython 3.4 pymalloc macOS 10.6+ Intel (x86-64, i386) Details
sonya-0.6.6-cp27-cp27m-manylinux1_x86_64.whl CPython 2.7 CPython 2.7 pymalloc Linux glibc 2.5+ x86-64 Details

Total release size: 10.5 MB

Release files / sonya-0.6.6.tar.gz

Download URL sonya-0.6.6.tar.gz
Size 290.2 kB
Tags Source
SHA-256 checksum
How to use checksums
5d2ec6c92a3d65de3826d5f6a53fc78060f70136c4ce857c823277a25fc06802
BLAKE2b-256 checksum
How to use checksums
ecd7252af8f0d26dbb5808c06bbced9b2a427939a2633e0764757cc8d599ea65
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / sonya-0.6.6-cp36-cp36m-manylinux1_x86_64.whl

Download URL sonya-0.6.6-cp36-cp36m-manylinux1_x86_64.whl
Size 1.3 MB
Tags CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
c308c60ec59c68d89dc9755ae018d52c308cabde7fb44a2d67210c483b54c6c2
BLAKE2b-256 checksum
How to use checksums
ad4d5ccfa4c07269681d2876b329a4ad16f3e02b8b62a2b6557e8bc07220dd96
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / sonya-0.6.6-cp36-cp36m-macosx_10_6_intel.whl

Download URL sonya-0.6.6-cp36-cp36m-macosx_10_6_intel.whl
Size 862.0 kB
Tags CPython 3.6 CPython 3.6 pymalloc macOS 10.6+ Intel (x86-64, i386)
SHA-256 checksum
How to use checksums
e4e7e49a6620f5dbe9763506dc5fb17b5a9784111af47d29f5b2748a629fca97
BLAKE2b-256 checksum
How to use checksums
f533494f9c2356ddf66a9fac68cfc6f7c230aefd092c9f268ccc3595cffe55c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / sonya-0.6.6-cp36-cp36m-linux_armv7l.whl

File added late

This file was uploaded more than 14 days after the first file in this release.

While project maintainers occasionally add legitimate files to an existing release, late additions can also indicate a security compromise.

We recommend inspecting the release file before installing.

Download URL sonya-0.6.6-cp36-cp36m-linux_armv7l.whl
Size 1.2 MB
Tags CPython 3.6 CPython 3.6 pymalloc linux armv7l
SHA-256 checksum
How to use checksums
7db6875991d2aae073b6597cc13ce1febbb3ef72d84110f02f6c4cad17ab6c01
BLAKE2b-256 checksum
How to use checksums
efcc3eb1365092a7a2641b96cd2ac3e59b835e741fedc77a7fd1ade6627cbd1d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / sonya-0.6.6-cp35-cp35m-manylinux1_x86_64.whl

Download URL sonya-0.6.6-cp35-cp35m-manylinux1_x86_64.whl
Size 1.3 MB
Tags CPython 3.5 CPython 3.5 pymalloc Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
6f4de40f6cdd593a75fcb9c69b1c8c3c6f434b0662b4fbe224694277f515fe13
BLAKE2b-256 checksum
How to use checksums
7614f123289c9df27c6e81c2a6bf4069c0d726fe399be93169e8349dfcbc907f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / sonya-0.6.6-cp35-cp35m-macosx_10_6_intel.whl

Download URL sonya-0.6.6-cp35-cp35m-macosx_10_6_intel.whl
Size 1.5 MB
Tags CPython 3.5 CPython 3.5 pymalloc macOS 10.6+ Intel (x86-64, i386)
SHA-256 checksum
How to use checksums
7a325a7d85b5bd31fe485cea06812fe33a664ba5568d2968f37412bbd9e65907
BLAKE2b-256 checksum
How to use checksums
cf3f2152751b2517b4eda551614a313ca33e9590fb8c1d684b2b2297c9b69ab3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / sonya-0.6.6-cp34-cp34m-manylinux1_x86_64.whl

Download URL sonya-0.6.6-cp34-cp34m-manylinux1_x86_64.whl
Size 1.3 MB
Tags CPython 3.4 CPython 3.4 pymalloc Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
a4d157cc69097198dec911b05cd8086d2684fed641411df82497d5024cb093ff
BLAKE2b-256 checksum
How to use checksums
d981331e3768a91029390dadcc1aa6ee80987390ace2e94e1930bfcd646103ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / sonya-0.6.6-cp34-cp34m-macosx_10_6_intel.whl

Download URL sonya-0.6.6-cp34-cp34m-macosx_10_6_intel.whl
Size 1.5 MB
Tags CPython 3.4 CPython 3.4 pymalloc macOS 10.6+ Intel (x86-64, i386)
SHA-256 checksum
How to use checksums
01340ebe15b3576aaeaf2ae046f840cb96e40d8249819fecd97aabb31425c318
BLAKE2b-256 checksum
How to use checksums
7c8d2b04a9b05c79d19b5054d1e71f2dcd002a7f4fd90e45c7a798529898a72b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / sonya-0.6.6-cp27-cp27m-manylinux1_x86_64.whl

Download URL sonya-0.6.6-cp27-cp27m-manylinux1_x86_64.whl
Size 1.2 MB
Tags CPython 2.7 CPython 2.7 pymalloc Linux glibc 2.5+ x86-64
SHA-256 checksum
How to use checksums
f2d7ef913b4346ec48e307119582490397eabc48c3907971a396303aa199c914
BLAKE2b-256 checksum
How to use checksums
46aab462b54c7c5bfe7213dda607abf18185f1e2ad946d15e859e672aa220eca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.6.6 This release

9 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page