buildlib/Lucy/Build/Binding/Document.pm

filter: Statementstmt 100% 38 / 38 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 3 totaltotal 93.4% 43 / 46 CC 2 SCAR 2.9
CC
2
Cov
100%
bind_doc
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.3
« blib/lib/LucyX/Index/LongFieldSim.pm ↑ Summary buildlib/Lucy/Build/Binding/Misc.pm »
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 package Lucy::Build::Binding::Document;
16 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 our $VERSION = '0.008000';
20 1 $VERSION = eval $VERSION;
21  
22 1 ▶sub bind_all {
Subroutine bind_all: called CC=1 SCAR=0.0
Pod undocumented
23 1 my $class = shift;
24 1 $class->bind_doc;
25 1 $class->bind_hitdoc;
26 }
27  
28 1 ▶sub bind_doc {
Subroutine bind_doc: called CC=2 SCAR=6.9
Pod undocumented
29 1 my @hand_rolled = qw( Store Set_Fields Get_Fields );
30  
31 1 my $pod_spec = Clownfish::CFC::Binding::Perl::Pod->new;
32 1 my $synopsis = <<'END_SYNOPSIS';
33 my $doc = Lucy::Document::Doc->new(
34 fields => { foo => 'foo foo', bar => 'bar bar' },
35 );
36 $indexer->add_doc($doc);
37  
38 Doc objects allow access to field values via hashref overloading:
39  
40 $doc->{foo} = 'new value for field "foo"';
41 print "foo: $doc->{foo}\n";
42 END_SYNOPSIS
43 1 my $constructor = <<'END_CONSTRUCTOR';
44 my $doc = Lucy::Document::Doc->new(
45 fields => { foo => 'foo foo', bar => 'bar bar' },
46 );
47 END_CONSTRUCTOR
48 1 my $store_pod = <<'END_POD';
49 =head2 store
50  
51 $doc->store($field, $value);
52  
53 Store a field value in the Doc.
54  
55 =over
56  
57 =item *
58  
59 B<field> - The field name.
60  
61 =item *
62  
63 B<value> - The value.
64  
65 =back
66 END_POD
67 1 my $get_fields_pod = <<'END_POD';
68 =head2 get_fields
69  
70 my $hashref = $doc->get_fields();
71  
72 Return the Doc's backing fields hash.
73 END_POD
74 1 $pod_spec->set_synopsis($synopsis);
75 1 $pod_spec->add_constructor( alias => 'new', sample => $constructor );
76 1 $pod_spec->add_method(
77 method => 'Store',
78 alias => 'store',
79 pod => $store_pod,
80 );
81 1 $pod_spec->add_method(
82 method => 'Get_Fields',
83 alias => 'get_fields',
84 pod => $get_fields_pod,
85 );
86  
87 1 my $xs_code = <<'END_XS_CODE';
88 MODULE = Lucy PACKAGE = Lucy::Document::Doc
89  
90 SV*
91 new(either_sv, ...)
92 SV *either_sv;
93 CODE:
94 {
95 static const XSBind_ParamSpec param_specs[2] = {
96 XSBIND_PARAM("fields", false),
97 XSBIND_PARAM("doc_id", false)
98 };
99 int32_t locations[2];
100 SV* fields_sv = NULL;
101 HV *fields = NULL;
102 int32_t doc_id = 0;
103 lucy_Doc *self = NULL;
104  
105 XSBind_locate_args(aTHX_ &ST(0), 1, items, param_specs, locations, 2);
106  
107 fields_sv = locations[0] < items ? ST(locations[0]) : NULL;
108 doc_id = locations[1] < items ? (int32_t)SvIV(ST(locations[1])) : 0;
109  
110 if (fields_sv && XSBind_sv_defined(aTHX_ fields_sv)) {
111 if (SvROK(fields_sv)) {
112 fields = (HV*)SvRV(fields_sv);
113 }
114 if (!fields || SvTYPE((SV*)fields) != SVt_PVHV) {
115 CFISH_THROW(CFISH_ERR, "fields is not a hashref");
116 }
117 }
118  
119 self = (lucy_Doc*)XSBind_new_blank_obj(aTHX_ either_sv);
120 lucy_Doc_init(self, fields, doc_id);
121 RETVAL = CFISH_OBJ_TO_SV_NOINC(self);
122 }
123 OUTPUT: RETVAL
124  
125 void
126 store(self, field, value_sv)
127 lucy_Doc *self;
128 cfish_String *field;
129 SV *value_sv;
130 PPCODE:
131 {
132 cfish_Obj *value
133 = (cfish_Obj*)XSBind_perl_to_cfish(aTHX_ value_sv, CFISH_OBJ);
134 LUCY_Doc_Store(self, field, value);
135 CFISH_DECREF(value);
136 }
137  
138 SV*
139 get_fields(self, ...)
140 lucy_Doc *self;
141 CODE:
142 CFISH_UNUSED_VAR(items);
143 RETVAL = newRV_inc((SV*)LUCY_Doc_Get_Fields(self));
144 OUTPUT: RETVAL
145  
146 void
147 set_fields(self, fields)
148 lucy_Doc *self;
149 HV *fields;
150 PPCODE:
151 LUCY_Doc_Set_Fields_IMP(self, fields);
152 END_XS_CODE
153  
154 1 my $binding = Clownfish::CFC::Binding::Perl::Class->new(
155 parcel => "Lucy",
156 class_name => "Lucy::Document::Doc",
157 );
158 1 $binding->append_xs($xs_code);
159 1 $binding->exclude_method($_) for @hand_rolled;
160 1 $binding->exclude_constructor;
161 1 $binding->set_pod_spec($pod_spec);
162  
163 1 Clownfish::CFC::Binding::Perl::Class->register($binding);
164 }
165  
166 1 ▶sub bind_hitdoc {
Subroutine bind_hitdoc: called CC=1 SCAR=0.0
Pod undocumented
167 1 my $pod_spec = Clownfish::CFC::Binding::Perl::Pod->new;
168 1 my $synopsis = <<'END_SYNOPSIS';
169 while ( my $hit_doc = $hits->next ) {
170 print "$hit_doc->{title}\n";
171 print $hit_doc->get_score . "\n";
172 ...
173 }
174 END_SYNOPSIS
175 1 $pod_spec->set_synopsis($synopsis);
176  
177 1 my $xs_code = <<'END_XS_CODE';
178 MODULE = Lucy PACKAGE = Lucy::Document::HitDoc
179  
180 SV*
181 new(either_sv, ...)
182 SV *either_sv;
183 CODE:
184 {
185 static const XSBind_ParamSpec param_specs[3] = {
186 XSBIND_PARAM("fields", false),
187 XSBIND_PARAM("doc_id", false),
188 XSBIND_PARAM("score", false)
189 };
190 int32_t locations[3];
191 SV *fields_sv = NULL;
192 HV *fields = NULL;
193 int32_t doc_id = 0;
194 float score = 0.0f;
195 lucy_HitDoc *self = NULL;
196  
197 XSBind_locate_args(aTHX_ &ST(0), 1, items, param_specs, locations, 3);
198  
199 fields_sv = locations[0] < items ? ST(locations[0]) : NULL;
200 doc_id = locations[1] < items ? (int32_t)SvIV(ST(locations[1])) : 0;
201 score = locations[2] < items ? (float)SvNV(ST(locations[2])) : 0.0f;
202  
203 if (fields_sv && XSBind_sv_defined(aTHX_ fields_sv)) {
204 if (SvROK(fields_sv)) {
205 fields = (HV*)SvRV(fields_sv);
206 }
207 if (!fields || SvTYPE((SV*)fields) != SVt_PVHV) {
208 CFISH_THROW(CFISH_ERR, "fields is not a hashref");
209 }
210 }
211  
212 self = (lucy_HitDoc*)XSBind_new_blank_obj(aTHX_ either_sv);
213 lucy_HitDoc_init(self, fields, doc_id, score);
214 RETVAL = CFISH_OBJ_TO_SV_NOINC(self);
215 }
216 OUTPUT: RETVAL
217 END_XS_CODE
218  
219 1 my $binding = Clownfish::CFC::Binding::Perl::Class->new(
220 parcel => "Lucy",
221 class_name => "Lucy::Document::HitDoc",
222 );
223 1 $binding->append_xs($xs_code);
224 1 $binding->set_pod_spec($pod_spec);
225 1 $binding->exclude_constructor;
226  
227 1 Clownfish::CFC::Binding::Perl::Class->register($binding);
228 }
229  
230 1 1;
« blib/lib/LucyX/Index/LongFieldSim.pm ↑ Summary buildlib/Lucy/Build/Binding/Misc.pm »