- Synchronizer

# Synchronizer Class

The Synchronizer class handles synchronizing between two databases. More...

Header: | `#include <Synchronizer>`

- List of all members, including inherited members

## Properties

- resolve_to_source : bool
- source : Database *
- sync_output : const QList<QVariant>

- synchronize : bool
- targets : QVariant

## Public Functions

| Synchronizer(QObject * parent = 0)
bool | getResolveToSource()
Database * | getSource()
bool | getSync()
QList<QVariant> | getSyncOutput()
QVariant | getTargets()
void | setResolveToSource(bool resolve_to_source)
void | setSource(Database * source)
void | setSync(bool synchronize)
void | setSyncOutput(QList<QVariant> sync_output)
void | setTargets(QVariant targets)

## Detailed Description

The Synchronizer class handles synchronizing between two databases.

## Property Documentation

### resolve_to_source : bool

Access functions:

bool | getResolveToSource()
void | setResolveToSource(bool resolve_to_source)

### source : Database *

Returns a source Database.

Access functions:

Database * | getSource()
void | setSource(Database * source)

### sync_output : const QList<QVariant>

This property holds synchronizer::getSyncOutput.

Returns the output from a sync session. The list should contain numerous QVariantMaps, each of which will have various meta-data with informative information about what happened in the background of the session.

In some cases the information will be about errors or warnings, and in other cases simple log messages. Also included would noramlly be associated properties, elements and other data.

The information can be used in any number of ways, such as on screen within an app, testing, console output, logs and more. This is designed to be flexible enough that the app developer can decide themselves how to best use the data.

Access functions:

QList<QVariant> | getSyncOutput()

### synchronize : bool

Access functions:

bool | getSync()
void | setSync(bool synchronize)

### targets : QVariant

Sets meta-data for databases to be used during a synchronization session.

The QVariant is a list that can contain definitions for more than one database to be used as a target. For example:

targets: [{remote:true}, {remote:true, ip:"127.0.0.1", port: 7777, name:"example1.u1db", resolve_to_source:true}, {remote:"OK"}]

The above example defines three databases. Two of the three definitions in the example are invalid, the first ({remote:true}) and the third ({remote:"OK"}), because they are incomplete.

The second definition is a fully defined and valid definition for a local to remote synchronization of two databases:

{remote:true, ip:"127.0.0.1", port: 7777, name:"example1.u1db", resolve_to_source:true}

'remote' determines whether the database is on disk or located on a server. 'ip' and 'port' for a server are used only when 'remote' is set to true 'name' is the name of the local (on disk) or remote database. Note: If 'remote' is false this is the relative/absolute file location. 'resolve_to_source' determines whether to resolve conflicts automatically in favor of the source (aka local) database's values or the target's.

Access functions:

QVariant | getTargets()
void | setTargets(QVariant targets)

## Member Function Documentation

### Synchronizer::Synchronizer(QObject * parent = 0)

Create a new Synchronizer element, with an optional parent, usually by declaring it as a QML item.

Synchronizer elements sync two databases together, a 'source' database and a remote or local 'target' database.

Example use in a QML application:

U1db.Synchronizer{ id: aSynchronizer synchronize: false source: aDatabase targets: [{remote:true, ip:"127.0.0.1", port: 7777, name:"example1.u1db", resolve_to_source:true}]

}

Short description of properties:

id: The element's identification.

bool synchronize: Is the element actively synching or not. Should be set to false.

U1DB::Database source: The id of a local database that will be used for synchronization.

QVariant targets: One or more target databases that will be synched with the local database.

bool targets.remote: Is the target database a remote or local database.

QString targets.ip: The ip address of a remote database (if applicable).

int targets.port: Port number of the remote server.

QString targets.name: The name of the database.

bool targets.resolve_to_source: In case of conflict should the sync resolve to the source's data (if true).

Example use with u1db-serve:

1. In a terminal cd into a directory where the u1db Python reference implemented has been downloaded from lp:u1db. 2. Using Python create a database called 'example1.u1db' using u1db, and a document 'helloworld':

# python

>>> import u1db >>> db = u1db.open("example1.u1db",create=True) >>> content = {"hello": { "world": { "message":"Hello World Updated" } } } >>> db.create_doc(content, doc_id="helloworld")

ctrl+d

3. From the u1db directory above type './u1db-serve --port=7777' and hit enter. 4. Open another terminal tab. 5. Change into a directory containing u1db-qt (assuming this class is included in that directory and the installed version on the host computer). 6. Change into the directory where u1db-qt-example-6.qml is located. 7. Type 'qmlscene u1db-qt-example-6.qml' and hit enter. 8. Click the button labelled 'Sync'. 9. Check the terminal windows for output from either the client or server.

### void Synchronizer::setSyncOutput(QList<QVariant> sync_output)

Sets the current value for the active session's sync_output.