Package advene :: Package model :: Package backends :: Module sqlite :: Class _SqliteBackend
[hide private]
[frames] | no frames]

Class _SqliteBackend

source code

object --+
         |
        _SqliteBackend

I am the reference implementation of advene backend instances.

A number of conventions are used in all methods.

Method naming rationale
=======================

When the parameters *or* return type of methods depend on the element type
they handle, distinct methods with the element type in their name are
defined (e.g. `create_annotation` vs. `create_view`). On the other hand, if
neither the parameters nor the return value change (type-wise) w.r.t. the
element type, a single method is defined, with the element type as a
parameter (e.g. `delete_element`).

Note that, as far as the rule above is concerned, tuples of different size
have different types, as well as iterators yielding objects of different
types.

Note also there is a notable exception to that rule: `get_element`, which
does not expect the type of an element, but returns a tuple with all the
elements attributes. A protocol forcing to first get the element type,
then call the appropriate method, whould have been more regular but
inconvenient, and probably less efficient.

Parameter names and semantics
=============================

package_id
  the package id as returned in second position by `create` or `bind`. The
  operation will apply to that particular package.

package_ids
  an iterable of package ids as described above. The operation will apply
  in one shot on all these packages.

id
  an element id. This is always used in conjunction with ``package_id``.

element_type
  one of the constants defined in `advene.model.core.element`. It is always
  used in conjunction with ``package_id`` and ``id`` and *must* be
  consistent with them (i.e. be the type of the identified element if it
  exists). The behaviour of the method is *unspecified* if ``element_type``
  is not consistent. As a consequence, the fact that this particular
  implementation ignores an inconsistent ``element_type`` and works anyway
  must *not* be relied on.

Instance Methods [hide private]
 
get_bound_url(self, package_id)
Return the backend-URL the given package is bound to.
source code
 
get_url(self, package_id) source code
 
update_url(self, package_id, uri) source code
 
get_uri(self, package_id) source code
 
update_uri(self, package_id, uri) source code
 
close(self, package_id)
Inform the backend that a given package will no longer be used.
source code
 
delete(self, package_id)
Delete from the backend all the data about a bound package.
source code
 
create_media(self, package_id, id, url, frame_of_reference)
Create a new media.
source code
 
create_annotation(self, package_id, id, media, begin, end, mimetype, model, url)
Create a new annotation and its associated content.
source code
 
create_relation(self, package_id, id, mimetype, model, url)
Create a new empty relation and its associated content.
source code
 
create_view(self, package_id, id, mimetype, model, url)
Create a new view and its associated content.
source code
 
create_resource(self, package_id, id, mimetype, model, url)
Create a new resource and its associated content.
source code
 
create_tag(self, package_id, id)
Create a new tag.
source code
 
create_list(self, package_id, id)
Create a new empty list.
source code
 
create_query(self, package_id, id, mimetype, model, url)
Create a new query and its associated content.
source code
 
create_import(self, package_id, id, url, uri)
Create a new import.
source code
 
has_element(self, package_id, id, element_type=None)
Return True if the given package has an element with the given id.
source code
 
get_element(self, package_id, id)
Return the tuple describing a given element.
source code
 
iter_references(self, package_ids, element)
Iter over all the elements relying on the identified element (where `element` is a uri-ref).
source code
 
iter_references_with_import(self, package_id, id)
Iter over all the elements relying on the identified import.
source code
 
iter_medias(self, package_ids, id=None, url=None, foref=None)
Yield tuples of the form(MEDIA, package_id, id, url,).
source code
 
iter_annotations(self, package_ids, id=None, media=None, begin=None, begin_min=None, begin_max=None, end=None, end_min=None, end_max=None)
Yield tuples of the form (ANNOTATION, package_id, id, media, begin, end, mimetype, model, url), ordered by begin, end and media id-ref.
source code
 
iter_relations(self, package_ids, id=None, member=None, pos=None)
Yield tuples of the form (RELATION, package_id, id, mimetype, model, url).
source code
 
iter_views(self, package_ids, id=None)
Yield tuples of the form (VIEW, package_id, id, mimetype, model, url).
source code
 
iter_resources(self, package_ids, id=None)
Yield tuples of the form (RESOURCE, package_id, id, mimetype, model, url).
source code
 
iter_tags(self, package_ids, id=None, meta=None)
Yield tuples of the form (TAG, package_id, id,).
source code
 
iter_lists(self, package_ids, id=None, item=None, pos=None, meta=None)
Yield tuples of the form (LIST, package_id, id,).
source code
 
iter_queries(self, package_ids, id=None)
Yield tuples of the form (QUERY, package_id, id, mimetype, model, url).
source code
 
iter_imports(self, package_ids, id=None, url=None, uri=None)
Yield tuples of the form (IMPORT, package_id, id, url, uri).
source code
 
count_medias(self, package_ids, id=None, url=None, foref=None)
Count the medias matching the criteria.
source code
 
count_annotations(self, package_ids, id=None, media=None, begin=None, begin_min=None, begin_max=None, end=None, end_min=None, end_max=None)
Return the number of annotations matching the criteria.
source code
 
count_relations(self, package_ids, id=None, member=None, pos=None)
Return the number of relations matching the criteria.
source code
 
count_views(self, package_ids, id=None)
Return the number of views matching the criteria.
source code
 
count_resources(self, package_ids, id=None)
Return the number of resources matching the criteria.
source code
 
count_tags(self, package_ids, id=None, meta=None)
Return the number of tags matching the criteria.
source code
 
count_lists(self, package_ids, id=None, item=None, pos=None, meta=None)
Return the number of lists matching the criteria.
source code
 
count_queries(self, package_ids, id=None)
Return the number of querties matching the criteria.
source code
 
count_imports(self, package_ids, id=None, url=None, uri=None)
Return the number of imports matching the criteria.
source code
 
update_media(self, package_id, id, url, frame_of_reference) source code
 
update_annotation(self, package_id, id, media, begin, end)
``media`` is the id-ref of an own or directly imported media.
source code
 
update_import(self, package_id, id, url, uri) source code
 
rename_element(self, package_id, old_id, element_type, new_id)
Rename an own elemenent of package_id.
source code
 
rename_references(self, package_ids, old_uriref, new_id)
Reflect the renaming of an element in several packages.
source code
 
delete_element(self, package_id, id, element_type)
Delete the identified element.
source code
 
get_content_info(self, package_id, id, element_type)
Return information about the content of an element, or None.
source code
 
update_content_info(self, package_id, id, element_type, mimetype, model, url)
Update the content information of the identified element.
source code
 
get_content_data(self, package_id, id, element_type)
Return the stored data, as a string, of the content of an element.
source code
 
update_content_data(self, package_id, id, element_type, data)
Update the content data of the identified element.
source code
 
iter_contents_with_model(self, package_ids, model)
Return tuples of the form (package_id, id) of all the elements with a content having the given model.
source code
 
iter_meta(self, package_id, id, element_type)
Iter over the metadata, sorting keys in alphabetical order.
source code
 
get_meta(self, package_id, id, element_type, key)
Return the given metadata of the identified element.
source code
 
set_meta(self, package_id, id, element_type, key, val, val_is_id)
Set the given metadata of the identified element.
source code
 
iter_meta_refs(self, package_ids, uriref, element_type)
Iter over the metadata whose value is a reference to the element identified by uriref.
source code
 
insert_member(self, package_id, id, member, pos, n=-1)
Insert a member at the given position.
source code
 
update_member(self, package_id, id, member, pos)
Remobv the member at the given position in the identified relation.
source code
 
count_members(self, package_id, id)
Count the members of the identified relations.
source code
 
get_member(self, package_id, id, pos, n=-1)
Return the id-ref of the member at the given position in the identified relation.
source code
 
iter_members(self, package_id, id)
Iter over all the members of the identified relation.
source code
 
remove_member(self, package_id, id, pos)
Remove the member at the given position in the identified relation.
source code
 
insert_item(self, package_id, id, item, pos, n=-1)
Insert an item at the given position.
source code
 
update_item(self, package_id, id, item, pos)
Remobv the item at the given position in the identified list.
source code
 
count_items(self, package_id, id)
Count the items of the identified lists.
source code
 
get_item(self, package_id, id, pos, n=-1)
Return the id-ref of the item at the given position in the identified list.
source code
 
iter_items(self, package_id, id)
Iter over all the items of the identified list.
source code
 
remove_item(self, package_id, id, pos)
Remove the item at the given position in the identified list.
source code
 
associate_tag(self, package_id, element, tag)
Associate a tag to an element.
source code
 
dissociate_tag(self, package_id, element, tag)
Dissociate a tag from an element.
source code
 
iter_tags_with_element(self, package_ids, element)
Iter over all the tags associated to element in the given packages.
source code
 
iter_elements_with_tag(self, package_ids, tag)
Iter over all the elements associated to tag in the given packages.
source code
 
iter_taggers(self, package_ids, element, tag)
Iter over all the packages associating element to tag.
source code
 
iter_external_tagging(self, package_id)
Iter over all tagging involving two imported elements.
source code
 
__init__(self, path, conn, force)
Is not part of the interface.
source code
 
_bind(self, package_id, package) source code
 
_check_unused(self, package_id) source code
 
_begin_transaction(self, mode='')
Begin a transaction.
source code
 
_create_element(self, execute, package_id, id, element_type)
Perform controls and insertions common to all elements.
source code

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __str__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

close(self, package_id)

source code 

Inform the backend that a given package will no longer be used.

NB: this implementation is robust to packages forgetting to close themselves, i.e. when packages are garbage collected, this is detected and they are automatically unbound.

delete(self, package_id)

source code 

Delete from the backend all the data about a bound package.

Obviously, a deleted package does not need to be closed.

create_media(self, package_id, id, url, frame_of_reference)

source code 

Create a new media.

Raise a ModelException if the identifier already exists in the package.

create_annotation(self, package_id, id, media, begin, end, mimetype, model, url)

source code 
Create a new annotation and its associated content.

Specific parameters
-------------------
media
  id-ref of the media this annotation refs to
begin, end
  int boundaries of the annotated temporal fragment
mimetype
  the mimetype of the annotation content
model
  the id-ref of the content model for the annotation (can be empty)
url
  if non empty, the annotation content will be not be stored, and will
  be fetched on demand from that URL

Raise a ModelException if the identifier already exists in the package.

create_relation(self, package_id, id, mimetype, model, url)

source code 
Create a new empty relation and its associated content.

Specific parameters
-------------------
mimetype
  the mimetype of the annotation content
model
  the id-ref of the content model for the annotation (can be empty)
url
  if non empty, the annotation content will be not be stored, and will
  be fetched on demand from that URL

Raise a ModelException if the identifier already exists in the package.

create_view(self, package_id, id, mimetype, model, url)

source code 
Create a new view and its associated content.

Specific parameters
-------------------
mimetype
  the mimetype of the view content
model
  the id-ref of the content model the view (can be empty)
url
  if non empty, the view content will be not be stored, and will
  be fetched on demand from that URL

Raise a ModelException if the identifier already exists in the package.

create_resource(self, package_id, id, mimetype, model, url)

source code 
Create a new resource and its associated content.

Specific parameters
-------------------
mimetype
  the mimetype of the resource content
model
  the id-ref of the content model for the resource (can be empty)
url
  if non empty, the resource content will be not be stored, and will
  be fetched on demand from that URL

Raise a ModelException if the identifier already exists in the package.

create_tag(self, package_id, id)

source code 

Create a new tag.

Raise a ModelException if the identifier already exists in the package.

create_list(self, package_id, id)

source code 

Create a new empty list.

Raise a ModelException if the identifier already exists in the package.

create_query(self, package_id, id, mimetype, model, url)

source code 
Create a new query and its associated content.

Specific parameters
-------------------
mimetype
  the mimetype of the query content
model
  the id-ref of the content model for the query (can be empty)
url
  if non empty, the query content will be not be stored, and will
  be fetched on demand from that URL

Raise a ModelException if the identifier already exists in the package.

create_import(self, package_id, id, url, uri)

source code 

Create a new import.

Raise a ModelException if the identifier already exists in the package.

has_element(self, package_id, id, element_type=None)

source code 

Return True if the given package has an element with the given id. If element_type is provided, only return true if the element has the the given type.

get_element(self, package_id, id)

source code 

Return the tuple describing a given element.

If the element does not exist, None is returned.

iter_references(self, package_ids, element)

source code 

Iter over all the elements relying on the identified element
(where `element` is a uri-ref).

Yields 3-tuples where the first item one of the given package_ids,
the second element is either an element id-ref or an empty string to
identify the package itself. The thirs item describes the relation
between the second item and the identified element. It can be either:
  an attribute name with an element as its value
    in that case, the identified element is the value of the attribute
    for the element or package identified by the first item.
  the string ":item %s" where %s is an int i
    the first item identifies a list, and its i'th item is the
    identified element
  the string ":member %s" where %s is an int i
    the first item identifies a relation, and its i'th item is the
    identified element
  the string ":meta %s" where %s is a metadata key
    in that case, the identified element is the value of that metadata
    for the element or package identified by the first item.
  the string ":tag %s" where %s is an id-ref
    the identified element is a tag, to which this package identified
    by the first parameter associates the element identified by the
    id-ref.
  the string ":tagged %s" where %s is an id-ref
    this package associates the identified element the tag identified
    by the id-ref.

The attribute names that may be returned are ``media`` and
``content_model``.

iter_references_with_import(self, package_id, id)

source code 
Iter over all the elements relying on the identified import.

Yields 3-tuples where the first item is either an element id-ref
or an empty string to identify the package itself and the third item is
the id (without the importe prefix) of an element imported through the
import in question. The second item describes the relation between the
first and third ones. It can be either:
  an attribute name with an element as its value
    in that case, the imported element is the value of the attribute
    for the element or package identified by the first item.
  the string ":item %s" where %s is an int i
    the first item identifies a list, and its i'th item is the
    imported element
  the string ":member %s" where %s is an int i
    the first item identifies a relation, and its i'th item is the
    imported element
  the string ":meta %s" where %s is a metadata key
    in that case, the imported element is the value of that metadata
    for the element or package identified by the first item.
  the string ":tag %s" where %s is an id-ref
    the identified element is a tag, to which this package identified
    by the first parameter associates the element identified by the
    id-ref.
  the string ":tagged %s" where %s is an id-ref
    this package associates the identified element the tag identified
    by the id-ref.

The attribute names that may be returned are ``media`` and
``content_model``.

iter_annotations(self, package_ids, id=None, media=None, begin=None, begin_min=None, begin_max=None, end=None, end_min=None, end_max=None)

source code 

Yield tuples of the form (ANNOTATION, package_id, id, media, begin, end, mimetype, model, url), ordered by begin, end and media id-ref.

``media`` is the uri-ref of a media or an iterable of uri-refs.

count_annotations(self, package_ids, id=None, media=None, begin=None, begin_min=None, begin_max=None, end=None, end_min=None, end_max=None)

source code 

Return the number of annotations matching the criteria.

``media`` is the uri-ref of a media or an iterable of uri-refs.

rename_element(self, package_id, old_id, element_type, new_id)

source code 

Rename an own elemenent of package_id.

NB: element_type must be provided and must be the type constant of the identified element, or the behaviour of this method is unspecified.

NB: This does not update references to that element. For that, you must also use `rename_references`. This however does update the id-ref of imported elements if the renamed element is an import.

rename_references(self, package_ids, old_uriref, new_id)

source code 

Reflect the renaming of an element in several packages.

Apply the change of id of an element (formerly known as old_uriref) in all references to that element in package_ids.

delete_element(self, package_id, id, element_type)

source code 

Delete the identified element.

NB: This does not delete references to that element, *even* in the same package. The appropriate methods (`iter_references`, `iter_references_with_import`) must be used to detect and delete those references prior to deletion.

get_content_info(self, package_id, id, element_type)

source code 

Return information about the content of an element, or None.

The information is a tuple of the form (mimetype, model_id, url), where ``model_id`` and ``url`` can be empty strings.

None is returned if the element does not exist or has no content.

Note that this method will not be used often since this information is provided by get_element for all elements having a content.

update_content_info(self, package_id, id, element_type, mimetype, model, url)

source code 

Update the content information of the identified element.

``model`` is the id of an own or directly imported resource, or an empty string to specify no model (not None).

If ``url`` is not an empty string, any data stored in the backend for this content will be discarded.

get_content_data(self, package_id, id, element_type)

source code 

Return the stored data, as a string, of the content of an element.

This method will return an empty string if the content is externally stored (non-empty ``url`` attribute).

update_content_data(self, package_id, id, element_type, data)

source code 

Update the content data of the identified element.

If `data` is not an empty string, the ``url`` attribute of the content will be cleared.

iter_contents_with_model(self, package_ids, model)

source code 

Return tuples of the form (package_id, id) of all the elements with a content having the given model.

@param model the uri-ref of a resource

iter_meta(self, package_id, id, element_type)

source code 

Iter over the metadata, sorting keys in alphabetical order.

Yield tuples of the form (key, val, val_is_id) (cf. `get_meta` and `set_meta`).

If package metadata is targeted, id should be an empty string (in that case, element_type will be ignored).

get_meta(self, package_id, id, element_type, key)

source code 

Return the given metadata of the identified element.

Return a tuple of the form (val, val_is_id) where the second item is a boolean, indicating whether the first item must be interpreted as an id-ref, or None if the element has no metadata with that key.

If package metadata is targeted, id should be an empty string (in that case, element_type will be ignored).

set_meta(self, package_id, id, element_type, key, val, val_is_id)

source code 

Set the given metadata of the identified element.

Parameter ``val_is_id`` indicates whether parameter ``val`` must be interpreted as an id-ref rather than a plain string. Note that ``val`` can also be None to unset the corresponding metadata; in that case, val_is_id is ignored.

If package metadata is targeted, id should be an empty string (in that case, element_type will be ignored).

iter_meta_refs(self, package_ids, uriref, element_type)

source code 

Iter over the metadata whose value is a reference to the element identified by uriref.

The returned iterator yields triples of the form (package_id, element_id, metadata_key) where element_id is the empty string for package metadata.d

insert_member(self, package_id, id, member, pos, n=-1)

source code 

Insert a member at the given position. ``member`` is the id-ref of an own or directly imported member. ``pos`` may be any value between -1 and n (inclusive), where n is the current number of members. If -1, the member will be appended at the end (**note** that this is not the same behaviour as ``list.insert`` in python2.5). If non-negative, the member will be inserted at that position.

NB: the total number of members, n, if known, may be provided, as an optimization.

update_member(self, package_id, id, member, pos)

source code 

Remobv the member at the given position in the identified relation. ``member`` is the id-ref of an own or directly imported member.

count_members(self, package_id, id)

source code 

Count the members of the identified relations.

This should return 0 if the relation does not exist.

get_member(self, package_id, id, pos, n=-1)

source code 

Return the id-ref of the member at the given position in the identified relation.

NB: the total number of members, n, if known, may be provided, as an optimization.

insert_item(self, package_id, id, item, pos, n=-1)

source code 

Insert an item at the given position. ``item`` is the id-ref of an own or directly imported item. ``pos`` may be any value between -1 and n (inclusive), where n is the current number of items. If -1, the item will be appended at the end (**note** that this is not the same behaviour as ``list.insert`` in python2.5). If non-negative, the item will be inserted at that position.

NB: the total number of members, n, if known, may be provided, as an optimization.

update_item(self, package_id, id, item, pos)

source code 

Remobv the item at the given position in the identified list. ``item`` is the id-ref of an own or directly imported item.

count_items(self, package_id, id)

source code 

Count the items of the identified lists.

This should return 0 if the list does not exist.

get_item(self, package_id, id, pos, n=-1)

source code 

Return the id-ref of the item at the given position in the identified list.

NB: the total number of members, n, if known, may be provided, as an optimization.

associate_tag(self, package_id, element, tag)

source code 

Associate a tag to an element.

@param element the id-ref of an own or directly imported element @param tag the id-ref of an own or directly imported tag

dissociate_tag(self, package_id, element, tag)

source code 

Dissociate a tag from an element.

@param element the id-ref of an own or directly imported element @param tag the id-ref of an own or directly imported tag

iter_tags_with_element(self, package_ids, element)

source code 

Iter over all the tags associated to element in the given packages.

@param element the uri-ref of an element

iter_elements_with_tag(self, package_ids, tag)

source code 

Iter over all the elements associated to tag in the given packages.

@param tag the uri-ref of a tag

iter_taggers(self, package_ids, element, tag)

source code 

Iter over all the packages associating element to tag.

@param element the uri-ref of an element @param tag the uri-ref of a tag

iter_external_tagging(self, package_id)

source code 

Iter over all tagging involving two imported elements.

This is useful for serialization.

__init__(self, path, conn, force)
(Constructor)

source code 

Is not part of the interface. Instances must be created either with the create or the bind module functions.

Create a backend, and bind it to the given URL.

Overrides: object.__init__

_begin_transaction(self, mode='')

source code 

Begin a transaction.

This method must *always* be used to begin a transaction (do *not* use `self._curs.execute("BEGIN")` directly. See `_FlushableIterator` .

_create_element(self, execute, package_id, id, element_type)

source code 

Perform controls and insertions common to all elements.

NB: This starts a transaction that must be commited by caller.