forked from couchbase/couchbase-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pump_json.py
executable file
·189 lines (162 loc) · 6.76 KB
/
pump_json.py
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
#!/usr/bin/env python
import logging
import os
import json
import struct
import sys
import shutil
import tempfile
import zipfile
import couchbaseConstants
import pump
JSON_SCHEME = "json://"
class JSONSource(pump.Source):
"""Reads json file or directory or zip file that contains json files."""
def __init__(self, opts, spec, source_bucket, source_node,
source_map, sink_map, ctl, cur):
super(JSONSource, self).__init__(opts, spec, source_bucket, source_node,
source_map, sink_map, ctl, cur)
self.done = False
self.docs = list()
self.file_iter = None
@staticmethod
def can_handle(opts, spec):
return spec.startswith(JSON_SCHEME) and \
(os.path.isfile(spec.replace(JSON_SCHEME, "")) or \
os.path.isdir(spec.replace(JSON_SCHEME, "")) or \
spec.endswith(".zip"))
@staticmethod
def check(opts, spec):
return 0, {'spec': spec,
'buckets': [{'name': os.path.normpath(os.path.basename(spec)),
'nodes': [{'hostname': 'N/A'}]}]}
def save_doc(self, batch, dockey, docvalue):
cmd = couchbaseConstants.CMD_TAP_MUTATION
vbucket_id = 0x0000ffff
# common flags: 0x02000000 (JSON)
# legacy flags: 0x00000006 (JSON)
cas, exp, flg = 0, 0, 0x02000006
try:
doc = json.loads(docvalue)
if '_id' not in doc:
msg = (cmd, vbucket_id, dockey, flg, exp, cas, '', docvalue, 0, 0, 0, 0)
batch.append(msg, len(docvalue))
else:
id = doc['_id'].encode('UTF-8')
del doc['_id']
docdata = {"doc":{
"json": doc,
"meta":{"id":id}
}}
if not is_data:
batch.append(json.dumps(docdata), len(docdata))
except ValueError, error:
logging.error("Fail to read json file with error:" + str(error))
@staticmethod
def gen_dockey(filename):
return os.path.splitext(os.path.basename(filename))[0]
@staticmethod
def enumerate_files(subdir, file_candidate, skip_views, skip_docs):
for item in os.listdir(subdir):
path = os.path.join(subdir, item)
if os.path.isfile(path):
if (not skip_views and "design_docs" in path.split(os.path.sep)) or \
(not skip_docs and "docs" in path.split(os.path.sep)):
file_candidate.append(path)
else:
if not ((skip_docs and "docs" in path.split(os.path.sep)) or \
(skip_views and "design_docs" in path.split(os.path.sep))):
JSONSource.enumerate_files(path, file_candidate, skip_views, skip_docs)
@staticmethod
def provide_design(opts, source_spec, source_bucket, source_map):
design_files = list()
f = source_spec.replace(JSON_SCHEME, "")
if os.path.isfile(f) and f.endswith(".zip"):
zf = zipfile.ZipFile(f)
for path in zf.namelist():
file = os.path.basename(path)
# Skip the design_docs directory listing
if file == "design_docs":
continue
dir = os.path.basename(os.path.dirname(path))
# Skip all files not in the design docs directory
if dir != "design_docs":
continue
design_files.append(zf.read(path))
zf.close()
elif os.path.isdir(f):
files = list()
JSONSource.enumerate_files(f, files, False, True)
for path in files:
if os.path.isfile(path):
f = open(path, 'r')
design_files.append(f.read())
f.close()
return 0, design_files
def provide_batch(self):
if self.done:
return 0, None
# During the first iteration load the file names, this is only run once
if not self.docs:
self.prepare_docs()
batch = pump.Batch(self)
f = self.spec.replace(JSON_SCHEME, "")
batch_max_size = self.opts.extra['batch_max_size']
# Each iteration should return a batch or mark the loading a finished
if os.path.isfile(f) and f.endswith(".zip"):
zf = zipfile.ZipFile(f)
while batch.size() < batch_max_size and self.docs:
path = self.docs.pop()
key = os.path.basename(path)
if key.endswith('.json'):
key = key[:-5]
value = zf.read(path)
self.save_doc(batch, key, value)
zf.close()
else:
while batch.size() < batch_max_size and self.docs:
path = self.docs.pop()
key = os.path.basename(path)
if key.endswith('.json'):
key = key[:-5]
try:
fp = open(path, 'r')
value = fp.read()
fp.close()
self.save_doc(batch, key, value)
except IOError, error:
logging.error("Fail to load json file with error" + str(error))
if not self.docs:
self.done = True
return 0, batch
def prepare_docs(self):
f = self.spec.replace(JSON_SCHEME, "")
root_name = os.path.basename(f).split('.')[0]
if os.path.isfile(f) and f.endswith(".zip"):
zf = zipfile.ZipFile(f)
for path in zf.namelist():
file = os.path.basename(path)
# Skip the docs directory listing
if file == "docs":
continue
dir = os.path.basename(os.path.dirname(path))
# This condition is not allowed by the spec, but we allowing it
# because the training team did properly follow the spec and we
# don't want to break their training material. Since this tool
# will be deprecated soon we are making an exception and
# allowing this.
if dir == "" or dir == root_name:
self.docs.append(path)
continue
# Skip all files not in the docs directory
# Note that we use a forward slash for all operating systems
# because this is a zip file and zip paths always use a forward
# slash
if "docs" not in path.split("/"):
continue
self.docs.append(path)
zf.close()
elif os.path.isdir(f):
JSONSource.enumerate_files(f, self.docs, True, False)
else:
self.docs.append(f)