This is an example of collection use written in Python.
1import sys
2import struct
3import math
4import os
5import platform
6import json
7
8if (platform.system() == "Windows"):
9 sys.path.append("C:\\Program Files\\Actian\\Zen\\bin")
10else:
11 sys.path.append("/usr/local/actianzen/lib64")
12
13import btrievePython
14
15min_x = 0
16max_x = 255
17btrieveCollectionName = "squaresAndSquareRoots.btr"
18
19
20if (len(sys.argv) != 2):
21 sys.exit("Usage: " + os.path.basename(sys.argv[0]) + " uint8_value")
22
23integerValue = int(sys.argv[1])
24
25
26if (integerValue < min_x) or (integerValue > max_x):
27 sys.exit("Usage: " + os.path.basename(sys.argv[0]) + " uint8_value")
28
29key = sys.argv[1]
30
31btrieveClient = btrievePython.BtrieveClient()
32assert(btrieveClient != None)
33
34
35rc = btrieveClient.CollectionCreate(btrieveCollectionName)
36assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::CollectionCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
37
38
39
40btrieveCollection = btrievePython.BtrieveCollection()
41assert(btrieveCollection != None)
42
43
44
45rc = btrieveClient.CollectionOpen(btrieveCollection, btrieveCollectionName)
46assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::CollectionOpen():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
47
48
49
50for i in range(min_x, max_x + 1):
51 iSquared = i * i
52 iSquareRoot = math.sqrt(i)
53 document = {}
54 document["x"] = i
55 document["xSquared"] = iSquared
56 document["xSquareRoot"] = iSquareRoot
57 document["text"] = "The square of %d is %d and its square root is %f." % (i, iSquared, iSquareRoot)
58 documentJson = json.dumps(document)
59
60
61 rc = btrieveCollection.DocumentCreate(documentJson)
62 assert(rc >= 0), "Error: BtrieveCollection::DocumentCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveCollection.GetLastStatusCode()))
63
64
65
66btrieveDocumentSet = btrievePython.BtrieveDocumentSet()
67assert(btrieveDocumentSet != None)
68
69
70query = '{ "x": ' + key + '}'
71
72
73rc = btrieveCollection.Query(btrieveDocumentSet, query)
74assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveCollection::Query():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
75
76
77
78rc = btrieveDocumentSet.Size()
79assert(rc >= 0), "Error: BtrieveDocumentSet::Size():%d:%s\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveDocumentSet.GetLastStatusCode()))
80
81
82assert(rc == 1), "Error: BtrieveDocumentSet::Size():%d:%s\n" % (rc, "Unexpected size. Size should be one.")
83
84
85rc = btrieveDocumentSet.PopFirst()
86assert(rc >= 0), "Error: BtrieveDocumentSet::Size():%d:%s\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveDocumentSet.GetLastStatusCode()))
87
88
89
90rc = btrieveCollection.DocumentRetrieveJson(rc)
91assert(rc != None), "Error: BtrieveCollection::DocumentRetrieveJson():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveCollection.GetLastStatusCode()))
92
93
94print ("json:", rc)
95
96
97rc = btrieveClient.CollectionClose(btrieveCollection)
98assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::CollectionClose():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
99
100
101
102rc = btrieveClient.CollectionDelete(btrieveCollectionName)
103assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::CollectionDelete():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
104