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

This is an example of record append 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, min_x, min_x * min_x, math.sqrt(min_x))
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 + 1, max_x + 1):
58 chunk = struct.pack(chunkFormat, i, i * i, math.sqrt(i))
59
60
61 rc = btrieveFile.RecordAppendChunk(chunk)
62 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFile::RecordAppendChunk():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
63
64
65# print chunk
66chunk = struct.pack(chunkFormat, 0, 0, 0)
67
68rc = btrieveFile.RecordRetrieveFirst(btrievePython.Btrieve.INDEX_NONE, chunk)
69assert(rc == chunkLength), "Error: BtrieveFile::RecordRetrieveFirst():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
70
71for i in range(min_x, max_x + 1):
72 # If this is the first chunk.
73 if (i == min_x):
74 rc = btrieveFile.RecordRetrieveChunk(0, chunkLength, chunk)
75 assert(rc == chunkLength), "Error: BtrieveFile::RecordRetrieveChunk():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
76 else:
77 rc = btrieveFile.RecordRetrieveChunk(chunkLength, chunk)
78 assert(rc == chunkLength), "Error: BtrieveFile::RecordRetrieveChunk():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
79
80 x, xSquared, xSquareRoot = struct.unpack(chunkFormat, chunk)
81
82 # If key has been found.
83 if (x == integerValue):
84 break
85
86print ("chunk:", struct.unpack(chunkFormat, chunk))
87
88rc = btrieveClient.FileClose(btrieveFile)
89assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileClose():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
90
91rc = btrieveClient.FileDelete(btrieveFileName)
92assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileDelete():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))