User Tools

Site Tools


rmsaccesshighapiexample

Using the initial module IndexedFile.py

import struct
from vms.rmsdef import RMS__SUC, RMS__RNF, RMS__OK_DUP
from vms.fabdef import FAB_M_PUT, FAB_M_GET, FAB_M_DEL, FAB_M_UPD, \
       FAB_M_SHRPUT, FAB_M_SHRGET, FAB_M_SHRDEL, FAB_M_SHRUPD
from vms.rms.IndexedFile import IndexedFile
from construct import *
 
TestRec = Struct("TestRec", String("f1", 5), SLInt32("f2"), String("f3", 11))
 
class TestFile(IndexedFile):
    Name = 'test.dat'
    def __init__(self):
        IndexedFile.__init__(self, TestFile.Name, TestRec)
 
    def primary_keynum(self):
        return 0
 
    def keyval(self, rec, keynum):
        if keynum == 0:
            return rec.f1
        elif keynum == 1:
            return rec.f2
        else:
            raise "Invalid keynum parameter"
 
    def pack_key(self, keynum, keyval):
        if keynum == 0:
            return struct.pack("=5s", keyval)
        elif keynum == 1:
            return struct.pack("=i", keyval)
        else:
           raise KeyError
 
if __name__=='__main__':
    f = TestFile()
    print
    print 'Clear file'
    f.deleteall()
 
    print
    print 'Insert a few records'
    rec = Container(f1 = 'AA5AA', f2 = 5, f3 = '1234567890A')
    f.put(rec)
    rec = Container(f1 = 'BB2BB', f2 = 2, f3 = '1234567890B')
    f.put(rec)
    rec = Container(f1 = 'CC4CC', f2 = 4, f3 = '1234567890C')
    f.put(rec)
    rec = Container(f1 = 'DD1DD', f2 = 1, f3 = '1234567890D')
    f.put(rec)
    rec = Container(f1 = 'EE1EE', f2 = 2, f3 = '1234567890E')
    f.put(rec)
    rec = Container(f1 = 'FF3FF', f2 = 3, f3 = '1234567890F')
    f.put(rec)
 
    print
    print 'Initial records using an iterator, primary key order'
    for rec in f:
        print rec.f1, rec.f2, rec.f3
 
    print
    print 'Initial records using an iterator, secondary key order'
    f.iterator_keynum(1)
    for rec in f:
        print rec.f1, rec.f2, rec.f3
 
    print
    print 'Update all records using an iterator'
    # iterator key is automatically reset to primary key
    for rec in f:
        rec.f2 = rec.f2 + 10
        f.update_current(rec)
 
    print
    print 'Updated records using an iterator'
    for rec in f:
        print rec.f1, rec.f2, rec.f3
 
    print
    print 'build a list of all records'
    lst = f.fetchall(0)
    print len(lst)
 
    print
    print 'delete a record using secondary key value 14'
    f.delete(1, 14)
 
    print
    print 'Records using an iterator, secondary key order'
    f.iterator_keynum(1)
    for rec in f:
        print rec.f1, rec.f2, rec.f3
 
    print 'delete all records using secondary key value 12'
    f.open(acc = FAB_M_GET + FAB_M_DEL)
    f.usekey(1)
    f.rewind()
    f.find(None, f.pack_key(1, 12))
    for rec in f:
        if rec.f2 == 12:
           f.delete_current()
        else:
           break
    f.close()
 
    print
    print 'Records using an iterator, primary key order'
    for rec in f:
        print rec.f1, rec.f2, rec.f3

Result:

Reset file

Insert a few records

Initial records using an iterator, primary key order
AA5AA 5 1234567890A
BB2BB 2 1234567890B
CC4CC 4 1234567890C
DD1DD 1 1234567890D
EE1EE 2 1234567890E
FF3FF 3 1234567890F

Initial records using an iterator, secondary key order
DD1DD 1 1234567890D
BB2BB 2 1234567890B
EE1EE 2 1234567890E
FF3FF 3 1234567890F
CC4CC 4 1234567890C
AA5AA 5 1234567890A

Update all records using an iterator

Updated records using an iterator
AA5AA 15 1234567890A
BB2BB 12 1234567890B
CC4CC 14 1234567890C
DD1DD 11 1234567890D
EE1EE 12 1234567890E
FF3FF 13 1234567890F

build a list of all records
6

delete a record using secondary key value 14

Records using an iterator, secondary key order
DD1DD 11 1234567890D
BB2BB 12 1234567890B
EE1EE 12 1234567890E
FF3FF 13 1234567890F
AA5AA 15 1234567890A
delete all records using secondary key value 12

Records using an iterator, primary key order
AA5AA 15 1234567890A
DD1DD 11 1234567890D
FF3FF 13 1234567890F

Using the new module IndexedFile2

File FDL

SYSTEM
        SOURCE                  "OpenVMS"

FILE
        NAME                    "master001.dbs"
        ORGANIZATION            indexed
        GLBUFF_FLAGS_V83        none

RECORD
        CARRIAGE_CONTROL        none
        FORMAT                  fixed
        SIZE                    30

KEY 0
        CHANGES                 no
        SEG0_LENGTH             10
        SEG0_POSITION           6
        TYPE                    string

KEY 1
        SEG0_LENGTH             6
        SEG0_POSITION           16
        TYPE                    string
        SEG1_LENGTH             2
        SEG1_POSITION           26
        TYPE                    string

Initial Data

F01AAAF003AAAAA1XXXXA30005AAZZ
F01BABF003AAAAB1XXXXA10004BBXX
F01CACF003AAAAC1XXXXA20003DDEE
F01DADF003AAAAD1XXXXA40001TTDD
# -*- coding: iso-8859-15 -*-
 
from __future__ import with_statement
import struct
from vms.rms.IndexedFile2 import IndexedFile
from construct import *
 
# import pdb; pdb.set_trace()
 
class KeyStruct(object):
    pass
 
 
RecordMASTER_1 = Struct("RecordMASTER_1",
    String('F01L001', 4, padchar=' '),
    String('F01L002', 2, padchar=' '),
    String('F01L003', 10, padchar=' '),
    String('F01L004', 6, padchar=' '),
    String('F01L005', 4, padchar=' '),
    String('F01L006', 2, padchar=' '),
    String('F01L007', 2, padchar=' '),
)
 
 
class AccessIntFileKey0Struct(KeyStruct):
    key = String('F01L003', 10, padchar=' ')
 
 
class AccessIntFileKey1Struct(KeyStruct):
    key = Struct('key',
                 String('F01L004', 6, padchar=' '),
                 String('F01L006', 2, padchar=' '),
                 )
 
 
class TestFile(IndexedFile):
    name = 'master001.dbs'
    def __init__(self):
        super(TestFile, self).__init__(self.name, RecordMASTER_1,
                         [AccessIntFileKey0Struct.key,
                          AccessIntFileKey1Struct.key])
 
    def keyval(self, rec, keynum):
        return getattr(rec, self.keys[keynum])
 
f = TestFile()
f.open()
 
f.usekey(1)
f.rewind()
print 'Find', f.find(1, Container(F01L004='XXXXA3', F01L006='AA'))
for rec in f:
    print rec # prints out a record form DB file.
 
 
f.usekey(0)
f.rewind()
print 'Find', f.find(0, 'F003AAAAC1')
for rec in f:
    print rec # prints out a record form DB file.
 
f.rewind()
print 'Find', f.find(0, 'F003AAAAC', '>=')
for rec in f:
    print rec # prints out a record form DB file.

Result

Find True
Container:
    F01L001 = 'F01A'
    F01L002 = 'AA'
    F01L003 = 'F003AAAAA1'
    F01L004 = 'XXXXA3'
    F01L005 = '0005'
    F01L006 = 'AA'
    F01L007 = 'ZZ'
Container:
    F01L001 = 'F01D'
    F01L002 = 'AD'
    F01L003 = 'F003AAAAD1'
    F01L004 = 'XXXXA4'
    F01L005 = '0001'
    F01L006 = 'TT'
    F01L007 = 'DD'
Find True
Container:
    F01L001 = 'F01C'
    F01L002 = 'AC'
    F01L003 = 'F003AAAAC1'
    F01L004 = 'XXXXA2'
    F01L005 = '0003'
    F01L006 = 'DD'
    F01L007 = 'EE'
Container:
    F01L001 = 'F01D'
    F01L002 = 'AD'
    F01L003 = 'F003AAAAD1'
    F01L004 = 'XXXXA4'
    F01L005 = '0001'
    F01L006 = 'TT'
    F01L007 = 'DD'
Find True
Container:
    F01L001 = 'F01C'
    F01L002 = 'AC'
    F01L003 = 'F003AAAAC1'
    F01L004 = 'XXXXA2'
    F01L005 = '0003'
    F01L006 = 'DD'
    F01L007 = 'EE'
Container:
    F01L001 = 'F01D'
    F01L002 = 'AD'
    F01L003 = 'F003AAAAD1'
    F01L004 = 'XXXXA4'
    F01L005 = '0001'
    F01L006 = 'TT'
    F01L007 = 'DD'
rmsaccesshighapiexample.txt · Last modified: 2016/04/22 10:45 by jfp