# Copyright 2011-2012 Canonical Ltd.
#
# This file is part of u1db.
#
# u1db is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation.
#
# u1db is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with u1db. If not, see .
#
"""A Cython wrapper around the C implementation of U1DB Database backend."""
cdef extern from "Python.h":
object PyString_FromStringAndSize(char *s, Py_ssize_t n)
int PyString_AsStringAndSize(object o, char **buf, Py_ssize_t *length
) except -1
char *PyString_AsString(object) except NULL
char *PyString_AS_STRING(object)
char *strdup(char *)
void *calloc(size_t, size_t)
void free(void *)
ctypedef struct FILE:
pass
fprintf(FILE *, char *, ...)
FILE *stderr
size_t strlen(char *)
cdef extern from "stdarg.h":
ctypedef struct va_list:
pass
void va_start(va_list, void*)
void va_start_int "va_start" (va_list, int)
void va_end(va_list)
cdef extern from "u1db/u1db.h":
ctypedef struct u1database:
pass
ctypedef struct u1db_document:
char *doc_id
size_t doc_id_len
char *doc_rev
size_t doc_rev_len
char *json
size_t json_len
int has_conflicts
# Note: u1query is actually defined in u1db_internal.h, and in u1db.h it is
# just an opaque pointer. However, older versions of Cython don't let
# you have a forward declaration and a full declaration, so we just
# expose the whole thing here.
ctypedef struct u1query:
char *index_name
int num_fields
char **fields
cdef struct u1db_oauth_creds:
int auth_kind
char *consumer_key
char *consumer_secret
char *token_key
char *token_secret
ctypedef union u1db_creds
ctypedef u1db_creds* const_u1db_creds_ptr "const u1db_creds *"
ctypedef char* const_char_ptr "const char*"
ctypedef int (*u1db_doc_callback)(void *context, u1db_document *doc)
ctypedef int (*u1db_key_callback)(void *context, int num_fields,
const_char_ptr *key)
ctypedef int (*u1db_doc_gen_callback)(void *context,
u1db_document *doc, int gen, const_char_ptr trans_id)
ctypedef int (*u1db_trans_info_callback)(void *context,
const_char_ptr doc_id, int gen, const_char_ptr trans_id)
u1database * u1db_open(char *fname)
void u1db_free(u1database **)
int u1db_set_replica_uid(u1database *, char *replica_uid)
int u1db_set_document_size_limit(u1database *, int limit)
int u1db_get_replica_uid(u1database *, const_char_ptr *replica_uid)
int u1db_create_doc_from_json(u1database *db, char *json, char *doc_id,
u1db_document **doc)
int u1db_delete_doc(u1database *db, u1db_document *doc)
int u1db_get_doc(u1database *db, char *doc_id, int include_deleted,
u1db_document **doc)
int u1db_get_docs(u1database *db, int n_doc_ids, const_char_ptr *doc_ids,
int check_for_conflicts, int include_deleted,
void *context, u1db_doc_callback cb)
int u1db_get_all_docs(u1database *db, int include_deleted, int *generation,
void *context, u1db_doc_callback cb)
int u1db_put_doc(u1database *db, u1db_document *doc)
int u1db__validate_source(u1database *db, const_char_ptr replica_uid,
int replica_gen, const_char_ptr replica_trans_id)
int u1db__put_doc_if_newer(u1database *db, u1db_document *doc,
int save_conflict, char *replica_uid,
int replica_gen, char *replica_trans_id,
int *state, int *at_gen)
int u1db_resolve_doc(u1database *db, u1db_document *doc,
int n_revs, const_char_ptr *revs)
int u1db_delete_doc(u1database *db, u1db_document *doc)
int u1db_whats_changed(u1database *db, int *gen, char **trans_id,
void *context, u1db_trans_info_callback cb)
int u1db__get_transaction_log(u1database *db, void *context,
u1db_trans_info_callback cb)
int u1db_get_doc_conflicts(u1database *db, char *doc_id, void *context,
u1db_doc_callback cb)
int u1db_sync(u1database *db, const_char_ptr url,
const_u1db_creds_ptr creds, int *local_gen) nogil
int u1db_create_index_list(u1database *db, char *index_name,
int n_expressions, const_char_ptr *expressions)
int u1db_create_index(u1database *db, char *index_name, int n_expressions,
...)
int u1db_get_from_index_list(u1database *db, u1query *query, void *context,
u1db_doc_callback cb, int n_values,
const_char_ptr *values)
int u1db_get_from_index(u1database *db, u1query *query, void *context,
u1db_doc_callback cb, int n_values, char *val0,
...)
int u1db_get_range_from_index(u1database *db, u1query *query,
void *context, u1db_doc_callback cb,
int n_values, const_char_ptr *start_values,
const_char_ptr *end_values)
int u1db_delete_index(u1database *db, char *index_name)
int u1db_list_indexes(u1database *db, void *context,
int (*cb)(void *context, const_char_ptr index_name,
int n_expressions, const_char_ptr *expressions))
int u1db_get_index_keys(u1database *db, char *index_name, void *context,
u1db_key_callback cb)
int u1db_simple_lookup1(u1database *db, char *index_name, char *val1,
void *context, u1db_doc_callback cb)
int u1db_query_init(u1database *db, char *index_name, u1query **query)
void u1db_free_query(u1query **query)
int U1DB_OK
int U1DB_INVALID_PARAMETER
int U1DB_REVISION_CONFLICT
int U1DB_INVALID_DOC_ID
int U1DB_DOCUMENT_ALREADY_DELETED
int U1DB_DOCUMENT_DOES_NOT_EXIST
int U1DB_NOT_IMPLEMENTED
int U1DB_INVALID_JSON
int U1DB_DOCUMENT_TOO_BIG
int U1DB_USER_QUOTA_EXCEEDED
int U1DB_INVALID_VALUE_FOR_INDEX
int U1DB_INVALID_FIELD_SPECIFIER
int U1DB_INVALID_GLOBBING
int U1DB_BROKEN_SYNC_STREAM
int U1DB_DUPLICATE_INDEX_NAME
int U1DB_INDEX_DOES_NOT_EXIST
int U1DB_INVALID_GENERATION
int U1DB_INVALID_TRANSACTION_ID
int U1DB_INVALID_TRANSFORMATION_FUNCTION
int U1DB_UNKNOWN_OPERATION
int U1DB_INTERNAL_ERROR
int U1DB_TARGET_UNAVAILABLE
int U1DB_INSERTED
int U1DB_SUPERSEDED
int U1DB_CONVERGED
int U1DB_CONFLICTED
int U1DB_OAUTH_AUTH
void u1db_free_doc(u1db_document **doc)
int u1db_doc_set_json(u1db_document *doc, char *json)
int u1db_doc_get_size(u1db_document *doc)
cdef extern from "u1db/u1db_internal.h":
ctypedef struct u1db_row:
u1db_row *next
int num_columns
int *column_sizes
unsigned char **columns
ctypedef struct u1db_table:
int status
u1db_row *first_row
ctypedef struct u1db_record:
u1db_record *next
char *doc_id
char *doc_rev
char *doc
ctypedef struct u1db_sync_exchange:
int target_gen
int num_doc_ids
char **doc_ids_to_return
int *gen_for_doc_ids
const_char_ptr *trans_ids_for_doc_ids
ctypedef int (*u1db__trace_callback)(void *context, const_char_ptr state)
ctypedef struct u1db_sync_target:
int (*get_sync_info)(u1db_sync_target *st, char *source_replica_uid,
const_char_ptr *st_replica_uid, int *st_gen,
char **st_trans_id, int *source_gen,
char **source_trans_id) nogil
int (*record_sync_info)(u1db_sync_target *st,
char *source_replica_uid, int source_gen, char *trans_id) nogil
int (*sync_exchange)(u1db_sync_target *st,
char *source_replica_uid, int n_docs,
u1db_document **docs, int *generations,
const_char_ptr *trans_ids,
int *target_gen, char **target_trans_id,
void *context, u1db_doc_gen_callback cb,
void *ensure_callback) nogil
int (*sync_exchange_doc_ids)(u1db_sync_target *st,
u1database *source_db, int n_doc_ids,
const_char_ptr *doc_ids, int *generations,
const_char_ptr *trans_ids,
int *target_gen, char **target_trans_id,
void *context,
u1db_doc_gen_callback cb,
void *ensure_callback) nogil
int (*get_sync_exchange)(u1db_sync_target *st,
char *source_replica_uid,
int last_known_source_gen,
u1db_sync_exchange **exchange) nogil
void (*finalize_sync_exchange)(u1db_sync_target *st,
u1db_sync_exchange **exchange) nogil
int (*_set_trace_hook)(u1db_sync_target *st,
void *context, u1db__trace_callback cb) nogil
void u1db__set_zero_delays()
int u1db__get_generation(u1database *, int *db_rev)
int u1db__get_document_size_limit(u1database *, int *limit)
int u1db__get_generation_info(u1database *, int *db_rev, char **trans_id)
int u1db__get_trans_id_for_gen(u1database *, int db_rev, char **trans_id)
int u1db_validate_gen_and_trans_id(u1database *, int db_rev,
const_char_ptr trans_id)
char *u1db__allocate_doc_id(u1database *)
int u1db__sql_close(u1database *)
u1database *u1db__copy(u1database *)
int u1db__sql_is_open(u1database *)
u1db_table *u1db__sql_run(u1database *, char *sql, size_t n)
void u1db__free_table(u1db_table **table)
u1db_record *u1db__create_record(char *doc_id, char *doc_rev, char *doc)
void u1db__free_records(u1db_record **)
int u1db__allocate_document(char *doc_id, char *revision, char *content,
int has_conflicts, u1db_document **result)
int u1db__generate_hex_uuid(char *)
int u1db__get_replica_gen_and_trans_id(u1database *db, char *replica_uid,
int *generation, char **trans_id)
int u1db__set_replica_gen_and_trans_id(u1database *db, char *replica_uid,
int generation, char *trans_id)
int u1db__sync_get_machine_info(u1database *db, char *other_replica_uid,
int *other_db_rev, char **my_replica_uid,
int *my_db_rev)
int u1db__sync_record_machine_info(u1database *db, char *replica_uid,
int db_rev)
int u1db__sync_exchange_seen_ids(u1db_sync_exchange *se, int *n_ids,
const_char_ptr **doc_ids)
int u1db__format_query(int n_fields, const_char_ptr *values, char **buf,
int *wildcard)
int u1db__get_sync_target(u1database *db, u1db_sync_target **sync_target)
int u1db__free_sync_target(u1db_sync_target **sync_target)
int u1db__sync_db_to_target(u1database *db, u1db_sync_target *target,
int *local_gen_before_sync) nogil
int u1db__sync_exchange_insert_doc_from_source(u1db_sync_exchange *se,
u1db_document *doc, int source_gen, const_char_ptr trans_id)
int u1db__sync_exchange_find_doc_ids_to_return(u1db_sync_exchange *se)
int u1db__sync_exchange_return_docs(u1db_sync_exchange *se, void *context,
int (*cb)(void *context,
u1db_document *doc, int gen,
const_char_ptr trans_id))
int u1db__create_http_sync_target(char *url, u1db_sync_target **target)
int u1db__create_oauth_http_sync_target(char *url,
char *consumer_key, char *consumer_secret,
char *token_key, char *token_secret,
u1db_sync_target **target)
cdef extern from "u1db/u1db_http_internal.h":
int u1db__format_sync_url(u1db_sync_target *st,
const_char_ptr source_replica_uid, char **sync_url)
int u1db__get_oauth_authorization(u1db_sync_target *st,
char *http_method, char *url,
char **oauth_authorization)
cdef extern from "u1db/u1db_vectorclock.h":
ctypedef struct u1db_vectorclock_item:
char *replica_uid
int generation
ctypedef struct u1db_vectorclock:
int num_items
u1db_vectorclock_item *items
u1db_vectorclock *u1db__vectorclock_from_str(char *s)
void u1db__free_vectorclock(u1db_vectorclock **clock)
int u1db__vectorclock_increment(u1db_vectorclock *clock, char *replica_uid)
int u1db__vectorclock_maximize(u1db_vectorclock *clock,
u1db_vectorclock *other)
int u1db__vectorclock_as_str(u1db_vectorclock *clock, char **result)
int u1db__vectorclock_is_newer(u1db_vectorclock *maybe_newer,
u1db_vectorclock *older)
from u1db import errors
from sqlite3 import dbapi2
cdef int _append_trans_info_to_list(void *context, const_char_ptr doc_id,
int generation,
const_char_ptr trans_id) with gil:
a_list =