
import contextlib
import rdb_workload_common

@contextlib.contextmanager
def make_memcache_connection(opts):
    with rdb_workload_common.make_table_and_connection(opts) as (table, conn):
        yield MemcacheRdbShim(table, conn)

class MemcacheRdbShim(object):
    def __init__(self, table, conn):
        self.table = table
        self.conn = conn

    def get(self, key):
        response = self.table.get(key).run(self.conn)
        if response:
            return response['val']

    def set(self, key, val):
        response = self.table.insert({
            'id': key,
            'val': val
            },
            upsert=True
            ).run(self.conn)

        error = response.get('first_error')
        if error:
            raise Exception(error)

        return response['inserted'] | response['replaced'] | response['unchanged']

    def delete(self, key):
        response = self.table.get(key).delete().run(self.conn)

        error = response.get('first_error')
        if error:
            raise Exception(error)

        return response['deleted']


def option_parser_for_memcache():
    return rdb_workload_common.option_parser_for_connect()


import contextlib
import rdb_workload_common

@contextlib.contextmanager
def make_memcache_connection(opts):
    with rdb_workload_common.make_table_and_connection(opts) as (table, conn):
        yield MemcacheRdbShim(table, conn)

class MemcacheRdbShim(object):
    def __init__(self, table, conn):
        self.table = table
        self.conn = conn

    def get(self, key):
        response = self.table.get(key).run(self.conn)
        if response:
            return response['val']

    def set(self, key, val):
        response = self.table.insert({
            'id': key,
            'val': val
            },
            conflict='replace'
            ).run(self.conn)

        error = response.get('first_error')
        if error:
            raise Exception(error)

        return response['inserted'] | response['replaced'] | response['unchanged']

    def delete(self, key):
        response = self.table.get(key).delete().run(self.conn)

        error = response.get('first_error')
        if error:
            raise Exception(error)

        return response['deleted']


def option_parser_for_memcache():
    return rdb_workload_common.option_parser_for_connect()

Replace upsert=True with conflict='replace' in tests
Replace upsert=True with conflict='replace' in tests

Review 1804 by @gchpaco
Related to #2733

  
  import contextlib
  import rdb_workload_common
  
  @contextlib.contextmanager
  def make_memcache_connection(opts):
      with rdb_workload_common.make_table_and_connection(opts) as (table, conn):
          yield MemcacheRdbShim(table, conn)
  
  class MemcacheRdbShim(object):
      def __init__(self, table, conn):
          self.table = table
          self.conn = conn
  
      def get(self, key):
          response = self.table.get(key).run(self.conn)
          if response:
              return response['val']
  
      def set(self, key, val):
          response = self.table.insert({
              'id': key,
              'val': val
              },
-             upsert=True
+             conflict='replace'
              ).run(self.conn)
  
          error = response.get('first_error')
          if error:
              raise Exception(error)
  
          return response['inserted'] | response['replaced'] | response['unchanged']
  
      def delete(self, key):
          response = self.table.get(key).delete().run(self.conn)
  
          error = response.get('first_error')
          if error:
              raise Exception(error)
  
          return response['deleted']
  
  
  def option_parser_for_memcache():
      return rdb_workload_common.option_parser_for_connect()
  
Replace upsert=True with conflict='replace' in tests
## Code Before:

import contextlib
import rdb_workload_common

@contextlib.contextmanager
def make_memcache_connection(opts):
    with rdb_workload_common.make_table_and_connection(opts) as (table, conn):
        yield MemcacheRdbShim(table, conn)

class MemcacheRdbShim(object):
    def __init__(self, table, conn):
        self.table = table
        self.conn = conn

    def get(self, key):
        response = self.table.get(key).run(self.conn)
        if response:
            return response['val']

    def set(self, key, val):
        response = self.table.insert({
            'id': key,
            'val': val
            },
            upsert=True
            ).run(self.conn)

        error = response.get('first_error')
        if error:
            raise Exception(error)

        return response['inserted'] | response['replaced'] | response['unchanged']

    def delete(self, key):
        response = self.table.get(key).delete().run(self.conn)

        error = response.get('first_error')
        if error:
            raise Exception(error)

        return response['deleted']


def option_parser_for_memcache():
    return rdb_workload_common.option_parser_for_connect()

## Instruction:
Replace upsert=True with conflict='replace' in tests
## Code After:

import contextlib
import rdb_workload_common

@contextlib.contextmanager
def make_memcache_connection(opts):
    with rdb_workload_common.make_table_and_connection(opts) as (table, conn):
        yield MemcacheRdbShim(table, conn)

class MemcacheRdbShim(object):
    def __init__(self, table, conn):
        self.table = table
        self.conn = conn

    def get(self, key):
        response = self.table.get(key).run(self.conn)
        if response:
            return response['val']

    def set(self, key, val):
        response = self.table.insert({
            'id': key,
            'val': val
            },
            conflict='replace'
            ).run(self.conn)

        error = response.get('first_error')
        if error:
            raise Exception(error)

        return response['inserted'] | response['replaced'] | response['unchanged']

    def delete(self, key):
        response = self.table.get(key).delete().run(self.conn)

        error = response.get('first_error')
        if error:
            raise Exception(error)

        return response['deleted']


def option_parser_for_memcache():
    return rdb_workload_common.option_parser_for_connect()
