File: write_file.py

package info (click to toggle)
python-vertica 1.4.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 948 kB
  • sloc: python: 6,914; makefile: 4
file content (63 lines) | stat: -rw-r--r-- 2,274 bytes parent folder | download | duplicates (2)
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
# Copyright (c) 2020-2024 Open Text.
#
# Licensed 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.


"""
WriteFile message

In the copy-local protocol, the server may send a WriteFile message when it
receives a batch of copy data. If the COPY FROM LOCAL command uses the
REJECTED DATA and/or EXCEPTIONS parameters, this message contains content of
rejected rows or exceptions output files. If the command uses the
RETURNREJECTED parameters instead, this message is a series of row numbers
saying which rows in the load were rejected.
"""

from __future__ import print_function, division, absolute_import, annotations

from struct import unpack_from

from ..message import BackendMessage


class WriteFile(BackendMessage):
    message_id = b'O'

    def __init__(self, filename, file_length, data=None):
        BackendMessage.__init__(self)
        self.filename = filename
        self.file_length = file_length

        # Parse RETURNREJECTED data
        if self.filename == '':
            row_count = self.file_length // 8
            # Rejected row numbers come in little endian format
            self.rejected_rows = unpack_from('<{0}Q'.format(row_count), data)

    def write_to_disk(self, connection, buffer_size):
        # Read the rest of the message from wire and write the file
        bytes_left = self.file_length
        with open(self.filename, 'ab') as f:
            pos = 0
            while bytes_left > 0:
                bytes_to_read = min(buffer_size, bytes_left)
                content = connection.read_bytes(bytes_to_read)
                f.write(content)
                bytes_left -= bytes_to_read

    def __str__(self):
        return "WriteFile: Filename = {}, FileLength = {}".format(self.filename, self.file_length)


BackendMessage.register(WriteFile)