Repository navigation
Expand file tree
/
Copy pathsphinxjson.h
More file actions
143 lines (117 loc) · 4.03 KB
/
Copy pathsphinxjson.h
File metadata and controls
143 lines (117 loc) · 4.03 KB
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
//
// $Id$
//
//
// Copyright (c) 2011-2016, Andrew Aksyonoff
// Copyright (c) 2011-2016, Sphinx Technologies Inc
// All rights reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License. You should have
// received a copy of the GPL license along with this program; if you
// did not, you can find it at http://www.gnu.org/
//
#ifndef _sphinxjson_
#define _sphinxjson_
#include "sphinx.h"
/// supported JSON value types
enum ESphJsonType
{
JSON_EOF = 0,
JSON_INT32 = 1,
JSON_INT64 = 2,
JSON_DOUBLE = 3,
JSON_STRING = 4,
JSON_STRING_VECTOR = 5,
JSON_INT32_VECTOR = 6,
JSON_INT64_VECTOR = 7,
JSON_DOUBLE_VECTOR = 8,
JSON_MIXED_VECTOR = 9,
JSON_OBJECT = 10,
JSON_TRUE = 11,
JSON_FALSE = 12,
JSON_NULL = 13,
JSON_ROOT = 14,
JSON_TOTAL
};
/// get stored value from SphinxBSON blob
inline int sphJsonLoadInt ( const BYTE ** pp )
{
DWORD uRes = sphGetDword(*pp);
*pp += 4;
return int(uRes);
}
/// get stored value from SphinxBSON blob
inline int64_t sphJsonLoadBigint ( const BYTE ** pp )
{
uint64_t uRes = (DWORD)sphJsonLoadInt ( pp );
uRes += ( uint64_t ( sphJsonLoadInt ( pp ) )<<32 );
return uRes;
}
/// unpack value from SphinxBSON blob
/// 0..251 == value itself
/// 252 == 2 more bytes
/// 253 == 3 more bytes
/// 254 == 4 more bytes
/// 255 == reserved
inline DWORD sphJsonUnpackInt ( const BYTE ** pp )
{
const BYTE * p = *pp;
DWORD uRes = p[0];
switch ( p[0] )
{
default:
(*pp) += 1;
break;
case 252:
uRes = p[1] + ( p[2]<<8 );
(*pp) += 3;
break;
case 253:
uRes = p[1] + ( p[2]<<8 ) + ( p[3]<<16 );
(*pp) += 4;
break;
case 254:
uRes = p[1] + ( p[2]<<8 ) + ( p[3]<<16 ) + ( p[4]<<24 );
(*pp) += 5;
break;
case 255:
assert ( 0 && "unexpected reserved length code 255" );
(*pp) += 1;
break;
}
return uRes;
}
/// parse JSON, convert it into SphinxBSON blob
bool sphJsonParse ( CSphVector<BYTE> & dData, char * sData, bool bAutoconv, bool bToLowercase, CSphString & sError );
/// convert SphinxBSON blob back to JSON document
void sphJsonFormat ( CSphVector<BYTE> & dOut, const BYTE * pData );
/// convert SphinxBSON blob back to JSON document
/// NOTE, bQuoteString==false is intended to format individual values only (and avoid quoting string values in that case)
const BYTE * sphJsonFieldFormat ( CSphVector<BYTE> & dOut, const BYTE * pData, ESphJsonType eType, bool bQuoteString=true );
/// compute key mask (for Bloom filtering) from the key name
DWORD sphJsonKeyMask ( const char * sKey, int iLen );
/// find first value in SphinxBSON blob, return associated type
ESphJsonType sphJsonFindFirst ( const BYTE ** ppData );
/// find value by key in SphinxBSON blob, return associated type
ESphJsonType sphJsonFindByKey ( ESphJsonType eType, const BYTE ** ppValue, const void * pKey, int iLen, DWORD uMask );
/// find value by index in SphinxBSON blob, return associated type
ESphJsonType sphJsonFindByIndex ( ESphJsonType eType, const BYTE ** ppValue, int iIndex );
/// split name to object and key parts, return false if not JSON name
bool sphJsonNameSplit ( const char * sName, CSphString * sColumn, CSphString * sKey );
/// compute node size, in bytes
/// returns -1 when data itself is required to compute the size, but pData is NULL
int sphJsonNodeSize ( ESphJsonType eType, const BYTE * pData );
/// skip the current node, and update the pointer
void sphJsonSkipNode ( ESphJsonType eType, const BYTE ** ppData );
/// return object length or array length, in elements
/// POD types return 1, empty objects return 0
int sphJsonFieldLength ( ESphJsonType eType, const BYTE * pData );
/// inplace JSON update, both for realtime and non-realtime indexes, returns true if update is possible
bool sphJsonInplaceUpdate ( ESphJsonType eValueType, int64_t iValue, ISphExpr * pExpr, BYTE * pStrings, const CSphRowitem * pRow, bool bUpdate );
/// converts string to number
bool sphJsonStringToNumber ( const char * s, int iLen, ESphJsonType & eType, int64_t & iVal, double & fVal );
#endif // _sphinxjson_
//
// $Id$
//