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

This is an example of a key only file written in Python.

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 = 2
15max_x = 255
16btrieveFileName = "primes.btr"
17recordFormat = "<i"
18recordLength = 4
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_greater_than_one")
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_greater_than_one")
29
30btrieveClient = btrievePython.BtrieveClient()
31assert(btrieveClient != None)
32
33btrieveKeySegment = btrievePython.BtrieveKeySegment()
34assert(btrieveKeySegment != None)
35
36rc = btrieveKeySegment.SetField(0, recordLength, btrievePython.Btrieve.DATA_TYPE_UNSIGNED_BINARY)
37assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveKeySegment::SetField():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
38
39btrieveIndexAttributes = btrievePython.BtrieveIndexAttributes()
40assert(btrieveIndexAttributes != None)
41
42rc = btrieveIndexAttributes.AddKeySegment(btrieveKeySegment)
43assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveIndexAttributes::AddKeySegment():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
44
45btrieveFileAttributes = btrievePython.BtrieveFileAttributes()
46assert(btrieveFileAttributes != None)
47
48
49rc = btrieveFileAttributes.SetKeyOnly(True)
50assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFileAttributes::SetKeyOnly():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
51
52
53rc = btrieveFileAttributes.SetFixedRecordLength(recordLength)
54assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFileAttributes::SetFixedRecordLength():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
55
56rc = btrieveClient.FileCreate(btrieveFileAttributes, btrieveIndexAttributes, btrieveFileName, btrievePython.Btrieve.CREATE_MODE_OVERWRITE)
57assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
58
59btrieveFile = btrievePython.BtrieveFile()
60assert(btrieveFile != None)
61
62rc = btrieveClient.FileOpen(btrieveFile, btrieveFileName, None, btrievePython.Btrieve.OPEN_MODE_NORMAL)
63assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileOpen():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
64
65# For all the values of x.
66for i in range(min_x, max_x + 1):
67 record = struct.pack(recordFormat, i)
68
69 rc = btrieveFile.RecordCreate(record)
70 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFile::RecordCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
71
72# For all the prime numbers in the range min_x through max_x.
73for j in range(2, int(max_x / 2) + 1):
74 for i in range(j * 2, max_x + 1, j):
75 record = bytes(recordLength)
76 key = struct.pack(recordFormat, i)
77
78 rc = btrieveFile.RecordRetrieve(btrievePython.Btrieve.COMPARISON_EQUAL, btrievePython.Btrieve.INDEX_1, key, record)
79
80 if (rc != recordLength):
81 rc = btrieveFile.GetLastStatusCode()
82 assert(rc == btrievePython.Btrieve.STATUS_CODE_KEY_VALUE_NOT_FOUND), "Error: BtrieveFile::RecordRetrieve():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
83 continue
84
85
86 rc = btrieveFile.RecordDelete()
87 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFile::RecordDelete():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
88
89
90# print record
91key = struct.pack(recordFormat, integerValue)
92
93
94rc = btrieveFile.KeyRetrieve(btrievePython.Btrieve.COMPARISON_EQUAL, btrievePython.Btrieve.INDEX_1, key)
95
96if (rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR):
97 print(integerValue, "is prime.")
98elif (rc == btrievePython.Btrieve.STATUS_CODE_KEY_VALUE_NOT_FOUND):
99 print(integerValue, "is not prime.")
100else:
101 assert(False), "Error: BtrieveFile::KeyRetrieve():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
102
103
104rc = btrieveClient.FileClose(btrieveFile)
105assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileClose():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
106
107rc = btrieveClient.FileDelete(btrieveFileName)
108assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileDelete():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))