Beginnings of a Java client API implementation.

--HG--
branch : mjc
rename : Doxyfile => docs/Doxyfile
extra : transplant_source : O%FA%B3%84%98%D2%10%B4%B5%B7%BD%CA%86V%F4b%3Ff%0Cu
This commit is contained in:
Michael Cahill
2010-12-02 13:24:06 +11:00
parent e21c6124fe
commit 024b514d39
10 changed files with 5833 additions and 11 deletions

View File

@@ -0,0 +1,210 @@
# WiredTiger public interface
import struct
from urlparse import urlparse
from wiredtiger.service import WiredTiger
from service.ttypes import WT_RECORD
from thrift.transport import TSocket, TTransport
from thrift.protocol import TBinaryProtocol
def __wt2struct(fmt):
if not fmt:
return None, fmt
# Big endian with no alignment is the default
if fmt[0] in '@=<>!':
tfmt = fmt[0]
fmt = fmt[1:]
else:
tfmt = '>'
return tfmt, fmt.replace('r', 'Q')
def unpack(fmt, s):
tfmt, fmt = __wt2struct(fmt)
if not fmt:
return ()
result = ()
pfmt = tfmt
for f in fmt:
if f.isdigit():
sizebytes += 1
# With a fixed size, everything is encoded as a string
if f in 'Su' and sizebytes > 0:
f = 's'
if f not in 'Su':
pfmt += f
sizebytes = 0
continue
# We've hit something that needs special handling, split any fixed-size
# values we've already passed
if len(pfmt) > 1:
size = struct.calcsize(pfmt)
result += struct.unpack_from(pfmt, s)
s = s[size:]
if f == 'S':
l = s.find('\0')
result += (s[:l],)
s = s[l+1:]
if f == 'u':
l = struct.unpack_from(tfmt + 'l', s)[0]
s = s[struct.calcsize(tfmt + 'l'):]
result += (s[:l],)
s = s[l:]
pfmt = tfmt
sizebytes = 0
if len(pfmt) > 1:
result += struct.unpack(pfmt, s)
return result
def pack(fmt, *values):
pfmt, fmt = __wt2struct(fmt)
if not fmt:
return ''
i = sizebytes = 0
for f in fmt:
if f == 'S':
# Note: this code is being careful about embedded NUL characters
if sizebytes == 0:
l = values[i].find('\0') + 1
if not l:
l = len(values[i]) + 1
pfmt += str(l)
sizebytes = len(str(l))
f = 's'
elif f == 'u':
if sizebytes == 0:
l = len(values[i])
pfmt += 'l' + str(l)
values = values[:i] + (l,) + values[i:]
sizebytes = len(str(l))
f = 's'
pfmt += f
if f.isdigit():
sizebytes += 1
continue
if f != 's' and sizebytes > 0:
i += int(pfmt[-sizebytes:])
else:
i += 1
sizebytes = 0
return struct.pack(pfmt, *values)
class Cursor:
def __init__(self, session, handle):
self.session = session
self.id = handle.id
self.keyfmt = handle.keyfmt
self.valuefmt = handle.valuefmt
def close(self, config=''):
return self.client.close_cursor(self.id, config)
def get_key(self):
return None
def get_value(self):
return None
def set_key(self, *args):
pass
def set_value(self, *args):
pass
def first(self):
self.client.move_first(self.id)
def last(self):
self.client.move_last(self.id)
def next(self):
self.client.move_next(self.id)
def prev(self):
self.client.move_prev(self.id)
def search(self):
return self.client.search(self.id, WT_RECORD(self.key, self.value))
def insert(self):
self.key = self.client.insert_record(self.id, WT_RECORD(self.key, self.value))
def update(self):
return self.client.update_record(self.id, self.value)
def delete(self):
return self.client.delete_record(self.id)
class Session:
def __init__(self, conn, id):
self.conn = conn
self.client = conn.client
self.id = id
def close(self, config=''):
self.client.close_session(self.id, config)
def open_cursor(self, uri, config=''):
return Cursor(self, self.client.open_cursor(self.id, uri, config))
def create_table(self, name, config=''):
self.client.create_table(self.id, name, config)
def rename_table(self, oldname, newname, config=''):
self.client.rename_table(self.id, oldname, newname, config)
def drop_table(self, name, config=''):
self.client.drop_table(self.id, name, config)
def truncate_table(self, name, start=None, end=None, config=''):
self.client.truncate_table(self.id, name, name, start and start.id or 0, end and end.id or 0, config)
def verify_table(self, name, config=''):
self.client.verify_table(self.id, name, config)
def begin_transaction(self, config=''):
self.client.begin_transaction(self.id, config)
def commit_transaction(self):
self.client.begin_transaction(self.id)
def rollback_transaction(self):
self.client.rollback_transaction(self.id)
def checkpoint(self, config=''):
self.client.checkpoint(self.id, config)
class Connection:
def __init__(self, uri, config=''):
url = urlparse(uri)
parts = url[1].split(':')
host = parts[0]
if len(parts) > 1:
port = int(parts[1])
else:
port = 9090
home = url[2]
socket = TSocket.TSocket(host, port)
self.transport = TTransport.TBufferedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(self.transport)
self.client = WiredTiger.Client(protocol)
self.transport.open()
self.id = self.client.open(home, config)
def close(self, config=''):
self.client.close_connection(self.id, config)
self.transport.close()
def version(self):
v = self.client.version(self.id, config)
return v.version_string, v.major, v.minor, v.patch
def open_session(self, config=''):
id = self.client.open_session(self.id, config)
return Session(self, id)

View File

@@ -0,0 +1,260 @@
#!/usr/bin/env python
#
# Autogenerated by Thrift
#
# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
#
import sys
import pprint
from urlparse import urlparse
from thrift.transport import TTransport
from thrift.transport import TSocket
from thrift.transport import THttpClient
from thrift.protocol import TBinaryProtocol
import WiredTiger
from ttypes import *
if len(sys.argv) <= 1 or sys.argv[1] == '--help':
print ''
print 'Usage: ' + sys.argv[0] + ' [-h host:port] [-u url] [-f[ramed]] function [arg1 [arg2...]]'
print ''
print 'Functions:'
print ' string strerror(i32 err)'
print ' WT_VERSION version()'
print ' WT_HANDLE open(string home, string config)'
print ' WT_HANDLE open_session(WT_HANDLE connection, string config)'
print ' void close_connection(WT_HANDLE connection, string config)'
print ' void close_session(WT_HANDLE session, string config)'
print ' WT_CURSOR_HANDLE open_cursor(WT_HANDLE session, string uri, string config)'
print ' WT_CURSOR_HANDLE dup_cursor(WT_HANDLE session, WT_HANDLE cursor, string config)'
print ' void create_table(WT_HANDLE session, string name, string config)'
print ' void rename_table(WT_HANDLE session, string oldname, string newname, string config)'
print ' void drop_table(WT_HANDLE session, string name, string config)'
print ' void truncate_table(WT_HANDLE session, string name, WT_HANDLE cursor_start, WT_HANDLE cursor_end, string config)'
print ' void verify_table(WT_HANDLE session, string name, string config)'
print ' void begin_transaction(WT_HANDLE session, string config)'
print ' void commit_transaction(WT_HANDLE session)'
print ' void rollback_transaction(WT_HANDLE session)'
print ' void checkpoint(WT_HANDLE session, string config)'
print ' WT_RECORD move_first(WT_HANDLE cursor)'
print ' WT_RECORD move_last(WT_HANDLE cursor)'
print ' WT_RECORD move_next(WT_HANDLE cursor)'
print ' WT_RECORD move_prev(WT_HANDLE cursor)'
print ' i32 search(WT_HANDLE cursor, WT_RECORD record)'
print ' string insert_record(WT_HANDLE cursor, WT_RECORD record)'
print ' void update_record(WT_HANDLE cursor, string value)'
print ' void delete_record(WT_HANDLE cursor)'
print ' void close_cursor(WT_HANDLE cursor, string config)'
print ''
sys.exit(0)
pp = pprint.PrettyPrinter(indent = 2)
host = 'localhost'
port = 9090
uri = ''
framed = False
http = False
argi = 1
if sys.argv[argi] == '-h':
parts = sys.argv[argi+1].split(':')
host = parts[0]
port = int(parts[1])
argi += 2
if sys.argv[argi] == '-u':
url = urlparse(sys.argv[argi+1])
parts = url[1].split(':')
host = parts[0]
if len(parts) > 1:
port = int(parts[1])
else:
port = 80
uri = url[2]
if url[4]:
uri += '?%s' % url[4]
http = True
argi += 2
if sys.argv[argi] == '-f' or sys.argv[argi] == '-framed':
framed = True
argi += 1
cmd = sys.argv[argi]
args = sys.argv[argi+1:]
if http:
transport = THttpClient.THttpClient(host, port, uri)
else:
socket = TSocket.TSocket(host, port)
if framed:
transport = TTransport.TFramedTransport(socket)
else:
transport = TTransport.TBufferedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = WiredTiger.Client(protocol)
transport.open()
if cmd == 'strerror':
if len(args) != 1:
print 'strerror requires 1 args'
sys.exit(1)
pp.pprint(client.strerror(eval(args[0]),))
elif cmd == 'version':
if len(args) != 0:
print 'version requires 0 args'
sys.exit(1)
pp.pprint(client.version())
elif cmd == 'open':
if len(args) != 2:
print 'open requires 2 args'
sys.exit(1)
pp.pprint(client.open(args[0],args[1],))
elif cmd == 'open_session':
if len(args) != 2:
print 'open_session requires 2 args'
sys.exit(1)
pp.pprint(client.open_session(eval(args[0]),args[1],))
elif cmd == 'close_connection':
if len(args) != 2:
print 'close_connection requires 2 args'
sys.exit(1)
pp.pprint(client.close_connection(eval(args[0]),args[1],))
elif cmd == 'close_session':
if len(args) != 2:
print 'close_session requires 2 args'
sys.exit(1)
pp.pprint(client.close_session(eval(args[0]),args[1],))
elif cmd == 'open_cursor':
if len(args) != 3:
print 'open_cursor requires 3 args'
sys.exit(1)
pp.pprint(client.open_cursor(eval(args[0]),args[1],args[2],))
elif cmd == 'dup_cursor':
if len(args) != 3:
print 'dup_cursor requires 3 args'
sys.exit(1)
pp.pprint(client.dup_cursor(eval(args[0]),eval(args[1]),args[2],))
elif cmd == 'create_table':
if len(args) != 3:
print 'create_table requires 3 args'
sys.exit(1)
pp.pprint(client.create_table(eval(args[0]),args[1],args[2],))
elif cmd == 'rename_table':
if len(args) != 4:
print 'rename_table requires 4 args'
sys.exit(1)
pp.pprint(client.rename_table(eval(args[0]),args[1],args[2],args[3],))
elif cmd == 'drop_table':
if len(args) != 3:
print 'drop_table requires 3 args'
sys.exit(1)
pp.pprint(client.drop_table(eval(args[0]),args[1],args[2],))
elif cmd == 'truncate_table':
if len(args) != 5:
print 'truncate_table requires 5 args'
sys.exit(1)
pp.pprint(client.truncate_table(eval(args[0]),args[1],eval(args[2]),eval(args[3]),args[4],))
elif cmd == 'verify_table':
if len(args) != 3:
print 'verify_table requires 3 args'
sys.exit(1)
pp.pprint(client.verify_table(eval(args[0]),args[1],args[2],))
elif cmd == 'begin_transaction':
if len(args) != 2:
print 'begin_transaction requires 2 args'
sys.exit(1)
pp.pprint(client.begin_transaction(eval(args[0]),args[1],))
elif cmd == 'commit_transaction':
if len(args) != 1:
print 'commit_transaction requires 1 args'
sys.exit(1)
pp.pprint(client.commit_transaction(eval(args[0]),))
elif cmd == 'rollback_transaction':
if len(args) != 1:
print 'rollback_transaction requires 1 args'
sys.exit(1)
pp.pprint(client.rollback_transaction(eval(args[0]),))
elif cmd == 'checkpoint':
if len(args) != 2:
print 'checkpoint requires 2 args'
sys.exit(1)
pp.pprint(client.checkpoint(eval(args[0]),args[1],))
elif cmd == 'move_first':
if len(args) != 1:
print 'move_first requires 1 args'
sys.exit(1)
pp.pprint(client.move_first(eval(args[0]),))
elif cmd == 'move_last':
if len(args) != 1:
print 'move_last requires 1 args'
sys.exit(1)
pp.pprint(client.move_last(eval(args[0]),))
elif cmd == 'move_next':
if len(args) != 1:
print 'move_next requires 1 args'
sys.exit(1)
pp.pprint(client.move_next(eval(args[0]),))
elif cmd == 'move_prev':
if len(args) != 1:
print 'move_prev requires 1 args'
sys.exit(1)
pp.pprint(client.move_prev(eval(args[0]),))
elif cmd == 'search':
if len(args) != 2:
print 'search requires 2 args'
sys.exit(1)
pp.pprint(client.search(eval(args[0]),eval(args[1]),))
elif cmd == 'insert_record':
if len(args) != 2:
print 'insert_record requires 2 args'
sys.exit(1)
pp.pprint(client.insert_record(eval(args[0]),eval(args[1]),))
elif cmd == 'update_record':
if len(args) != 2:
print 'update_record requires 2 args'
sys.exit(1)
pp.pprint(client.update_record(eval(args[0]),args[1],))
elif cmd == 'delete_record':
if len(args) != 1:
print 'delete_record requires 1 args'
sys.exit(1)
pp.pprint(client.delete_record(eval(args[0]),))
elif cmd == 'close_cursor':
if len(args) != 2:
print 'close_cursor requires 2 args'
sys.exit(1)
pp.pprint(client.close_cursor(eval(args[0]),args[1],))
else:
print 'Unrecognized method %s' % cmd
sys.exit(1)
transport.close()

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1 @@
__all__ = ['ttypes', 'constants', 'WiredTiger']

View File

@@ -0,0 +1,9 @@
#
# Autogenerated by Thrift
#
# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
#
from thrift.Thrift import *
from ttypes import *

View File

@@ -0,0 +1,339 @@
#
# Autogenerated by Thrift
#
# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
#
from thrift.Thrift import *
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol, TProtocol
try:
from thrift.protocol import fastbinary
except:
fastbinary = None
class WT_RECORD:
"""
Attributes:
- key
- value
"""
thrift_spec = (
None, # 0
(1, TType.STRING, 'key', None, None, ), # 1
(2, TType.STRING, 'value', None, None, ), # 2
)
def __init__(self, key=None, value=None,):
self.key = key
self.value = value
def read(self, iprot):
if iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None and fastbinary is not None:
fastbinary.decode_binary(self, iprot.trans, (self.__class__, self.thrift_spec))
return
iprot.readStructBegin()
while True:
(fname, ftype, fid) = iprot.readFieldBegin()
if ftype == TType.STOP:
break
if fid == 1:
if ftype == TType.STRING:
self.key = iprot.readString();
else:
iprot.skip(ftype)
elif fid == 2:
if ftype == TType.STRING:
self.value = iprot.readString();
else:
iprot.skip(ftype)
else:
iprot.skip(ftype)
iprot.readFieldEnd()
iprot.readStructEnd()
def write(self, oprot):
if oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and self.thrift_spec is not None and fastbinary is not None:
oprot.trans.write(fastbinary.encode_binary(self, (self.__class__, self.thrift_spec)))
return
oprot.writeStructBegin('WT_RECORD')
if self.key != None:
oprot.writeFieldBegin('key', TType.STRING, 1)
oprot.writeString(self.key)
oprot.writeFieldEnd()
if self.value != None:
oprot.writeFieldBegin('value', TType.STRING, 2)
oprot.writeString(self.value)
oprot.writeFieldEnd()
oprot.writeFieldStop()
oprot.writeStructEnd()
def validate(self):
return
def __repr__(self):
L = ['%s=%r' % (key, value)
for key, value in self.__dict__.iteritems()]
return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
def __eq__(self, other):
return isinstance(other, self.__class__) and self.__dict__ == other.__dict__
def __ne__(self, other):
return not (self == other)
class WT_CURSOR_HANDLE:
"""
Attributes:
- id
- keyfmt
- valuefmt
"""
thrift_spec = (
None, # 0
(1, TType.I32, 'id', None, None, ), # 1
(2, TType.STRING, 'keyfmt', None, None, ), # 2
(3, TType.STRING, 'valuefmt', None, None, ), # 3
)
def __init__(self, id=None, keyfmt=None, valuefmt=None,):
self.id = id
self.keyfmt = keyfmt
self.valuefmt = valuefmt
def read(self, iprot):
if iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None and fastbinary is not None:
fastbinary.decode_binary(self, iprot.trans, (self.__class__, self.thrift_spec))
return
iprot.readStructBegin()
while True:
(fname, ftype, fid) = iprot.readFieldBegin()
if ftype == TType.STOP:
break
if fid == 1:
if ftype == TType.I32:
self.id = iprot.readI32();
else:
iprot.skip(ftype)
elif fid == 2:
if ftype == TType.STRING:
self.keyfmt = iprot.readString();
else:
iprot.skip(ftype)
elif fid == 3:
if ftype == TType.STRING:
self.valuefmt = iprot.readString();
else:
iprot.skip(ftype)
else:
iprot.skip(ftype)
iprot.readFieldEnd()
iprot.readStructEnd()
def write(self, oprot):
if oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and self.thrift_spec is not None and fastbinary is not None:
oprot.trans.write(fastbinary.encode_binary(self, (self.__class__, self.thrift_spec)))
return
oprot.writeStructBegin('WT_CURSOR_HANDLE')
if self.id != None:
oprot.writeFieldBegin('id', TType.I32, 1)
oprot.writeI32(self.id)
oprot.writeFieldEnd()
if self.keyfmt != None:
oprot.writeFieldBegin('keyfmt', TType.STRING, 2)
oprot.writeString(self.keyfmt)
oprot.writeFieldEnd()
if self.valuefmt != None:
oprot.writeFieldBegin('valuefmt', TType.STRING, 3)
oprot.writeString(self.valuefmt)
oprot.writeFieldEnd()
oprot.writeFieldStop()
oprot.writeStructEnd()
def validate(self):
return
def __repr__(self):
L = ['%s=%r' % (key, value)
for key, value in self.__dict__.iteritems()]
return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
def __eq__(self, other):
return isinstance(other, self.__class__) and self.__dict__ == other.__dict__
def __ne__(self, other):
return not (self == other)
class WT_VERSION:
"""
Attributes:
- version_string
- major
- minor
- patch
"""
thrift_spec = (
None, # 0
(1, TType.STRING, 'version_string', None, None, ), # 1
(2, TType.I32, 'major', None, None, ), # 2
(3, TType.I32, 'minor', None, None, ), # 3
(4, TType.I32, 'patch', None, None, ), # 4
)
def __init__(self, version_string=None, major=None, minor=None, patch=None,):
self.version_string = version_string
self.major = major
self.minor = minor
self.patch = patch
def read(self, iprot):
if iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None and fastbinary is not None:
fastbinary.decode_binary(self, iprot.trans, (self.__class__, self.thrift_spec))
return
iprot.readStructBegin()
while True:
(fname, ftype, fid) = iprot.readFieldBegin()
if ftype == TType.STOP:
break
if fid == 1:
if ftype == TType.STRING:
self.version_string = iprot.readString();
else:
iprot.skip(ftype)
elif fid == 2:
if ftype == TType.I32:
self.major = iprot.readI32();
else:
iprot.skip(ftype)
elif fid == 3:
if ftype == TType.I32:
self.minor = iprot.readI32();
else:
iprot.skip(ftype)
elif fid == 4:
if ftype == TType.I32:
self.patch = iprot.readI32();
else:
iprot.skip(ftype)
else:
iprot.skip(ftype)
iprot.readFieldEnd()
iprot.readStructEnd()
def write(self, oprot):
if oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and self.thrift_spec is not None and fastbinary is not None:
oprot.trans.write(fastbinary.encode_binary(self, (self.__class__, self.thrift_spec)))
return
oprot.writeStructBegin('WT_VERSION')
if self.version_string != None:
oprot.writeFieldBegin('version_string', TType.STRING, 1)
oprot.writeString(self.version_string)
oprot.writeFieldEnd()
if self.major != None:
oprot.writeFieldBegin('major', TType.I32, 2)
oprot.writeI32(self.major)
oprot.writeFieldEnd()
if self.minor != None:
oprot.writeFieldBegin('minor', TType.I32, 3)
oprot.writeI32(self.minor)
oprot.writeFieldEnd()
if self.patch != None:
oprot.writeFieldBegin('patch', TType.I32, 4)
oprot.writeI32(self.patch)
oprot.writeFieldEnd()
oprot.writeFieldStop()
oprot.writeStructEnd()
def validate(self):
return
def __repr__(self):
L = ['%s=%r' % (key, value)
for key, value in self.__dict__.iteritems()]
return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
def __eq__(self, other):
return isinstance(other, self.__class__) and self.__dict__ == other.__dict__
def __ne__(self, other):
return not (self == other)
class WT_ERROR(Exception):
"""
Attributes:
- err
- message
"""
thrift_spec = (
None, # 0
(1, TType.I32, 'err', None, None, ), # 1
(2, TType.STRING, 'message', None, None, ), # 2
)
def __init__(self, err=None, message=None,):
self.err = err
self.message = message
def read(self, iprot):
if iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None and fastbinary is not None:
fastbinary.decode_binary(self, iprot.trans, (self.__class__, self.thrift_spec))
return
iprot.readStructBegin()
while True:
(fname, ftype, fid) = iprot.readFieldBegin()
if ftype == TType.STOP:
break
if fid == 1:
if ftype == TType.I32:
self.err = iprot.readI32();
else:
iprot.skip(ftype)
elif fid == 2:
if ftype == TType.STRING:
self.message = iprot.readString();
else:
iprot.skip(ftype)
else:
iprot.skip(ftype)
iprot.readFieldEnd()
iprot.readStructEnd()
def write(self, oprot):
if oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and self.thrift_spec is not None and fastbinary is not None:
oprot.trans.write(fastbinary.encode_binary(self, (self.__class__, self.thrift_spec)))
return
oprot.writeStructBegin('WT_ERROR')
if self.err != None:
oprot.writeFieldBegin('err', TType.I32, 1)
oprot.writeI32(self.err)
oprot.writeFieldEnd()
if self.message != None:
oprot.writeFieldBegin('message', TType.STRING, 2)
oprot.writeString(self.message)
oprot.writeFieldEnd()
oprot.writeFieldStop()
oprot.writeStructEnd()
def validate(self):
return
def __str__(self):
return repr(self)
def __repr__(self):
L = ['%s=%r' % (key, value)
for key, value in self.__dict__.iteritems()]
return '%s(%s)' % (self.__class__.__name__, ', '.join(L))
def __eq__(self, other):
return isinstance(other, self.__class__) and self.__dict__ == other.__dict__
def __ne__(self, other):
return not (self == other)