Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions | Private Attributes | List of all members
grepros.plugins.auto.dbbase.BaseDataSink Class Reference
Inheritance diagram for grepros.plugins.auto.dbbase.BaseDataSink:
Inheritance graph
[legend]

Public Member Functions

def __init__ (self, args=None, **kwargs)
 
def close (self)
 
def close_output (self)
 
def emit (self, topic, msg, stamp=None, match=None, index=None)
 
def validate (self)
 
- Public Member Functions inherited from grepros.outputs.Sink
def __enter__ (self)
 
def __exit__ (self, exc_type, exc_value, traceback)
 
def autodetect (cls, target)
 
def bind (self, source)
 
def emit_meta (self)
 
def flush (self)
 
def is_highlighting (self)
 
def thread_excepthook (self, text, exc)
 

Public Attributes

 COMMIT_INTERVAL
 
 db
 Database connection. More...
 
- Public Attributes inherited from grepros.outputs.Sink
 args
 
 source
 inputs.Source instance bound to this sink More...
 
 valid
 Result of validate() More...
 

Static Public Attributes

int COMMIT_INTERVAL = 1000
 Number of emits between commits; 0 is autocommit. More...
 
 DEFAULT_ARGS = dict(META=False, WRITE_OPTIONS={}, VERBOSE=False)
 Constructor argument defaults. More...
 
 ENGINE = None
 Database engine name, overridden in subclasses. More...
 
list MESSAGE_TYPE_BASECOLS
 Default columns for pkg/MsgType tables. More...
 
list MESSAGE_TYPE_NESTCOLS
 Additional default columns for pkg/MsgType tables with nesting output. More...
 
list MESSAGE_TYPE_TOPICCOLS
 Default topic-related columns for pkg/MsgType tables. More...
 
- Static Public Attributes inherited from grepros.outputs.Sink
 DEFAULT_ARGS = dict(META=False)
 Constructor argument defaults. More...
 
tuple FILE_EXTENSIONS = ()
 Auto-detection file extensions for subclasses, as (".ext", ) More...
 
- Static Public Attributes inherited from grepros.plugins.auto.sqlbase.SqlMixin
 DEFAULT_ARGS = dict(META=False, WRITE_OPTIONS={}, MATCH_WRAPPER=None, VERBOSE=False)
 Constructor argument defaults. More...
 
string DEFAULT_DIALECT = "sqlite"
 Default SQL dialect used if dialect not specified. More...
 
dictionary DIALECTS
 Supported SQL dialects and options. More...
 
list KEYWORDS
 Words that need quoting if in name context, like table name. More...
 

Private Member Functions

def _connect (self)
 
def _ensure_columns (self, cols)
 
def _ensure_execute (self, sql, args)
 
def _execute_insert (self, sql, args)
 
def _executemany (self, sql, argses)
 
def _executescript (self, sql)
 
def _get_next_id (self, table)
 
def _init_db (self)
 
def _load_schema (self)
 
def _make_cursor (self)
 
def _make_db_label (self)
 
def _populate_type (self, topic, msg, stamp, rootmsg=None, parent_type=None, parent_id=None)
 
def _process_message (self, topic, msg, stamp)
 
def _process_topic (self, topic, msg)
 
def _process_type (self, msg, rootmsg=None)
 

Private Attributes

 _checkeds
 
 _close_printed
 
 _cursor
 
 _dialect
 
 _nested_counts
 
 _nesting
 
 _sql_queue
 

Detailed Description

Base class for writing messages to a database.

Output will have:
- table "topics", with topic and message type names
- table "types", with message type definitions

plus:
- table "pkg/MsgType" for each topic message type, with detailed fields,
  BYTEA fields for uint8[], array fields for scalar list attributes,
  and JSONB fields for lists of ROS messages;
  with foreign keys if nesting else subtype values as JSON dictionaries;
  plus underscore-prefixed fields for metadata, like `_topic` as the topic name.
  If not nesting, only topic message type tables are created.
- view "/full/topic/name" for each topic, selecting from the message type table

If a message type table already exists but for a type with a different MD5 hash,
the new table will have its MD5 hash appended to end, as "pkg/MsgType (hash)".

Definition at line 24 of file dbbase.py.

Constructor & Destructor Documentation

◆ __init__()

def grepros.plugins.auto.dbbase.BaseDataSink.__init__ (   self,
  args = None,
**  kwargs 
)
@param   args                 arguments as namespace or dictionary, case-insensitive;
                      or a single item as the database connection string
@param   args.write           database connection string
@param   args.write_options   ```
                      {"commit-interval": transaction size (0 is autocommit),
                      "nesting": "array" to recursively insert arrays
                                  of nested types, or "all" for any nesting)}
                      ```
@param   args.meta            whether to emit metainfo
@param   args.verbose         whether to emit debug information
@param   kwargs               any and all arguments as keyword overrides, case-insensitive

Reimplemented from grepros.outputs.Sink.

Reimplemented in grepros.plugins.auto.postgres.PostgresSink, and grepros.plugins.auto.sqlite.SqliteSink.

Definition at line 67 of file dbbase.py.

Member Function Documentation

◆ _connect()

def grepros.plugins.auto.dbbase.BaseDataSink._connect (   self)
private
Returns new database connection.

Reimplemented in grepros.plugins.auto.sqlite.SqliteSink, and grepros.plugins.auto.postgres.PostgresSink.

Definition at line 363 of file dbbase.py.

◆ _ensure_columns()

def grepros.plugins.auto.dbbase.BaseDataSink._ensure_columns (   self,
  cols 
)
private
Adds specified columns to any type tables lacking them.

Definition at line 340 of file dbbase.py.

◆ _ensure_execute()

def grepros.plugins.auto.dbbase.BaseDataSink._ensure_execute (   self,
  sql,
  args 
)
private
Executes SQL if in autocommit mode, else caches arguments for batch execution.

Definition at line 354 of file dbbase.py.

◆ _execute_insert()

def grepros.plugins.auto.dbbase.BaseDataSink._execute_insert (   self,
  sql,
  args 
)
private
Executes INSERT statement, returns inserted ID.

Reimplemented in grepros.plugins.auto.sqlite.SqliteSink, and grepros.plugins.auto.postgres.PostgresSink.

Definition at line 368 of file dbbase.py.

◆ _executemany()

def grepros.plugins.auto.dbbase.BaseDataSink._executemany (   self,
  sql,
  argses 
)
private
Executes SQL with all args sequences.

Reimplemented in grepros.plugins.auto.sqlite.SqliteSink, and grepros.plugins.auto.postgres.PostgresSink.

Definition at line 373 of file dbbase.py.

◆ _executescript()

def grepros.plugins.auto.dbbase.BaseDataSink._executescript (   self,
  sql 
)
private
Executes SQL with one or more statements.

Reimplemented in grepros.plugins.auto.sqlite.SqliteSink, and grepros.plugins.auto.postgres.PostgresSink.

Definition at line 378 of file dbbase.py.

◆ _get_next_id()

def grepros.plugins.auto.dbbase.BaseDataSink._get_next_id (   self,
  table 
)
private
Returns next ID value for table.

Reimplemented in grepros.plugins.auto.sqlite.SqliteSink, and grepros.plugins.auto.postgres.PostgresSink.

Definition at line 383 of file dbbase.py.

◆ _init_db()

def grepros.plugins.auto.dbbase.BaseDataSink._init_db (   self)
private
Opens database connection, and populates schema if not already existing.

Reimplemented in grepros.plugins.auto.sqlite.SqliteSink, and grepros.plugins.auto.postgres.PostgresSink.

Definition at line 176 of file dbbase.py.

◆ _load_schema()

def grepros.plugins.auto.dbbase.BaseDataSink._load_schema (   self)
private
Populates instance attributes with schema metainfo.

Reimplemented in grepros.plugins.auto.sqlite.SqliteSink, and grepros.plugins.auto.postgres.PostgresSink.

Definition at line 193 of file dbbase.py.

◆ _make_cursor()

def grepros.plugins.auto.dbbase.BaseDataSink._make_cursor (   self)
private
Returns new database cursor.

Definition at line 388 of file dbbase.py.

◆ _make_db_label()

def grepros.plugins.auto.dbbase.BaseDataSink._make_db_label (   self)
private
Returns formatted label for database.

Reimplemented in grepros.plugins.auto.sqlite.SqliteSink, and grepros.plugins.auto.postgres.PostgresSink.

Definition at line 393 of file dbbase.py.

◆ _populate_type()

def grepros.plugins.auto.dbbase.BaseDataSink._populate_type (   self,
  topic,
  msg,
  stamp,
  rootmsg = None,
  parent_type = None,
  parent_id = None 
)
private
Inserts pkg/MsgType row for message.

If nesting is enabled, inserts sub-rows for subtypes in message,
and returns inserted ID.

Definition at line 295 of file dbbase.py.

◆ _process_message()

def grepros.plugins.auto.dbbase.BaseDataSink._process_message (   self,
  topic,
  msg,
  stamp 
)
private
Inserts pkg/MsgType row for this message.

Inserts sub-rows for subtypes in message if nesting enabled.
Commits transaction if interval due.

Reimplemented in grepros.plugins.auto.sqlite.SqliteSink.

Definition at line 280 of file dbbase.py.

◆ _process_topic()

def grepros.plugins.auto.dbbase.BaseDataSink._process_topic (   self,
  topic,
  msg 
)
private
Inserts topics-row and creates view `/topic/name` if not already existing.

Also creates types-row and pkg/MsgType table for this message if not existing.
If nesting enabled, creates types recursively.

Definition at line 206 of file dbbase.py.

◆ _process_type()

def grepros.plugins.auto.dbbase.BaseDataSink._process_type (   self,
  msg,
  rootmsg = None 
)
private
Creates types-row and pkg/MsgType table if not already existing.

@return   created types-row, or None if already existed

Definition at line 233 of file dbbase.py.

◆ close()

def grepros.plugins.auto.dbbase.BaseDataSink.close (   self)
Closes database connection, if any, emits metainfo.

Reimplemented from grepros.outputs.Sink.

Definition at line 139 of file dbbase.py.

◆ close_output()

def grepros.plugins.auto.dbbase.BaseDataSink.close_output (   self)
Closes database connection, if any, executing any pending statements.

Definition at line 164 of file dbbase.py.

◆ emit()

def grepros.plugins.auto.dbbase.BaseDataSink.emit (   self,
  topic,
  msg,
  stamp = None,
  match = None,
  index = None 
)
Writes message to database.

Reimplemented from grepros.outputs.Sink.

Reimplemented in grepros.plugins.auto.sqlite.SqliteSink.

Definition at line 127 of file dbbase.py.

◆ validate()

def grepros.plugins.auto.dbbase.BaseDataSink.validate (   self)
Returns whether args.write_options has valid values, if any.

Checks parameters "commit-interval" and "nesting".

Reimplemented from grepros.outputs.Sink.

Reimplemented in grepros.plugins.auto.postgres.PostgresSink, and grepros.plugins.auto.sqlite.SqliteSink.

Definition at line 106 of file dbbase.py.

Member Data Documentation

◆ _checkeds

grepros.plugins.auto.dbbase.BaseDataSink._checkeds
private

Definition at line 99 of file dbbase.py.

◆ _close_printed

grepros.plugins.auto.dbbase.BaseDataSink._close_printed
private

Definition at line 91 of file dbbase.py.

◆ _cursor

grepros.plugins.auto.dbbase.BaseDataSink._cursor
private

Definition at line 89 of file dbbase.py.

◆ _dialect

grepros.plugins.auto.dbbase.BaseDataSink._dialect
private

Definition at line 90 of file dbbase.py.

◆ _nested_counts

grepros.plugins.auto.dbbase.BaseDataSink._nested_counts
private

Definition at line 101 of file dbbase.py.

◆ _nesting

grepros.plugins.auto.dbbase.BaseDataSink._nesting
private

Definition at line 97 of file dbbase.py.

◆ _sql_queue

grepros.plugins.auto.dbbase.BaseDataSink._sql_queue
private

Definition at line 100 of file dbbase.py.

◆ COMMIT_INTERVAL [1/2]

int grepros.plugins.auto.dbbase.BaseDataSink.COMMIT_INTERVAL = 1000
static

Number of emits between commits; 0 is autocommit.

Definition at line 49 of file dbbase.py.

◆ COMMIT_INTERVAL [2/2]

grepros.plugins.auto.dbbase.BaseDataSink.COMMIT_INTERVAL

Definition at line 182 of file dbbase.py.

◆ db

grepros.plugins.auto.dbbase.BaseDataSink.db

Database connection.

Definition at line 87 of file dbbase.py.

◆ DEFAULT_ARGS

grepros.plugins.auto.dbbase.BaseDataSink.DEFAULT_ARGS = dict(META=False, WRITE_OPTIONS={}, VERBOSE=False)
static

Constructor argument defaults.

Definition at line 64 of file dbbase.py.

◆ ENGINE

grepros.plugins.auto.dbbase.BaseDataSink.ENGINE = None
static

Database engine name, overridden in subclasses.

Definition at line 46 of file dbbase.py.

◆ MESSAGE_TYPE_BASECOLS

list grepros.plugins.auto.dbbase.BaseDataSink.MESSAGE_TYPE_BASECOLS
static
Initial value:
= [("_dt", "TIMESTAMP"),
("_timestamp", "INTEGER"),
("_id", "INTEGER NOT NULL "
"PRIMARY KEY AUTOINCREMENT"), ]

Default columns for pkg/MsgType tables.

Definition at line 55 of file dbbase.py.

◆ MESSAGE_TYPE_NESTCOLS

list grepros.plugins.auto.dbbase.BaseDataSink.MESSAGE_TYPE_NESTCOLS
static
Initial value:
= [("_parent_type", "TEXT"),
("_parent_id", "INTEGER"), ]

Additional default columns for pkg/MsgType tables with nesting output.

Definition at line 60 of file dbbase.py.

◆ MESSAGE_TYPE_TOPICCOLS

list grepros.plugins.auto.dbbase.BaseDataSink.MESSAGE_TYPE_TOPICCOLS
static
Initial value:
= [("_topic", "TEXT"),
("_topic_id", "INTEGER"), ]

Default topic-related columns for pkg/MsgType tables.

Definition at line 52 of file dbbase.py.


The documentation for this class was generated from the following file:


grepros
Author(s): Erki Suurjaak
autogenerated on Sat Jan 6 2024 03:11:30