1+ NEURODB_RETURNDATA = 1
2+ NEURODB_SELECTDB = 2
3+ NEURODB_EOF = 3
4+ NEURODB_NODES = 6
5+ NEURODB_LINKS = 7
6+ NEURODB_EXIST = 17
7+ NEURODB_NIL = 18
8+ NEURODB_RECORD = 19
9+ NEURODB_RECORDS = 20
10+
11+ NDB_6BITLEN = 0
12+ NDB_14BITLEN = 1
13+ NDB_32BITLEN = 2
14+ NDB_ENCVAL = 3
15+ # NDB_LENERR =UINT_MAX
16+
17+ VO_STRING = 1
18+ VO_NUM = 2
19+ VO_STRING_ARRY = 3
20+ VO_NUM_ARRY = 4
21+ VO_NODE = 5
22+ VO_LINK = 6
23+ VO_PATH = 7
24+ VO_VAR = 8
25+ VO_VAR_PATTERN = 9
26+
27+ class Node (object ):
28+ def __init__ (self ,id ,labels ,properties ):
29+ self .id = id
30+ self .labels = labels
31+ self .properties = properties
32+
33+ class Link (object ):
34+ def __init__ (self ,id ,startNodeId ,endNodeId ,type ,properties ):
35+ self .id = id
36+ self .startNodeId = startNodeId
37+ self .endNodeId = endNodeId
38+ self .type = type
39+ self .properties = properties
40+
41+ class ColVal (object ):
42+
43+ type = 0
44+ val = None
45+ aryLen = 0
46+
47+ def getNum (self ):
48+ return float (self .val )
49+ def getNumArray (self ):
50+ return list (self .val )
51+ def getString (self ):
52+ return str (self .val )
53+ def getStringArry (self ):
54+ return list (self .val )
55+ def getNode (self ):
56+ self .val .__class__ = Node
57+ return self .val
58+ def getLink (self ):
59+ self .val .__class__ = Link
60+ return self .val
61+ def getPath (self ):
62+ return list (self .val )
63+
64+ class RecordSet (object ):
65+
66+ labels = []
67+ types = []
68+ keyNames = []
69+ nodes = []
70+ links = []
71+ records = [[]]
72+
73+ class ResultSet (object ):
74+
75+ status = 0
76+ cursor = 0
77+ results = 0
78+ addNodes = 0
79+ addLinks = 0
80+ modifyNodes = 0
81+ modifyLinks = 0
82+ deleteNodes = 0
83+ deleteLinks = 0
84+ msg = None
85+ recordSet = None
86+
87+ class StringCur (object ):
88+
89+ cur = 0
90+ s = None
91+
92+ def __init__ (self ,bts ):
93+ self .s = bts
94+
95+ def get (self ,size ):
96+ bts = self .s [self .cur :self .cur + size ]
97+ str = bts .decode ('utf-8' )
98+ self .cur = self .cur + size
99+ return str
100+
101+ def getType (self ):
102+ type = str (self .s [self .cur ])
103+ self .cur = self .cur + 1
104+ return type
105+
106+ import socket
107+
108+ class NeuroDBDriver (object ):
109+ client = None
110+ def __init__ (self ,ip ,port ):
111+ self .client = socket .socket ()
112+ self .client .connect ((ip , port ))
113+
114+ def clase ():
115+ self .client .close ()
116+
117+ def executeQuery (self ,query ):
118+ self .client .send (query .encode ("utf-8" ))
119+ resultSet = ResultSet ()
120+ bts = self .client .recv (1 )
121+ btsstr = str (bts , 'UTF-8' )
122+ type = btsstr [0 ]
123+ if type == '@' :
124+ resultSet .status = 1
125+ elif type == '$' :
126+ resultSet .msg = readLine (self .client )
127+ elif type == '#' :
128+ resultSet .msg = readLine (self .client )
129+ elif type == '*' :
130+ line = readLine (self .client )
131+ head = line .split (',' )
132+ resultSet .status = int (head [0 ])
133+ resultSet .cursor = int (head [1 ])
134+ resultSet .results = int (head [2 ])
135+ resultSet .addNodes = int (head [3 ])
136+ resultSet .addLinks = int (head [4 ])
137+ resultSet .modifyNodes = int (head [5 ])
138+ resultSet .modifyLinks = int (head [6 ])
139+ resultSet .deleteNodes = int (head [7 ])
140+ resultSet .deleteLinks = int (head [8 ])
141+
142+ bodyLen = int (head [9 ])
143+ body = self .client .recv (bodyLen )
144+ readLine (self .client )
145+ recordSet = self .deserializeReturnData (body )
146+ resultSet .recordSet = recordSet
147+ else :
148+ raise Exception ("reply type erro" )
149+ return resultSet
150+
151+
152+ def deserializeType (self ,cur ):
153+ return int (cur .getType ())
154+
155+ def deserializeUint (self ,cur ):
156+ buf = [0 ,0 ,0 ]
157+ buf [0 ] = int .from_bytes (cur .get (1 ).encode ('utf-8' ) ,'little' )
158+ buf [1 ] = int .from_bytes (cur .get (1 ).encode ('utf-8' ) ,'little' )
159+ buf [2 ] = int .from_bytes (cur .get (1 ).encode ('utf-8' ) ,'little' )
160+ return (buf [0 ]& 0x7f )<< 14 | (buf [1 ]& 0x7f )<< 7 | buf [2 ]
161+
162+ def deserializeString (self ,cur ):
163+ len = self .deserializeUint (cur )
164+ val = cur .get (len )
165+ return val
166+
167+ def deserializeStringList (self ,cur ):
168+ listlen = self .deserializeUint (cur )
169+ l = []
170+ while listlen > 0 :
171+ s = self .deserializeString (cur )
172+ l .append (s )
173+ listlen = listlen - 1
174+ return l
175+
176+ def deserializeLabels (self ,cur ,labeList ):
177+ listlen = self .deserializeUint (cur )
178+ l = []
179+ while listlen > 0 :
180+ i = self .deserializeUint (cur )
181+ l .append (labeList [i ])
182+ listlen = listlen - 1
183+ return l
184+
185+
186+ def deserializeKVList (self ,cur ,keyNames ):
187+ listlen = self .deserializeUint (cur )
188+ properties = {}
189+ while listlen > 0 :
190+ i = self .deserializeUint (cur )
191+ key = keyNames [i ]
192+ type = self .deserializeUint (cur )
193+ aryLen = 0
194+ val = ColVal ()
195+ val .type = type
196+ if type == VO_STRING :
197+ val .val = self .deserializeString (cur )
198+ elif type == VO_NUM :
199+ doubleStr = self .deserializeString (cur )
200+ val .val = float (doubleStr )
201+ elif type == VO_STRING_ARRY :
202+ aryLen = self .deserializeUint (cur )
203+ valAry = []
204+ for i in range (0 ,aryLen ):
205+ valAry [i ] = self .deserializeString (cur )
206+ val .val = valAry
207+ elif type == VO_NUM_ARRY :
208+ aryLen = self .deserializeUint (cur )
209+ valAry = []
210+ for i in range (0 ,aryLen ):
211+ doubleStr = self .deserializeString (cur )
212+ valAry [i ] = float (doubleStr )
213+ val .val = valAry
214+ else :
215+ raise Exception ("Error Type" )
216+ properties [key ] = val
217+ listlen = listlen - 1
218+
219+ return properties
220+
221+
222+ def deserializeCNode (self ,cur , labels , keyNames ):
223+ id = self .deserializeUint (cur )
224+ nlabels = self .deserializeLabels (cur , labels )
225+ kvs = self .deserializeKVList (cur , keyNames )
226+ n = Node (id , nlabels , kvs )
227+ return n
228+
229+ def deserializeCLink (self ,cur , types , keyNames ):
230+ id = self .deserializeUint (cur )
231+ hid = self .deserializeUint (cur )
232+ tid = self .deserializeUint (cur )
233+ ty = self .deserializeType (cur )
234+ if ty == NEURODB_EXIST :
235+ typeIndex = self .deserializeUint (cur )
236+ type = types [typeIndex ]
237+ #elif ty == NEURODB_NIL:
238+
239+ kvs = self .deserializeKVList (cur , keyNames )
240+ l = Link (id , hid , tid , type , kvs )
241+ return l
242+
243+
244+ def getNodeById (self ,nodes ,id ):
245+ for node in nodes :
246+ if node .id == id :
247+ return node
248+ return None
249+
250+ def getLinkById (self ,links , id ):
251+ for link in links :
252+ if link .id == id :
253+ return link
254+ return null
255+
256+
257+ def deserializeReturnData ( self ,body ):
258+ cur = StringCur (body )
259+ rd = RecordSet ()
260+ path = None
261+ #/*读取labels、types、keyNames列表*/
262+ if self .deserializeType (cur ) != NEURODB_RETURNDATA :
263+ raise Exception ("Error Type" )
264+ rd .labels = self .deserializeStringList (cur )
265+ rd .types = self .deserializeStringList (cur )
266+ rd .keyNames = self .deserializeStringList (cur )
267+ #/*读取节点列表*/
268+ if self .deserializeType (cur ) != NEURODB_NODES :
269+ raise Exception ("Error Type" )
270+ cnt_nodes = self .deserializeUint (cur )
271+ for i in range (0 ,cnt_nodes ):
272+ n = self .deserializeCNode (cur , rd .labels , rd .keyNames )
273+ rd .nodes .append (n )
274+ #/*读取关系列表*/
275+ if self .deserializeType (cur ) != NEURODB_LINKS :
276+ raise Exception ("Error Type" )
277+ cnt_links = self .deserializeUint (cur )
278+ for i in range (0 , cnt_links ):
279+ l = self .deserializeCLink (cur , rd .getTypes (), rd .keyNames )
280+ rd .links .append (l )
281+ #/*读取return结果集列表*/
282+ if self .deserializeType (cur ) != NEURODB_RECORDS :
283+ raise Exception ("Error Type" )
284+ cnt_records = self .deserializeUint (cur )
285+ for i in range (0 ,cnt_records ):
286+ if self .deserializeType (cur ) != NEURODB_RECORD :
287+ raise Exception ("Error Type" )
288+ cnt_column = self .deserializeUint (cur )
289+ record = []
290+ for i in range (0 ,cnt_column ):
291+ aryLen = 0
292+ type = self .deserializeType (cur )
293+ val = ColVal ()
294+ val .type = type
295+ #if type == NEURODB_NIL:
296+ #/*val =NULL*/
297+ #} else
298+ if type == VO_NODE :
299+ id = self .deserializeUint (cur )
300+ n = self .getNodeById (rd .nodes , id )
301+ val .val = n
302+ elif type == VO_LINK :
303+ id = self .deserializeUint (cur )
304+ l = self .getLinkById (rd .getLinks (), id )
305+ val .val = l
306+ elif type == VO_PATH :
307+ len = self .deserializeUint (cur )
308+ path = []
309+ for i in range (0 ,len ):
310+ id = self .deserializeUint (cur )
311+ if i % 2 == 0 :
312+ nd = self .getNodeById (rd .getNodes (), id )
313+ path .append (nd )
314+ else :
315+ lk = self .getLinkById (rd .getLinks (), id )
316+ path .append (lk )
317+ val .val = path
318+ elif type == VO_STRING :
319+ val .val = self .deserializeString (cur )
320+ elif type == VO_NUM :
321+ doubleStr = self .deserializeString (cur )
322+ val .val = float (doubleStr )
323+ elif type == VO_STRING_ARRY :
324+ aryLen = self .deserializeUint (cur )
325+ valAry = []
326+ for i in range (0 ,aryLen ):
327+ valAry [i ] = self .deserializeString (cur )
328+ val .val = valAry
329+ elif type == VO_NUM_ARRY :
330+ aryLen = self .deserializeUint (cur )
331+ valAry = []
332+ for i in range (0 ,aryLen ):
333+ doubleStr = self .deserializeString (cur )
334+ valAry [i ] = float (doubleStr )
335+ val .val = valAry
336+ else :
337+ raise Exception ("Error Type" )
338+ record .append (val )
339+ rd .records .append (record )
340+ #/*读取结束标志*/
341+ if self .deserializeType (cur ) != NEURODB_EOF :
342+ raise Exception ("Error Type" )
343+ return rd
344+
345+
346+ def readLine (client ):
347+ sb = ""
348+ bts = client .recv (1 )
349+ btsstr = str (bts , 'UTF-8' )
350+ c = btsstr [0 ]
351+ while c != None :
352+ sb = sb + c
353+ if c == '\n ' :
354+ break
355+ bts = client .recv (1 )
356+ btsstr = str (bts , 'UTF-8' )
357+ c = btsstr [0 ]
358+ return sb .replace ("\r \n " ,"" )
359+
360+
361+ driver = NeuroDBDriver ("127.0.0.1" ,8839 )
362+ result = driver .executeQuery ("match (n) return n" )
363+ print ("ok" )
0 commit comments