InfAdoptedAlgorithm

InfAdoptedAlgorithm — adOPTed implementation

Stability Level

Unstable, unless otherwise indicated

Synopsis

#include <libinfinity/adopted/inf-adopted-algorithm.h>

enum                InfAdoptedAlgorithmError;
                    InfAdoptedAlgorithm;
struct              InfAdoptedAlgorithmClass;
InfAdoptedAlgorithm * inf_adopted_algorithm_new         (InfUserTable *user_table,
                                                         InfBuffer *buffer);
InfAdoptedAlgorithm * inf_adopted_algorithm_new_full    (InfUserTable *user_table,
                                                         InfBuffer *buffer,
                                                         guint max_total_log_size);
InfAdoptedStateVector * inf_adopted_algorithm_get_current
                                                        (InfAdoptedAlgorithm *algorithm);
InfAdoptedRequest * inf_adopted_algorithm_get_execute_request
                                                        (InfAdoptedAlgorithm *algorithm);
InfAdoptedRequest * inf_adopted_algorithm_generate_request
                                                        (InfAdoptedAlgorithm *algorithm,
                                                         InfAdoptedRequestType type,
                                                         InfAdoptedUser *user,
                                                         InfAdoptedOperation *operation);
InfAdoptedRequest * inf_adopted_algorithm_translate_request
                                                        (InfAdoptedAlgorithm *algorithm,
                                                         InfAdoptedRequest *request,
                                                         InfAdoptedStateVector *to);
gboolean            inf_adopted_algorithm_execute_request
                                                        (InfAdoptedAlgorithm *algorithm,
                                                         InfAdoptedRequest *request,
                                                         gboolean apply,
                                                         GError **error);
void                inf_adopted_algorithm_cleanup       (InfAdoptedAlgorithm *algorithm);
gboolean            inf_adopted_algorithm_can_undo      (InfAdoptedAlgorithm *algorithm,
                                                         InfAdoptedUser *user);
gboolean            inf_adopted_algorithm_can_redo      (InfAdoptedAlgorithm *algorithm,
                                                         InfAdoptedUser *user);

Object Hierarchy

  GObject
   +----InfAdoptedAlgorithm

Properties

  "buffer"                   InfBuffer*            : Read / Write / Construct Only
  "buffer-modified-state"    InfAdoptedStateVector*  : Read
  "current-state"            InfAdoptedStateVector*  : Read
  "max-total-log-size"       guint                 : Read / Write / Construct Only
  "user-table"               InfUserTable*         : Read / Write / Construct Only

Signals

  "begin-execute-request"                          : Run Last
  "can-redo-changed"                               : Run Last
  "can-undo-changed"                               : Run Last
  "end-execute-request"                            : Run Last

Description

InfAdoptedAlgorithm implements the adOPTed algorithm for concurrency control as described in the paper "An integrating, transformation-oriented approach to concurrency control and undo in group editors" by Matthias Ressel, Doris Nitsche-Ruhland and Rul Gunzenhäuser (http://portal.acm.org/citation.cfm?id=240305).

It is based on requests, represented by the InfAdoptedRequest class. If there is at least one local InfUser in the algorithm's user table, then you can create own reequests by the functions inf_adopted_algorithm_generate_request(), inf_adopted_algorithm_generate_request_noexec(), inf_adopted_algorithm_generate_undo() and inf_adopted_algorithm_generate_redo(). Remote requests can be applied via inf_adopted_algorithm_receive_request(). This class does not take care of transfering the generated requests to other users which is the scope of InfAdoptedSession.

The implementation is not tied to text editing. It can handle any operations implementing InfAdoptedOperation as long as they define sufficient transformation functions. The libinftext library provides operations for text editing, see InfTextInsertOperation and InfTextDeleteOperation.

Details

enum InfAdoptedAlgorithmError

typedef enum {
  INF_ADOPTED_ALGORITHM_ERROR_NO_UNDO,
  INF_ADOPTED_ALGORITHM_ERROR_NO_REDO,
  
  INF_ADOPTED_ALGORITHM_ERROR_FAILED
} InfAdoptedAlgorithmError;

Error codes for InfAdoptedAlgorithm. These can occur when invalid requests are passed to inf_adopted_algorithm_execute_request().

INF_ADOPTED_ALGORITHM_ERROR_NO_UNDO

An undo request was about to be executed but there is no operation to undo.

INF_ADOPTED_ALGORITHM_ERROR_NO_REDO

A redo request was about to be executed but there is no operation to redo.

INF_ADOPTED_ALGORITHM_ERROR_FAILED

No further specified error code.

InfAdoptedAlgorithm

typedef struct _InfAdoptedAlgorithm InfAdoptedAlgorithm;

InfAdoptedAlgorithm is an opaque data type. You should only access it via the public API functions.


struct InfAdoptedAlgorithmClass

struct InfAdoptedAlgorithmClass {
  void(*can_undo_changed)(InfAdoptedAlgorithm* algorithm,
                          InfAdoptedUser* user,
                          gboolean can_undo);

  void(*can_redo_changed)(InfAdoptedAlgorithm* algorithm,
                          InfAdoptedUser* user,
                          gboolean can_redo);

  void(*begin_execute_request)(InfAdoptedAlgorithm* algorithm,
                               InfAdoptedUser* user,
                               InfAdoptedRequest* request);

  void(*end_execute_request)(InfAdoptedAlgorithm* algorithm,
                             InfAdoptedUser* user,
                             InfAdoptedRequest* request,
                             InfAdoptedRequest* translated_request,
                             const GError* error);
};

Signals for the InfAdoptedAlgorithm class.

can_undo_changed ()

Default signal handler for the "can_undo_changed" signal.

can_redo_changed ()

Default signal handler for the "can_redo_changed" signal.

begin_execute_request ()

Default signal handler for the "begin-execute-request" signal.

end_execute_request ()

Default signal handler for the "end-execute-request" signal.

inf_adopted_algorithm_new ()

InfAdoptedAlgorithm * inf_adopted_algorithm_new         (InfUserTable *user_table,
                                                         InfBuffer *buffer);

Creates a InfAdoptedAlgorithm.

user_table :

The table of participating users.

buffer :

The buffer to apply operations to.

Returns :

A new InfAdoptedAlgorithm.

inf_adopted_algorithm_new_full ()

InfAdoptedAlgorithm * inf_adopted_algorithm_new_full    (InfUserTable *user_table,
                                                         InfBuffer *buffer,
                                                         guint max_total_log_size);

Note that it is possible that request logs need to grow a bit larger than max_total_log_size in high-latency situations or when a user does not send status updates frequently. However, when all requests have been processed by all users, the sum of all requests in the logs is guaranteed to be lower or equal to this value.

Set to G_MAXUINT to disable limitation. In theory, this would allow everyone to undo every operation up to the first one ever made. In practise, this issues a huge amount of data that needs to be synchronized on user join and is too expensive to compute anyway.

The default value is 2048.

user_table :

The table of participating users.

buffer :

The buffer to apply operations to.

max_total_log_size :

The maxmimum number of operations to keep in all user's request logs.

Returns :

A new InfAdoptedAlgorithm.

inf_adopted_algorithm_get_current ()

InfAdoptedStateVector * inf_adopted_algorithm_get_current
                                                        (InfAdoptedAlgorithm *algorithm);

Returns the current vector time of algorithm.

algorithm :

A InfAdoptedAlgorithm.

Returns :

A InfAdoptedStateVector owned by algorithm.

inf_adopted_algorithm_get_execute_request ()

InfAdoptedRequest * inf_adopted_algorithm_get_execute_request
                                                        (InfAdoptedAlgorithm *algorithm);

Returns whether the algorithm is currently transforming a request to the current state and appling its state to the buffer. If it is the function is returning the request that was received and is currently being executed, other wise the function returns NULL. Note that the request execution is not re-entrant, i.e. two requests cannot be executed concurrently at the same time, or recursively.

algorithm :

A InfAdoptedAlgorithm.

Returns :

The request that algorithm is currently processing, or NULL. The return value must not be freed by the caller.

inf_adopted_algorithm_generate_request ()

InfAdoptedRequest * inf_adopted_algorithm_generate_request
                                                        (InfAdoptedAlgorithm *algorithm,
                                                         InfAdoptedRequestType type,
                                                         InfAdoptedUser *user,
                                                         InfAdoptedOperation *operation);

Creates a new request that can be applied to the current document state. The request is made by the given user. If operation is of type INF_ADOPTED_REQUEST_DO, then operation specifies the operation to be performed. Otherwise, operation must be NULL.

To apply the effect of the request to the document, run inf_adopted_algorithm_execute_request(). Note that even if the effect is already applied to the document, the function must still be called with the apply parameter set to FALSE, so that the algorithm knows that the request has been applied.

algorithm :

A InfAdoptedAlgorithm.

type :

The type of request to create.

user :

The user for which to create the request.

operation :

The operation to perform, or NULL.

Returns :

A new InfAdoptedRequest. Free with g_object_unref() when no longer needed.

inf_adopted_algorithm_translate_request ()

InfAdoptedRequest * inf_adopted_algorithm_translate_request
                                                        (InfAdoptedAlgorithm *algorithm,
                                                         InfAdoptedRequest *request,
                                                         InfAdoptedStateVector *to);

Translates request so that it can be applied to the document at state to. request will not be modified but a new, translated request is returned instead.

There are several preconditions for this function to be called. to must be a reachable point in the state space. Also, requests can only be translated in forward direction, so request's vector time must be causally before (see inf_adopted_state_vector_causally_before()) to.

algorithm :

A InfAdoptedAlgorithm.

request :

A InfAdoptedRequest.

to :

The state vector to translate request to.

Returns :

A new or cached InfAdoptedRequest. Free with g_object_unref() when no longer needed.

inf_adopted_algorithm_execute_request ()

gboolean            inf_adopted_algorithm_execute_request
                                                        (InfAdoptedAlgorithm *algorithm,
                                                         InfAdoptedRequest *request,
                                                         gboolean apply,
                                                         GError **error);

This function transforms the given request such that it can be applied to the current document state and then applies it the buffer and adds it to the request log of the algorithm, so that it is used for future transformations of other requests.

If apply is FALSE then the request is not applied to the buffer. In this case, it is assumed that the buffer is already modified, and that the request is made as a result from the buffer modification. This also means that the request must be applicable to the current document state, without requiring transformation.

In addition, the function emits the "begin-execute-request" and "end-execute-request" signals, and makes inf_adopted_algorithm_get_execute_request() return request during that period.

This allows other code to hook in before and after request processing. This does not cause any loss of generality because this function is not re-entrant anyway: it cannot work when used concurrently by multiple threads nor in a recursive manner, because only when one request has been added to the log the next request can be translated, since it might need the previous request for the translation path and it needs to be translated to a state where the effect of the previous request is included so that it can consistently applied to the buffer.

There are also runtime errors that can occur if request execution fails. In this case the function returns FALSE and error is set. Possible reasons for this include request being an INF_ADOPTED_REQUEST_UNDO or INF_ADOPTED_REQUEST_REDO request without there being an operation to undo or redo, or if the translated operation cannot be applied to the buffer. This usually means that the input request was invalid. However, this is not considered a programmer error because typically requests are received from untrusted input sources such as network connections. Note that there cannot be any runtime errors if apply is set to FALSE. In that case it is safe to call the function with NULL error.

algorithm :

A InfAdoptedAlgorithm.

request :

The request to execute.

apply :

Whether to apply the request to the buffer.

error :

Location to store error information, if any.

Returns :

TRUE on success or FALSE on error.

inf_adopted_algorithm_cleanup ()

void                inf_adopted_algorithm_cleanup       (InfAdoptedAlgorithm *algorithm);

Removes requests in all users request logs which are no longer needed. This includes requests which cannot be undone or redone anymore due to the constraints of the "max-total-log-size" property, and requests that every participant is guaranteed to have processed already.

This function can be called after every executed request to keep memory use to a minimum, or it can be called in regular intervals, or it can also be omitted if the request history should be preserved.

algorithm :

A InfAdoptedAlgorithm.

inf_adopted_algorithm_can_undo ()

gboolean            inf_adopted_algorithm_can_undo      (InfAdoptedAlgorithm *algorithm,
                                                         InfAdoptedUser *user);

Returns whether user can issue an undo request in the current state. Note that if user is non-local, then the result of this function does not depend on the current state but on the state that we know user is guaranteed to have reached. This is because user might still issue an Undo request even if the max-total-log-size is already exceeded if user does not know yet that it is exceeded.

algorithm :

A InfAdoptedAlgorithm.

user :

A local InfAdoptedUser.

Returns :

TRUE if Undo is possible, FALSE otherwise.

inf_adopted_algorithm_can_redo ()

gboolean            inf_adopted_algorithm_can_redo      (InfAdoptedAlgorithm *algorithm,
                                                         InfAdoptedUser *user);

Returns whether user can issue a redo request in the current state. Note that if user is non-local, then the result of this function does not depend on the current state but on the state that we know user is guaranteed to have reached. This is because user might still issue a Redo request even if the max-total-log-size is already exceeded if user does not know yet that it is exceeded.

algorithm :

A InfAdoptedAlgorithm.

user :

A local InfAdoptedUser.

Returns :

TRUE if Redo is possible, FALSE otherwise.

Property Details

The "buffer" property

  "buffer"                   InfBuffer*            : Read / Write / Construct Only

The buffer to apply operations to.


The "buffer-modified-state" property

  "buffer-modified-state"    InfAdoptedStateVector*  : Read

The state in which the buffer is considered not being modified.


The "current-state" property

  "current-state"            InfAdoptedStateVector*  : Read

The state vector describing the current document state.


The "max-total-log-size" property

  "max-total-log-size"       guint                 : Read / Write / Construct Only

The maximum number of requests to keep in all user's logs.

Default value: 2048


The "user-table" property

  "user-table"               InfUserTable*         : Read / Write / Construct Only

The user table.

Signal Details

The "begin-execute-request" signal

void                user_function                      (InfAdoptedAlgorithm *algorithm,
                                                        InfAdoptedUser      *user,
                                                        InfAdoptedRequest   *request,
                                                        gpointer             user_data)      : Run Last

This signal is emitted every time the algorithm executes a request, i.e. transforms it such that it can be applied to the current state, resolves undo/redo operations and applies the resulting operation to the buffer.

algorithm :

The InfAdoptedAlgorithm executing a request.

user :

The InfAdoptedUser executing the request.

request :

The InfAdoptedRequest being executed.

user_data :

user data set when the signal handler was connected.

The "can-redo-changed" signal

void                user_function                      (InfAdoptedAlgorithm *algorithm,
                                                        InfAdoptedUser      *user,
                                                        gboolean             can_undo,
                                                        gpointer             user_data)      : Run Last

This signal is emitted every time the can-redo state of a local user in algorithm's user table changed. The can-redo state defines whether user can generate a redo request (via inf_adopted_algorithm_generate_redo()) in the current situation, see also inf_adopted_algorithm_can_redo().

algorithm :

The InfAdoptedAlgorithm for which a user's can-redo state changed.

user :

The InfAdoptedUser whose can-redo state has changed.

can_undo :

Whether user can issue a redo request in the current state or not.

user_data :

user data set when the signal handler was connected.

The "can-undo-changed" signal

void                user_function                      (InfAdoptedAlgorithm *algorithm,
                                                        InfAdoptedUser      *user,
                                                        gboolean             can_undo,
                                                        gpointer             user_data)      : Run Last

This signal is emitted every time the can-undo state of a local user in algorithm's user table changed. The can-undo state defines whether user can generate an undo request (via inf_adopted_algorithm_generate_undo()) in the current situation, see also inf_adopted_algorithm_can_undo().

algorithm :

The InfAdoptedAlgorithm for which a user's can-undo state changed.

user :

The InfAdoptedUser whose can-undo state has changed.

can_undo :

Whether user can issue an undo request in the current state or not.

user_data :

user data set when the signal handler was connected.

The "end-execute-request" signal

void                user_function                      (InfAdoptedAlgorithm *algorithm,
                                                        InfAdoptedUser      *user,
                                                        InfAdoptedRequest   *request,
                                                        InfAdoptedRequest   *translated,
                                                        gpointer             error,
                                                        gpointer             user_data)       : Run Last

This signal is emitted after a request has been executed. The request parameter is not necessarily the same as the one in the corresponding emission of "begin-execute-request", however its effect on the buffer is the same. The difference is that the request in this signal might be reversible while the request in the "begin-execute-request" emission might not be reversible. The algorithm can make some requests reversible during their execution.

The translated request is the result of the transformation, i.e. it is always a INF_ADOPTED_REQUEST_DO type request and its state vector corresponds to the current state. It has already been applied on the buffer by the algorithm. If request is of type INF_ADOPTED_REQUEST_UNDO or INF_ADOPTED_REQUEST_REDO then translated represents the operation that actually was performed on the buffer to undo or redo the effect of a previous request.

It can happen that an error occurs during execution. Usually this is due to invalid input, such as a request that cannot be transformed to the current state, a INF_ADOPTED_REQUEST_UNDO request which has no corresponding INF_ADOPTED_REQUEST_DO or INF_ADOPTED_REQUEST_REDO request, or a request that ends up in an invalid operation (e.g. inserting text behind the end of the document). If such an error occurs then request is the same as the one in the InfAdoptedAlgorithm::begin-execute-request emission, translated may or may not be NULL and error contains information on the error occured.

algorithm :

The InfAdoptedAlgorithm executing a request.

user :

The InfAdoptedUser executing the request.

request :

The InfAdoptedRequest that was executed.

translated :

The result of the request transformation, or NULL.

error :

The error that occurred during execution, or NULL.

user_data :

user data set when the signal handler was connected.

See Also

InfAdoptedSession