github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/matn/text-index-find.go (about) 1 /* For license and copyright information please see LEGAL file in repository */ 2 3 package matn 4 5 import ( 6 er "../error" 7 "../json" 8 "../syllab" 9 ) 10 11 // IndexTextFindReq is request structure of IndexTextFind() 12 type IndexTextFindReq struct { 13 Term string 14 RecordStructure uint64 15 PageNumber uint64 16 } 17 18 // IndexTextFindRes is response structure of IndexTextFind() 19 type IndexTextFindRes struct { 20 Tokens [10]PhraseToken 21 } 22 23 // IndexTextFind return index data of given terms if any exist 24 func IndexTextFind(req *IndexTextFindReq) (res *IndexTextFindRes, err protocol.Error) { 25 // TODO::: now just one word work!!! 26 var w = &IndexWord{ 27 Word: req.Term, 28 RecordStructure: req.RecordStructure, 29 } 30 var phraseTokens []PhraseToken 31 phraseTokens, err = w.FindByWordRecordStructure(req.PageNumber*10, 10) 32 33 res = &IndexTextFindRes{ 34 // Tokens: *(*[10]PhraseToken)(unsafe.Pointer(&phraseTokens)), 35 } 36 // TODO::: fix to use unsafe 37 for i := 0; i < len(phraseTokens); i++ { 38 res.Tokens[i] = phraseTokens[i] 39 } 40 return 41 } 42 43 /* 44 Request Encoders & Decoders 45 */ 46 47 // FromSyllab decode syllab to given IndexTextFindReq 48 func (req *IndexTextFindReq) FromSyllab(payload []byte, stackIndex uint32) { 49 if uint32(len(buf)) < req.LenOfSyllabStack() { 50 err = syllab.ErrShortArrayDecode 51 return 52 } 53 54 req.Term = syllab.UnsafeGetString(buf, 0) 55 req.RecordStructure = syllab.GetUInt64(buf, 8) 56 req.PageNumber = syllab.GetUInt64(buf, 16) 57 return 58 } 59 60 // ToSyllab encode given IndexTextFindReq to syllab format 61 func (req *IndexTextFindReq) ToSyllab(payload []byte, stackIndex, heapIndex uint32) (freeHeapIndex uint32) 62 var hi uint32 = req.LenOfSyllabStack() // Heap index || Stack size! 63 64 hi = syllab.SetString(buf, req.Term, 0, hi) 65 syllab.SetUInt64(buf, 8, req.RecordStructure) 66 syllab.SetUInt64(buf, 16, req.PageNumber) 67 return 68 } 69 70 // LenOfSyllabStack return stack length of IndexTextFindReq 71 func (req *IndexTextFindReq) LenOfSyllabStack() uint32 { 72 return 24 73 } 74 75 // LenOfSyllabHeap return heap length of IndexTextFindReq 76 func (req *IndexTextFindReq) LenOfSyllabHeap() (ln uint32) { 77 ln += uint32(len(req.Term)) 78 return 79 } 80 81 // LenAsSyllab return whole length of IndexTextFindReq 82 func (req *IndexTextFindReq) LenAsSyllab() uint64 { 83 return uint64(req.LenOfSyllabStack() + req.LenOfSyllabHeap()) 84 } 85 86 // JSONDecoder decode json to given IndexTextFindReq 87 func (req *IndexTextFindReq) FromJSON(payload []byte) (err protocol.Error) { 88 var decoder = json.DecoderUnsafeMinified{Buf: payload} 89 for err == nil { 90 var keyName = decoder.DecodeKey() 91 switch keyName { 92 case "Term": 93 req.Term, err = decoder.DecodeString() 94 case "RecordStructure": 95 req.RecordStructure, err = decoder.DecodeUInt64() 96 case "PageNumber": 97 req.PageNumber, err = decoder.DecodeUInt64() 98 default: 99 err = decoder.NotFoundKeyStrict() 100 } 101 102 if decoder.End() { 103 return 104 } 105 } 106 return 107 } 108 109 func (req *IndexTextFindReq) ToJSON(payload []byte) []byte { 110 var encoder = json.Encoder{Buf: payload} 111 encoder.EncodeString(`{"Term":"`) 112 encoder.EncodeString(req.Term) 113 encoder.EncodeString(`","RecordStructure":`) 114 encoder.EncodeUInt64(req.RecordStructure) 115 encoder.EncodeString(`,"PageNumber":`) 116 encoder.EncodeUInt64(req.PageNumber) 117 encoder.EncodeByte('}') 118 return encoder.Buf 119 } 120 121 // JSONLen return json needed len to encode! 122 func (req *IndexTextFindReq) JSONLen() (ln int) { 123 ln = len(req.Term) 124 ln += 84 125 return 126 } 127 128 /* 129 Response Encoders & Decoders 130 */ 131 132 // FromSyllab decode syllab to given IndexTextFindRes 133 func (res *IndexTextFindRes) FromSyllab(payload []byte, stackIndex uint32) { 134 if uint32(len(buf)) < res.LenOfSyllabStack() { 135 err = syllab.ErrShortArrayDecode 136 return 137 } 138 139 for i := 0; i < 10; i++ { 140 res.Tokens[i].FromSyllab(buf, uint32(i)*res.Tokens[i].LenOfSyllabStack()) 141 } 142 return 143 } 144 145 // ToSyllab encode given IndexTextFindRes to syllab format 146 func (res *IndexTextFindRes) ToSyllab(payload []byte, stackIndex, heapIndex uint32) (freeHeapIndex uint32) 147 for i := 0; i < 10; i++ { 148 res.Tokens[i].ToSyllab(buf, uint32(i)*res.Tokens[i].LenOfSyllabStack()) 149 } 150 return 151 } 152 153 // LenOfSyllabStack return stack length of IndexTextFindRes 154 func (res *IndexTextFindRes) LenOfSyllabStack() uint32 { 155 ln = 10 * res.Tokens[0].LenOfSyllabStack() 156 return 157 } 158 159 // LenOfSyllabHeap return heap length of IndexTextFindRes 160 func (res *IndexTextFindRes) LenOfSyllabHeap() (ln uint32) { 161 // ln += 10 * res.Tokens[0].LenOfSyllabHeap() 162 return 163 } 164 165 // LenAsSyllab return whole length of IndexTextFindRes 166 func (res *IndexTextFindRes) LenAsSyllab() uint64 { 167 return uint64(res.LenOfSyllabStack() + res.LenOfSyllabHeap()) 168 } 169 170 // JSONDecoder decode json to given IndexTextFindRes 171 func (res *IndexTextFindRes) FromJSON(payload []byte) (err protocol.Error) { 172 var decoder = json.DecoderUnsafeMinified{Buf: payload} 173 for err == nil { 174 var keyName = decoder.DecodeKey() 175 switch keyName { 176 case "Tokens": 177 for i := 0; i < 10; i++ { 178 err = res.Tokens[i].jsonDecoder(&decoder) 179 } 180 default: 181 err = decoder.NotFoundKeyStrict() 182 } 183 184 if decoder.End() { 185 return 186 } 187 } 188 return 189 } 190 191 func (res *IndexTextFindRes) ToJSON(payload []byte) []byte { 192 var encoder = json.Encoder{Buf: payload} 193 194 var encoder = json.Encoder{ 195 Buf: make([]byte, 0, res.JSONLen()), 196 } 197 198 encoder.EncodeString(`{"Tokens":[`) 199 for i := 0; i < 10; i++ { 200 res.Tokens[i].ToJSON(&encoder) 201 encoder.EncodeByte(',') 202 } 203 encoder.RemoveTrailingComma() 204 205 encoder.EncodeString(`]}`) 206 } 207 208 // JSONLen return json needed len to encode! 209 func (res *IndexTextFindRes) JSONLen() (ln int) { 210 ln = 10 * res.Tokens[0].LenAsJSON() 211 ln += 13 212 return 213 }