Skip to content

Commit aa69a66

Browse files
authored
Adds commit history feature and fixes to_type on non stored things (#425)
* Adds commit history and fixed to_type issue.
1 parent 8d9f6c7 commit aa69a66

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+2498
-216
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# v1.8.0-alpha6
1+
# v1.8.0-alpha9
22

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

1319
# v1.7.6
1420

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ set(SOURCES
137137
src/ti/closure.c
138138
src/ti/collection.c
139139
src/ti/collections.c
140+
src/ti/commit.c
141+
src/ti/commits.c
140142
src/ti/condition.c
141143
src/ti/connect.c
142144
src/ti/counters.c
@@ -238,11 +240,12 @@ set(SOURCES
238240
src/ti/store/storeaccess.c
239241
src/ti/store/storecollection.c
240242
src/ti/store/storecollections.c
243+
src/ti/store/storecommits.c
241244
src/ti/store/storeenums.c
242245
src/ti/store/storegcollect.c
243246
src/ti/store/storemodules.c
244-
src/ti/store/storenames.c
245247
src/ti/store/storenamedrooms.c
248+
src/ti/store/storenames.c
246249
src/ti/store/storeprocedures.c
247250
src/ti/store/storestatus.c
248251
src/ti/store/storetasks.c

inc/doc.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020
#define DOC_BYTES DOC_SEE("collection-api/bytes")
2121
#define DOC_CHANGE_ID DOC_SEE("collection-api/change_id")
2222
#define DOC_CLOSURE DOC_SEE("collection-api/closure")
23+
#define DOC_COMMIT DOC_SEE("collection-api/commit")
2324
#define DOC_DATETIME DOC_SEE("collection-api/datetime")
2425
#define DOC_DEEP DOC_SEE("collection-api/deep")
2526
#define DOC_DEL_ENUM DOC_SEE("collection-api/del_enum")
2627
#define DOC_DEL_TYPE DOC_SEE("collection-api/del_type")
2728
#define DOC_ENUM DOC_SEE("collection-api/enum")
29+
#define DOC_ENUMS_INFO DOC_SEE("collection-api/enums_info")
2830
#define DOC_ENUM_INFO DOC_SEE("collection-api/enum_info")
2931
#define DOC_ENUM_MAP DOC_SEE("collection-api/enum_map")
30-
#define DOC_ENUMS_INFO DOC_SEE("collection-api/enums_info")
3132
#define DOC_ERR DOC_SEE("collection-api/err")
3233
#define DOC_EXPORT DOC_SEE("collection-api/export")
3334
#define DOC_FLOAT DOC_SEE("collection-api/float")
@@ -153,6 +154,7 @@
153154
#define DOC_COLLECTIONS_INFO DOC_SEE("thingsdb-api/collections_info")
154155
#define DOC_DEL_COLLECTION DOC_SEE("thingsdb-api/del_collection")
155156
#define DOC_DEL_EXPIRED DOC_SEE("thingsdb-api/del_expired")
157+
#define DOC_DEL_HISTORY DOC_SEE("thingsdb-api/del_history")
156158
#define DOC_DEL_MODULE DOC_SEE("thingsdb-api/del_module")
157159
#define DOC_DEL_NODE DOC_SEE("thingsdb-api/del_node")
158160
#define DOC_DEL_TOKEN DOC_SEE("thingsdb-api/del_token")
@@ -164,6 +166,7 @@
164166
#define DOC_HAS_NODE DOC_SEE("thingsdb-api/has_node")
165167
#define DOC_HAS_TOKEN DOC_SEE("thingsdb-api/has_token")
166168
#define DOC_HAS_USER DOC_SEE("thingsdb-api/has_user")
169+
#define DOC_HISTORY DOC_SEE("thingsdb-api/history")
167170
#define DOC_MODULE_INFO DOC_SEE("thingsdb-api/module_info")
168171
#define DOC_MODULES_INFO DOC_SEE("thingsdb-api/modules_info")
169172
#define DOC_NEW_COLLECTION DOC_SEE("thingsdb-api/new_collection")
@@ -178,6 +181,7 @@
178181
#define DOC_RESTORE DOC_SEE("thingsdb-api/restore")
179182
#define DOC_REVOKE DOC_SEE("thingsdb-api/revoke")
180183
#define DOC_SET_DEFAULT_DEEP DOC_SEE("thingsdb-api/set_default_deep")
184+
#define DOC_SET_HISTORY DOC_SEE("thingsdb-api/set_history")
181185
#define DOC_SET_MODULE_CONF DOC_SEE("thingsdb-api/set_module_conf")
182186
#define DOC_SET_MODULE_SCOPE DOC_SEE("thingsdb-api/set_module_scope")
183187
#define DOC_SET_PASSWORD DOC_SEE("thingsdb-api/set_password")

inc/ti.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ struct ti_s
107107
vec_t * users; /* ti_user_t */
108108
vec_t * access_node; /* ti_auth_t */
109109
vec_t * access_thingsdb; /* ti_auth_t */
110+
vec_t * commits; /* ti_commit_t */
110111
smap_t * procedures; /* ti_procedure_t */
111112
smap_t * names; /* weak map for ti_name_t */
112113
smap_t * qcache; /* pointer to cache in stack */

inc/ti/collection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ ti_collection_t * ti_collection_create(
2525
uint8_t deep);
2626
void ti_collection_destroy(ti_collection_t * collection);
2727
void ti_collection_drop(ti_collection_t * collection);
28+
int ti_collection_to_pk(ti_collection_t * collection, msgpack_packer * pk);
2829
_Bool ti_collection_name_check(const char * name, size_t n, ex_t * e);
2930
int ti_collection_rename(
3031
ti_collection_t * collection,

inc/ti/collection.inline.h

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,6 @@
88
#include <ti/collection.h>
99
#include <ti/thing.h>
1010

11-
static inline int ti_collection_to_pk(
12-
ti_collection_t * collection,
13-
msgpack_packer * pk)
14-
{
15-
return -(
16-
msgpack_pack_map(pk, 7) ||
17-
18-
mp_pack_str(pk, "collection_id") ||
19-
msgpack_pack_uint64(pk, collection->id) ||
20-
21-
mp_pack_str(pk, "name") ||
22-
mp_pack_strn(pk, collection->name->data, collection->name->n) ||
23-
24-
mp_pack_str(pk, "created_at") ||
25-
msgpack_pack_uint64(pk, collection->created_at) ||
26-
27-
mp_pack_str(pk, "things") ||
28-
msgpack_pack_uint64(pk, collection->things->n + collection->gc->n) ||
29-
30-
mp_pack_str(pk, "time_zone") ||
31-
mp_pack_strn(pk, collection->tz->name, collection->tz->n) ||
32-
33-
mp_pack_str(pk, "default_deep") ||
34-
msgpack_pack_uint64(pk, collection->deep) ||
35-
36-
mp_pack_str(pk, "next_free_id") ||
37-
msgpack_pack_uint64(pk, collection->next_free_id)
38-
);
39-
}
40-
4111
/*
4212
* Return a thing with a borrowed reference from the collection, or,
4313
* if not found, tries to restore the thing from the garbage collector.

inc/ti/collection.t.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ typedef struct ti_collection_s ti_collection_t;
88

99
#include <uv.h>
1010
#include <inttypes.h>
11-
#include <ti/raw.t.h>
1211
#include <ti/enums.t.h>
12+
#include <ti/commit.h>
13+
#include <ti/raw.t.h>
1314
#include <ti/thing.t.h>
14-
#include <ti/tz.h>
1515
#include <ti/types.t.h>
16+
#include <ti/tz.h>
1617
#include <util/guid.h>
1718
#include <util/imap.h>
1819
#include <util/queue.h>
@@ -39,6 +40,7 @@ struct ti_collection_s
3940
uv_mutex_t * lock; /* only for watch/ unwatch/ away-mode */
4041
vec_t * futures; /* no reference, type: ti_future_t */
4142
vec_t * vtasks; /* tasks, type: ti_vtask_t */
43+
vec_t * commits; /* migration changes ti_commit_t */
4244
guid_t guid; /* derived from collection->id */
4345
};
4446

inc/ti/commit.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* ti/commit.h
3+
*/
4+
#ifndef TI_COMMIT_H_
5+
#define TI_COMMIT_H_
6+
7+
#include <stdlib.h>
8+
#include <stdint.h>
9+
#include <time.h>
10+
#include <ti/raw.t.h>
11+
#include <ti/val.t.h>
12+
#include <util/mpack.h>
13+
#include <util/vec.h>
14+
15+
typedef struct ti_commit_s ti_commit_t;
16+
17+
ti_commit_t * ti_commit_from_up(mp_unp_t * up);
18+
ti_commit_t * ti_commit_make(
19+
uint64_t id,
20+
const char * code,
21+
ti_raw_t * by,
22+
ti_raw_t * message);
23+
void ti_commit_destroy(ti_commit_t * commit);
24+
int ti_commit_to_pk(ti_commit_t * commit, msgpack_packer * pk);
25+
int ti_commit_to_client_pk(
26+
ti_commit_t * commit,
27+
_Bool detail,
28+
msgpack_packer * pk);
29+
ti_val_t * ti_commit_as_mpval(ti_commit_t * commit, _Bool detail);
30+
31+
struct ti_commit_s
32+
{
33+
uint64_t id; /* equal to the change id */
34+
time_t ts; /* timestamp of the change */
35+
ti_raw_t * code; /* original query string */
36+
ti_raw_t * message; /* never NULL */
37+
ti_raw_t * by; /* never NULL */
38+
ti_raw_t * err_msg; /* may be NULL */
39+
};
40+
41+
#endif /* TI_COMMIT_H_ */

inc/ti/commits.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* ti/commits.h
3+
*/
4+
#ifndef TI_COMMITS_H_
5+
#define TI_COMMITS_H_
6+
7+
#include <ti/auth.h>
8+
#include <ti/datetime.h>
9+
#include <ti/raw.t.h>
10+
#include <ti/regex.t.h>
11+
#include <ti/thing.t.h>
12+
#include <ti/varr.t.h>
13+
#include <ti/vbool.h>
14+
#include <ti/vint.h>
15+
#include <util/vec.h>
16+
#include <ex.h>
17+
18+
#define TI_COMMITS_MASK TI_AUTH_QUERY|TI_AUTH_CHANGE
19+
20+
typedef struct
21+
{
22+
ti_raw_t * scope;
23+
ti_raw_t * contains;
24+
ti_regex_t * match;
25+
ti_vint_t * id;
26+
ti_vint_t * last;
27+
ti_vint_t * first;
28+
ti_datetime_t * before;
29+
ti_datetime_t * after;
30+
ti_vbool_t * has_err;
31+
ti_vbool_t * detail;
32+
} ti_commits_options_t;
33+
34+
typedef struct
35+
{
36+
vec_t ** commits;
37+
vec_t ** access;
38+
uint64_t scope_id;
39+
} ti_commits_history_t;
40+
41+
void ti_commits_destroy(vec_t ** commits);
42+
int ti_commits_options(
43+
ti_commits_options_t * options,
44+
ti_thing_t * thing,
45+
_Bool allow_detail,
46+
ex_t * e);
47+
int ti_commits_history(
48+
ti_commits_history_t * history,
49+
ti_commits_options_t * options,
50+
ex_t * e);
51+
vec_t ** ti_commits_from_scope(ti_raw_t * scope, ex_t * e);
52+
int ti_commits_set_history(vec_t ** commits, _Bool state);
53+
vec_t * ti_commits_find(vec_t * commits, ti_commits_options_t * options);
54+
vec_t * ti_commits_del(vec_t ** commits, ti_commits_options_t * options);
55+
56+
#endif /* TI_COMMITS_H_ */

inc/ti/fn/fn.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#include <ti/member.h>
3636
#include <ti/member.inline.h>
3737
#include <ti/method.h>
38+
#include <ti/commit.h>
39+
#include <ti/commits.h>
3840
#include <ti/mod/expose.h>
3941
#include <ti/mod/expose.t.h>
4042
#include <ti/mod/github.h>
@@ -474,6 +476,17 @@ static inline int fn_not_thingsdb_or_collection_scope(
474476
return e->nr;
475477
}
476478

479+
static inline int fn_commit(const char * name, ti_query_t * query, ex_t * e)
480+
{
481+
if (*ti_query_commits(query) && !query->commit)
482+
ex_set(e, EX_OPERATION,
483+
"function `%s` requires a commit "
484+
"before it can be used in the `%s` scope"DOC_COMMIT,
485+
name,
486+
ti_query_scope_name(query));
487+
return e->nr;
488+
}
489+
477490
static int fn_call(ti_query_t * query, cleri_node_t * nd, ex_t * e)
478491
{
479492
cleri_node_t * child = nd->children; /* first in argument list */

0 commit comments

Comments
 (0)