<commit_before>#!/usr/bin/env python
"""
Binary memcached test client.

Copyright (c) 2007  <NAME> <<EMAIL>>
"""

import exceptions
import hmac
import json
import random
import re
import select
import socket
import struct
import sys
import time

from memcacheConstants import REQ_MAGIC_BYTE, RES_MAGIC_BYTE
from memcacheConstants import REQ_PKT_FMT, RES_PKT_FMT, MIN_RECV_PACKET
from memcacheConstants import SET_PKT_FMT, DEL_PKT_FMT, INCRDECR_RES_FMT
from memcacheConstants import TOUCH_PKT_FMT, GAT_PKT_FMT, GETL_PKT_FMT
from memcacheConstants import COMPACT_DB_PKT_FMT
import memcacheConstants

def parse_address(addr):
    """Parse a host string with optional port number into a
    (host, port, family) triple."""

    # Sane defaults
    family = socket.AF_UNSPEC
    port = 11210
    # Is this IPv6?
    if addr.startswith('['):
        matches = re.match(r'^\[([^\]]+)\](:(\d+))?$', addr)
        family = socket.AF_INET6
    else:
        matches = re.match(r'^([^:]+)(:(\d+))?$', addr)
    if matches:
        # The host is the first group
        host = matches.group(1)
        # Optional port is the 3rd group
        if matches.group(3):
            port = int(matches.group(3))
    else:
        raise Exception("Invalid format for host string: '{}'".format(addr))

    return host, port, family

class TimeoutError(exceptions.Exception):
    def __init__(self, time):
        exceptions.Exception.__init__(self, "Operation timed out")
        self.time = time

    def __str__(self):
        return self.__repr__()

    def __repr__(self):
        str = 'Error: Operation timed out (%d seconds)\n' % self.time
        str += 'Please check list of arguments (e.g., IP address, port number) '
        str += 'passed or the connectivity to a server to be connected'
        return str


# This metaclass causes any instantiation of MemcachedError to transparently
# construct the appropriate subclass if the error code is known.
class MemcachedErrorMetaclass(type):
    _dispatch = {}
    def __new__(meta, name, bases, dct):
        cls = (super(MemcachedErrorMetaclass, meta)
               .__new__(meta, name, bases, dct))
        if 'ERRCODE' in dct:
            meta._dispatch[dct['ERRCODE']] = cls
        return cls

    def __call__(cls, *args, **kwargs):
        err = None
        if 'status' in kwargs:
            err = kwargs['status']
        elif len(args):
            err = args[0]

        if err in cls._dispatch:
            cls = cls._dispatch[err]

        return (super(MemcachedErrorMetaclass, cls)
                .__call__(*args, **kwargs))

class MemcachedError(exceptions.Exception):
    """Error raised when a command fails."""

    __metaclass__ = MemcachedErrorMetaclass

    def __init__(self, status, msg):
        supermsg='Memcached error #' + repr(status)
        if msg: supermsg += ":  " + msg
        exceptions.Exception.__init__(self, supermsg)

        self.status=status
        self.msg=msg

    def __repr__(self):
        return "<MemcachedError #%d ``%s''>" % (self.status, self.msg)

class ErrorKeyEnoent(MemcachedError): ERRCODE = 0x1
class ErrorKeyEexists(MemcachedError): ERRCODE = 0x2
class ErrorE2big(MemcachedError): ERRCODE = 0x3
class ErrorEinval(MemcachedError): ERRCODE = 0x4
class ErrorNotStored(MemcachedError): ERRCODE = 0x5
class ErrorDeltaBadval(MemcachedError): ERRCODE = 0x6
class ErrorNotMyVbucket(MemcachedError): ERRCODE = 0x7
class ErrorNoBucket(MemcachedError): ERRCODE = 0x8
class ErrorLocked(MemcachedError): ERRCODE = 0x9
class ErrorAuthStale(MemcachedError): ERRCODE = 0x1f
class ErrorAuthError(MemcachedError): ERRCODE = 0x20
class ErrorAuthContinue(MemcachedError): ERRCODE = 0x21
class ErrorErange(MemcachedError): ERRCODE = 0x22
class ErrorRollback(MemcachedError): ERRCODE = 0x23
class ErrorEaccess(MemcachedError): ERRCODE = 0x24
class ErrorNotInitialized(MemcachedError): ERRCODE = 0x25
class ErrorUnknownCommand(MemcachedError): ERRCODE = 0x81
class ErrorEnomem(MemcachedError): ERRCODE = 0x82
class ErrorNotSupported(MemcachedError): ERRCODE = 0x83
class ErrorEinternal(MemcachedError): ERRCODE = 0x84
class ErrorEbusy(MemcachedError): ERRCODE = 0x85
class ErrorEtmpfail(MemcachedError): ERRCODE = 0x86
class ErrorXattrEinval(MemcachedError): ERRCODE = 0x87
class ErrorUnknownCollection(MemcachedError): ERRCODE = 0x88
class ErrorSubdocPathEnoent(MemcachedError): ERRCODE = 0xc0
class ErrorSubdocPathMismatch(MemcachedError): ERRCODE = 0xc1
class ErrorSubdocPathEinval(MemcachedError): ERRCODE = 0xc2
class ErrorSubdocPathE2big(MemcachedError): ERRCODE = 0xc3
class ErrorSubdocPathE2deep(MemcachedError): ERRCODE = 0xc4
class ErrorSubdocValueCantinsert(MemcachedError): ERRCODE = 0xc5
class ErrorSubdocDocNotjson(MemcachedError): ERRCODE = 0xc6
class ErrorSubdocNumErange(MemcachedError): ERRCODE = 0xc7
class ErrorSubdocDeltaEinval(MemcachedError): ERRCODE = 0xc8
class ErrorSubdocPathEexists(MemcachedError): ERRCODE = 0xc9
class ErrorSubdocValueEtoodeep(MemcachedError): ERRCODE = 0xca
class ErrorSubdocInvalidCombo(MemcachedError): ERRCODE = 0xcb
class ErrorSubdocMultiPathFailure(MemcachedError): ERRCODE = 0xcc
class ErrorSubdocSuccessDeleted(MemcachedError): ERRCODE = 0xcd
class ErrorSubdocXattrInvalidFlagCombo(MemcachedError): ERRCODE = 0xce
class ErrorSubdocXattrInvalidKeyCombo(MemcachedError): ERRCODE = 0xcf
class ErrorSubdocXattrUnknownMacro(MemcachedError): ERRCODE = 0xd0

class MemcachedClient(object):
    """Simple memcached client."""

    vbucketId = 0

    def __init__(self, host='127.0.0.1', port=11211, family=socket.AF_UNSPEC):
        self.host = host
        self.port = port

        # Iterate all addresses for the given family; using the first
        # one we can successfully connect to. If none succeed raise
        # the last attempted as an exception.
        for info in socket.getaddrinfo(self.host, self.port, family,
                                       socket.SOCK_STREAM):
            _family, socktype, proto, _, sockaddr = info
            try:
                sock = socket.socket(_family, socktype, proto)
                sock.settimeout(10)
                sock.connect(sockaddr)
                self.s = sock
                break
            except socket.error as sock_error:
                # If we get here socket objects will be close()d via
                # garbage collection.
                pass
        else:
            # Didn't break from the loop, re-raise the last error
            raise sock_error

        self.s.setblocking(0)
        self.r=random.Random()
        self.req_features = set()
        self.features = set()
        self.error_map = None
        self.error_map_version = 1

    def close(self):
        self.s.close()

    def __del__(self):
        self.close()

    def _sendCmd(self, cmd, key, val, opaque, extraHeader='', cas=0):
        self._sendMsg(cmd, key, val, opaque, extraHeader=extraHeader, cas=cas,
                      vbucketId=self.vbucketId)

    def _sendMsg(self, cmd, key, val, opaque, extraHeader='', cas=0,
                 dtype=0, vbucketId=0,
                 fmt=REQ_PKT_FMT, magic=REQ_MAGIC_BYTE):
        msg=struct.pack(fmt, magic,
            cmd, len(key), len(extraHeader), dtype, vbucketId,
                len(key) + len(extraHeader) + len(val), opaque, cas)
        self.s.send(msg + extraHeader + key + val)<commit_msg>MB-29412 Use sendall instead of send<commit_after>#!/usr/bin/env python
"""
Binary memcached test client.

Copyright (c) 2007  <NAME> <<EMAIL>>
"""

import exceptions
import hmac
import json
import random
import re
import select
import socket
import struct
import sys
import time

from memcacheConstants import REQ_MAGIC_BYTE, RES_MAGIC_BYTE
from memcacheConstants import REQ_PKT_FMT, RES_PKT_FMT, MIN_RECV_PACKET
from memcacheConstants import SET_PKT_FMT, DEL_PKT_FMT, INCRDECR_RES_FMT
from memcacheConstants import TOUCH_PKT_FMT, GAT_PKT_FMT, GETL_PKT_FMT
from memcacheConstants import COMPACT_DB_PKT_FMT
import memcacheConstants

def parse_address(addr):
    """Parse a host string with optional port number into a
    (host, port, family) triple."""

    # Sane defaults
    family = socket.AF_UNSPEC
    port = 11210
    # Is this IPv6?
    if addr.startswith('['):
        matches = re.match(r'^\[([^\]]+)\](:(\d+))?$', addr)
        family = socket.AF_INET6
    else:
        matches = re.match(r'^([^:]+)(:(\d+))?$', addr)
    if matches:
        # The host is the first group
        host = matches.group(1)
        # Optional port is the 3rd group
        if matches.group(3):
            port = int(matches.group(3))
    else:
        raise Exception("Invalid format for host string: '{}'".format(addr))

    return host, port, family

class TimeoutError(exceptions.Exception):
    def __init__(self, time):
        exceptions.Exception.__init__(self, "Operation timed out")
        self.time = time

    def __str__(self):
        return self.__repr__()

    def __repr__(self):
        str = 'Error: Operation timed out (%d seconds)\n' % self.time
        str += 'Please check list of arguments (e.g., IP address, port number) '
        str += 'passed or the connectivity to a server to be connected'
        return str


# This metaclass causes any instantiation of MemcachedError to transparently
# construct the appropriate subclass if the error code is known.
class MemcachedErrorMetaclass(type):
    _dispatch = {}
    def __new__(meta, name, bases, dct):
        cls = (super(MemcachedErrorMetaclass, meta)
               .__new__(meta, name, bases, dct))
        if 'ERRCODE' in dct:
            meta._dispatch[dct['ERRCODE']] = cls
        return cls

    def __call__(cls, *args, **kwargs):
        err = None
        if 'status' in kwargs:
            err = kwargs['status']
        elif len(args):
            err = args[0]

        if err in cls._dispatch:
            cls = cls._dispatch[err]

        return (super(MemcachedErrorMetaclass, cls)
                .__call__(*args, **kwargs))

class MemcachedError(exceptions.Exception):
    """Error raised when a command fails."""

    __metaclass__ = MemcachedErrorMetaclass

    def __init__(self, status, msg):
        supermsg='Memcached error #' + repr(status)
        if msg: supermsg += ":  " + msg
        exceptions.Exception.__init__(self, supermsg)

        self.status=status
        self.msg=msg

    def __repr__(self):
        return "<MemcachedError #%d ``%s''>" % (self.status, self.msg)

class ErrorKeyEnoent(MemcachedError): ERRCODE = 0x1
class ErrorKeyEexists(MemcachedError): ERRCODE = 0x2
class ErrorE2big(MemcachedError): ERRCODE = 0x3
class ErrorEinval(MemcachedError): ERRCODE = 0x4
class ErrorNotStored(MemcachedError): ERRCODE = 0x5
class ErrorDeltaBadval(MemcachedError): ERRCODE = 0x6
class ErrorNotMyVbucket(MemcachedError): ERRCODE = 0x7
class ErrorNoBucket(MemcachedError): ERRCODE = 0x8
class ErrorLocked(MemcachedError): ERRCODE = 0x9
class ErrorAuthStale(MemcachedError): ERRCODE = 0x1f
class ErrorAuthError(MemcachedError): ERRCODE = 0x20
class ErrorAuthContinue(MemcachedError): ERRCODE = 0x21
class ErrorErange(MemcachedError): ERRCODE = 0x22
class ErrorRollback(MemcachedError): ERRCODE = 0x23
class ErrorEaccess(MemcachedError): ERRCODE = 0x24
class ErrorNotInitialized(MemcachedError): ERRCODE = 0x25
class ErrorUnknownCommand(MemcachedError): ERRCODE = 0x81
class ErrorEnomem(MemcachedError): ERRCODE = 0x82
class ErrorNotSupported(MemcachedError): ERRCODE = 0x83
class ErrorEinternal(MemcachedError): ERRCODE = 0x84
class ErrorEbusy(MemcachedError): ERRCODE = 0x85
class ErrorEtmpfail(MemcachedError): ERRCODE = 0x86
class ErrorXattrEinval(MemcachedError): ERRCODE = 0x87
class ErrorUnknownCollection(MemcachedError): ERRCODE = 0x88
class ErrorSubdocPathEnoent(MemcachedError): ERRCODE = 0xc0
class ErrorSubdocPathMismatch(MemcachedError): ERRCODE = 0xc1
class ErrorSubdocPathEinval(MemcachedError): ERRCODE = 0xc2
class ErrorSubdocPathE2big(MemcachedError): ERRCODE = 0xc3
class ErrorSubdocPathE2deep(MemcachedError): ERRCODE = 0xc4
class ErrorSubdocValueCantinsert(MemcachedError): ERRCODE = 0xc5
class ErrorSubdocDocNotjson(MemcachedError): ERRCODE = 0xc6
class ErrorSubdocNumErange(MemcachedError): ERRCODE = 0xc7
class ErrorSubdocDeltaEinval(MemcachedError): ERRCODE = 0xc8
class ErrorSubdocPathEexists(MemcachedError): ERRCODE = 0xc9
class ErrorSubdocValueEtoodeep(MemcachedError): ERRCODE = 0xca
class ErrorSubdocInvalidCombo(MemcachedError): ERRCODE = 0xcb
class ErrorSubdocMultiPathFailure(MemcachedError): ERRCODE = 0xcc
class ErrorSubdocSuccessDeleted(MemcachedError): ERRCODE = 0xcd
class ErrorSubdocXattrInvalidFlagCombo(MemcachedError): ERRCODE = 0xce
class ErrorSubdocXattrInvalidKeyCombo(MemcachedError): ERRCODE = 0xcf
class ErrorSubdocXattrUnknownMacro(MemcachedError): ERRCODE = 0xd0

class MemcachedClient(object):
    """Simple memcached client."""

    vbucketId = 0

    def __init__(self, host='127.0.0.1', port=11211, family=socket.AF_UNSPEC):
        self.host = host
        self.port = port

        # Iterate all addresses for the given family; using the first
        # one we can successfully connect to. If none succeed raise
        # the last attempted as an exception.
        for info in socket.getaddrinfo(self.host, self.port, family,
                                       socket.SOCK_STREAM):
            _family, socktype, proto, _, sockaddr = info
            try:
                sock = socket.socket(_family, socktype, proto)
                sock.settimeout(10)
                sock.connect(sockaddr)
                self.s = sock
                break
            except socket.error as sock_error:
                # If we get here socket objects will be close()d via
                # garbage collection.
                pass
        else:
            # Didn't break from the loop, re-raise the last error
            raise sock_error

        self.s.setblocking(0)
        self.r=random.Random()
        self.req_features = set()
        self.features = set()
        self.error_map = None
        self.error_map_version = 1

    def close(self):
        self.s.close()

    def __del__(self):
        self.close()

    def _sendCmd(self, cmd, key, val, opaque, extraHeader='', cas=0):
        self._sendMsg(cmd, key, val, opaque, extraHeader=extraHeader, cas=cas,
                      vbucketId=self.vbucketId)

    def _sendMsg(self, cmd, key, val, opaque, extraHeader='', cas=0,
                 dtype=0, vbucketId=0,
                 fmt=REQ_PKT_FMT, magic=REQ_MAGIC_BYTE):
        msg=struct.pack(fmt, magic,
            cmd, len(key), len(extraHeader), dtype, vbucketId,
                len(key) + len(extraHeader) + len(val), opaque, cas)
        self.s.sendall(msg + extraHeader + key + val)
{'fraction_non_alphanumeric': 0.09378400435255713, 'fraction_numerical': 0.017886289445048966, 'mean_word_length': 3.3339227821986444, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1772114', 'n_tokens_mistral': 4650, 'n_tokens_neox': 4441, 'n_words': 1194}