Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# v1.8.0-alpha6
# v1.8.0-alpha9

* Added `TI_PROTO_CLIENT_REQ_EMIT_PEER` protocol, pr #414.
* Changed `copy()` behavior for wrapped type, pr #415.
Expand All @@ -9,6 +9,12 @@
* Include export enumerator members of type `thing`, pr #420.
* Fixed missing "ID" (`#`) field in export, issue #421.
* Added `type_all()` function, pr #422.
* Fixed handling non-stored thing with `to_type()`, issue #424.
* Added commit history with new functions, pr #425 and discussion #423.
- `commit()`: https://docs.thingsdb.io/v1/collection-api/commit/
- `history()`: https://docs.thingsdb.io/v1/thingsdb-api/history/
- `set_history()`: https://docs.thingsdb.io/v1/thingsdb-api/set_history/
- `del_history()`: https://docs.thingsdb.io/v1/thingsdb-api/del_history/

# v1.7.6

Expand Down
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ set(SOURCES
src/ti/closure.c
src/ti/collection.c
src/ti/collections.c
src/ti/commit.c
src/ti/commits.c
src/ti/condition.c
src/ti/connect.c
src/ti/counters.c
Expand Down Expand Up @@ -238,11 +240,12 @@ set(SOURCES
src/ti/store/storeaccess.c
src/ti/store/storecollection.c
src/ti/store/storecollections.c
src/ti/store/storecommits.c
src/ti/store/storeenums.c
src/ti/store/storegcollect.c
src/ti/store/storemodules.c
src/ti/store/storenames.c
src/ti/store/storenamedrooms.c
src/ti/store/storenames.c
src/ti/store/storeprocedures.c
src/ti/store/storestatus.c
src/ti/store/storetasks.c
Expand Down
6 changes: 5 additions & 1 deletion inc/doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
#define DOC_BYTES DOC_SEE("collection-api/bytes")
#define DOC_CHANGE_ID DOC_SEE("collection-api/change_id")
#define DOC_CLOSURE DOC_SEE("collection-api/closure")
#define DOC_COMMIT DOC_SEE("collection-api/commit")
#define DOC_DATETIME DOC_SEE("collection-api/datetime")
#define DOC_DEEP DOC_SEE("collection-api/deep")
#define DOC_DEL_ENUM DOC_SEE("collection-api/del_enum")
#define DOC_DEL_TYPE DOC_SEE("collection-api/del_type")
#define DOC_ENUM DOC_SEE("collection-api/enum")
#define DOC_ENUMS_INFO DOC_SEE("collection-api/enums_info")
#define DOC_ENUM_INFO DOC_SEE("collection-api/enum_info")
#define DOC_ENUM_MAP DOC_SEE("collection-api/enum_map")
#define DOC_ENUMS_INFO DOC_SEE("collection-api/enums_info")
#define DOC_ERR DOC_SEE("collection-api/err")
#define DOC_EXPORT DOC_SEE("collection-api/export")
#define DOC_FLOAT DOC_SEE("collection-api/float")
Expand Down Expand Up @@ -153,6 +154,7 @@
#define DOC_COLLECTIONS_INFO DOC_SEE("thingsdb-api/collections_info")
#define DOC_DEL_COLLECTION DOC_SEE("thingsdb-api/del_collection")
#define DOC_DEL_EXPIRED DOC_SEE("thingsdb-api/del_expired")
#define DOC_DEL_HISTORY DOC_SEE("thingsdb-api/del_history")
#define DOC_DEL_MODULE DOC_SEE("thingsdb-api/del_module")
#define DOC_DEL_NODE DOC_SEE("thingsdb-api/del_node")
#define DOC_DEL_TOKEN DOC_SEE("thingsdb-api/del_token")
Expand All @@ -164,6 +166,7 @@
#define DOC_HAS_NODE DOC_SEE("thingsdb-api/has_node")
#define DOC_HAS_TOKEN DOC_SEE("thingsdb-api/has_token")
#define DOC_HAS_USER DOC_SEE("thingsdb-api/has_user")
#define DOC_HISTORY DOC_SEE("thingsdb-api/history")
#define DOC_MODULE_INFO DOC_SEE("thingsdb-api/module_info")
#define DOC_MODULES_INFO DOC_SEE("thingsdb-api/modules_info")
#define DOC_NEW_COLLECTION DOC_SEE("thingsdb-api/new_collection")
Expand All @@ -178,6 +181,7 @@
#define DOC_RESTORE DOC_SEE("thingsdb-api/restore")
#define DOC_REVOKE DOC_SEE("thingsdb-api/revoke")
#define DOC_SET_DEFAULT_DEEP DOC_SEE("thingsdb-api/set_default_deep")
#define DOC_SET_HISTORY DOC_SEE("thingsdb-api/set_history")
#define DOC_SET_MODULE_CONF DOC_SEE("thingsdb-api/set_module_conf")
#define DOC_SET_MODULE_SCOPE DOC_SEE("thingsdb-api/set_module_scope")
#define DOC_SET_PASSWORD DOC_SEE("thingsdb-api/set_password")
Expand Down
1 change: 1 addition & 0 deletions inc/ti.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ struct ti_s
vec_t * users; /* ti_user_t */
vec_t * access_node; /* ti_auth_t */
vec_t * access_thingsdb; /* ti_auth_t */
vec_t * commits; /* ti_commit_t */
smap_t * procedures; /* ti_procedure_t */
smap_t * names; /* weak map for ti_name_t */
smap_t * qcache; /* pointer to cache in stack */
Expand Down
1 change: 1 addition & 0 deletions inc/ti/collection.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ ti_collection_t * ti_collection_create(
uint8_t deep);
void ti_collection_destroy(ti_collection_t * collection);
void ti_collection_drop(ti_collection_t * collection);
int ti_collection_to_pk(ti_collection_t * collection, msgpack_packer * pk);
_Bool ti_collection_name_check(const char * name, size_t n, ex_t * e);
int ti_collection_rename(
ti_collection_t * collection,
Expand Down
30 changes: 0 additions & 30 deletions inc/ti/collection.inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,6 @@
#include <ti/collection.h>
#include <ti/thing.h>

static inline int ti_collection_to_pk(
ti_collection_t * collection,
msgpack_packer * pk)
{
return -(
msgpack_pack_map(pk, 7) ||

mp_pack_str(pk, "collection_id") ||
msgpack_pack_uint64(pk, collection->id) ||

mp_pack_str(pk, "name") ||
mp_pack_strn(pk, collection->name->data, collection->name->n) ||

mp_pack_str(pk, "created_at") ||
msgpack_pack_uint64(pk, collection->created_at) ||

mp_pack_str(pk, "things") ||
msgpack_pack_uint64(pk, collection->things->n + collection->gc->n) ||

mp_pack_str(pk, "time_zone") ||
mp_pack_strn(pk, collection->tz->name, collection->tz->n) ||

mp_pack_str(pk, "default_deep") ||
msgpack_pack_uint64(pk, collection->deep) ||

mp_pack_str(pk, "next_free_id") ||
msgpack_pack_uint64(pk, collection->next_free_id)
);
}

/*
* Return a thing with a borrowed reference from the collection, or,
* if not found, tries to restore the thing from the garbage collector.
Expand Down
6 changes: 4 additions & 2 deletions inc/ti/collection.t.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ typedef struct ti_collection_s ti_collection_t;

#include <uv.h>
#include <inttypes.h>
#include <ti/raw.t.h>
#include <ti/enums.t.h>
#include <ti/commit.h>
#include <ti/raw.t.h>
#include <ti/thing.t.h>
#include <ti/tz.h>
#include <ti/types.t.h>
#include <ti/tz.h>
#include <util/guid.h>
#include <util/imap.h>
#include <util/queue.h>
Expand All @@ -39,6 +40,7 @@ struct ti_collection_s
uv_mutex_t * lock; /* only for watch/ unwatch/ away-mode */
vec_t * futures; /* no reference, type: ti_future_t */
vec_t * vtasks; /* tasks, type: ti_vtask_t */
vec_t * commits; /* migration changes ti_commit_t */
guid_t guid; /* derived from collection->id */
};

Expand Down
41 changes: 41 additions & 0 deletions inc/ti/commit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* ti/commit.h
*/
#ifndef TI_COMMIT_H_
#define TI_COMMIT_H_

#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <ti/raw.t.h>
#include <ti/val.t.h>
#include <util/mpack.h>
#include <util/vec.h>

typedef struct ti_commit_s ti_commit_t;

ti_commit_t * ti_commit_from_up(mp_unp_t * up);
ti_commit_t * ti_commit_make(
uint64_t id,
const char * code,
ti_raw_t * by,
ti_raw_t * message);
void ti_commit_destroy(ti_commit_t * commit);
int ti_commit_to_pk(ti_commit_t * commit, msgpack_packer * pk);
int ti_commit_to_client_pk(
ti_commit_t * commit,
_Bool detail,
msgpack_packer * pk);
ti_val_t * ti_commit_as_mpval(ti_commit_t * commit, _Bool detail);

struct ti_commit_s
{
uint64_t id; /* equal to the change id */
time_t ts; /* timestamp of the change */
ti_raw_t * code; /* original query string */
ti_raw_t * message; /* never NULL */
ti_raw_t * by; /* never NULL */
ti_raw_t * err_msg; /* may be NULL */
};

#endif /* TI_COMMIT_H_ */
56 changes: 56 additions & 0 deletions inc/ti/commits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* ti/commits.h
*/
#ifndef TI_COMMITS_H_
#define TI_COMMITS_H_

#include <ti/auth.h>
#include <ti/datetime.h>
#include <ti/raw.t.h>
#include <ti/regex.t.h>
#include <ti/thing.t.h>
#include <ti/varr.t.h>
#include <ti/vbool.h>
#include <ti/vint.h>
#include <util/vec.h>
#include <ex.h>

#define TI_COMMITS_MASK TI_AUTH_QUERY|TI_AUTH_CHANGE

typedef struct
{
ti_raw_t * scope;
ti_raw_t * contains;
ti_regex_t * match;
ti_vint_t * id;
ti_vint_t * last;
ti_vint_t * first;
ti_datetime_t * before;
ti_datetime_t * after;
ti_vbool_t * has_err;
ti_vbool_t * detail;
} ti_commits_options_t;

typedef struct
{
vec_t ** commits;
vec_t ** access;
uint64_t scope_id;
} ti_commits_history_t;

void ti_commits_destroy(vec_t ** commits);
int ti_commits_options(
ti_commits_options_t * options,
ti_thing_t * thing,
_Bool allow_detail,
ex_t * e);
int ti_commits_history(
ti_commits_history_t * history,
ti_commits_options_t * options,
ex_t * e);
vec_t ** ti_commits_from_scope(ti_raw_t * scope, ex_t * e);
int ti_commits_set_history(vec_t ** commits, _Bool state);
vec_t * ti_commits_find(vec_t * commits, ti_commits_options_t * options);
vec_t * ti_commits_del(vec_t ** commits, ti_commits_options_t * options);

#endif /* TI_COMMITS_H_ */
13 changes: 13 additions & 0 deletions inc/ti/fn/fn.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include <ti/member.h>
#include <ti/member.inline.h>
#include <ti/method.h>
#include <ti/commit.h>
#include <ti/commits.h>
#include <ti/mod/expose.h>
#include <ti/mod/expose.t.h>
#include <ti/mod/github.h>
Expand Down Expand Up @@ -474,6 +476,17 @@ static inline int fn_not_thingsdb_or_collection_scope(
return e->nr;
}

static inline int fn_commit(const char * name, ti_query_t * query, ex_t * e)
{
if (*ti_query_commits(query) && !query->commit)
ex_set(e, EX_OPERATION,
"function `%s` requires a commit "
"before it can be used in the `%s` scope"DOC_COMMIT,
name,
ti_query_scope_name(query));
return e->nr;
}

static int fn_call(ti_query_t * query, cleri_node_t * nd, ex_t * e)
{
cleri_node_t * child = nd->children; /* first in argument list */
Expand Down
89 changes: 89 additions & 0 deletions inc/ti/fn/fncommit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <ti/fn/fn.h>

static int do__f_commit(ti_query_t * query, cleri_node_t * nd, ex_t * e)
{
const int nargs = fn_get_nargs(nd);
ti_raw_t * message;
vec_t ** commits = ti_query_commits(query);
ti_commit_t * commit;

if (fn_not_thingsdb_or_collection_scope("commit", query, e) ||
fn_nargs("commit", DOC_COMMIT, 1, nargs, e) ||
ti_do_statement(query, nd->children, e) ||
fn_arg_str("commit", DOC_COMMIT, 1, query->rval, e))
return e->nr;

message = (ti_raw_t *) query->rval;
query->rval = (ti_val_t *) ti_nil_get();

if (!(*commits))
{
ex_set(e, EX_OPERATION,
"commit history is not enabled"
DOC_SET_HISTORY);
goto fail0;
}

switch ((ti_query_with_enum) query->with_tp)
{
case TI_QUERY_WITH_PARSERES:
/* we use parseres, so it must be a query */
break;
case TI_QUERY_WITH_PROCEDURE:
ex_set(e, EX_OPERATION,
"function `commit` is not allowed in a procedure");
goto fail0;
case TI_QUERY_WITH_FUTURE:
ex_set(e, EX_OPERATION,
"function `commit` is not allowed in a future");
goto fail0;
case TI_QUERY_WITH_TASK:
case TI_QUERY_WITH_TASK_FINISH:
ex_set(e, EX_OPERATION,
"function `commit` is not allowed in a task");
goto fail0;
}

if (!query->change)
{
ex_set(e, EX_OPERATION, "commit without a change"DOC_SET_HISTORY);
goto fail0;
}

if (!query->user)
{
ex_set(e, EX_OPERATION, "commit without a user"DOC_SET_HISTORY);
goto fail0;
}

if (!message->n)
{
ex_set(e, EX_VALUE_ERROR, "commit message must not be empty");
goto fail0;
}

if (query->commit)
{
ex_set(e, EX_OPERATION, "commit message already set");
goto fail0;
}

commit = ti_commit_make(
query->change->id,
query->with.parseres->str,
query->user->name,
message);

if (!commit || vec_push(commits, commit))
{
ti_commit_destroy(commit);
ex_set_mem(e);
goto fail0;
}

query->commit = commit;

fail0:
ti_val_unsafe_drop((ti_val_t *) message);
return e->nr;
}
Loading