teptris 0.2.43 → 0.2.44
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/teptris_ext/engine/README.md +1 -1
- data/ext/teptris_ext/engine/src/include/teptris/teptris.h +13 -0
- data/ext/teptris_ext/engine/src/teptris/api.c +53 -0
- data/ext/teptris_ext/materialize.c +350 -0
- data/lib/teptris/toml.rb +61 -0
- data/lib/teptris/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ae43d87fead417c98ec36e817e942d01de144968c560d37dae3be0a896d1583a
|
|
4
|
+
data.tar.gz: 547691ab240104d8471e67552f973fc0758afd8d52303e64d3433f1651ff0af8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9ab5c1f9fd138efc7050457a107ac880d513775322d94255bab6cf9790e15ff44859766c35e9fbfa00fae68e565cea55b279e9201e2f4aa96cd7025ce61d9880
|
|
7
|
+
data.tar.gz: 469fec7e464fbab28c627c1246729b41e95caaea5ad07694305b5aba8d5b995af67744405a0e6c9112db3748568ce0a01e5369016437368bff11e95852903b03
|
|
@@ -91,6 +91,19 @@ TEPTRIS_API teptris_status teptris_parse(const char *data, size_t len,
|
|
|
91
91
|
const teptris_options *opts,
|
|
92
92
|
teptris_document **out);
|
|
93
93
|
|
|
94
|
+
/* Parse N TOML documents in a single call. Caller pre-allocates
|
|
95
|
+
* `out_docs[N]` and `out_statuses[N]` and frees each doc with
|
|
96
|
+
* teptris_document_free. Per-document parse failures are reported
|
|
97
|
+
* through out_statuses[i] (out_docs[i] is set so the caller can read
|
|
98
|
+
* the error via teptris_document_error and free). TEPTRIS_ERR_ALLOC
|
|
99
|
+
* means no documents were created — caller must not free. The `opts`
|
|
100
|
+
* pointer is shared across the batch. n == 0 is TEPTRIS_OK and a
|
|
101
|
+
* no-op. */
|
|
102
|
+
TEPTRIS_API teptris_status teptris_parse_batch(
|
|
103
|
+
const char *const *data, const size_t *lens, size_t n,
|
|
104
|
+
const teptris_options *opts,
|
|
105
|
+
teptris_document **out_docs, teptris_status *out_statuses);
|
|
106
|
+
|
|
94
107
|
/* Memory: frees every allocation reachable from the document. */
|
|
95
108
|
TEPTRIS_API void teptris_document_free(teptris_document *doc);
|
|
96
109
|
|
|
@@ -34,6 +34,59 @@ teptris_status teptris_parse(const char *data, size_t len,
|
|
|
34
34
|
return st;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
teptris_status teptris_parse_batch(const char *const *data, const size_t *lens,
|
|
38
|
+
size_t n, const teptris_options *opts,
|
|
39
|
+
teptris_document **out_docs,
|
|
40
|
+
teptris_status *out_statuses)
|
|
41
|
+
{
|
|
42
|
+
if (n == 0) {
|
|
43
|
+
return TEPTRIS_OK;
|
|
44
|
+
}
|
|
45
|
+
if (data == NULL || lens == NULL || out_docs == NULL ||
|
|
46
|
+
out_statuses == NULL) {
|
|
47
|
+
return TEPTRIS_ERR_ARG;
|
|
48
|
+
}
|
|
49
|
+
/* Zero the per-doc outputs up front: on the alloc-fail path every
|
|
50
|
+
* entry must look like "no doc to free" so callers don't double-free
|
|
51
|
+
* a partial batch. */
|
|
52
|
+
for (size_t i = 0; i < n; i++) {
|
|
53
|
+
out_docs[i] = NULL;
|
|
54
|
+
out_statuses[i] = TEPTRIS_OK;
|
|
55
|
+
}
|
|
56
|
+
uint32_t max_depth =
|
|
57
|
+
(opts != NULL && opts->max_depth != 0) ? opts->max_depth : 512;
|
|
58
|
+
for (size_t i = 0; i < n; i++) {
|
|
59
|
+
teptris_document *doc = calloc(1, sizeof(teptris_document));
|
|
60
|
+
if (doc == NULL) {
|
|
61
|
+
/* Free any documents we already created; statuses for the
|
|
62
|
+
* failed slot onward were zero-initialized, so out_statuses
|
|
63
|
+
* stays consistent. */
|
|
64
|
+
for (size_t j = 0; j < i; j++) {
|
|
65
|
+
teptris_arena_destroy(&out_docs[j]->arena);
|
|
66
|
+
free(out_docs[j]);
|
|
67
|
+
out_docs[j] = NULL;
|
|
68
|
+
}
|
|
69
|
+
return TEPTRIS_ERR_ALLOC;
|
|
70
|
+
}
|
|
71
|
+
teptris_arena_init(&doc->arena);
|
|
72
|
+
doc->max_depth = max_depth;
|
|
73
|
+
doc->err.status = TEPTRIS_OK;
|
|
74
|
+
doc->err.message = "";
|
|
75
|
+
teptris_status st = teptris_parser_run(doc, data[i], lens[i]);
|
|
76
|
+
if (st != TEPTRIS_OK && doc->err.status == TEPTRIS_OK) {
|
|
77
|
+
doc->err.status = st;
|
|
78
|
+
if (st == TEPTRIS_ERR_ALLOC) {
|
|
79
|
+
snprintf(doc->err_msg, sizeof(doc->err_msg),
|
|
80
|
+
"out of memory");
|
|
81
|
+
}
|
|
82
|
+
doc->err.message = doc->err_msg;
|
|
83
|
+
}
|
|
84
|
+
out_docs[i] = doc;
|
|
85
|
+
out_statuses[i] = st;
|
|
86
|
+
}
|
|
87
|
+
return TEPTRIS_OK;
|
|
88
|
+
}
|
|
89
|
+
|
|
37
90
|
void teptris_document_free(teptris_document *doc)
|
|
38
91
|
{
|
|
39
92
|
if (doc == NULL) {
|
|
@@ -590,9 +590,359 @@ static VALUE ext_plan_emit(VALUE self, VALUE rb_plan, VALUE toml)
|
|
|
590
590
|
return out;
|
|
591
591
|
}
|
|
592
592
|
|
|
593
|
+
|
|
594
|
+
/* ---- lazy mode (#79) ----------------------------------------------
|
|
595
|
+
* load_lazy returns a LazyValue wrapping the parsed tree: scalar
|
|
596
|
+
* access materializes scalars only, tables/arrays stay wrapped until
|
|
597
|
+
* walked. The document and the borrowed-input String live in a
|
|
598
|
+
* single owner object every wrapper references, so GC lifetime is
|
|
599
|
+
* one edge deep. Free order is GC-driven via TypedData dfree. */
|
|
600
|
+
|
|
601
|
+
static VALUE cLazyOwner, cLazyValue;
|
|
602
|
+
|
|
603
|
+
typedef struct {
|
|
604
|
+
teptris_document *doc;
|
|
605
|
+
VALUE input; /* the borrowed views' buffer */
|
|
606
|
+
} lazy_owner;
|
|
607
|
+
|
|
608
|
+
typedef struct {
|
|
609
|
+
VALUE owner; /* LazyOwner: keeps doc + input alive */
|
|
610
|
+
const teptris_node *node;
|
|
611
|
+
} lazy_value;
|
|
612
|
+
|
|
613
|
+
static void lazy_owner_free(void *p) {
|
|
614
|
+
lazy_owner *o = (lazy_owner *)p;
|
|
615
|
+
if (o->doc != NULL) teptris_document_free(o->doc);
|
|
616
|
+
xfree(o);
|
|
617
|
+
}
|
|
618
|
+
static size_t lazy_owner_size(const void *p) { return sizeof(lazy_owner); }
|
|
619
|
+
static const rb_data_type_t lazy_owner_type = {
|
|
620
|
+
"TeptrisExt/LazyOwner",
|
|
621
|
+
{NULL, lazy_owner_free, lazy_owner_size, NULL, 0},
|
|
622
|
+
NULL, NULL, RUBY_TYPED_FREE_IMMEDIATELY};
|
|
623
|
+
|
|
624
|
+
static void lazy_value_mark(void *p) {
|
|
625
|
+
lazy_value *v = (lazy_value *)p;
|
|
626
|
+
rb_gc_mark(v->owner);
|
|
627
|
+
}
|
|
628
|
+
static void lazy_value_free(void *p) { xfree(p); }
|
|
629
|
+
static size_t lazy_value_size(const void *p) { return sizeof(lazy_value); }
|
|
630
|
+
static const rb_data_type_t lazy_value_type = {
|
|
631
|
+
"TeptrisExt/LazyValue",
|
|
632
|
+
{lazy_value_mark, lazy_value_free, lazy_value_size, NULL, 0},
|
|
633
|
+
NULL, NULL, RUBY_TYPED_FREE_IMMEDIATELY};
|
|
634
|
+
|
|
635
|
+
static VALUE lazy_wrap(VALUE owner, const teptris_node *n) {
|
|
636
|
+
lazy_value *v;
|
|
637
|
+
VALUE obj = TypedData_Make_Struct(cLazyValue, lazy_value,
|
|
638
|
+
&lazy_value_type, v);
|
|
639
|
+
v->owner = owner;
|
|
640
|
+
v->node = n;
|
|
641
|
+
return obj;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
static lazy_value *lazy_self(VALUE self) {
|
|
645
|
+
lazy_value *v;
|
|
646
|
+
TypedData_Get_Struct(self, lazy_value, &lazy_value_type, v);
|
|
647
|
+
return v;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
static VALUE ext_load_lazy(VALUE self, VALUE toml) {
|
|
651
|
+
StringValue(toml);
|
|
652
|
+
lazy_owner *o;
|
|
653
|
+
VALUE owner = TypedData_Make_Struct(cLazyOwner, lazy_owner,
|
|
654
|
+
&lazy_owner_type, o);
|
|
655
|
+
o->doc = NULL;
|
|
656
|
+
o->input = rb_str_new_frozen(toml); /* borrowed views: own a copy */
|
|
657
|
+
teptris_status st = teptris_parse(RSTRING_PTR(o->input),
|
|
658
|
+
(size_t)RSTRING_LEN(o->input),
|
|
659
|
+
NULL, &o->doc);
|
|
660
|
+
if (st != TEPTRIS_OK) {
|
|
661
|
+
const teptris_error *e = teptris_document_error(o->doc);
|
|
662
|
+
VALUE ex = rb_exc_new(eParseError, e->message,
|
|
663
|
+
(long)strlen(e->message));
|
|
664
|
+
rb_iv_set(ex, "@line", SIZET2NUM(e->line));
|
|
665
|
+
rb_iv_set(ex, "@column", SIZET2NUM(e->column));
|
|
666
|
+
rb_exc_raise(ex); /* owner GC-frees the failed doc */
|
|
667
|
+
}
|
|
668
|
+
return lazy_wrap(owner, teptris_document_root(o->doc));
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
static VALUE lazy_kind(VALUE self) {
|
|
672
|
+
switch (teptris_node_kind(lazy_self(self)->node)) {
|
|
673
|
+
case TEPTRIS_TABLE: return ID2SYM(rb_intern("table"));
|
|
674
|
+
case TEPTRIS_ARRAY: return ID2SYM(rb_intern("array"));
|
|
675
|
+
default: return ID2SYM(rb_intern("scalar"));
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
static VALUE lazy_size(VALUE self) {
|
|
680
|
+
const teptris_node *n = lazy_self(self)->node;
|
|
681
|
+
switch (teptris_node_kind(n)) {
|
|
682
|
+
case TEPTRIS_TABLE: return SIZET2NUM(teptris_node_table_length(n));
|
|
683
|
+
case TEPTRIS_ARRAY: return SIZET2NUM(teptris_node_array_length(n));
|
|
684
|
+
default: return Qnil;
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
/* table: iterate entries; array: iterate items; scalar: raise */
|
|
689
|
+
static VALUE lazy_each(VALUE self) {
|
|
690
|
+
RETURN_SIZED_ENUMERATOR(self, 0, 0, 0);
|
|
691
|
+
const teptris_node *n = lazy_self(self)->node;
|
|
692
|
+
switch (teptris_node_kind(n)) {
|
|
693
|
+
case TEPTRIS_TABLE: {
|
|
694
|
+
size_t len = teptris_node_table_length(n);
|
|
695
|
+
for (size_t i = 0; i < len; i++) {
|
|
696
|
+
teptris_view key;
|
|
697
|
+
const teptris_node *v = teptris_node_table_at(n, i, &key);
|
|
698
|
+
VALUE kv = rb_ary_new_capa(2);
|
|
699
|
+
rb_ary_push(kv, rb_enc_str_new(key.ptr, (long)key.len,
|
|
700
|
+
rb_utf8_encoding()));
|
|
701
|
+
rb_ary_push(kv, lazy_wrap(lazy_self(self)->owner, v));
|
|
702
|
+
rb_yield(kv);
|
|
703
|
+
}
|
|
704
|
+
break;
|
|
705
|
+
}
|
|
706
|
+
case TEPTRIS_ARRAY: {
|
|
707
|
+
size_t len = teptris_node_array_length(n);
|
|
708
|
+
for (size_t i = 0; i < len; i++)
|
|
709
|
+
rb_yield(lazy_wrap(lazy_self(self)->owner,
|
|
710
|
+
teptris_node_array_at(n, i)));
|
|
711
|
+
break;
|
|
712
|
+
}
|
|
713
|
+
default:
|
|
714
|
+
rb_raise(eError, "scalar node has nothing to iterate");
|
|
715
|
+
}
|
|
716
|
+
return self;
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
/* [] is polymorphic by node kind: tables take string keys (hash
|
|
720
|
+
* index), arrays take integer indices (contiguous array) - both O(1)
|
|
721
|
+
* per access, which is the whole point of the lazy mode */
|
|
722
|
+
static VALUE lazy_get(VALUE self, VALUE key) {
|
|
723
|
+
lazy_value *v = lazy_self(self);
|
|
724
|
+
const teptris_node *child = NULL;
|
|
725
|
+
switch (teptris_node_kind(v->node)) {
|
|
726
|
+
case TEPTRIS_TABLE: {
|
|
727
|
+
const char *k;
|
|
728
|
+
long kl;
|
|
729
|
+
if (RB_TYPE_P(key, T_STRING)) {
|
|
730
|
+
k = RSTRING_PTR(key); kl = RSTRING_LEN(key);
|
|
731
|
+
} else {
|
|
732
|
+
key = rb_funcall(key, rb_intern("to_s"), 0);
|
|
733
|
+
k = RSTRING_PTR(key); kl = RSTRING_LEN(key);
|
|
734
|
+
}
|
|
735
|
+
child = teptris_node_table_get(v->node, k, (size_t)kl);
|
|
736
|
+
break;
|
|
737
|
+
}
|
|
738
|
+
case TEPTRIS_ARRAY: {
|
|
739
|
+
long i = NUM2LONG(key);
|
|
740
|
+
if (i < 0) i += (long)teptris_node_array_length(v->node);
|
|
741
|
+
child = teptris_node_array_at(v->node, (size_t)i);
|
|
742
|
+
break;
|
|
743
|
+
}
|
|
744
|
+
default:
|
|
745
|
+
rb_raise(eError, "scalar node is not subscriptable");
|
|
746
|
+
}
|
|
747
|
+
if (child == NULL) return Qnil;
|
|
748
|
+
return lazy_wrap(v->owner, child);
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
static VALUE lazy_dig(int argc, VALUE *argv, VALUE self) {
|
|
752
|
+
VALUE cur = self;
|
|
753
|
+
for (long i = 0; i < argc; i++) {
|
|
754
|
+
VALUE nxt = rb_funcall(cur, rb_intern("[]"), 1, argv[i]);
|
|
755
|
+
if (nxt == Qnil || nxt == Qundef) return Qnil;
|
|
756
|
+
cur = nxt;
|
|
757
|
+
}
|
|
758
|
+
return cur;
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
/* full materialization: the eager path, owned */
|
|
762
|
+
static VALUE lazy_to(VALUE self) {
|
|
763
|
+
lazy_value *v = lazy_self(self);
|
|
764
|
+
return obj_from_node(v->node, 0);
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
/* ---------------------------------------------------------------- batch --
|
|
768
|
+
* Many-small batch path (teptris-ruby#108 ask 3, complements the C
|
|
769
|
+
* `teptris_parse_batch`): accept an Array of TOML strings + an options
|
|
770
|
+
* hash, parse every entry in one C call (one opts struct, one
|
|
771
|
+
* per-doc-status report), and return one Ruby Hash per document. The
|
|
772
|
+
* options bag is parsed ONCE and shared across the batch — per-doc
|
|
773
|
+
* safe_load/datetime_policy work happens in C, not in N Ruby frames.
|
|
774
|
+
* On the alloc-fail path the C call leaves no partial docs; the Ruby
|
|
775
|
+
* caller sees a Teptris::Error. Per-doc parse failures are collected
|
|
776
|
+
* alongside the successes in a parallel array (the lazy variant
|
|
777
|
+
* raises on the first failure so callers can `rescue` without losing
|
|
778
|
+
* the partial batch). */
|
|
779
|
+
|
|
780
|
+
/* Combined scratch layout — one alloc, four arrays packed end-to-end.
|
|
781
|
+
* Splitting into four mallocs showed up as the dominant cost in the
|
|
782
|
+
* per-doc benchmark for tiny docs (4× malloc/free per call, 16–64 KB
|
|
783
|
+
* of scratch per call). One alloc amortizes the heap and the cache. */
|
|
784
|
+
typedef struct {
|
|
785
|
+
const char **data;
|
|
786
|
+
size_t *lens;
|
|
787
|
+
teptris_document **docs;
|
|
788
|
+
teptris_status *statuses;
|
|
789
|
+
} batch_scratch;
|
|
790
|
+
|
|
791
|
+
static void batch_scratch_free(batch_scratch *s) {
|
|
792
|
+
free(s->data);
|
|
793
|
+
free(s);
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
static batch_scratch *batch_scratch_alloc(long n) {
|
|
797
|
+
batch_scratch *s = (batch_scratch *)malloc(sizeof(*s));
|
|
798
|
+
if (s == NULL) return NULL;
|
|
799
|
+
size_t bytes =
|
|
800
|
+
(size_t)n * (sizeof(const char *) + sizeof(size_t) +
|
|
801
|
+
sizeof(teptris_document *) + sizeof(teptris_status));
|
|
802
|
+
void *blob = malloc(bytes);
|
|
803
|
+
if (blob == NULL) {
|
|
804
|
+
free(s);
|
|
805
|
+
return NULL;
|
|
806
|
+
}
|
|
807
|
+
char *p = (char *)blob;
|
|
808
|
+
s->data = (const char **)p;
|
|
809
|
+
p += (size_t)n * sizeof(const char *);
|
|
810
|
+
s->lens = (size_t *)p;
|
|
811
|
+
p += (size_t)n * sizeof(size_t);
|
|
812
|
+
s->docs = (teptris_document **)p;
|
|
813
|
+
p += (size_t)n * sizeof(teptris_document *);
|
|
814
|
+
s->statuses = (teptris_status *)p;
|
|
815
|
+
return s;
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
static unsigned flags_from_opts(VALUE opts) {
|
|
819
|
+
unsigned flags = 0;
|
|
820
|
+
if (NIL_P(opts)) return 0;
|
|
821
|
+
VALUE sd = rb_hash_aref(opts, ID2SYM(rb_intern("string_datetimes")));
|
|
822
|
+
VALUE ft = rb_hash_aref(opts, ID2SYM(rb_intern("forbid_time")));
|
|
823
|
+
VALUE fd = rb_hash_aref(opts, ID2SYM(rb_intern("forbid_date")));
|
|
824
|
+
if (RTEST(sd)) flags |= FMT_STRING_DATES;
|
|
825
|
+
if (RTEST(ft)) flags |= FMT_FORBID_TIME;
|
|
826
|
+
if (RTEST(fd)) flags |= FMT_FORBID_DATE;
|
|
827
|
+
return flags;
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
/* Parse-only batch (returns an Array of Lazy wrappers, one per doc):
|
|
831
|
+
* eager parse, deferred materialization. Same per-doc error semantics
|
|
832
|
+
* as ext_load_lazy. */
|
|
833
|
+
static VALUE ext_load_lazy_batch(VALUE self, VALUE strings) {
|
|
834
|
+
Check_Type(strings, T_ARRAY);
|
|
835
|
+
long n = RARRAY_LEN(strings);
|
|
836
|
+
if (n == 0) return rb_ary_new();
|
|
837
|
+
batch_scratch *s = batch_scratch_alloc(n);
|
|
838
|
+
if (s == NULL) rb_raise(eError, "out of memory batching %ld documents", n);
|
|
839
|
+
for (long i = 0; i < n; i++) {
|
|
840
|
+
VALUE str = rb_ary_entry(strings, i);
|
|
841
|
+
StringValue(str); /* raises TypeError if not coercible */
|
|
842
|
+
s->data[i] = RSTRING_PTR(str);
|
|
843
|
+
s->lens[i] = (size_t)RSTRING_LEN(str);
|
|
844
|
+
}
|
|
845
|
+
teptris_status batch =
|
|
846
|
+
teptris_parse_batch(s->data, s->lens, (size_t)n, NULL,
|
|
847
|
+
s->docs, s->statuses);
|
|
848
|
+
if (batch != TEPTRIS_OK) {
|
|
849
|
+
batch_scratch_free(s);
|
|
850
|
+
rb_raise(eError, "batch parse failed (status %d)", (int)batch);
|
|
851
|
+
}
|
|
852
|
+
long first_fail = -1;
|
|
853
|
+
for (long i = 0; i < n; i++) {
|
|
854
|
+
if (s->statuses[i] != TEPTRIS_OK && first_fail == -1) first_fail = i;
|
|
855
|
+
}
|
|
856
|
+
if (first_fail != -1) {
|
|
857
|
+
const teptris_error *e = teptris_document_error(s->docs[first_fail]);
|
|
858
|
+
VALUE ex =
|
|
859
|
+
rb_exc_new(eParseError, e->message, (long)strlen(e->message));
|
|
860
|
+
rb_iv_set(ex, "@line", SIZET2NUM(e->line));
|
|
861
|
+
rb_iv_set(ex, "@column", SIZET2NUM(e->column));
|
|
862
|
+
for (long i = 0; i < n; i++) teptris_document_free(s->docs[i]);
|
|
863
|
+
batch_scratch_free(s);
|
|
864
|
+
rb_exc_raise(ex);
|
|
865
|
+
}
|
|
866
|
+
VALUE out = rb_ary_new_capa(n);
|
|
867
|
+
for (long i = 0; i < n; i++) {
|
|
868
|
+
lazy_owner *o;
|
|
869
|
+
VALUE owner = TypedData_Make_Struct(cLazyOwner, lazy_owner,
|
|
870
|
+
&lazy_owner_type, o);
|
|
871
|
+
o->doc = s->docs[i];
|
|
872
|
+
o->input = rb_ary_entry(strings, i);
|
|
873
|
+
rb_ary_push(out, lazy_wrap(owner, teptris_document_root(s->docs[i])));
|
|
874
|
+
}
|
|
875
|
+
batch_scratch_free(s);
|
|
876
|
+
return out;
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
/* Eager batch: returns Array of Hashes with the same datetime contract
|
|
880
|
+
* as ext_load. opts is parsed once; safe_load semantics apply uniformly.
|
|
881
|
+
* The caller's input Array is read-only; its strings stay referenced
|
|
882
|
+
* for the duration of the C call (no separate keeper Array). */
|
|
883
|
+
static VALUE ext_load_batch(int argc, VALUE *argv, VALUE self) {
|
|
884
|
+
VALUE strings, opts;
|
|
885
|
+
rb_scan_args(argc, argv, "11", &strings, &opts);
|
|
886
|
+
Check_Type(strings, T_ARRAY);
|
|
887
|
+
long n = RARRAY_LEN(strings);
|
|
888
|
+
if (n == 0) return rb_ary_new();
|
|
889
|
+
unsigned flags = flags_from_opts(opts);
|
|
890
|
+
batch_scratch *s = batch_scratch_alloc(n);
|
|
891
|
+
if (s == NULL) rb_raise(eError, "out of memory batching %ld documents", n);
|
|
892
|
+
for (long i = 0; i < n; i++) {
|
|
893
|
+
VALUE str = rb_ary_entry(strings, i);
|
|
894
|
+
StringValue(str);
|
|
895
|
+
s->data[i] = RSTRING_PTR(str);
|
|
896
|
+
s->lens[i] = (size_t)RSTRING_LEN(str);
|
|
897
|
+
}
|
|
898
|
+
teptris_status batch =
|
|
899
|
+
teptris_parse_batch(s->data, s->lens, (size_t)n, NULL,
|
|
900
|
+
s->docs, s->statuses);
|
|
901
|
+
if (batch != TEPTRIS_OK) {
|
|
902
|
+
batch_scratch_free(s);
|
|
903
|
+
rb_raise(eError, "batch parse failed (status %d)", (int)batch);
|
|
904
|
+
}
|
|
905
|
+
long first_fail = -1;
|
|
906
|
+
for (long i = 0; i < n; i++) {
|
|
907
|
+
if (s->statuses[i] != TEPTRIS_OK && first_fail == -1) first_fail = i;
|
|
908
|
+
}
|
|
909
|
+
if (first_fail != -1) {
|
|
910
|
+
const teptris_error *e = teptris_document_error(s->docs[first_fail]);
|
|
911
|
+
VALUE ex =
|
|
912
|
+
rb_exc_new(eParseError, e->message, (long)strlen(e->message));
|
|
913
|
+
rb_iv_set(ex, "@line", SIZET2NUM(e->line));
|
|
914
|
+
rb_iv_set(ex, "@column", SIZET2NUM(e->column));
|
|
915
|
+
for (long i = 0; i < n; i++) teptris_document_free(s->docs[i]);
|
|
916
|
+
batch_scratch_free(s);
|
|
917
|
+
rb_exc_raise(ex);
|
|
918
|
+
}
|
|
919
|
+
VALUE out = rb_ary_new_capa(n);
|
|
920
|
+
for (long i = 0; i < n; i++) {
|
|
921
|
+
const teptris_node *root = teptris_document_root(s->docs[i]);
|
|
922
|
+
rb_ary_store(out, i, obj_from_node(root, flags));
|
|
923
|
+
teptris_document_free(s->docs[i]);
|
|
924
|
+
}
|
|
925
|
+
batch_scratch_free(s);
|
|
926
|
+
return out;
|
|
927
|
+
}
|
|
928
|
+
|
|
593
929
|
void Init_teptris_ext(void) {
|
|
594
930
|
VALUE m = rb_define_module("TeptrisExt");
|
|
595
931
|
rb_define_module_function(m, "load", ext_load, -1);
|
|
932
|
+
rb_define_module_function(m, "load_lazy", ext_load_lazy, 1);
|
|
933
|
+
rb_define_module_function(m, "load_batch", ext_load_batch, -1);
|
|
934
|
+
rb_define_module_function(m, "load_lazy_batch", ext_load_lazy_batch, 1);
|
|
935
|
+
cLazyOwner = rb_define_class_under(m, "LazyOwner", rb_cObject);
|
|
936
|
+
cLazyValue = rb_define_class_under(m, "LazyValue", rb_cObject);
|
|
937
|
+
rb_define_method(cLazyValue, "kind", lazy_kind, 0);
|
|
938
|
+
rb_define_method(cLazyValue, "size", lazy_size, 0);
|
|
939
|
+
rb_define_method(cLazyValue, "each", lazy_each, 0);
|
|
940
|
+
rb_define_method(cLazyValue, "[]", lazy_get, 1);
|
|
941
|
+
rb_define_method(cLazyValue, "at", lazy_get, 1);
|
|
942
|
+
rb_define_method(cLazyValue, "dig", lazy_dig, -1);
|
|
943
|
+
rb_define_method(cLazyValue, "to_h", lazy_to, 0);
|
|
944
|
+
rb_define_method(cLazyValue, "to_a", lazy_to, 0);
|
|
945
|
+
rb_define_method(cLazyValue, "value", lazy_to, 0);
|
|
596
946
|
rb_define_module_function(m, "dump", ext_dump, 1);
|
|
597
947
|
rb_define_module_function(m, "engine_version", ext_version, 0);
|
|
598
948
|
rb_define_module_function(m, "plan_build", ext_plan_build, 2);
|
data/lib/teptris/toml.rb
CHANGED
|
@@ -36,6 +36,42 @@ module Teptris
|
|
|
36
36
|
TeptrisExt.load(toml, opts)
|
|
37
37
|
end
|
|
38
38
|
|
|
39
|
+
# Lazy load: returns a wrapper that parses eagerly (one pass)
|
|
40
|
+
# but materializes host objects only along the paths actually
|
|
41
|
+
# accessed. Same datetime contract as load; to_h/to_a flatten to
|
|
42
|
+
# the eager shape.
|
|
43
|
+
def load_lazy(toml)
|
|
44
|
+
raise ArgumentError, "input must be a String" unless toml.is_a?(String)
|
|
45
|
+
|
|
46
|
+
TeptrisExt.load_lazy(toml)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Many-small batch path (teptris-ruby#108 ask 3): parse N inputs
|
|
50
|
+
# in one C call, share the safe_load/datetime_policy options
|
|
51
|
+
# struct across the batch, return one eager Hash per document.
|
|
52
|
+
# Same datetime contract as load. A failing document raises
|
|
53
|
+
# Teptris::ParseError carrying the first failure's line/column.
|
|
54
|
+
def load_batch(tomls, safe_load: nil, datetime_policy: :native)
|
|
55
|
+
_validate_batch!(tomls, safe_load, datetime_policy)
|
|
56
|
+
TeptrisExt.load_batch(tomls, _safe_load_opts(safe_load, datetime_policy))
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Lazy twin of load_batch: parse N inputs eagerly, hand back an
|
|
60
|
+
# Array of Lazy wrappers — materialization happens only on access.
|
|
61
|
+
# Same per-doc error semantics as load_lazy (first failure raises).
|
|
62
|
+
def load_lazy_batch(tomls)
|
|
63
|
+
_validate_batch!(tomls, nil, :native)
|
|
64
|
+
TeptrisExt.load_lazy_batch(tomls)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Many-small path with file paths: read each file, then call
|
|
68
|
+
# load_batch. Symmetric with load_file's single-doc shape.
|
|
69
|
+
def load_files(paths, safe_load: nil, datetime_policy: :native)
|
|
70
|
+
_validate_batch!(paths, safe_load, datetime_policy, kind: :paths)
|
|
71
|
+
load_batch(paths.map { |p| File.read(p) },
|
|
72
|
+
safe_load: safe_load, datetime_policy: datetime_policy)
|
|
73
|
+
end
|
|
74
|
+
|
|
39
75
|
# One-pass schema materialization over a compiled
|
|
40
76
|
# Teptris::Descriptor: unplanned keys are never materialized
|
|
41
77
|
# (teptris#46).
|
|
@@ -61,6 +97,31 @@ module Teptris
|
|
|
61
97
|
def dump(obj)
|
|
62
98
|
TeptrisExt.dump(obj)
|
|
63
99
|
end
|
|
100
|
+
|
|
101
|
+
private
|
|
102
|
+
|
|
103
|
+
def _validate_batch!(input, safe_load, datetime_policy, kind: :strings)
|
|
104
|
+
unless input.is_a?(Array)
|
|
105
|
+
raise ArgumentError, "expected Array of #{kind}, got #{input.class}"
|
|
106
|
+
end
|
|
107
|
+
if !safe_load.nil? && !safe_load.is_a?(Array) && !safe_load.is_a?(Symbol)
|
|
108
|
+
raise ArgumentError, "safe_load must be nil, an Array, or a Symbol"
|
|
109
|
+
end
|
|
110
|
+
unless %i[native string].include?(datetime_policy)
|
|
111
|
+
raise ArgumentError, "datetime_policy must be :native or :string"
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def _safe_load_opts(safe_load, datetime_policy)
|
|
116
|
+
opts = {}
|
|
117
|
+
opts[:string_datetimes] = true if datetime_policy == :string
|
|
118
|
+
if safe_load && datetime_policy == :native
|
|
119
|
+
permitted = Array(safe_load) + [:__scalar]
|
|
120
|
+
opts[:forbid_time] = true unless permitted.include?(Time)
|
|
121
|
+
opts[:forbid_date] = true unless permitted.include?(Date)
|
|
122
|
+
end
|
|
123
|
+
opts.empty? ? nil : opts
|
|
124
|
+
end
|
|
64
125
|
end
|
|
65
126
|
end
|
|
66
127
|
end
|
data/lib/teptris/version.rb
CHANGED