Skip to content

FireflyAPI dbentrust API

9miao Mobile Game edited this page Aug 7, 2014 · 1 revision

FireflyAPI dbentrust API

package dbentrust

Package dbentrust is mainly dealing with database and memcached storage, and encapsulates inside mapping memcached data to python objects. It could operate stored data in memcached by manipulating python object, and make sure the data consistency of objects, as long as instantiating homonymous memobject objects in disparate process. It can also synchronize object’s data to database. There is no need to write any sql sentence.

Class DBPool

Database connection pool

InitPool

Initialize database connection pool

type method
//cwd firefly/dbentrust/dbpool.py
Prams initPool(**kw)
**kw: key-word parameter, is a dictionary; key: value is the corresponding values filled in configuration file config.json respectively:
"host":"localhost",
"user":"root",
"passwd":"111",
"port":3306,
"db":"test",
"charset":"utf8"

Use to initialize database connection pool

Connection

Obtain a database connection

type method
//cwd firefly/dbentrust/dbpool.py
Params connecton(self)

Class Memclient

Memcached client connection class, allocates different namespace by adding prefix class mode.

Connect

Connect memcached service

type method
//cwd firefly/dbentrust/memclient.py
Params connect(urls,hostname)
urls: the required ip and port number of connecting memcached service
Hostname, hostname that it connected to

The above two parameter’s value are the corresponding value filled in the configuration file config.json:
"urls":["127.0.0.1:11211"],
"hostname":"anheisg"
Use to connect memcached service

produceKey(keyname)

Regenerate key

type method
//cwd firefly/dbentrust/memclient.py
Params produceKey(self,keyname)
key: key name when it accesses value, generate uniform rules new key name according to this key name
Key must be str type , otherwise it will return type error

Get

Obtain the corresponding value of key
type method
//cwd firefly/dbentrust/memclient.py
Prams get(key)

get_multi

Obtain multiple key values at one time, faster than for-loop calling get method
@param key**: list(str)** : key’s list
type method
//cwd firefly/dbentrust/memclient.py
Params get_multi(keys)
keys is list type, keys = [key1,key2,...keyn],key1,key2 are str type

Set

Set the keyname value as ‘value’

Type method
//cwd firefly/dbentrust/memclient.py
Params set(keyname, value)

Return True on success, 0 on failure.

set_multi

Set multiple Key-Value Pair
type method
//cwd firefly/dbentrust/memclient.py
Params set_multi(mapping)
mapping is dic type, mapping = {key1:balue1,key2:value2}, return True on success, 0 on failure.

incr

addself
type method
//cwd firefly/dbentrust/memclient.py
Params incr(key, delta)
Key: key name
Delta: value that added
Add delta to key’s value and return the value that added delta

Delete

Delete the assigned key
type method
//cwd firefly/dbentrust/memclient.py
Params delete(key)
key: key name
Delete key and its corresponding value, and return value is 1

delete_multi

delete multiple key at one time
type method
//cwd firefly/dbentrust/memclient.py
Params delete_multi(keys)

keys is list type, keys = [key1,key2,...keyn] key1,key2 is str type

flush_all

Wipe all data (use with caution)
type method
//cwd firefly/dbentrust/memclient.py
Params flush_all(self)

class MemObject

Mapping class of memcached data to python objects, it realizes the customizing of memcached data format by inheriting this class.

produceKey

Regenerate key
type method
//cwd firefly/dbentrust/memobject.py
Params produceKey(self,keyname)
key: key name when it accesses value, generate uniform rules new key name according to this key name
Key must be str type , otherwise it will return type error

Locked

Check whether the object is locked
type method
//cwd firefly/dbentrust/memobject.py
Params lock(self)
Return object’s state: 1means object is locked and cannot be modified, 0 means it can be modified.

Lock

Lock the object
type method
//cwd firefly/dbentrust/memobject.py
Params lock(self)
Modify the object’s lock status to 1

Release

Release the lock
type method
//cwd firefly/dbentrust/memobject.py
Params release(self)
Modify the object’s lock state to 1

Get

Obtain key’s corresponding value
type method
//cwd firefly/dbentrust/memobject.py
Params get(key)

get_multi

Obtain multiple key values at one time, faster than for-loop calling get method
type method
//cwd firefly/dbentrust/memobject.py
Params get_multi(keys)
keys is list type, keys = [key1,key2,...keyn],key1,key2 are str type

Update

Set key’s value as values
type method
//cwd firefly/dbentrust/memobject.py
Params update(key, values)

update_multi

Modify multiple Key-Value Pair’s value at the same time
type method
//cwd firefly/dbentrust/memobject.py
Params update_multi(mapping)
mapping is dict type, mapping = {key1:balue1,key2:value2}, return True on success, 0 or false on failure.

Mdelete

Delete all data in this class
type method
//cwd firefly/dbentrust/memobject.py
Params mdelete(self)

incr

addself
type method
//cwd firefly/dbentrust/memclient.py
Params incr(key, delta)
Key: key name
Delta: value that added
Add delta to key’s value and return the value that added delta

Insert

Insert all data included in this class
type method
//cwd firefly/dbentrust/memobject.py
Params insert()

Class Mmode

Direct mapping class of database to memcached, it can modify data in database in deferred and asynchronous way, and reduces pressure on database. It has customized expiration time and data in memcached will be automatically wiped after this period. Here the definition of expiration is the interval time between two times the object be accessed. This class realizes itself by inheriting MemObject.

Update

Set key’s value as values
type method
//cwd firefly/dbentrust/memobject.py
Params update(key, values)

update_multi

modify multiple Key-Value Pair’s value at the same time
type method
//cwd firefly/dbentrust/memobject.py
Params update_multi(mapping)
mapping is dict type, mapping = {key1:balue1,key2:value2}, return True on success, 0 or false on failure.

Get

Obtain key’s corresponding value
type method
//cwd firefly/dbentrust/memobject.py
Params get(key)

get_multi

Obtain multiple key values at one time, faster than for-loop calling get method
type method
//cwd firefly/dbentrust/memobject.py
Params get_multi(keys)
keys is list type, keys = [key1,key2,...keyn],key1,key2 are str type

delete

Delete object, here we just modify the status of date to “deleted”
type method
//cwd firefly/dbentrust/ mmode.py
Params delete(self)

mdelete

Clean up objects, first synchronize data to database, and then actually clean the data in memecached.
type method
//cwd firefly/dbentrust/ mmode.py
Params mdelete(self)

IsEffective

Check whether the object is valid, return True on valid, false on invalid.
type method
//cwd firefly/dbentrust/ mmode.py
Params IsEffective(self)

syncDB

Synchronize data to database
type method
//cwd firefly/dbentrust/ mmode.py
Params syncDB(self)

checkSync

Synchronize data to database and check whether data is timeout, delete data in memcached if data is timeout
type method
//cwd firefly/dbentrust/ mmode.py
Params checkSync(self,timeout=TIMEOUT)

class MFKMode

Foreign key management

class MAdmin

mmode manager, can obtain a mmode instance according to primary key. A MAdmin management corresponds to a particular table in database and inherits the MemObject class. Its instantiation way is shown below:
ma = MAdmin("tb_user",'id',fk = 'group',incrkey='id')
user2 = ma.getObj(2)
user2.update('name','test')

it shows that tb_user is the corresponding table of database, it can obtain mmode object that id “2”record corresponds via ma.getObj(2).
user2.update('name','test'): modify the name field that this record corresponds to ‘test’.

Insert

Write all data in memcached.
type method
//cwd firefly/dbentrust/ mmode.py
Params insert(self)

Load

Write all messages of table in database that this manager corresponds into memcached.

type method
//cwd firefly/dbentrust/ mmode.py
Params load(self)

madmininfo

Obtain Madmin’s relevant information
type method
//cwd firefly/dbentrust/ mmode.py
Params madmininfo(self)

mfilter

Search the matching object (an inefficient way, not recommended for using)
type method
//cwd firefly/dbentrust/ mmode.py
Params mfilter(self,kw)

getAllPkByFk

Obtain primary key list according to foreign key
type method
//cwd firefly/dbentrust/ mmode.py
Params getAllPkByFk(self,fk)

fk: foreign key value
Search all eligible data in this list according to foreign key value, return primary key value’s list of this data.

getObj

Obtain corresponding recorded mmode object according to primary key.
type method
//cwd firefly/dbentrust/ mmode.py
Params getObj(pk)
pk: primary key’s value, return mmode object instance of data that this primary key corresponds, type is instance

getObjData

Obtain corresponding recorded mmode object’s field data according to primary key.
type method
//cwd firefly/dbentrust/ mmode.py
Params getObjData(self,pk)
pk: primary key’s value, return data that this primary key corresponds, type is dict

getObjList

Obtain corresponding recorded mmode object’s list according to primary key.
type method
//cwd firefly/dbentrust/ mmode.py
Params getObjList(self,pklist)
Pklist: primary key list, return list of mmode object instance that primary key corresponds’ data according to primary key in list. ([instance,instance,instance])

deleteMode

Delete corresponding mmode messages according to primary key.
type method
//cwd firefly/dbentrust/ mmode.py
Params deleteMode(self,pk)
pk: primary key’s value
Here we just modify the status of date to “deleted”

checkAll
Check the controlled object’s messages and synchronize them to database, and process time-out objects
type method
//cwd firefly/dbentrust/ mmode.py
check()

deleteAllFk

Delete all foreign keys
type method
//cwd firefly/dbentrust/ mmode.py
Params deleteAllFk(self)

It actually deletes data from memcached.

New

Create a new object and set a corresponding record in database in a not simultaneous and real-time way. It could actually synchronize to database after checkAll operation is executed.

type method
//cwd firefly/dbentrust/ mmode.py
Params new(self,data)

class MadminManager

madmin object manager, which actually is supervisor of whole database table records, yet the corresponding table Madmin must be registered here.

registe

Register Madmin manager
type method
//cwd firefly/dbentrust/ madminanager.py
Params registe(self,admin)
Admin: data model object instance
Add instance of data object Madmin to its singleton MadminManager

dropAdmin

Cancel the management of madmin object
type method
//cwd firefly/dbentrust/ madminanager.py
Params dropAdmin(self,adminname)
adminname: name of data model object instance
Delete data object Madmin’s instance from Madmin singleton MadminManager according to instance adminname.

getAdmin

Obtain registered madmin object according to table name
type method
//cwd firefly/dbentrust/ madminanager.py
Params getAdmin(self,adminname)
Adminnmae: name of data model object instance
Obtain data object Madmin’s instance from Madmin singleton MadminManager according to instance adminname.

checkAdmins

Synchronize all registered madmin responding records in MadminManager to database, and process time-out objects

type method
//cwd firefly/dbentrust/ madminanager.py
Params checkAdmins(self)