cleaner code for Buffer engine and Buffer Model class

This commit is contained in:
emakarov 2017-02-07 21:32:51 +03:00
parent e2e02ab029
commit 86a3fec143
3 changed files with 8 additions and 13 deletions

View File

@ -331,18 +331,16 @@ A ``Buffer`` engine is available for BufferModels. (See below how to use BufferM
engine = engines.Buffer(Person) # you need to initialize engine with main Model. Other default parameters will be used
# or:
engine = engines.Buffer(Person, table, num_layers=16, min_time=10,
engine = engines.Buffer(Person, num_layers=16, min_time=10,
max_time=100, min_rows=10000, max_rows=1000000,
min_bytes=10000000, max_bytes=100000000)
Buffer Models
-------------
Here's how you can define Model for Buffer Engine. The Buffer Model should be inherited from models.BufferModel and main model
Main model also should be specified in class::
Here's how you can define Model for Buffer Engine. The Buffer Model should be inherited from models.BufferModel and main Model::
class PersonBuffer(models.BufferModel, Person):
main_model = Person
engine = engines.Buffer(Person)
Then you can insert objects into Buffer model and they will be handled by Clickhouse properly::

View File

@ -69,8 +69,8 @@ class Buffer(Engine):
"""
#Buffer(database, table, num_layers, min_time, max_time, min_rows, max_rows, min_bytes, max_bytes)
def __init__(self, table, num_layers=16, min_time=10, max_time=100, min_rows=10000, max_rows=1000000, min_bytes=10000000, max_bytes=100000000):
self.table = table
def __init__(self, main_model, num_layers=16, min_time=10, max_time=100, min_rows=10000, max_rows=1000000, min_bytes=10000000, max_bytes=100000000):
self.main_model = main_model
self.num_layers = num_layers
self.min_time = min_time
self.max_time = max_time
@ -80,11 +80,11 @@ class Buffer(Engine):
self.max_bytes = max_bytes
def create_table_sql(self, db_name, main_model):
def create_table_sql(self, db_name):
# Overriden create_table_sql example:
#sql = 'ENGINE = Buffer(merge, hits, 16, 10, 100, 10000, 1000000, 10000000, 100000000)'
sql = 'ENGINE = Buffer(%s, %s, %d, %d, %d, %d, %d, %d, %d)' % (
db_name, main_model.table_name(), self.num_layers,
db_name, self.main_model.table_name(), self.num_layers,
self.min_time, self.max_time, self.min_rows,
self.max_rows, self.min_bytes, self.max_bytes
)

View File

@ -166,15 +166,12 @@ class Model(with_metaclass(ModelBase)):
class BufferModel(Model):
main_model = None # table's Model should be defined in implementation. It's a table where data will be flushed
@classmethod
def create_table_sql(cls, db_name):
'''
Returns the SQL command for creating a table for this model.
'''
parts = ['CREATE TABLE IF NOT EXISTS `%s`.`%s` AS `%s`.`%s`' % (db_name, cls.table_name(), db_name, cls.main_model.table_name())]
engine_str = cls.engine.create_table_sql(db_name, cls.main_model)
parts = ['CREATE TABLE IF NOT EXISTS `%s`.`%s` AS `%s`.`%s`' % (db_name, cls.table_name(), db_name, cls.engine.main_model.table_name())]
engine_str = cls.engine.create_table_sql(db_name)
parts.append(engine_str)
return ' '.join(parts)