📄 tokenbatch.pm
字号:
package KinoSearch::Analysis::TokenBatch;use strict;use warnings;use KinoSearch::Util::ToolSet;use base qw( KinoSearch::Util::CClass );1;__END____XS__MODULE = KinoSearch PACKAGE = KinoSearch::Analysis::TokenBatchvoidnew(either_sv) SV *either_sv;PREINIT: char *class; TokenBatch *batch;PPCODE: /* determine the class */ class = sv_isobject(either_sv) ? sv_reftype(either_sv, 0) : SvPV_nolen(either_sv); /* build object */ batch = Kino_TokenBatch_new(); ST(0) = sv_newmortal(); sv_setref_pv(ST(0), class, (void*)batch); XSRETURN(1);voidappend(batch, text_sv, start_offset, end_offset, ...) TokenBatch *batch; SV *text_sv; I32 start_offset; I32 end_offset;PREINIT: char *text; STRLEN len; I32 pos_inc = 1; Token *token;PPCODE: text = SvPV(text_sv, len); if (items == 5) pos_inc = SvIV( ST(4) ); else if (items > 5) Kino_confess("Too many arguments: %d", items); token = Kino_Token_new(text, len, start_offset, end_offset, pos_inc); Kino_TokenBatch_append(batch, token);=for commentAdd many tokens to the batch, by supplying the string to be tokenized, andarrays of token starts and token ends (specified in bytes).=cutvoidadd_many_tokens(batch, string_sv, starts_av, ends_av) TokenBatch *batch; SV *string_sv; AV *starts_av; AV *ends_av;PREINIT: char *string_start; STRLEN len, start_offset, end_offset; I32 i, max; SV **start_sv_ptr; SV **end_sv_ptr; Token *token;PPCODE:{ string_start = SvPV(string_sv, len); max = av_len(starts_av); for (i = 0; i <= max; i++) { /* retrieve start */ start_sv_ptr = av_fetch(starts_av, i, 0); if (start_sv_ptr == NULL) Kino_confess("Failed to retrieve @starts array element"); start_offset = SvIV(*start_sv_ptr); /* retrieve end */ end_sv_ptr = av_fetch(ends_av, i, 0); if (end_sv_ptr == NULL) Kino_confess("Failed to retrieve @ends array element"); end_offset = SvIV(*end_sv_ptr); /* sanity check the offsets to make sure they're inside the string */ if (start_offset > len) Kino_confess("start_offset > len (%d > %"UVuf")", start_offset, (UV)len); if (end_offset > len) Kino_confess("end_offset > len (%d > %"UVuf")", end_offset, (UV)len); /* calculate the start of the substring and add the token */ token = Kino_Token_new( (string_start + start_offset), (end_offset - start_offset), start_offset, end_offset, 1 ); Kino_TokenBatch_append(batch, token); }}=begin commentAdd the postings to the segment. Postings are serialized and dumped into aSortExternal sort pool. The actual writing takes place later.The serialization algo is designed so that postings emerge from the sortpool in the order ideal for writing an index after a simple lexical sort.The concatenated components are: field number term text null byte document number positions (C array of U32) term length=end comment=cutvoidbuild_posting_list(batch, doc_num, field_num) TokenBatch *batch; U32 doc_num; U16 field_num;PPCODE: Kino_TokenBatch_build_plist(batch, doc_num, field_num);voidset_all_texts(batch, texts_av) TokenBatch *batch; AV *texts_av;PREINIT: Token *token; I32 i, max; SV **sv_ptr; char *text; STRLEN len;PPCODE:{ token = batch->first; max = av_len(texts_av); for (i = 0; i <= max; i++) { if (token == NULL) { Kino_confess("Batch size %d doesn't match array size %d", batch->size, (max + 1)); } sv_ptr = av_fetch(texts_av, i, 0); if (sv_ptr == NULL) { Kino_confess("Encountered a null SV* pointer"); } text = SvPV(*sv_ptr, len); Kino_Safefree(token->text); token->text = Kino_savepvn(text, len); token->len = len; token = token->next; }}voidget_all_texts(batch) TokenBatch *batch;PREINIT: Token *token; AV *out_av;PPCODE:{ out_av = newAV(); token = batch->first; while (token != NULL) { SV *text = newSVpvn(token->text, token->len); av_push(out_av, text); token = token->next; } XPUSHs(sv_2mortal( newRV_noinc((SV*)out_av) )); XSRETURN(1);}SV*_set_or_get(batch, ...) TokenBatch *batch;ALIAS: set_text = 1 get_text = 2 set_start_offset = 3 get_start_offset = 4 set_end_offset = 5 get_end_offset = 6 set_pos_inc = 7 get_pos_inc = 8 set_size = 9 get_size = 10 set_postings = 11 get_postings = 12 set_tv_string = 13 get_tv_string = 14CODE:{ /* fail if looking for info on a single token but there isn't one */ if ((ix < 7) && (batch->current == NULL)) Kino_confess("TokenBatch doesn't currently hold a valid token"); KINO_START_SET_OR_GET_SWITCH case 1: { Token *current = batch->current; char *text; Kino_Safefree(current->text); text = SvPV( ST(1), current->len ); current->text = Kino_savepvn( text, current->len ); } /* fall through */ case 2: RETVAL = newSVpvn(batch->current->text, batch->current->len); break; case 3: batch->current->start_offset = SvIV( ST(1) ); /* fall through */ case 4: RETVAL = newSViv(batch->current->start_offset); break; case 5: batch->current->end_offset = SvIV( ST(1) ); /* fall through */ case 6: RETVAL = newSViv(batch->current->end_offset); break; case 7: batch->current->pos_inc = SvIV( ST(1) ); /* fall through */ case 8: RETVAL = newSViv(batch->current->pos_inc); break; case 9: Kino_confess("Can't set size on a TokenBatch object"); /* fall through */ case 10: RETVAL = newSVuv(batch->size); break; case 11: Kino_confess("can't set_postings"); /* fall through */ case 12: RETVAL = newRV_inc( (SV*)batch->postings ); break; case 13: Kino_confess("can't set_tv_string"); /* fall through */ case 14: RETVAL = newSVsv(batch->tv_string); break; KINO_END_SET_OR_GET_SWITCH}OUTPUT: RETVALvoidreset(batch) TokenBatch *batch;PPCODE: Kino_TokenBatch_reset(batch);I32next(batch) TokenBatch *batch;CODE: RETVAL = Kino_TokenBatch_next(batch);OUTPUT: RETVALvoidDESTROY(batch) TokenBatch *batch;PPCODE: Kino_TokenBatch_destroy(batch);__H__#ifndef H_KINOSEARCH_ANALYSIS_TOKENBATCH#define H_KINOSEARCH_ANALYSIS_TOKENBATCH 1#include "EXTERN.h"#include "perl.h"#include "XSUB.h"#include "ppport.h"#include "KinoSearchAnalysisToken.h"#include "KinoSearchIndexTerm.h"#include "KinoSearchUtilCarp.h"#include "KinoSearchUtilMathUtils.h"#include "KinoSearchUtilMemManager.h"#include "KinoSearchUtilStringHelper.h"typedef struct tokenbatch { Token *first; Token *last; Token *current; I32 size; I32 initialized; AV *postings; SV *tv_string;} TokenBatch;TokenBatch* Kino_TokenBatch_new();void Kino_TokenBatch_destroy(TokenBatch *batch);void Kino_TokenBatch_append(TokenBatch *batch, Token *token);I32 Kino_TokenBatch_next(TokenBatch *batch);void Kino_TokenBatch_reset(TokenBatch *batch);void Kino_TokenBatch_build_plist(TokenBatch*, U32, U16);#endif /* include guard */__C__#include "KinoSearchAnalysisTokenBatch.h"TokenBatch*Kino_TokenBatch_new() { TokenBatch *batch; /* allocate */ Kino_New(0, batch, 1, TokenBatch); /* init */ batch->first = NULL; batch->last = NULL; batch->current = NULL; batch->size = 0; batch->initialized = 0; batch->tv_string = &PL_sv_undef; batch->postings = (AV*)&PL_sv_undef; return batch;}voidKino_TokenBatch_destroy(TokenBatch *batch) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -