stasis-aries-wal/utilities/swig/ex1.py

47 lines
863 B
Python
Raw Normal View History

2007-05-30 23:48:36 +00:00
#!/usr/bin/python2.4
#
"""Example 1 in Python
"""
import stasis
2007-05-30 23:48:36 +00:00
import sys
import array
def runit(argv):
print "init"
stasis.Tinit()
2007-05-30 23:48:36 +00:00
# First transaction, for writing
print "begin & alloc"
xid = stasis.Tbegin();
record_id = stasis.Talloc(xid, 4);
2007-05-30 23:48:36 +00:00
print "update"
2008-02-22 19:27:35 +00:00
write_data = array.array('i', (42,))
2007-05-30 23:48:36 +00:00
data_out = write_data.tostring()
assert len(data_out) == 4
stasis.TupdateStr(xid, record_id, data_out, stasis.OPERATION_SET)
stasis.Tcommit(xid)
2007-05-30 23:48:36 +00:00
# Second transaction, for reading
print "read"
xid = stasis.Tbegin()
2007-05-30 23:48:36 +00:00
# Read from Stasis into memory for data_in
data_in = '1234'
stasis.TreadStr(xid, record_id, data_in)
2008-02-22 19:27:35 +00:00
read_data = array.array('i', data_in)
2007-05-30 23:48:36 +00:00
assert read_data[0] == 42
print "done"
stasis.Tdealloc(xid, record_id)
stasis.Tabort(xid)
2007-05-30 23:48:36 +00:00
stasis.Tdeinit()
2007-05-30 23:48:36 +00:00
if __name__ == "__main__":
runit(sys.argv)