/
py_source.cpp
2091 lines (1789 loc) · 56.9 KB
/
py_source.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef _WIN32
#pragma warning(disable:4996)
#pragma warning(disable:4127)
#endif
#include "sphinx.h"
#include "sphinxutils.h"
//#include "sphinx_internal.h"
#include "py_helper.h"
#include "py_source.h"
#include "sphinxsearch.h"
//py-layer HITMAN, -> change it if sphinx.cpp changed.
typedef Hitman_c<8> HITMAN;
//just copy from sphinx.cpp
#define SPH_INTERNAL_PROFILER 0
#if SPH_INTERNAL_PROFILER
enum ESphTimer
{
TIMER_root = 0,
#define DECLARE_TIMER(_arg) TIMER_##_arg,
#include "sphinxtimers.h"
#undef DECLARE_TIMER
TIMERS_TOTAL
};
static const char * const g_dTimerNames [ TIMERS_TOTAL ] =
{
"root",
#define DECLARE_TIMER(_arg) #_arg,
#include "sphinxtimers.h"
#undef DECLARE_TIMER
};
struct CSphTimer
{
int64_t m_iMicroSec;
ESphTimer m_eTimer;
int m_iParent;
int m_iChild;
int m_iNext;
int m_iPrev;
int m_iCalls;
CSphTimer ()
{
Alloc ( TIMER_root, -1 );
}
void Alloc ( ESphTimer eTimer, int iParent )
{
m_iParent = iParent;
m_iChild = -1;
m_iNext = -1;
m_iPrev = -1;
m_eTimer = eTimer;
m_iMicroSec = 0;
m_iCalls = 0;
}
void Start ()
{
m_iMicroSec -= sphMicroTimer ();
m_iCalls++;
}
void Stop ()
{
m_iMicroSec += sphMicroTimer ();
}
};
static const int SPH_MAX_TIMERS = 128;
static int g_iTimer = -1;
static int g_iTimers = 0;
static CSphTimer g_dTimers [ SPH_MAX_TIMERS ];
void sphProfilerInit ()
{
assert ( g_iTimers==0 );
assert ( g_iTimer==-1 );
// start root timer
g_iTimers = 1;
g_iTimer = 0;
g_dTimers[g_iTimer].Alloc ( TIMER_root, -1 );
g_dTimers[g_iTimer].Start ();
}
void sphProfilerPush ( ESphTimer eTimer )
{
assert ( g_iTimer>=0 && g_iTimer<SPH_MAX_TIMERS );
assert ( eTimer!=TIMER_root );
// search for match timer in current timer's children list
int iTimer;
for ( iTimer=g_dTimers[g_iTimer].m_iChild;
iTimer>0;
iTimer=g_dTimers[iTimer].m_iNext )
{
if ( g_dTimers[iTimer].m_eTimer==eTimer )
break;
}
// not found? let's alloc
if ( iTimer<0 )
{
assert ( g_iTimers<SPH_MAX_TIMERS );
iTimer = g_iTimers++;
// create child and make current timer it's parent
g_dTimers[iTimer].Alloc ( eTimer, g_iTimer );
// make it new children list head
g_dTimers[iTimer].m_iNext = g_dTimers[g_iTimer].m_iChild;
if ( g_dTimers[g_iTimer].m_iChild>=0 )
g_dTimers [ g_dTimers[g_iTimer].m_iChild ].m_iPrev = iTimer;
g_dTimers[g_iTimer].m_iChild = iTimer;
}
// make it new current one
assert ( iTimer>0 );
g_dTimers[iTimer].Start ();
g_iTimer = iTimer;
}
void sphProfilerPop ( ESphTimer eTimer )
{
assert ( g_iTimer>0 && g_iTimer<SPH_MAX_TIMERS );
assert ( g_dTimers[g_iTimer].m_eTimer==eTimer );
g_dTimers[g_iTimer].Stop ();
g_iTimer = g_dTimers[g_iTimer].m_iParent;
assert ( g_iTimer>=0 && g_iTimer<SPH_MAX_TIMERS );
}
void sphProfilerDone ()
{
assert ( g_iTimers>0 );
assert ( g_iTimer==0 );
// stop root timer
g_iTimers = 0;
g_iTimer = -1;
g_dTimers[0].Stop ();
}
void sphProfilerShow ( int iTimer=0, int iLevel=0 )
{
assert ( g_iTimers==0 );
assert ( g_iTimer==-1 );
if ( iTimer==0 )
fprintf ( stdout, "--- PROFILE ---\n" );
CSphTimer & tTimer = g_dTimers[iTimer];
int iChild;
// calc me
int iChildren = 0;
int64_t tmSelf = tTimer.m_iMicroSec;
for ( iChild=tTimer.m_iChild; iChild>0; iChild=g_dTimers[iChild].m_iNext, iChildren++ )
tmSelf -= g_dTimers[iChild].m_iMicroSec;
// dump me
if ( tTimer.m_iMicroSec<50 )
return;
char sName[32];
for ( int i=0; i<iLevel; i++ )
sName[2*i] = sName[2*i+1] = ' ';
sName[2*iLevel] = '\0';
strncat ( sName, g_dTimerNames [ tTimer.m_eTimer ], sizeof(sName) );
fprintf ( stdout, "%-32s | %6d.%02d ms | %6d.%02d ms self | %d calls\n",
sName,
int(tTimer.m_iMicroSec/1000), int(tTimer.m_iMicroSec%1000)/10,
int(tmSelf/1000), int(tmSelf%1000)/10,
tTimer.m_iCalls );
// dump my children
iChild = tTimer.m_iChild;
while ( iChild>0 && g_dTimers[iChild].m_iNext>0 )
iChild = g_dTimers[iChild].m_iNext;
while ( iChild>0 )
{
sphProfilerShow ( iChild, 1+iLevel );
iChild = g_dTimers[iChild].m_iPrev;
}
if ( iTimer==0 )
fprintf ( stdout, "---------------\n" );
}
class CSphEasyTimer
{
public:
CSphEasyTimer ( ESphTimer eTimer )
: m_eTimer ( eTimer )
{
if ( g_iTimer>=0 )
sphProfilerPush ( m_eTimer );
}
~CSphEasyTimer ()
{
if ( g_iTimer>=0 )
sphProfilerPop ( m_eTimer );
}
protected:
ESphTimer m_eTimer;
};
#define PROFILER_INIT() sphProfilerInit()
#define PROFILER_DONE() sphProfilerDone()
#define PROFILE_BEGIN(_arg) sphProfilerPush(TIMER_##_arg)
#define PROFILE_END(_arg) sphProfilerPop(TIMER_##_arg)
#define PROFILE_SHOW() sphProfilerShow()
#define PROFILE(_arg) CSphEasyTimer __t_##_arg ( TIMER_##_arg );
#else
#define PROFILER_INIT()
#define PROFILER_DONE()
#define PROFILE_BEGIN(_arg)
#define PROFILE_END(_arg)
#define PROFILE_SHOW()
#define PROFILE(_arg)
#endif // SPH_INTERNAL_PROFILER
#if USE_PYTHON
#define LOC_ERROR2(_msg,_arg,_arg2) { sError.SetSprintf ( _msg, _arg, _arg2 ); return false; }
PyObject* GetObjectAttr(PyObject *pInst, char* name);
CSphSource_Python::CSphSource_Python ( const char * sName )
: CSphSource_Document ( sName ),m_uMaxFetchedID(0), m_sError()
, m_iKillListSize( 0 )
, m_iKillListPos( 0 )
, m_iJoinedHitID( 0 )
, m_iJoinedHitField( 0 )
, m_JoinFieldsResultPos( 0 )
{
//sName the data source's name
main_module = NULL;
builtin_module = NULL;
m_pInstance = NULL;
m_pInstance_BuildHit = NULL;
m_pInstance_NextDocument = NULL;
m_pInstance_GetDocField = NULL;
m_pInstance_GetMVAValue = NULL;
m_bHaveCheckBuildHit = false;
m_Hit_collector = NULL;
m_JoinFieldsResult = NULL;
for(int i = 0; i< SPH_MAX_FIELDS; i++){
m_dFields[i] = NULL;
m_iJoinedHitPositions[i] = 0;
}
m_pKillList = NULL;
}
CSphSource_Python::~CSphSource_Python (){
Disconnect ();
if(m_pInstance_BuildHit) {
Py_XDECREF(m_pInstance_BuildHit);
m_pInstance_BuildHit = NULL;
}
if(m_pInstance_NextDocument){
Py_XDECREF(m_pInstance_BuildHit);
m_pInstance_BuildHit = NULL;
}
if(m_Hit_collector) {
Py_XDECREF(m_Hit_collector);
m_Hit_collector = NULL;
}
if(m_pInstance_GetDocField){
Py_XDECREF(m_pInstance_GetDocField);
m_pInstance_GetDocField = NULL;
}
if(m_pInstance_GetMVAValue){
Py_XDECREF(m_pInstance_GetMVAValue);
m_pInstance_GetMVAValue = NULL;
}
if(m_JoinFieldsResult){
Py_XDECREF(m_JoinFieldsResult);
m_JoinFieldsResult = NULL;
}
//can not be in Disconnect for OnIndexFinished still needs this.
if(m_pInstance) {
Py_XDECREF(m_pInstance);
m_pInstance = NULL;
}
}
// get string
#define LOC_GETS(_arg,_key) \
if ( hSource.Exists(_key) ) \
_arg = hSource[_key];
// get array of strings
#define LOC_GETAS(_arg,_key) \
for ( CSphVariant * pVal = hSource(_key); pVal; pVal = pVal->m_pNext ) \
_arg.Add ( pVal->cstr() );
bool CSphSource_Python::Setup ( const CSphConfigSection & hSource){
//check have python_name?
CSphString PySourceName;
LOC_GETS(PySourceName, "name");
if(PySourceName.Length() == 0) {
Error ( "field 'name' must be assigned.");
return false;
}
//Shame on me!! __main__ __builtin__ time no Refcnt +1
main_module = PyImport_AddModule("__main__");
builtin_module = PyImport_AddModule("__builtin__");
//do schema read, ready to used.
if (!main_module) { goto DONE; }
if (!builtin_module) {
//Py_XDECREF(main_module);
goto DONE;
}
if(InitDataSchema(hSource, PySourceName.cstr())!=0)
return false;
return true;
DONE:
return false;
}
//////////////////////////////////////////////////////////////////////////
// Helper Functions
//////////////////////////////////////////////////////////////////////////
bool CheckResult(PyObject * pResult)
{
if(!pResult) //no return default true
return true;
if(PyBool_Check(pResult)){
if(Py_False == pResult)
return false;
}
if(PyInt_Check(pResult)) {
if(PyInt_AsLong(pResult) == 0)
return false;
}
return true;
}
int CSphSource_Python::UpdatePySchema( PyObject * pList, CSphSchema * pInfo, CSphString & docid, CSphString & sError )
{
/*
- Update pInfo via pList.
- Most copy from pysource, for no pysource needed in pyExt mode.
@return 0, ok
@return <0, error happend.
*/
assert(pInfo);
pInfo->Reset();
char* doc_id_col = NULL; //used to save docid.
//
if(!pList || !PyList_Check(pList)) {
sError = "Feed list object to schema.";
return -1;
}
int size = (int)PyList_Size(pList);
for(int i = 0; i < size; i++) {
PyObject* item = PyList_GetItem(pList,i);
// item -> tuple ( name, {props} )
if(!PyTuple_Check(item)){
sError = "The schema list must be build by tuples.";
return -2;
}
if(PyTuple_GET_SIZE(item) < 2) {
// name, props
return -3;
}
PyObject* key = PyTuple_GetItem(item, 0);
PyObject* props = PyTuple_GetItem(item, 1);
//check type
if(PyString_Check(key) && PyDict_Check(props))
{
char* strkey = PyString_AsString(key);
CSphString skey(strkey);
// Currently support case-sensive column while indexing
//check key
#if USE_PYTHON_CASE_SENSIVE_ATTR
#else
{
CSphString skey_low = skey;
skey_low.ToLower();
if(skey_low != skey) {
sError.SetSprintf("The schema column name must be in lower case [%s]", skey.cstr());
return -3;
}
}
#endif
//check docid
PyObject * propValue = NULL;
PyObject * sizeValue = NULL;
PyObject * wordPartValue = NULL;
PyObject * delayCollectionValue = NULL;
int iBitCount = -1;
propValue = PyDict_GetItemString(props, "docid"); //+0
if(propValue && CheckResult(propValue)){
doc_id_col = strkey;
continue;
}
propValue = PyDict_GetItemString(props, "type");
if (propValue && !PyString_Check(propValue)) {
sError.SetSprintf( "Attribute %s's type is not a string value", strkey);
continue;
}
// set tCol.m_tLocator.m_iBitCount, to make index smaller
sizeValue = PyDict_GetItemString(props, "size");
if(sizeValue && (PyInt_Check(sizeValue))) {
//PyErr_Print();
iBitCount = PyInt_AsLong(sizeValue);
}
if(sizeValue && (PyLong_Check(sizeValue))) {
//PyErr_Print();
iBitCount = (int)PyLong_AsLongLong(sizeValue);
}
wordPartValue = PyDict_GetItemString(props, "wordpart"); //+0
if (wordPartValue){
if (PyString_Check(wordPartValue)) {
CSphString szFieldName(strkey);
char* strval = PyString_AsString(wordPartValue);
if(strcmp(strval,"prefix") == 0) {
if(!m_dPrefixFields.Contains ( strkey ))
m_dPrefixFields.AddUnique(strkey);
}
if(strcmp(strval,"infix") == 0 ) {
if(!m_dInfixFields.Contains ( strkey ))
m_dInfixFields.AddUnique(strkey);
}
}else {
sError.SetSprintf( "Attribute %s's type is not a string value, skip", "wordpart");
wordPartValue = NULL;
}
}
delayCollectionValue = PyDict_GetItemString(props, "delay"); //+0
if(delayCollectionValue && CheckResult(delayCollectionValue)){
//doc_id_col = strkey;
//continue;
}
// assign types
if(strcmp(PyString_AsString(propValue) , "float") == 0){
CSphColumnInfo tCol ( strkey, SPH_ATTR_FLOAT );
tCol.m_iIndex = i; //m_tSchema.GetAttrsCount (); //should be i in pList?
tCol.m_tLocator.m_iBitCount = iBitCount;
pInfo->AddAttr ( tCol, true);
}else
if(strcmp(PyString_AsString(propValue) , "integer") == 0){
CSphColumnInfo tCol ( strkey, SPH_ATTR_INTEGER );
tCol.m_iIndex = i; //m_tSchema.GetAttrsCount (); //should be i in pList?
tCol.m_tLocator.m_iBitCount = iBitCount;
pInfo->AddAttr ( tCol, true);
}else
if(strcmp(PyString_AsString(propValue) , "long") == 0){
CSphColumnInfo tCol ( strkey, SPH_ATTR_BIGINT );
tCol.m_iIndex = i; //m_tSchema.GetAttrsCount (); //should be i in pList?
tCol.m_tLocator.m_iBitCount = iBitCount;
pInfo->AddAttr ( tCol, true);
}else
if(strcmp(PyString_AsString(propValue) , "list") == 0){
CSphColumnInfo tCol ( strkey, SPH_ATTR_INTEGER );
tCol.m_iIndex = i; //m_tSchema.GetAttrsCount (); //should be i in pList?
tCol.m_eAttrType = SPH_ATTR_UINT32SET;
tCol.m_eSrc = SPH_ATTRSRC_FIELD;
// tCol.m_tLocator.m_iBitCount = iBitCount; //XXX: ????
pInfo->AddAttr ( tCol, true);
}else
if(strcmp(PyString_AsString(propValue) , "list-query") == 0){
CSphColumnInfo tCol ( strkey, SPH_ATTR_INTEGER );
tCol.m_iIndex = i; //m_tSchema.GetAttrsCount (); //should be i in pList?
tCol.m_eAttrType = SPH_ATTR_UINT32SET;
tCol.m_eSrc = SPH_ATTRSRC_QUERY;
// tCol.m_tLocator.m_iBitCount = iBitCount; //XXX: ????
pInfo->AddAttr ( tCol, true);
}else
if(strcmp(PyString_AsString(propValue) , "bool") == 0){
CSphColumnInfo tCol ( strkey, SPH_ATTR_BOOL );
tCol.m_iIndex = i; //m_tSchema.GetAttrsCount (); //should be i in pList?
pInfo->AddAttr ( tCol, true );
}else
if(strcmp(PyString_AsString(propValue) , "str2ord") == 0){
CSphColumnInfo tCol ( strkey, SPH_ATTR_ORDINAL );
tCol.m_iIndex = i; //m_tSchema.GetAttrsCount (); //should be i in pList?
pInfo->AddAttr ( tCol, true );
}else
if(strcmp(PyString_AsString(propValue) , "timestamp") == 0){
CSphColumnInfo tCol ( strkey, SPH_ATTR_TIMESTAMP );
tCol.m_iIndex = i; //m_tSchema.GetAttrsCount (); //should be i in pList?
pInfo->AddAttr ( tCol, true );
}else
if(strcmp(PyString_AsString(propValue) , "string") == 0){
CSphColumnInfo tCol ( strkey, SPH_ATTR_STRING );
tCol.m_iIndex = i; //m_tSchema.GetAttrsCount (); //should be i in pList?
pInfo->AddAttr ( tCol , true);
}else
//if(propValue == csft_string_fulltext)
{
//-> to support sql_field_string "string_text"
if(strcmp(PyString_AsString(propValue) , "string_text") == 0){
//append strings
CSphColumnInfo tCol ( strkey, SPH_ATTR_STRING );
tCol.m_iIndex = i; //m_tSchema.GetAttrsCount (); //should be i in pList?
pInfo->AddAttr ( tCol , true);
}else
if(strcmp(PyString_AsString(propValue) , "text") != 0){
sError.SetSprintf("Type %s is invalid, treated as full-text", PyString_AsString(propValue));
}
//default fulltext field
// AddFieldToSchema(skey.cstr(), i);
{
CSphColumnInfo tCol ( strkey );
// TODO: setup prefix, infix
//this used to set SPH_WORDPART_PREFIX | SPH_WORDPART_INFIX, in push hit mode, no needs at all.
SetupFieldMatch ( tCol );
tCol.m_iIndex = i;
pInfo->m_dFields.Add ( tCol );
}
}
}else{
///XXX? report error | continue;
}
} // for
if(!doc_id_col) {
if(PyErr_Occurred()) PyErr_Print();
sError.SetSprintf("Must set docid = True attribute in DataSource Scheme to declare document unique id");
PyErr_Clear();
return -1;
}
docid = doc_id_col;
return 0;
}
//////////////////////////////////////////////////////////////////////////
int CSphSource_Python::InitDataSchema_Python( CSphString & sError )
{
if (!m_pInstance) {
PyErr_Print();
m_sError.SetSprintf ( "Can no create source object");
return -1;
}
//all condition meets
m_tSchema.m_dFields.Reset ();
//enum all attrs
PyObject* pArgs = NULL;
PyObject* pResult = NULL;
PyObject* pFunc = PyObject_GetAttrString(m_pInstance, "GetScheme"); // +1
if (!pFunc) {
sError = ("Method SourceObj->GetScheme():{dict of attributes} missing.");
//fprintf(stderr,m_sError.cstr());
return -2; //Next Document must exist
}
if(!pFunc||!PyCallable_Check(pFunc)){
Py_XDECREF(pFunc);
sError = ("Method SourceObj->GetScheme():{dict of attributes} missing.");
//fprintf(stderr,m_sError.cstr());
return -2;
}
pArgs = Py_BuildValue("()");
pResult = PyEval_CallObject(pFunc, pArgs);
Py_XDECREF(pArgs);
Py_XDECREF(pFunc);
if(PyErr_Occurred()) PyErr_Print();
{
int nRet = UpdatePySchema(pResult, &m_tSchema, m_Doc_id_col, sError);
if(nRet)
return nRet;
}
Py_XDECREF(pResult);
/*
char sBuf [ 1024 ];
snprintf ( sBuf, sizeof(sBuf), "pysource(%s)", m_sCommand.cstr() );
m_tSchema.m_sName = sBuf;
*/
m_tDocInfo.Reset ( m_tSchema.GetRowSize() );
m_dStrAttrs.Resize ( m_tSchema.GetAttrsCount() );
// check it
if ( m_tSchema.m_dFields.GetLength()>SPH_MAX_FIELDS )
LOC_ERROR2 ( "too many fields (fields=%d, max=%d); raise SPH_MAX_FIELDS in sphinx.h and rebuild",
m_tSchema.m_dFields.GetLength(), SPH_MAX_FIELDS );
return 0;
}
/// connect to the source (eg. to the database)
/// connection settings are specific for each source type and as such
/// are implemented in specific descendants
bool CSphSource_Python::Connect ( CSphString & sError ){
//init the schema
if (!m_pInstance) {
PyErr_Print();
sError.SetSprintf ( "Can no create source object");
return false;
}
m_tHits.m_dData.Reserve ( m_iMaxHits );
//try to do connect
{
if (!m_pInstance)
{
if(PyErr_Occurred()) PyErr_Print();
PyErr_Clear();
return false;
}else{
PyObject* pArgs = NULL;
PyObject* pResult = NULL;
PyObject* pFunc = PyObject_GetAttrString(m_pInstance, "Connected");
if (!pFunc){
sError = ("'SourceObj->Connected():None' missing.\n");
PyErr_Clear();
//return false;
}else{
if(!PyCallable_Check(pFunc)){
Py_XDECREF(pFunc);
return false;
}
pArgs = Py_BuildValue("()");
pResult = PyEval_CallObject(pFunc, pArgs);
Py_XDECREF(pArgs);
Py_XDECREF(pFunc);
//check result
if(PyErr_Occurred()) PyErr_Print();
if(!CheckResult(pResult)) {
Py_XDECREF(pResult);
return false;
}
Py_XDECREF(pResult);
}
// init data-schema when connect
if(InitDataSchema_Python(sError) != 0)
return false;
// build the cache.
m_tSchema.BuildFieldIndexCache();
}
}
return true;
}
bool CSphSource_Python::CheckResult(PyObject * pResult)
{
if(!pResult) //no return default true
return true;
if(PyBool_Check(pResult)){
if(Py_False == pResult)
return false;
}
if(PyInt_Check(pResult)) {
if(PyInt_AsLong(pResult) == 0)
return false;
}
return true;
}
/// disconnect from the source
void CSphSource_Python::Disconnect (){
m_tSchema.Reset ();
m_tHits.m_dData.Reset();
}
/// check if there are any attributes configured
/// note that there might be NO actual attributes in the case if configured
/// ones do not match those actually returned by the source
bool CSphSource_Python::HasAttrsConfigured () {
return true;
}
void CSphSource_Python::PostIndex ()
{
if (!m_pInstance)
{
PyErr_Print();
goto DONE;
}else{
PyObject* pArgs = NULL;
PyObject* pResult = NULL;
PyObject* pFunc = PyObject_GetAttrString(m_pInstance, "OnIndexFinished");
if (!pFunc)
PyErr_Clear(); //is function can be undefined
if(!pFunc || pFunc == Py_None ||!PyCallable_Check(pFunc)){
Py_XDECREF(pFunc);
goto DONE;
}
pArgs = Py_BuildValue("()");
pResult = PyEval_CallObject(pFunc, pArgs);
if(PyErr_Occurred()) PyErr_Print();
PyErr_Clear();
Py_XDECREF(pArgs);
Py_XDECREF(pFunc);
Py_XDECREF(pResult);
}
DONE:
return ;
}
/// begin iterating document hits
/// to be implemented by descendants
bool CSphSource_Python::IterateStart ( CSphString & sError ){
int iFieldMVA = 0;
///TODO: call on_before_index function
if (!m_pInstance)
{
PyErr_Print();
goto DONE;
}else{
PyObject* pArgs = NULL;
PyObject* pResult = NULL;
PyObject* pFunc = PyObject_GetAttrString(m_pInstance, "OnBeforeIndex");
if (!pFunc)
PyErr_Clear(); //is function can be undefined
if(!pFunc || pFunc == Py_None ||!PyCallable_Check(pFunc)){
Py_XDECREF(pFunc);
goto DONE;
}
pArgs = Py_BuildValue("()");
pResult = PyEval_CallObject(pFunc, pArgs);
Py_XDECREF(pArgs);
Py_XDECREF(pFunc);
if(PyErr_Occurred()) PyErr_Print();
PyErr_Clear();
if(!CheckResult(pResult)) {
Py_XDECREF(pResult);
return false;
}
Py_XDECREF(pResult);
}
DONE:
//process GetFieldOrder
{
PyObject* pArgs = NULL;
PyObject* pResult = NULL;
PyObject* pFunc = PyObject_GetAttrString(m_pInstance, "GetFieldOrder");
if (!pFunc)
PyErr_Clear(); //is function can be undefined
if(!pFunc||!PyCallable_Check(pFunc)){
Py_XDECREF(pFunc);
//goto DONE;
}
pArgs = Py_BuildValue("()");
pResult = PyEval_CallObject(pFunc, pArgs);
Py_XDECREF(pArgs);
Py_XDECREF(pFunc);
if(PyErr_Occurred()) PyErr_Print();
PyErr_Clear();
//this is list -> list is supported by python source level-3
if (PyList_Check(pResult)) {
int iSize = (int)PyList_Size(pResult);
if(iSize>=1) { //must have at least one element
PyObject* pResult2 = PyList_GetItem(pResult,0); //borrow
m_tSchema.m_iBaseFields = 0;
if (PyTuple_Check(pResult2)) {
m_tSchema.m_iBaseFields = (int)PyTuple_Size(pResult2); //the base fields.
#if HAVE_SSIZE_T
for(Py_ssize_t iField = 0; iField< PyTuple_Size(pResult2); iField++)
#else
for(int iField = 0; iField< PyTuple_Size(pResult2); iField++)
#endif
{
PyObject* pItem = PyTuple_GetItem(pResult2, iField);
if(PyString_Check(pItem)){
//int j = this->m_tSchema.GetFieldIndex (PyString_AsString(pItem));
this->m_baseFields.Add(PyString_AsString(pItem));
}
}
}//end PyTuple_Check
else
//might be string only
if(PyString_Check(pResult2)){
this->m_baseFields.Add(PyString_AsString(pResult2));
}
else{
sError.SetSprintf ( "Must have at least one field as a first order field" );
return 0;
}
}//end if iSize >= 1
if(iSize==2) { //have joined fields
PyObject* pResult2 = PyList_GetItem(pResult,1); //borrow
if (PyTuple_Check(pResult2)) {
#if HAVE_SSIZE_T
for(Py_ssize_t iField = 0; iField< PyTuple_Size(pResult2); iField++)
#else
for(int iField = 0; iField< PyTuple_Size(pResult2); iField++)
#endif
{
PyObject* pItem = PyTuple_GetItem(pResult2, iField);
if(PyString_Check(pItem)){
//int j = this->m_tSchema.GetFieldIndex (PyString_AsString(pItem));
this->m_joinFields.Add(PyString_AsString(pItem));
}
}
}//end PyTuple_Check
if(PyString_Check(pResult2)){
this->m_joinFields.Add(PyString_AsString(pResult2));
}
}
}else
{
sError = ("Method SourceObj->GetFieldOrder():[list of fields tuple]). Only list is acceptable.");
Py_XDECREF(pResult);
return false;
}
Py_XDECREF(pResult);
} //end of get field order
// process of prefetch GetDocField, DocField can be NULL, if no delay collection field.
{
if(m_pInstance_GetDocField == NULL)
{
//PyObject* pFunc = NULL;
if (m_pInstance)
{
m_pInstance_GetDocField = PyObject_GetAttrString(m_pInstance, "GetDocField");
}
if(!m_pInstance_GetDocField){
// GetDocField CAN '404 Not found.'
if(PyErr_Occurred())
PyErr_Clear();
// just pass it.
}
}
}
// process MVA checking.
m_iFieldMVA = 0;
m_iFieldMVAIterator = 0;
m_dAttrToFieldMVA.Resize ( 0 );
for ( int i = 0; i < m_tSchema.GetAttrsCount (); i++ )
{
const CSphColumnInfo & tCol = m_tSchema.GetAttr ( i );
if ( ( tCol.m_eAttrType == SPH_ATTR_UINT32SET ) && tCol.m_eSrc == SPH_ATTRSRC_FIELD )
m_dAttrToFieldMVA.Add ( iFieldMVA++ );
else
m_dAttrToFieldMVA.Add ( -1 );
}
m_dFieldMVAs.Resize ( iFieldMVA );
ARRAY_FOREACH ( i, m_dFieldMVAs )
m_dFieldMVAs [i].Reserve ( 16 );
return true;
}
/// begin iterating values of out-of-document multi-valued attribute iAttr
/// will fail if iAttr is out of range, or is not multi-valued
/// can also fail if configured settings are invalid (eg. SQL query can not be executed)
bool CSphSource_Python::IterateMultivaluedStart ( int iAttr, CSphString & sError )
{
if ( iAttr<0 || iAttr>=m_tSchema.GetAttrsCount() )
return false;
m_iMultiAttr = iAttr;
const CSphColumnInfo & tAttr = m_tSchema.GetAttr(iAttr);
if ( !(tAttr.m_eAttrType==SPH_ATTR_UINT32SET || tAttr.m_eAttrType==SPH_ATTR_UINT64SET ) )
return false;
switch ( tAttr.m_eSrc )
{
case SPH_ATTRSRC_FIELD:
return false;
case SPH_ATTRSRC_QUERY:
{
if(m_pInstance_GetMVAValue == NULL)
{
//PyObject* pFunc = NULL;
if (m_pInstance)
{
m_pInstance_GetMVAValue = PyObject_GetAttrString(m_pInstance, "GetMVAValue");
}
if(!m_pInstance_GetMVAValue){
// GetDocField CAN '404 Not found.'
if(PyErr_Occurred())
PyErr_Clear();
sError = ("Method SourceObj->GetMVAValue():(docid, [list of values]) missing.");
return false;
}
}
if(m_pInstance_GetMVAValue)
return true;
}
default:
sError.SetSprintf ( "INTERNAL ERROR: unknown multi-valued attr source type %d", tAttr.m_eSrc );
return false;
} // end of switch
//return true;
}
/// get next multi-valued (id,attr-value) tuple to m_tDocInfo
bool CSphSource_Python::IterateMultivaluedNext ()
{
const CSphColumnInfo & tAttr = m_tSchema.GetAttr ( m_iMultiAttr );
assert ( tAttr.m_eAttrType==SPH_ATTR_UINT32SET || tAttr.m_eAttrType==SPH_ATTR_UINT64SET );
//m_iMultiAttr
if(m_pInstance_GetMVAValue)
{
//call GetMVA
{
PyObject* pArgs = NULL;
PyObject* pResult = NULL;
//create hit_c
if(m_Hit_collector == NULL)
m_Hit_collector = PyNewHitCollector(this, tAttr.m_sName, m_iJoinedHitField);
else{
csfHelper_HitCollectorObject *self = (csfHelper_HitCollectorObject *)m_Hit_collector;
self->iField = m_iJoinedHitField;
*(self->m_FieldName) = tAttr.m_sName;
}