Datastore Clients

The main component for pulsar datastore clients is the Store class which encapsulates the essential API for communicating and executing asynchronous commands on remote servers.

Getting Started

To create a data Store client one uses the create_store() function with a valid connection string:

>>> from pulsar.apps.data import create_store
>>> store = create_store('redis://user:password@127.0.0.1:6500/11')
>>> store.name
'redis'
>>> store.database
'11'
>>> store.dns
'redis://user:password@127.0.0.1:6500/11'

Additional parameters can be passed via the connection string or as key-valued parameters. For example:

>>> store = create_store('redis://user:password@127.0.0.1:6500/11',
                         namespace='test_')
>>> store.dns
'redis://user:password@127.0.0.1:6500/11?namespace=test_'

Connection String

The connection string is a way to specify the various parameters for the data store connection. All Store support a connection string of the form:

<scheme>://<username>:<password>@<host>/<database>?param1=...&param2=...

where:

  • schema is the store name
  • database is the database name (or number for redis)
  • username is an optional database username
  • password is an optional database password

Implement a Store

When implementing a new Store there are several methods which need to be covered:

  • ping() to check if the server is available
  • connect() to create a new connection
  • execute() to execute a command on the store server

All registered data stores are stored in the data_stores dictionary:

from pulsar.apps.data import data_stores

Pulsar provides two implementations, the redis client and the pulsards client.

API

The main component of the data store API is the create_store() function which creates in one function call a new Store object which can be used to interact with the backend database.

Create store

pulsar.apps.data.store.create_store(url, **kw)[source]

Create a new Store for a valid url.

Parameters:
  • url

    a valid url takes the following forms:

    Pulsar datastore:

    pulsar://user:password@127.0.0.1:6410
    

    Redis:

    redis://user:password@127.0.0.1:6500/11?namespace=testdb
    
  • kw – additional key-valued parameters to pass to the Store initialisation method. It can contains parameters such as database, user and password to override the url values. Additional parameters are processed by the Store._init() method.
Returns:

a Store.

Start store

pulsar.apps.data.pulsards.startds.start_store(app, url, workers=0, **kw)[source]

Equivalent to create_store() for most cases excepts when the url is for a pulsar store not yet started. In this case, a PulsarDS is started.

Register a new store

pulsar.apps.data.store.register_store(name, dotted_path)[source]

Register a new Store with schema name which can be found at the python dotted_path.

Store

class pulsar.apps.data.store.Store(name, host, database=None, user=None, password=None, encoding=None, **kw)[source]

Base class for an asynchronous data stores.

A Store should not be created directly, the high level create_store() function should be used instead.

_host

The remote host, tuple or string

_user

The user name

_password

The user password

name

Store name

database

Database name/number associated with this store.

encoding

Store encoding (usually utf-8)

dns

Domain name server

urlparams

url parameters in dns query

database_create(dbname=None, **kw)[source]

Create a new database in this store.

By default it does nothing, stores must implement this method only if they support database creation.

Parameters:dbname – optional database name. If not supplied a database with database is created.
table_all(**kw)[source]

Information about the table/collection mapping model

RemoteStore

class pulsar.apps.data.store.RemoteStore(name, host, loop=None, protocol_factory=None, **kw)[source]

Base class for remote data stores.

It is an Producer for accessing and retrieving data from remote data servers such as redis.

connect()[source]

Connect with store server

execute(*args, **options)[source]

Execute a command

ping()[source]

Used to check if the data server is available

client()[source]

Get a client for this store if implemented

pubsub(**kw)[source]

Obtain a PubSub handler for this store if implemented

channels(**kw)[source]

Obtain a Channels handler for this store if implemented

close()[source]

Close all open connections

flush()[source]

Flush the store.

encode_bytes(data)[source]

Encode bytes data

Parameters:data – a bytes string
Returns:bytes or string
dencode_bytes(data)[source]

Decode bytes data

Parameters:data – bytes or string
Returns:bytes