forked from apache/ignite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_binary.py
More file actions
190 lines (164 loc) · 5.07 KB
/
Copy pathmigrate_binary.py
File metadata and controls
190 lines (164 loc) · 5.07 KB
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
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from collections import OrderedDict
from datetime import date
from decimal import Decimal
from pyignite import Client, GenericObjectMeta
from pyignite.datatypes import (
BoolObject, DateObject, DecimalObject, LongObject, String,
)
# prepare old data
old_schema = OrderedDict([
('date', DateObject),
('reported', BoolObject),
('purpose', String),
('sum', DecimalObject),
('recipient', String),
('cashier_id', LongObject),
])
old_data = [
(1, {
'date': date(2017, 9, 21),
'reported': True,
'purpose': 'Praesent eget fermentum massa',
'sum': Decimal('666.67'),
'recipient': 'John Doe',
'cashier_id': 8,
}),
(2, {
'date': date(2017, 10, 11),
'reported': True,
'purpose': 'Proin in bibendum nulla',
'sum': Decimal('333.33'),
'recipient': 'Jane Roe',
'cashier_id': 9,
}),
(3, {
'date': date(2017, 10, 11),
'reported': True,
'purpose': 'Suspendisse nec dolor auctor, scelerisque ex eu, iaculis odio',
'sum': Decimal('400.0'),
'recipient': 'Jane Roe',
'cashier_id': 8,
}),
(4, {
'date': date(2017, 10, 24),
'reported': False,
'purpose': 'Quisque ut leo ligula',
'sum': Decimal('1234.5'),
'recipient': 'Joe Bloggs',
'cashier_id': 10,
}),
(5, {
'date': date(2017, 12, 1),
'reported': True,
'purpose': 'Quisque ut leo ligula',
'sum': Decimal('800.0'),
'recipient': 'Richard Public',
'cashier_id': 12,
}),
(6, {
'date': date(2017, 12, 1),
'reported': True,
'purpose': 'Aenean eget bibendum lorem, a luctus libero',
'sum': Decimal('135.79'),
'recipient': 'Joe Bloggs',
'cashier_id': 10,
}),
]
# - add `report_date`
# - set `report_date` to the current date if `reported` is True, None if False
# - delete `reported`
#
# new_schema = {
# 'date': DateObject,
# 'report_date': DateObject,
# 'purpose': String,
# 'sum': DecimalObject,
# 'recipient': String,
# 'cashier_id': LongObject,
# }
class ExpenseVoucher(
metaclass=GenericObjectMeta,
schema=old_schema,
):
pass
client = Client()
client.connect('127.0.0.1', 10800)
accounting = client.get_or_create_cache('accounting')
for key, value in old_data:
accounting.put(key, ExpenseVoucher(**value))
data_classes = client.query_binary_type('ExpenseVoucher')
print(data_classes)
# {
# -231598180: <class '__main__.ExpenseVoucher'>
# }
s_id, data_class = data_classes.popitem()
schema = data_class.schema
schema['expense_date'] = schema['date']
del schema['date']
schema['report_date'] = DateObject
del schema['reported']
schema['sum'] = DecimalObject
# define new data class
class ExpenseVoucherV2(
metaclass=GenericObjectMeta,
type_name='ExpenseVoucher',
schema=schema,
):
pass
def migrate(cache, data, new_class):
""" Migrate given data pages. """
for key, old_value in data:
# read data
print(old_value)
# ExpenseVoucher(
# date=datetime(2017, 9, 21, 0, 0),
# reported=True,
# purpose='Praesent eget fermentum massa',
# sum=Decimal('666.67'),
# recipient='John Doe',
# cashier_id=8,
# version=1
# )
# create new binary object
new_value = new_class()
# process data
new_value.sum = old_value.sum
new_value.purpose = old_value.purpose
new_value.recipient = old_value.recipient
new_value.cashier_id = old_value.cashier_id
new_value.expense_date = old_value.date
new_value.report_date = date.today() if old_value.reported else None
# replace data
cache.put(key, new_value)
# verify data
verify = cache.get(key)
print(verify)
# ExpenseVoucherV2(
# purpose='Praesent eget fermentum massa',
# sum=Decimal('666.67'),
# recipient='John Doe',
# cashier_id=8,
# expense_date=datetime(2017, 9, 21, 0, 0),
# report_date=datetime(2018, 8, 29, 0, 0),
# version=1,
# )
# migrate data
result = accounting.scan()
migrate(accounting, result, ExpenseVoucherV2)
# cleanup
accounting.destroy()
client.close()