buildlib/Lucy/Build/Binding/Misc.pm

filter: Statementstmt 100% 35 / 35 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 4 totaltotal 91.1% 41 / 45 CC 2 SCAR 2.5
CC
2
Cov
100%
bind_simple
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.3
« buildlib/Lucy/Build/Binding/Document.pm ↑ Summary buildlib/Lucy/Build/Binding/Index.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::Misc;
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, $hierarchy) = @_;
24  
25 1 $class->bind_lucy;
26 1 $class->bind_simple;
27 1 $class->bind_test;
28 }
29  
30 1 ▶sub bind_lucy {
Subroutine bind_lucy: called CC=1 SCAR=0.0
Pod undocumented
31 1 my $xs_code = <<'END_XS_CODE';
32 MODULE = Lucy PACKAGE = Lucy
33  
34 IV
35 _dummy_function()
36 CODE:
37 RETVAL = 1;
38 OUTPUT:
39 RETVAL
40  
41 void
42 STORABLE_freeze(self, ...)
43 cfish_Obj *self;
44 PPCODE:
45 {
46 CFISH_UNUSED_VAR(self);
47 if (items < 2 || !SvTRUE(ST(1))) {
48 SV *retval;
49 cfish_ByteBuf *serialized_bb;
50 lucy_RAMFileHandle *file_handle
51 = lucy_RAMFH_open(NULL, LUCY_FH_WRITE_ONLY | LUCY_FH_CREATE, NULL);
52 lucy_OutStream *target = lucy_OutStream_open((cfish_Obj*)file_handle);
53  
54 lucy_Freezer_serialize(self, target);
55  
56 LUCY_OutStream_Close(target);
57 serialized_bb
58 = LUCY_RAMFile_Get_Contents(LUCY_RAMFH_Get_File(file_handle));
59 retval = (SV*)CFISH_BB_To_Host(serialized_bb, NULL);
60 CFISH_DECREF(file_handle);
61 CFISH_DECREF(target);
62  
63 if (SvCUR(retval) == 0) { // Thwart Storable bug
64 THROW(CFISH_ERR, "Calling serialize produced an empty string");
65 }
66 ST(0) = sv_2mortal(retval);
67 XSRETURN(1);
68 }
69 }
70  
71 =begin comment
72  
73 Calls deserialize(), and copies the object pointer. Since deserialize is an
74 abstract method, it will confess() unless implemented.
75  
76 =end comment
77  
78 =cut
79  
80 void
81 STORABLE_thaw(blank_obj, cloning, serialized_sv)
82 SV *blank_obj;
83 SV *cloning;
84 SV *serialized_sv;
85 PPCODE:
86 {
87 char *class_name = HvNAME(SvSTASH(SvRV(blank_obj)));
88 cfish_String *class_name_str = CFISH_SSTR_WRAP_C(class_name);
89 cfish_Class *klass = cfish_Class_singleton(class_name_str, NULL);
90 STRLEN len;
91 char *ptr = SvPV(serialized_sv, len);
92 cfish_ByteBuf *contents = cfish_BB_new_bytes(ptr, len);
93 lucy_RAMFile *ram_file = lucy_RAMFile_new(contents, true);
94 lucy_RAMFileHandle *file_handle
95 = lucy_RAMFH_open(NULL, LUCY_FH_READ_ONLY, ram_file);
96 lucy_InStream *instream = lucy_InStream_open((cfish_Obj*)file_handle);
97 cfish_Obj *self = XSBind_foster_obj(aTHX_ blank_obj, klass);
98 cfish_Obj *deserialized = lucy_Freezer_deserialize(self, instream);
99  
100 CFISH_UNUSED_VAR(cloning);
101 CFISH_DECREF(contents);
102 CFISH_DECREF(ram_file);
103 CFISH_DECREF(file_handle);
104 CFISH_DECREF(instream);
105  
106 // Catch bad deserialize() override.
107 if (deserialized != self) {
108 THROW(CFISH_ERR, "Error when deserializing obj of class %o", klass);
109 }
110 }
111 END_XS_CODE
112  
113 1 my $binding = Clownfish::CFC::Binding::Perl::Class->new(
114 parcel => "Lucy",
115 class_name => "Lucy",
116 );
117 1 $binding->append_xs($xs_code);
118  
119 1 Clownfish::CFC::Binding::Perl::Class->register($binding);
120 }
121  
122 1 ▶sub bind_simple {
Subroutine bind_simple: called CC=2 SCAR=6.9
Pod undocumented
123 1 my @hand_rolled = qw( Add_Doc );
124  
125 1 my $pod_spec = Clownfish::CFC::Binding::Perl::Pod->new;
126 1 my $synopsis = <<'END_SYNOPSIS';
127 First, build an index of your documents.
128  
129 my $index = Lucy::Simple->new(
130 path => '/path/to/index/'
131 language => 'en',
132 );
133  
134 while ( my ( $title, $content ) = each %source_docs ) {
135 $index->add_doc({
136 title => $title,
137 content => $content,
138 });
139 }
140  
141 Later, search the index.
142  
143 my $total_hits = $index->search(
144 query => $query_string,
145 offset => 0,
146 num_wanted => 10,
147 );
148  
149 print "Total hits: $total_hits\n";
150 while ( my $hit = $index->next ) {
151 print "$hit->{title}\n",
152 }
153 END_SYNOPSIS
154 1 my $constructor = <<'END_CONSTRUCTOR';
155 my $lucy = Lucy::Simple->new(
156 path => '/path/to/index/',
157 language => 'en',
158 );
159 END_CONSTRUCTOR
160 1 my $add_doc_pod = <<'END_ADD_DOC_POD';
161 =head2 add_doc
162  
163 $lucy->add_doc({
164 location => $url,
165 title => $title,
166 content => $content,
167 });
168  
169 Add a document to the index. The document must be supplied as a hashref,
170 with field names as keys and content as values.
171  
172 END_ADD_DOC_POD
173 1 $pod_spec->set_synopsis($synopsis);
174  
175 1 $pod_spec->add_constructor( sample => $constructor );
176 # Override is necessary because there's no standard way to explain
177 # hash/hashref across multiple host languages.
178 1 $pod_spec->add_method(
179 method => 'Add_Doc',
180 alias => 'add_doc',
181 pod => $add_doc_pod,
182 );
183  
184 1 my $xs_code = <<'END_XS_CODE';
185 MODULE = Lucy PACKAGE = Lucy::Simple
186  
187 void
188 add_doc(self, doc_sv)
189 lucy_Simple *self;
190 SV *doc_sv;
191 PPCODE:
192 {
193 lucy_Doc *doc = NULL;
194  
195 // Either get a Doc or use the stock doc.
196 if (sv_isobject(doc_sv)
197 && sv_derived_from(doc_sv, "Lucy::Document::Doc")
198 ) {
199 IV tmp = SvIV(SvRV(doc_sv));
200 doc = INT2PTR(lucy_Doc*, tmp);
201 }
202 else if (XSBind_sv_defined(aTHX_ doc_sv) && SvROK(doc_sv)) {
203 HV *maybe_fields = (HV*)SvRV(doc_sv);
204 if (SvTYPE((SV*)maybe_fields) == SVt_PVHV) {
205 lucy_Indexer *indexer = LUCY_Simple_Get_Indexer(self);
206 doc = LUCY_Indexer_Get_Stock_Doc(indexer);
207 LUCY_Doc_Set_Fields(doc, maybe_fields);
208 }
209 }
210 if (!doc) {
211 THROW(CFISH_ERR, "Need either a hashref or a %o",
212 CFISH_Class_Get_Name(LUCY_DOC));
213 }
214  
215 LUCY_Simple_Add_Doc(self, doc);
216 }
217 END_XS_CODE
218  
219 1 my $binding = Clownfish::CFC::Binding::Perl::Class->new(
220 parcel => "Lucy",
221 class_name => "Lucy::Simple",
222 );
223 1 $binding->exclude_method($_) for @hand_rolled;
224 1 $binding->append_xs($xs_code);
225 1 $binding->set_pod_spec($pod_spec);
226  
227 1 Clownfish::CFC::Binding::Perl::Class->register($binding);
228 }
229  
230 1 ▶sub bind_test {
Subroutine bind_test: called CC=1 SCAR=0.0
Pod undocumented
231 1 my $xs_code = <<'END_XS_CODE';
232 MODULE = Lucy::Test PACKAGE = Lucy::Test
233  
234 #include "Clownfish/TestHarness/TestFormatter.h"
235 #include "Clownfish/TestHarness/TestSuite.h"
236  
237 bool
238 run_tests(package)
239 char *package;
240 CODE:
241 cfish_String *class_name = cfish_Str_newf("%s", package);
242 cfish_TestFormatter *formatter
243 = (cfish_TestFormatter*)cfish_TestFormatterTAP_new();
244 cfish_TestSuite *suite = testlucy_Test_create_test_suite();
245 bool result = CFISH_TestSuite_Run_Batch(suite, class_name, formatter);
246 CFISH_DECREF(class_name);
247 CFISH_DECREF(formatter);
248 CFISH_DECREF(suite);
249  
250 RETVAL = result;
251 OUTPUT: RETVAL
252 END_XS_CODE
253  
254 1 my $binding = Clownfish::CFC::Binding::Perl::Class->new(
255 parcel => "TestLucy",
256 class_name => "Lucy::Test",
257 );
258 1 $binding->append_xs($xs_code);
259  
260 1 Clownfish::CFC::Binding::Perl::Class->register($binding);
261 }
262  
263 1 1;
264  
« buildlib/Lucy/Build/Binding/Document.pm ↑ Summary buildlib/Lucy/Build/Binding/Index.pm »