Btrieve 2
Loading...
Searching...
No Matches
btestchunk.py

This is an example of record update and retrieve chunk based on btest.py .

1import sys
2import struct
3import math
4import os
5import platform
6
7if (platform.system() == "Windows"):
8 sys.path.append("C:\\Program Files\\Actian\\Zen\\bin")
9else:
10 sys.path.append("/usr/local/actianzen/lib64")
11
12import btrievePython
13
14min_x = 0
15max_x = 255
16btrieveFileName = "squaresAndSquareRoots.btr"
17chunkFormat = "<BHd"
18chunkLength = 11
19
20# If the incorrect number of arguments were given.
21if (len(sys.argv) != 2):
22 sys.exit("Usage: " + os.path.basename(sys.argv[0]) + " uint8_value")
23
24integerValue = int(sys.argv[1])
25
26# If integerValue is out of range.
27if (integerValue < min_x) or (integerValue > max_x):
28 sys.exit("Usage: " + os.path.basename(sys.argv[0]) + " uint8_value")
29
30btrieveClient = btrievePython.BtrieveClient()
31assert(btrieveClient != None)
32
33btrieveFileAttributes = btrievePython.BtrieveFileAttributes()
34assert(btrieveFileAttributes != None)
35
36rc = btrieveFileAttributes.SetFixedRecordLength(chunkLength)
37assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFileAttributes::SetFixedRecordLength():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
38
39rc = btrieveFileAttributes.SetVariableLengthRecordsMode(btrievePython.Btrieve.VARIABLE_LENGTH_RECORDS_MODE_YES)
40assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFileAttributes::SetVariableLengthRecordsMode():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
41
42rc = btrieveClient.FileCreate(btrieveFileAttributes, btrieveFileName, btrievePython.Btrieve.CREATE_MODE_OVERWRITE)
43assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
44
45btrieveFile = btrievePython.BtrieveFile()
46assert(btrieveFile != None)
47
48rc = btrieveClient.FileOpen(btrieveFile, btrieveFileName, None, btrievePython.Btrieve.OPEN_MODE_NORMAL)
49assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileOpen():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
50
51# For all the values of x.
52chunk = struct.pack(chunkFormat, 0, 0, 0)
53
54rc = btrieveFile.RecordCreate(chunk)
55assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFile::RecordCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
56
57for i in range(min_x, max_x + 1):
58 chunk = struct.pack(chunkFormat, i, i * i, math.sqrt(i))
59
60 if (i == min_x):
61
62 rc = btrieveFile.RecordUpdateChunk(0, chunk)
63 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFile::RecordUpdateChunk():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
64
65 else:
66
67 rc = btrieveFile.RecordUpdateChunk(chunk)
68 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFile::RecordUpdateChunk():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
69
70
71# print chunk
72chunk = struct.pack(chunkFormat, 0, 0, 0)
73
74rc = btrieveFile.RecordRetrieveFirst(btrievePython.Btrieve.INDEX_NONE, chunk)
75assert(rc == chunkLength), "Error: BtrieveFile::RecordRetrieveFirst():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
76
77for i in range(min_x, max_x + 1):
78 # If this is the first chunk.
79 if (i == min_x):
80
81 rc = btrieveFile.RecordRetrieveChunk(0, chunkLength, chunk)
82 assert(rc == chunkLength), "Error: BtrieveFile::RecordRetrieveChunk():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
83
84 else:
85
86 rc = btrieveFile.RecordRetrieveChunk(chunkLength, chunk)
87 assert(rc == chunkLength), "Error: BtrieveFile::RecordRetrieveChunk():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
88
89
90 x, xSquared, xSquareRoot = struct.unpack(chunkFormat, chunk)
91
92 # If key has been found.
93 if (x == integerValue):
94 break
95
96print ("chunk:", struct.unpack(chunkFormat, chunk))
97
98rc = btrieveClient.FileClose(btrieveFile)
99assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileClose():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
100
101rc = btrieveClient.FileDelete(btrieveFileName)
102assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileDelete():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))