summaryrefslogtreecommitdiff
path: root/lang/python/src/wiredtiger/__init__.py
blob: 149a95d5138877122f8dc3b409684a418a2083e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#
#
# Copyright (c) 2008-2012 WiredTiger, Inc.
#	All rights reserved.
#
# See the file LICENSE for redistribution information.
#
# WiredTiger public interface

'''
WiredTiger Python API.

This module exports several functions and classes.
'''

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
    sizebytes = 0
    for offset, f in enumerate(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':
            if offset == len(fmt) - 1:
                result += (s,)
            else:
                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 offset, f in enumerate(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 and offset != len(fmt) - 1:
                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.client = session.client
        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 unpack(self.keyfmt, self.key)

    def get_value(self):
        return unpack(self.keyfmt, self.value)

    def set_key(self, *args):
        self.key = pack(self.keyfmt, *args)

    def set_value(self, *args):
        self.value = pack(self.valuefmt, *args)

    def first(self):
        result = self.client.move_first(self.id)
        self.key, self.value = result.record.key, result.record.value
        return result.exact

    def last(self):
        result = self.client.move_last(self.id)
        self.key, self.value = result.record.key, result.record.value
        return result.exact

    def next(self):
        result = self.client.move_next(self.id)
        self.key, self.value = result.record.key, result.record.value
        return result.exact

    def prev(self):
        result = self.client.move_prev(self.id)
        self.key, self.value = result.record.key, result.record.value
        return result.exact

    def search(self):
        result = self.client.search(self.id, WT_RECORD(self.key, self.value))
        self.key, self.value = result.record.key, result.record.value
        return result.exact

    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 dup_cursor(self, c, config=''):
        return Cursor(self, self.client.dup_cursor(self.id, c.id, 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)