| 1 |
|
%% Copyright (c) 2022 Peter Morgan <peter.james.morgan@gmail.com> |
| 2 |
|
%% |
| 3 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
|
%% you may not use this file except in compliance with the License. |
| 5 |
|
%% You may obtain a copy of the License at |
| 6 |
|
%% |
| 7 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
|
%% |
| 9 |
|
%% Unless required by applicable law or agreed to in writing, software |
| 10 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
|
%% See the License for the specific language governing permissions and |
| 13 |
|
%% limitations under the License. |
| 14 |
|
|
| 15 |
|
|
| 16 |
|
-module(pgmp_tsquery). |
| 17 |
|
|
| 18 |
|
-export([decode/1]). |
| 19 |
|
-export([encode/1]). |
| 20 |
|
|
| 21 |
|
|
| 22 |
|
-define(QI_VAL, 1). |
| 23 |
|
-define(QI_OPR, 2). |
| 24 |
|
|
| 25 |
|
-define(OP_NOT, 1). |
| 26 |
|
-define(OP_AND, 2). |
| 27 |
|
-define(OP_OR, 3). |
| 28 |
|
-define(OP_PHRASE, 4). |
| 29 |
|
|
| 30 |
|
|
| 31 |
|
decode(<<N:32, Encoded/bytes>>) -> |
| 32 |
1 |
{<<>>, Decoded} = pgmp_binary:repeat( |
| 33 |
|
N, |
| 34 |
|
Encoded, |
| 35 |
|
fun items/2, |
| 36 |
|
[]), |
| 37 |
1 |
lists:reverse(Decoded). |
| 38 |
|
|
| 39 |
|
items(<<?QI_VAL:8, Weight:8, Prefix:8, Encoded/bytes>>, A) -> |
| 40 |
2 |
[Operand, Remainder] = binary:split(Encoded, <<0>>), |
| 41 |
2 |
{Remainder, |
| 42 |
|
[#{weight => Weight, |
| 43 |
|
prefix => prefix(Prefix), |
| 44 |
|
operand => Operand} | A]}; |
| 45 |
|
|
| 46 |
|
items(<<?QI_OPR:8, ?OP_NOT:8, Remainder/bytes>>, A) -> |
| 47 |
:-( |
{Remainder, ['not' | A]}; |
| 48 |
|
|
| 49 |
|
items(<<?QI_OPR:8, ?OP_AND:8, Remainder/bytes>>, A) -> |
| 50 |
1 |
{Remainder, ['and' | A]}; |
| 51 |
|
|
| 52 |
|
items(<<?QI_OPR:8, ?OP_OR:8, Remainder/bytes>>, A) -> |
| 53 |
:-( |
{Remainder, ['or' | A]}; |
| 54 |
|
|
| 55 |
|
items(<<?QI_OPR:8, ?OP_PHRASE:8, Distance:16, Remainder/bytes>>, A) -> |
| 56 |
:-( |
{Remainder, [{phrase, Distance} | A]}. |
| 57 |
|
|
| 58 |
|
prefix(0) -> |
| 59 |
2 |
false; |
| 60 |
|
prefix(1) -> |
| 61 |
:-( |
true; |
| 62 |
|
prefix(false) -> |
| 63 |
2 |
0; |
| 64 |
|
prefix(true) -> |
| 65 |
:-( |
1. |
| 66 |
|
|
| 67 |
|
|
| 68 |
|
encode(L) -> |
| 69 |
1 |
[<<(length(L)):32>>, |
| 70 |
|
lists:map( |
| 71 |
|
fun |
| 72 |
|
(#{weight := Weight, prefix := Prefix, operand := Operand}) -> |
| 73 |
2 |
<<?QI_VAL:8, |
| 74 |
|
Weight:8, |
| 75 |
|
(prefix(Prefix)):8, |
| 76 |
|
Operand/bytes, |
| 77 |
|
0:8>>; |
| 78 |
|
|
| 79 |
|
('not') -> |
| 80 |
:-( |
<<?QI_OPR:8, ?OP_NOT:8>>; |
| 81 |
|
|
| 82 |
|
('and') -> |
| 83 |
1 |
<<?QI_OPR:8, ?OP_AND:8>>; |
| 84 |
|
|
| 85 |
|
('or') -> |
| 86 |
:-( |
<<?QI_OPR:8, ?OP_OR:8>>; |
| 87 |
|
|
| 88 |
|
({phrase, Distance}) -> |
| 89 |
:-( |
<<?QI_OPR:8, ?OP_PHRASE:8, Distance:16>> |
| 90 |
|
end, |
| 91 |
|
L)]. |