Skip to content

Commit 094904d

Browse files
committed
adds testCities
1 parent bff6965 commit 094904d

File tree

3 files changed

+99
-9
lines changed

3 files changed

+99
-9
lines changed

dg/dgraph.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@
55
'''
66
import pydgraph
77
import json
8+
import time
89

910
class Dgraph(object):
1011
'''
1112
wrapper for https://dgraph.io/ GraphQL database
1213
'''
1314

14-
def __init__(self, host='localhost',port=9080, debug=False):
15+
def __init__(self, host='localhost',port=9080, debug=False,profile=False):
1516
'''
1617
Constructor
1718
'''
1819
self.host=host
1920
self.port=port
2021

2122
self.debug=debug
23+
self.profile=profile
2224
self.client_stub = pydgraph.DgraphClientStub('%s:%d' % (host,port))
2325
self.client =pydgraph.DgraphClient(self.client_stub)
2426

@@ -29,32 +31,57 @@ def addSchema(self,schema):
2931
result=self.client.alter(pydgraph.Operation(schema=schema))
3032
return result
3133

32-
def addData(self,obj=None,nquads=None):
34+
def addData(self,obj=None,nquads=None,limit=None, batchSize=None):
3335
'''
34-
add the given object or nquads if both are given nquads are ignored
36+
add the given object/list of objects or nquads if both are given nquads are ignored
3537
'''
38+
itemList=obj
39+
if itemList is None:
40+
self.addDataTxn(obj=None,nquads=nquads,itemTitle="nquads")
41+
else:
42+
if type(itemList) is list:
43+
if limit is not None:
44+
itemList=itemList[:limit]
45+
if batchSize is None:
46+
self.addDataTxn(obj=itemList)
47+
else:
48+
startTime=time.time()
49+
# store the list in batches
50+
for i in range(0, len(itemList), batchSize):
51+
itemBatch=itemList[i:i+batchSize]
52+
self.addDataTxn(obj=itemBatch, title="batch")
53+
print("addData for %9d items in %6.1f secs" % (len(itemList),time.time()-startTime))
54+
55+
56+
def addDataTxn(self,obj=None,nquads=None,title="addData",itemTitle="items"):
3657
response=None
3758
# Create a new transaction.
3859
txn = self.client.txn()
3960

4061
try:
62+
itemList=obj
63+
size=1
64+
startTime=time.time()
4165
# Run mutation.
42-
if obj is not None:
66+
if itemList is not None:
4367
# check whether obj is a list of items
4468
# if do a mutation for every item in the list
45-
if type(obj) is list:
46-
for item in obj:
69+
if type(itemList) is list:
70+
size=len(itemList)
71+
for item in itemList:
4772
txn.mutate(set_obj=item)
4873
else:
4974
# single object
5075
response = txn.mutate(set_obj=obj)
76+
if self.profile:
77+
print("%7s for %9d %s in %6.1f secs" % (title,size,itemTitle,time.time()-startTime))
5178
if nquads is not None:
5279
response = txn.mutate(set_nquads=nquads)
5380
# Commit transaction.
5481
txn.commit()
5582
finally:
5683
if self.debug:
57-
print(obj)
84+
print(itemList)
5885
# Clean up. Calling this after txn.commit() is a no-op and hence safe.
5986
txn.discard()
6087
return response

scripts/dgraph

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,13 @@ error() {
6262
# show usage
6363
#
6464
usage() {
65-
echo "$0 [-b|--bash|-h|--help|-k|--kill|-p|--pull]"
65+
echo "$0 [-b|--bash|-c|--clean|-h|--help|-k|--kill|-p|--pull]"
6666
echo ""
6767
echo "-b | --bash: start a bash terminal shell within the currently running container"
6868
echo "-h | --help: show this usage"
6969
echo "-k | --kill: stop the docker image"
7070
echo "-p | --pull: pull the docker image"
71+
echo "-c | --clean: clean start with kill and purge of all data"
7172
exit 1
7273
}
7374

@@ -112,7 +113,7 @@ zero() {
112113
# dgraph alpha
113114
#
114115
alpha() {
115-
$docker exec $it dgraph dgraph alpha --lru_mb 2048 --zero localhost:5080 --whitelist 0.0.0.0/0
116+
$docker exec $it dgraph dgraph alpha --lru_mb 4096 --zero localhost:5080 --whitelist 0.0.0.0/0
116117
}
117118

118119
#
@@ -181,6 +182,16 @@ do
181182
alpha) alpha;;
182183
ratel) ratel;;
183184
zero) zero;;
185+
-c|--clean)
186+
stopImage
187+
if [ -d ~/dgraph ]
188+
then
189+
color_msg $blue "removing content from ~/dgraph"
190+
rm -rf ~/dgraph/*
191+
fi
192+
# call me with no parameters to start terminals
193+
$me
194+
;;
184195
-p|--pull) pullImage;;
185196
-b|--bash) bashInto;;
186197
-k|--kill) stopImage;;

tests/testDgraph.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,58 @@ def setUp(self):
1919

2020
def tearDown(self):
2121
pass
22+
23+
def testCities(self):
24+
'''
25+
test a list of cities
26+
'''
27+
cityJsonUrl="https://raw.githubusercontent.com/lutangar/cities.json/master/cities.json"
28+
with urllib.request.urlopen(cityJsonUrl) as url:
29+
cityList=json.loads(url.read().decode())
30+
self.assertEqual(128769,(len(cityList)))
31+
cityIter=iter(cityList)
32+
limit=2000
33+
for i in range(limit):
34+
city=next(cityIter)
35+
city['dgraph.type']='City'
36+
lat=city['lat']
37+
lng=city['lng']
38+
#city['location']={'type': 'Point', 'coordinates': [lng,lat] }
39+
#print("%d: %s" % (i,city))
40+
dgraph=Dgraph(profile=True)
41+
dgraph.drop_all()
42+
schema='''
43+
name: string @index(exact) .
44+
country: string .
45+
lat: float .
46+
lng: float .
47+
location: geo .
48+
type City {
49+
name
50+
country
51+
lat
52+
lng
53+
location
54+
}'''
55+
dgraph.addSchema(schema)
56+
dgraph.addData(obj=cityList,limit=limit,batchSize=250)
57+
query='''{
58+
# get cities
59+
cities(func: has(name)) {
60+
country
61+
name
62+
lat
63+
lng
64+
location
65+
}
66+
}
67+
'''
68+
queryResult=dgraph.query(query)
69+
self.assertTrue('cities' in queryResult)
70+
qCityList=queryResult['cities']
71+
self.assertEqual(limit,qCityList)
72+
dgraph.close()
73+
2274

2375
def testCountries(self):
2476
'''

0 commit comments

Comments
 (0)