text:
from collections import MutableMapping
import ctypes as ct
from functools import reduce
import multiprocessing
import os

from .libbcc import lib, _RAW_CB_TYPE, _LOST_CB_TYPE
from .perf import Perf
from .utils import get_online_cpus
from .utils import get_possible_cpus
from subprocess import check_output

BPF_MAP_TYPE_HASH = 1
BPF_MAP_TYPE_ARRAY = 2
BPF_MAP_TYPE_PROG_ARRAY = 3
BPF_MAP_TYPE_PERF_EVENT_ARRAY = 4
BPF_MAP_TYPE_PERCPU_HASH = 5
BPF_MAP_TYPE_PERCPU_ARRAY = 6
BPF_MAP_TYPE_STACK_TRACE = 7
BPF_MAP_TYPE_CGROUP_ARRAY = 8
BPF_MAP_TYPE_LRU_HASH = 9
BPF_MAP_TYPE_LRU_PERCPU_HASH = 10
BPF_MAP_TYPE_LPM_TRIE = 11

stars_max = 40
log2_index_max = 65
linear_index_max = 1025

# helper functions, consider moving these to a utils module
def _stars(val, val_max, width):
    i = 0
    text = ""
    while (1):
        if (i > (width * val / val_max) - 1) or (i > width - 1):
            break
        text += "*"
        i += 1
    if val > val_max:
        text = text[:-1] + "+"
    return text

def _print_log2_hist(vals, val_type):
    global stars_max
    log2_dist_max = 64
    idx_max = -1
    val_max = 0

    for i, v in enumerate(vals):
        if v > 0: idx_max = i
        if v > val_max: val_max = v

    if idx_max <= 32:
        header = "     %-19s : count     distribution"
        body = "%10d -> %-10d : %-8d |%-*s|"
        stars = stars_max
    else:
        header = "               %-29s : count     distribution"
        body = "%20d -> %-20d : %-8d |%-*s|"
        stars = int(stars_max / 2)

    if idx_max > 0:
        print(header % val_type);
    for i in range(1, idx_max + 1):
        low = (1 << i) >> 1
        high = (1 << i) - 1
        if (low == high):
            low -= 1
        val = vals[i]
        print(body % (low, high, val, stars,
                      _stars(val, val_max, stars)))

def _print_linear_hist(vals, val_type):
    global stars_max
    log2_dist_max = 64
    idx_max = -1
    val_max = 0

    for i, v in enumerate(vals):
        if v > 0: idx_max = i
        if v > val_max: val_max = v

    header = "     %-13s : count     distribution"
    body = "        %-10d : %-8d |%-*s|"
    stars = stars_max

    if idx_max >= 0:
        print(header % val_type);
    for i in range(0, idx_max + 1):
        val = vals[i]
        print(body % (i, val, stars,
                      _stars(val, val_max, stars)))

def Table(bpf, map_id, map_fd, keytype, leaftype, **kwargs):
    """Table(bpf, map_id, map_fd, keytype, leaftype, **kwargs)

    Create a python object out of a reference to a bpf table handle"""

    ttype = lib.bpf_table_type_id(bpf.module, map_id)
    t = None
    if ttype == BPF_MAP_TYPE_HASH:
        t = HashTable(bpf, map_id, map_fd, keytype, leaftype)
    elif ttype == BPF_MAP_TYPE_ARRAY:
        t = Array(bpf, map_id, map_fd, keytype, leaftype)
    elif ttype == BPF_MAP_TYPE_PROG_ARRAY:
        t = ProgArray(bpf, map_id, map_fd, keytype, leaftype)
    elif ttype == BPF_MAP_TYPE_PERF_EVENT_ARRAY:
        t = PerfEventArray(bpf, map_id, map_fd, keytype, leaftype)
    elif ttype == BPF_MAP_TYPE_PERCPU_HASH:
        t = PerCpuHash(bpf, map_id, map_fd, keytype, leaftype, **kwargs)
    elif ttype == BPF_MAP_TYPE_PERCPU_ARRAY:
        t = PerCpuArray(bpf, map_id, map_fd, keytype, leaftype, **kwargs)
    elif ttype == BPF_MAP_TYPE_LPM_TRIE:
        t = LpmTrie(bpf, map_id, map_fd, keytype, leaftype)
    elif ttype == BPF_MAP_TYPE_STACK_TRACE:
        t = StackTrace(bpf, map_id, map_fd, keytype, leaftype)
    elif ttype == BPF_MAP_TYPE_LRU_HASH:
        t = LruHash(bpf, map_id, map_fd, keytype, leaftype)
    elif ttype == BPF_MAP_TYPE_LRU_PERCPU_HASH:
        t = LruPerCpuHash(bpf, map_id, map_fd, keytype, leaftype)
    if t == None:
        raise Exception("Unknown table type %d" % ttype)
    return t

class TableBase(MutableMapping):

    def __init__(self, bpf, map_id, map_fd, keytype, leaftype):
        self.bpf = bpf
        self.map_id = map_id
        self.map_fd = map_fd
        self.Key = keytype
        self.Leaf = leaftype
        self.ttype = lib.bpf_table_type_id(self.bpf.module, self.map_id)
        self.flags = lib.bpf_table_flags_id(self.bpf.module, self.map_id)
        self._cbs = {}

    def key_sprintf(self, key):
        key_p = ct.pointer(key)
        buf = ct.create_string_buffer(ct.sizeof(self.Key) * 8)
        res = lib.bpf_table_key_snprintf(self.bpf.module, self.map_id,
                buf, len(buf), key_p)
        if res < 0:
            raise Exception("Could not printf key")
        return buf.value

    def leaf_sprintf(self, leaf):
        leaf_p = ct.pointer(leaf)
        buf = ct.create_string_buffer(ct.sizeof(self.Leaf) * 8)
        res = lib.bpf_table_leaf_snprintf(self.bpf.module, self.map_id,
                buf, len(buf), leaf_p)
        if res < 0:
            raise Exception("Could not printf leaf")
        return buf.value

    def key_scanf(self, key_str):
        key = self.Key()
        key_p = ct.pointer(key)
        res = lib.bpf_table_key_sscanf(self.bpf.module, self.map_id,
                key_str, key_p)
        if res < 0:
            raise Exception("Could not scanf key")
        return key

    def leaf_scanf(self, leaf_str):
        leaf = self.Leaf()
        leaf_p = ct.pointer(leaf)
        res = lib.bpf_table_leaf_sscanf(self.bpf.module, self.map_id,
                leaf_str, leaf_p)
        if res < 0:
            raise Exception("Could not scanf leaf")
        return leaf

    def __getitem__(self, key):
        key_p = ct.pointer(key)
        leaf = self.Leaf()
        leaf_p = ct.pointer(leaf)
        res = lib.bpf_lookup_elem(self.map_fd,
                ct.cast(key_p, ct.c_void_p),
                ct.cast(leaf_p, ct.c_void_p))
        if res < 0:
            raise KeyError
        return leaf

    def __setitem__(self, key, leaf):
        key_p = ct.pointer(key)
        leaf_p = ct.pointer(leaf)
        res = lib.bpf_update_elem(self.map_fd,
                ct.cast(key_p, ct.c_void_p),
                ct.cast(leaf_p, ct.c_void_p), 0)
        if res < 0:
            errstr = os.strerror(ct.get_errno())
            raise Exception("Could not update table: %s" % errstr)

    # override the MutableMapping's implementation of these since they
    # don't handle KeyError nicely
    def itervalues(self):
        for key in self:
            # a map entry may be deleted in between discovering the key and
            # fetching the value, suppress such errors
            try:
                yield self[key]
            except KeyError:
                pass

    def iteritems(self):
        for key in self:
            try:
                yield (key, self[key])
            except KeyError:
                pass

    def items(self):
        return [item for item in self.iteritems()]

    def values(self):
        return [value for value in self.itervalues()]

    def clear(self):
        # default clear uses popitem, which can race with the bpf prog
        for k in self.keys():
            self.__delitem__(k)

    def zero(self):
        # Even though this is not very efficient, we grab the entire list of
        # keys before enumerating it. This helps avoid a potential race where
        # the leaf assignment changes a hash table bucket that is being
        # enumerated by the same loop, and may lead to a hang.
        for k in list(self.keys()):
            self[k] = self.Leaf()

    def __iter__(self):
        return TableBase.Iter(self, self.Key)

    def iter(self): return self.__iter__()
    def keys(self): return self.__iter__()

    class Iter(object):
        def __init__(self, table, keytype):
            self.Key = keytype
            self.table = table
            k = self.Key()
            kp = ct.pointer(k)
            # if 0 is a valid key, try a few alternatives
            if k in table:
                ct.memset(kp, 0xff, ct.sizeof(k))
                if k in table:
                    ct.memset(kp, 0x55, ct.sizeof(k))
                    if k in table:
                        raise Exception("Unable to allocate iterator")
            self.key = k
        def __iter__(self):
            return self
        def __next__(self):
            return self.next()
        def next(self):
            self.key = self.table.next(self.key)
            return self.key

    def next(self, key):
        next_key = self.Key()
        next_key_p = ct.pointer(next_key)
        key_p = ct.pointer(key)
        res = lib.bpf_get_next_key(self.map_fd,
                ct.cast(key_p, ct.c_void_p),
                ct.cast(next_key_p, ct.c_void_p))
        if res < 0:
            raise StopIteration()
        return next_key

    def print_log2_hist(self, val_type="value", section_header="Bucket ptr",
            section_print_fn=None, bucket_fn=None):
        """print_log2_hist(val_type="value", section_header="Bucket ptr",
                           section_print_fn=None, bucket_fn=None)

        Prints a table as a log2 histogram. The table must be stored as
        log2. The val_type argument is optional, and is a column header.
        If the histogram has a secondary key, multiple tables will print
        and section_header can be used as a header description for each.
        If section_print_fn is not None, it will be passed the bucket value
        to format into a string as it sees fit. If bucket_fn is not None,
        it will be used to produce a bucket value for the histogram keys.
        The maximum index allowed is log2_index_max (65), which will
        accomodate any 64-bit integer in the histogram.
        """
        if isinstance(self.Key(), ct.Structure):
            tmp = {}
            f1 = self.Key._fields_[0][0]
            f2 = self.Key._fields_[1][0]
            for k, v in self.items():
                bucket = getattr(k, f1)
                if bucket_fn:
                    bucket = bucket_fn(bucket)
                vals = tmp[bucket] = tmp.get(bucket, [0] * log2_index_max)
                slot = getattr(k, f2)
                vals[slot] = v.value
            for bucket, vals in tmp.items():
                if section_print_fn:
                    print("\n%s = %s" % (section_header,
                        section_print_fn(bucket)))
                else:
                    print("\n%s = %r" % (section_header, bucket))
                _print_log2_hist(vals, val_type)
        else:
            vals = [0] * log2_index_max
            for k, v in self.items():
                vals[k.value] = v.value
            _print_log2_hist(vals, val_type)

    def print_linear_hist(self, val_type="value", section_header="Bucket ptr",
            section_print_fn=None, bucket_fn=None):
        """print_linear_hist(val_type="value", section_header="Bucket ptr",
                           section_print_fn=None, bucket_fn=None)

        Prints a table as a linear histogram. This is intended to span integer
        ranges, eg, from 0 to 100. The val_type argument is optional, and is a
        column header.  If the histogram has a secondary key, multiple tables
        will print and section_header can be used as a header description for
        each.  If section_print_fn is not None, it will be passed the bucket
        value to format into a string as it sees fit. If bucket_fn is not None,
        it will be used to produce a bucket value for the histogram keys.
        The maximum index allowed is linear_index_max (1025), which is hoped
        to be sufficient for integer ranges spanned.
        """
        if isinstance(self.Key(), ct.Structure):
            tmp = {}
            f1 = self.Key._fields_[0][0]
            f2 = self.Key._fields_[1][0]
            for k, v in self.items():
                bucket = getattr(k, f1)
                if bucket_fn:
                    bucket = bucket_fn(bucket)
                vals = tmp[bucket] = tmp.get(bucket, [0] * linear_index_max)
                slot = getattr(k, f2)
                vals[slot] = v.value
            for bucket, vals in tmp.items():
                if section_print_fn:
                    print("\n%s = %s" % (section_header,
                        section_print_fn(bucket)))
                else:
                    print("\n%s = %r" % (section_header, bucket))
                _print_linear_hist(vals, val_type)
        else:
            vals = [0] * linear_index_max
            for k, v in self.items():
                try:
                    vals[k.value] = v.value
                except IndexError:
                    # Improve error text. If the limit proves a nusiance, this
                    # function be rewritten to avoid having one.
                    raise IndexError(("Index in print_linear_hist() of %d " +
                        "exceeds max of %d.") % (k.value, linear_index_max))
            _print_linear_hist(vals, val_type)

class HashTable(TableBase):
    def __init__(self, *args, **kwargs):
        super(HashTable, self).__init__(*args, **kwargs)

    def __len__(self):
        i = 0
        for k in self: i += 1
        return i

    def __delitem__(self, key):
        key_p = ct.pointer(key)
        res = lib.bpf_delete_elem(self.map_fd, ct.cast(key_p, ct.c_void_p))
        if res < 0:
            raise KeyError

class LruHash(HashTable):
    def __init__(self, *args, **kwargs):
        super(LruHash, self).__init__(*args, **kwargs)

class ArrayBase(TableBase):
    def __init__(self, *args, **kwargs):
        super(ArrayBase, self).__init__(*args, **kwargs)
        self.max_entries = int(lib.bpf_table_max_entries_id(self.bpf.module,
                self.map_id))

    def _normalize_key(self, key):
        if isinstance(key, int):
            if key < 0:
                key = len(self) + key
            key = self.Key(key)
        if not isinstance(key, ct._SimpleCData):
            raise IndexError("Array index must be an integer type")
        if key.value >= len(self):
            raise IndexError("Array index out of range")
        return key

    def __len__(self):
        return self.max_entries

    def __getitem__(self, key):
        key = self._normalize_key(key)
        return super(ArrayBase, self).__getitem__(key)

    def __setitem__(self, key, leaf):
        key = self._normalize_key(key)
        super(ArrayBase, self).__setitem__(key, leaf)

    def __delitem__(self, key):
        key = self._normalize_key(key)
        key_p = ct.pointer(key)

        # Deleting from array type maps does not have an effect, so
        # zero out the entry instead.
        leaf = self.Leaf()
        leaf_p = ct.pointer(leaf)
        res = lib.bpf_update_elem(self.map_fd, ct.cast(key_p, ct.c_void_p),
                ct.cast(leaf_p, ct.c_void_p), 0)
        if res < 0:
            raise Exception("Could not clear item")

    def __iter__(self):
        return ArrayBase.Iter(self, self.Key)

    class Iter(object):
        def __init__(self, table, keytype):
            self.Key = keytype
            self.table = table
            self.i = -1

        def __iter__(self):
            return self
        def __next__(self):
            return self.next()
        def next(self):
            self.i += 1
            if self.i == len(self.table):
                raise StopIteration()
            return self.Key(self.i)

class Array(ArrayBase):
    def __init__(self, *args, **kwargs):
        super(Array, self).__init__(*args, **kwargs)

class ProgArray(ArrayBase):
    def __init__(self, *args, **kwargs):
        super(ProgArray, self).__init__(*args, **kwargs)

    def __setitem__(self, key, leaf):
        if isinstance(leaf, int):
            leaf = self.Leaf(leaf)
        if isinstance(leaf, self.bpf.Function):
            leaf = self.Leaf(leaf.fd)
        super(ProgArray, self).__setitem__(key, leaf)

    def __delitem__(self, key):
        key = self._normalize_key(key)
        key_p = ct.pointer(key)
        res = lib.bpf_delete_elem(self.map_fd, ct.cast(key_p, ct.c_void_p))
        if res < 0:
            raise Exception("Could not delete item")

class PerfEventArray(ArrayBase):
    class Event(object):
        def __init__(self, typ, config):
            self.typ = typ
            self.config = config

    HW_CPU_CYCLES                = Event(Perf.PERF_TYPE_HARDWARE, 0)
    HW_INSTRUCTIONS              = Event(Perf.PERF_TYPE_HARDWARE, 1)
    HW_CACHE_REFERENCES          = Event(Perf.PERF_TYPE_HARDWARE, 2)
    HW_CACHE_MISSES              = Event(Perf.PERF_TYPE_HARDWARE, 3)
    HW_BRANCH_INSTRUCTIONS       = Event(Perf.PERF_TYPE_HARDWARE, 4)
    HW_BRANCH_MISSES             = Event(Perf.PERF_TYPE_HARDWARE, 5)
    HW_BUS_CYCLES                = Event(Perf.PERF_TYPE_HARDWARE, 6)
    HW_STALLED_CYCLES_FRONTEND   = Event(Perf.PERF_TYPE_HARDWARE, 7)
    HW_STALLED_CYCLES_BACKEND    = Event(Perf.PERF_TYPE_HARDWARE, 8)
    HW_REF_CPU_CYCLES            = Event(Perf.PERF_TYPE_HARDWARE, 9)

    # not yet supported, wip
    #HW_CACHE_L1D_READ        = Event(Perf.PERF_TYPE_HW_CACHE, 0<<0|0<<8|0<<16)
    #HW_CACHE_L1D_READ_MISS   = Event(Perf.PERF_TYPE_HW_CACHE, 0<<0|0<<8|1<<16)
    #HW_CACHE_L1D_WRITE       = Event(Perf.PERF_TYPE_HW_CACHE, 0<<0|1<<8|0<<16)
    #HW_CACHE_L1D_WRITE_MISS  = Event(Perf.PERF_TYPE_HW_CACHE, 0<<0|1<<8|1<<16)
    #HW_CACHE_L1D_PREF        = Event(Perf.PERF_TYPE_HW_CACHE, 0<<0|2<<8|0<<16)
    #HW_CACHE_L1D_PREF_MISS   = Event(Perf.PERF_TYPE_HW_CACHE, 0<<0|2<<8|1<<16)
    #HW_CACHE_L1I_READ        = Event(Perf.PERF_TYPE_HW_CACHE, 1<<0|0<<8|0<<16)
    #HW_CACHE_L1I_READ_MISS   = Event(Perf.PERF_TYPE_HW_CACHE, 1<<0|0<<8|1<<16)
    #HW_CACHE_L1I_WRITE       = Event(Perf.PERF_TYPE_HW_CACHE, 1<<0|1<<8|0<<16)
    #HW_CACHE_L1I_WRITE_MISS  = Event(Perf.PERF_TYPE_HW_CACHE, 1<<0|1<<8|1<<16)
    #HW_CACHE_L1I_PREF        = Event(Perf.PERF_TYPE_HW_CACHE, 1<<0|2<<8|0<<16)
    #HW_CACHE_L1I_PREF_MISS   = Event(Perf.PERF_TYPE_HW_CACHE, 1<<0|2<<8|1<<16)
    #HW_CACHE_LL_READ         = Event(Perf.PERF_TYPE_HW_CACHE, 2<<0|0<<8|0<<16)
    #HW_CACHE_LL_READ_MISS    = Event(Perf.PERF_TYPE_HW_CACHE, 2<<0|0<<8|1<<16)
    #HW_CACHE_LL_WRITE        = Event(Perf.PERF_TYPE_HW_CACHE, 2<<0|1<<8|0<<16)
    #HW_CACHE_LL_WRITE_MISS   = Event(Perf.PERF_TYPE_HW_CACHE, 2<<0|1<<8|1<<16)
    #HW_CACHE_LL_PREF         = Event(Perf.PERF_TYPE_HW_CACHE, 2<<0|2<<8|0<<16)
    #HW_CACHE_LL_PREF_MISS    = Event(Perf.PERF_TYPE_HW_CACHE, 2<<0|2<<8|1<<16)

    def __init__(self, *args, **kwargs):
        super(PerfEventArray, self).__init__(*args, **kwargs)

    def __delitem__(self, key):
        super(PerfEventArray, self).__delitem__(key)
        self.close_perf_buffer(key)

    def open_perf_buffer(self, callback, page_cnt=8, lost_cb=None):
        """open_perf_buffers(callback)

        Opens a set of per-cpu ring buffer to receive custom perf event
        data from the bpf program. The callback will be invoked for each
        event submitted from the kernel, up to millions per second. Use
        page_cnt to change the size of the per-cpu ring buffer. The value
        must be a power of two and defaults to 8.
        """

        if page_cnt & (page_cnt - 1) != 0:
            raise Exception("Perf buffer page_cnt must be a power of two")

        for i in get_online_cpus():
            self._open_perf_buffer(i, callback, page_cnt, lost_cb)

    def _open_perf_buffer(self, cpu, callback, page_cnt, lost_cb):
        fn = _RAW_CB_TYPE(lambda _, data, size: callback(cpu, data, size))
        lost_fn = _LOST_CB_TYPE(lambda lost: lost_cb(lost)) if lost_cb else ct.cast(None, _LOST_CB_TYPE)
        reader = lib.bpf_open_perf_buffer(fn, lost_fn, None, -1, cpu, page_cnt)
        if not reader:
            raise Exception("Could not open perf buffer")
        fd = lib.perf_reader_fd(reader)
        self[self.Key(cpu)] = self.Leaf(fd)
        self.bpf._add_kprobe((id(self), cpu), reader)
        # keep a refcnt
        self._cbs[cpu] = (fn, lost_fn)

    def close_perf_buffer(self, key):
        reader = self.bpf.open_kprobes.get((id(self), key))
        if reader:
            lib.perf_reader_free(reader)
            self.bpf._del_kprobe((id(self), key))
        del self._cbs[key]

    def _open_perf_event(self, cpu, typ, config):
        fd = lib.bpf_open_perf_event(typ, config, -1, cpu)
        if fd < 0:
            raise Exception("bpf_open_perf_event failed")
        try:
            self[self.Key(cpu)] = self.Leaf(fd)
        finally:
            # the fd is kept open in the map itself by the kernel
            os.close(fd)

    def open_perf_event(self, ev):
        """open_perf_event(ev)

        Configures the table such that calls from the bpf program to
        table.perf_read(bpf_get_smp_processor_id()) will return the hardware
        counter denoted by event ev on the local cpu.
        """
        if not isinstance(ev, self.Event):
            raise Exception("argument must be an Event, got %s", type(ev))

        for i in get_online_cpus():
            self._open_perf_event(i, ev.typ, ev.config)

class PerCpuHash(HashTable):
    def __init__(self, *args, **kwargs):
        self.reducer = kwargs.pop("reducer", None)
        super(PerCpuHash, self).__init__(*args, **kwargs)
        self.sLeaf = self.Leaf
        self.total_cpu = len(get_possible_cpus())
        # This needs to be 8 as hard coded into the linux kernel.
        self.alignment = ct.sizeof(self.sLeaf) % 8
        if self.alignment is 0:
            self.Leaf = self.sLeaf * self.total_cpu
        else:
            # Currently Float, Char, un-aligned structs are not supported
            if self.sLeaf == ct.c_uint:
                self.Leaf = ct.c_uint64 * self.total_cpu
            elif self.sLeaf == ct.c_int:
                self.Leaf = ct.c_int64 * self.total_cpu
            else:
                raise IndexError("Leaf must be aligned to 8 bytes")

    def getvalue(self, key):
        result = super(PerCpuHash, self).__getitem__(key)
        if self.alignment is 0:
            ret = result
        else:
            ret = (self.sLeaf * self.total_cpu)()
            for i in range(0, self.total_cpu):
                ret[i] = result[i]
        return ret

    def __getitem__(self, key):
        if self.reducer:
            return reduce(self.reducer, self.getvalue(key))
        else:
            return self.getvalue(key)

    def __setitem__(self, key, leaf):
        super(PerCpuHash, self).__setitem__(key, leaf)

    def sum(self, key):
        if isinstance(self.Leaf(), ct.Structure):
            raise IndexError("Leaf must be an integer type for default sum functions")
        return self.sLeaf(sum(self.getvalue(key)))

    def max(self, key):
        if isinstance(self.Leaf(), ct.Structure):
            raise IndexError("Leaf must be an integer type for default max functions")
        return self.sLeaf(max(self.getvalue(key)))

    def average(self, key):
        result = self.sum(key)
        return result.value / self.total_cpu

class LruPerCpuHash(PerCpuHash):
    def __init__(self, *args, **kwargs):
        super(LruPerCpuHash, self).__init__(*args, **kwargs)

class PerCpuArray(ArrayBase):
    def __init__(self, *args, **kwargs):
        self.reducer = kwargs.pop("reducer", None)
        super(PerCpuArray, self).__init__(*args, **kwargs)
        self.sLeaf = self.Leaf
        self.total_cpu = len(get_possible_cpus())
        # This needs to be 8 as hard coded into the linux kernel.
        self.alignment = ct.sizeof(self.sLeaf) % 8
        if self.alignment is 0:
            self.Leaf = self.sLeaf * self.total_cpu
        else:
            # Currently Float, Char, un-aligned structs are not supported
            if self.sLeaf == ct.c_uint:
                self.Leaf = ct.c_uint64 * self.total_cpu
            elif self.sLeaf == ct.c_int:
                self.Leaf = ct.c_int64 * self.total_cpu
            else:
                raise IndexError("Leaf must be aligned to 8 bytes")

    def getvalue(self, key):
        result = super(PerCpuArray, self).__getitem__(key)
        if self.alignment is 0:
            ret = result
        else:
            ret = (self.sLeaf * self.total_cpu)()
            for i in range(0, self.total_cpu):
                ret[i] = result[i]
        return ret

    def __getitem__(self, key):
        if (self.reducer):
            return reduce(self.reducer, self.getvalue(key))
        else:
            return self.getvalue(key)

    def __setitem__(self, key, leaf):
        super(PerCpuArray, self).__setitem__(key, leaf)

    def sum(self, key):
        if isinstance(self.Leaf(), ct.Structure):
            raise IndexError("Leaf must be an integer type for default sum functions")
        return self.sLeaf(sum(self.getvalue(key)))

    def max(self, key):
        if isinstance(self.Leaf(), ct.Structure):
            raise IndexError("Leaf must be an integer type for default max functions")
        return self.sLeaf(max(self.getvalue(key)))

    def average(self, key):
        result = self.sum(key)
        return result.value / self.total_cpu

class LpmTrie(TableBase):
    def __init__(self, *args, **kwargs):
        super(LpmTrie, self).__init__(*args, **kwargs)

    def __len__(self):
        raise NotImplementedError

    def __delitem__(self, key):
        # Not implemented for lpm trie as of kernel commit
        # b95a5c4db09bc7c253636cb84dc9b12c577fd5a0
        raise NotImplementedError

class StackTrace(TableBase):
    MAX_DEPTH = 127

    def __init__(self, *args, **kwargs):
        super(StackTrace, self).__init__(*args, **kwargs)

    class StackWalker(object):
        def __init__(self, stack, resolve=None):
            self.stack = stack
            self.n = -1
            self.resolve = resolve

        def __iter__(self):
            return self

        def __next__(self):
            return self.next()

        def next(self):
            self.n += 1
            if self.n == StackTrace.MAX_DEPTH:
                raise StopIteration()

            addr = self.stack.ip[self.n]
            if addr == 0 :
                raise StopIteration()

            return self.resolve(addr) if self.resolve else addr

    def walk(self, stack_id, resolve=None):
        return StackTrace.StackWalker(self[self.Key(stack_id)], resolve)

    def __len__(self):
        i = 0
        for k in self: i += 1
        return i

    def __delitem__(self, key):
        key_p = ct.pointer(key)
        res = lib.bpf_delete_elem(self.map_fd, ct.cast(key_p, ct.c_void_p))
        if res < 0:
            raise KeyError

    def clear(self):
        pass


text:
package org.cytoscape.d3.internal.serializer;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.cytoscape.model.CyRow;
import org.cytoscape.model.CyTable;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

public class D3RowSerializer extends JsonSerializer<CyRow> {

	private static final Pattern REPLACE_INVALID_JS_CHAR_PATTERN = Pattern.compile("^[^a-zA-Z_]+|[^a-zA-Z_0-9]+");

	@Override
	public void serialize(final CyRow row, JsonGenerator jgen, SerializerProvider provider) throws IOException,
			JsonProcessingException {

		final CyTable table = row.getTable();
		final Map<String, Object> values = row.getAllValues();

		for (final String columnName : values.keySet()) {
			final Object value = values.get(columnName);
			if (value == null)
				continue;

			Class<?> type = table.getColumn(columnName).getType();
			if (type == List.class) {
				type = table.getColumn(columnName).getListElementType();
				writeList(type, columnName, (List<?>) value, jgen);
			} else {
				jgen.writeFieldName(replaceColumnName(columnName));
				writeValue(type, value, jgen);
			}
		}
	}

	private void writeList(final Class<?> type, String columnName, List<?> values, JsonGenerator jgen)
			throws JsonGenerationException, IOException {
		jgen.writeFieldName(replaceColumnName(columnName));
		jgen.writeStartArray();

		for (Object value : values)
			writeValue(type, value, jgen);

		jgen.writeEndArray();
	}

	private final String replaceColumnName(final String columnName) {
		final Matcher matcher = REPLACE_INVALID_JS_CHAR_PATTERN.matcher(columnName);
		return matcher.replaceAll("_");
	}

	private final void writeValue(final Class<?> type, Object value, JsonGenerator jgen)
			throws JsonGenerationException, IOException {
		if (type == String.class) {
			jgen.writeString(value.toString());
		} else if (type == Boolean.class) {
			jgen.writeBoolean((Boolean) value);
		} else if (type == Double.class) {
			jgen.writeNumber((Double) value);
		} else if (type == Integer.class) {
			jgen.writeNumber((Integer) value);
		} else if (type == Long.class) {
			jgen.writeNumber((Long) value);
		} else if (type == Float.class) {
			jgen.writeNumber((Double) value);
		}
	}

	@Override
	public Class<CyRow> handledType() {
		return CyRow.class;
	}
}

redpajama_set_name: RedPajamaGithub


text:
(function($) {

  module('LocalStorageCollection#add()', {
    // This will run before each test in this module.
    setup: function() {
      this.elems = $('#qunit-fixture').children();

    }
  });

  asyncTest('alfter add() collection.content_arr length = 1', function() {
    expect(1);

		var collection = new Collection('message_test');
		// collection.drop();
		// collection.use_websql();
		collection.empty();

		var obj = {
			type :'type',
			mid :'mid',
			uid :'uid',
			uname :'uname',
			avatar :'avatar',
			sid :'sid',
			sname:'sname',
			timestamp :'timestamp',
			msg :'msg'
		}

		collection.add(obj);
		collection.add(obj);

		collection.save();

		setTimeout(function(){
			var content_arr = collection.all();

			equal(content_arr.length, 2, 'add a obj,collection.content_arr length should be 2');
			start();
		},200);
    // Not a bad test to run on collection methods.

  });

}(jQuery));


text:
package org.kaaproject.kaa.server.admin.client.mvp.activity;

import java.util.Collections;
import java.util.List;

import org.kaaproject.avro.ui.gwt.client.util.BusyAsyncCallback;
import org.kaaproject.avro.ui.gwt.client.widget.ActionsButton.ActionMenuItemListener;
import org.kaaproject.avro.ui.gwt.client.widget.dialog.ConfirmDialog;
import org.kaaproject.avro.ui.shared.RecordField;
import org.kaaproject.kaa.common.dto.ctl.CTLSchemaExportMethod;
import org.kaaproject.kaa.common.dto.ctl.CTLSchemaMetaInfoDto;
import org.kaaproject.kaa.common.dto.ctl.CTLSchemaScopeDto;
import org.kaaproject.kaa.server.admin.client.KaaAdmin;
import org.kaaproject.kaa.server.admin.client.mvp.ClientFactory;
import org.kaaproject.kaa.server.admin.client.mvp.place.ConfigurationSchemasPlace;
import org.kaaproject.kaa.server.admin.client.mvp.place.CtlSchemaPlace;
import org.kaaproject.kaa.server.admin.client.mvp.place.CtlSchemaPlace.SchemaType;
import org.kaaproject.kaa.server.admin.client.mvp.place.EventClassPlace;
import org.kaaproject.kaa.server.admin.client.mvp.place.LogSchemasPlace;
import org.kaaproject.kaa.server.admin.client.mvp.place.NotificationSchemasPlace;
import org.kaaproject.kaa.server.admin.client.mvp.place.ProfileSchemasPlace;
import org.kaaproject.kaa.server.admin.client.mvp.place.ServerProfileSchemasPlace;
import org.kaaproject.kaa.server.admin.client.mvp.view.CtlSchemaView;
import org.kaaproject.kaa.server.admin.client.mvp.view.widget.RecordPanel.FormDataLoader;
import org.kaaproject.kaa.server.admin.client.servlet.ServletHelper;
import org.kaaproject.kaa.server.admin.client.util.ErrorMessageCustomizer;
import org.kaaproject.kaa.server.admin.client.util.SchemaErrorMessageCustomizer;
import org.kaaproject.kaa.server.admin.client.util.Utils;
import org.kaaproject.kaa.server.admin.shared.schema.ConfigurationSchemaViewDto;
import org.kaaproject.kaa.server.admin.shared.schema.ConverterType;
import org.kaaproject.kaa.server.admin.shared.schema.CtlSchemaFormDto;
import org.kaaproject.kaa.server.admin.shared.schema.EventClassViewDto;
import org.kaaproject.kaa.server.admin.shared.schema.LogSchemaViewDto;
import org.kaaproject.kaa.server.admin.shared.schema.NotificationSchemaViewDto;
import org.kaaproject.kaa.server.admin.shared.schema.ProfileSchemaViewDto;
import org.kaaproject.kaa.server.admin.shared.schema.ServerProfileSchemaViewDto;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class CtlSchemaActivity extends AbstractDetailsActivity<CtlSchemaFormDto, CtlSchemaView, CtlSchemaPlace>
        implements ErrorMessageCustomizer, FormDataLoader {

    protected static final ErrorMessageCustomizer schemaErrorMessageCustomizer = new SchemaErrorMessageCustomizer();

    protected Integer version = null;
    private String ctlSchemaID;
    private String nameEC;

    public CtlSchemaActivity(CtlSchemaPlace place, ClientFactory clientFactory) {
        super(place, clientFactory);
        version = place.getVersion();
    }

    @Override
    protected CtlSchemaFormDto newEntity() {
        return null;
    }

    protected void bindDetailsView(boolean fireChanged) {
        if (!create) {
            List<Integer> schemaVersions = entity.getMetaInfo().getVersions();
            Collections.sort(schemaVersions);

            if (version == null && !schemaVersions.isEmpty()) {
                version = schemaVersions.get(schemaVersions.size() - 1);
            }
            if (version != null) {
                detailsView.getVersion().setValue(version);
            }
            detailsView.getVersion().setAcceptableValues(schemaVersions);
            detailsView.setTitle(entity.getMetaInfo().getFqn());

            registrations.add(detailsView.getExportActionsButton().addMenuItem(Utils.constants.shallow(), new ActionMenuItemListener() {
                @Override
                public void onMenuItemSelected() {
                    exportSchema(CTLSchemaExportMethod.SHALLOW);
                }
            }));
            if (entity.hasDependencies()) {
                registrations.add(detailsView.getExportActionsButton().addMenuItem(Utils.constants.deep(), new ActionMenuItemListener() {
                    @Override
                    public void onMenuItemSelected() {
                        exportSchema(CTLSchemaExportMethod.DEEP);
                    }
                }));
                registrations.add(detailsView.getExportActionsButton().addMenuItem(Utils.constants.flat(), new ActionMenuItemListener() {
                    @Override
                    public void onMenuItemSelected() {
                        exportSchema(CTLSchemaExportMethod.FLAT);
                    }
                }));
            }
            registrations.add(detailsView.getExportActionsButton().addMenuItem(Utils.constants.javaLibrary(), new ActionMenuItemListener() {
                @Override
                public void onMenuItemSelected() {
                    exportSchema(CTLSchemaExportMethod.LIBRARY);
                }
            }));
        }
        detailsView.getScope().setText(Utils.getCtlScopeTitleString(entity.getMetaInfo().getScope()));
        detailsView.getCreatedUsername().setValue(entity.getCreatedUsername());
        detailsView.getCreatedDateTime().setValue(Utils.millisecondsToDateTimeString(entity.getCreatedTime()));
        detailsView.getSchemaForm().setValue(entity.getSchema(), fireChanged);
    }

    protected void exportSchema(CTLSchemaExportMethod method) {
        AsyncCallback<String> schemaExportCallback = new AsyncCallback<String>() {
            @Override
            public void onFailure(Throwable caught) {
                Utils.handleException(caught, detailsView);
            }

            @Override
            public void onSuccess(String key) {
                ServletHelper.exportCtlSchema(key);
            }
        };
        KaaAdmin.getDataSource().prepareCTLSchemaExport(entity.getId(), method, schemaExportCallback);
    }

    @Override
    protected void onSave() {
        entity.setSchema(detailsView.getSchemaForm().getValue());
    }

    @Override
    public String customizeErrorMessage(Throwable caught) {
        String errorMessage = schemaErrorMessageCustomizer.customizeErrorMessage(caught);
        if (errorMessage == null) {
            errorMessage = caught.getLocalizedMessage();
        }
        return errorMessage;
    }

    @Override
    protected CtlSchemaView getView(boolean create) {
        if (create) {
            return clientFactory.getCreateCtlSchemaView();
        } else {
            if (place.isEditable()) {
                if (place.getScope() == CTLSchemaScopeDto.APPLICATION) {
                    return clientFactory.getEditApplicationCtlSchemaView();
                } else {
                    return clientFactory.getEditCtlSchemaView();
                }
            } else {
                return clientFactory.getViewCtlSchemaView();
            }
        }
    }

    @Override
    protected void getEntity(String id,
                             AsyncCallback<CtlSchemaFormDto> callback) {
        if (version == null) {
            KaaAdmin.getDataSource().getLatestCTLSchemaForm(id, callback);
        } else {
            KaaAdmin.getDataSource().getCTLSchemaFormByMetaInfoIdAndVer(id, version, callback);
        }
    }

    @Override
    protected void editEntity(final CtlSchemaFormDto entity,
                              final AsyncCallback<CtlSchemaFormDto> callback) {
        if (place.getScope().getLevel() > CTLSchemaScopeDto.SYSTEM.getLevel()) {
            KaaAdmin.getDataSource().checkFqnExists(entity, new BusyAsyncCallback<Boolean>() {
                @Override
                public void onFailureImpl(Throwable caught) {
                    Utils.handleException(caught, detailsView);
                }

                @Override
                public void onSuccessImpl(Boolean result) {
                    if (!result) {
                        editSchema(entity, callback);
                    } else {
                        ConfirmDialog.ConfirmListener listener = new ConfirmDialog.ConfirmListener() {
                            @Override
                            public void onNo() {
                            }

                            @Override
                            public void onYes() {
                                editSchema(entity, callback);
                            }
                        };
                        ConfirmDialog dialog = new ConfirmDialog(listener, Utils.messages.commonTypeFqnAlreadyExistTitle(),
                                Utils.messages.commonTypeFqnAlreadyExistsQuestion());
                        dialog.center();
                        dialog.show();
                    }
                }
            });
        } else {
            editSchema(entity, callback);
        }
    }

    @Override
    protected String getEntityId(CtlSchemaPlace place) {
        return place.isCreate() ? null : place.getMetaInfoId();
    }

    @Override
    protected void bind(final EventBus eventBus) {
        if (create) {
            detailsView.getSchemaForm().setFormDataLoader(this);
        } else {
            registrations.add(detailsView.getVersion().addValueChangeHandler(new ValueChangeHandler<Integer>() {
                @Override
                public void onValueChange(ValueChangeEvent<Integer> event) {
                    detailsView.getVersion().setValue(version);
                    if (place.getScope() == CTLSchemaScopeDto.APPLICATION) {
                        goTo(new CtlSchemaPlace(place.getMetaInfoId(), event.getValue(), place.getScope(),
                                place.getApplicationId(), place.isEditable(), false));
                    } else if (place.getScope() == CTLSchemaScopeDto.TENANT) {
                        goTo(new CtlSchemaPlace(place.getMetaInfoId(), event.getValue(), place.getScope(),
                                place.getEcfId(), place.getEcfVersionId(), place.getEcfVersion(), place.isEditable(), false));
                    }
                }
            }));
            registrations.add(detailsView.getCreateNewSchemaVersionButton().addClickHandler(new ClickHandler() {
                @Override
                public void onClick(ClickEvent event) {
                    Integer version = detailsView.getVersion().getValue();
                    CtlSchemaPlace newPlace = null;
                    if (place.getScope() == CTLSchemaScopeDto.APPLICATION) {
                        newPlace = new CtlSchemaPlace(place.getMetaInfoId(), version, place.getScope(), place.getApplicationId(), true, true);
                    } else if (place.getScope() == CTLSchemaScopeDto.TENANT) {
                        newPlace = new CtlSchemaPlace(place.getMetaInfoId(), version, place.getScope(),
                                place.getEcfId(), place.getEcfVersionId(), place.getEcfVersion(), true, true);
                    }
                    newPlace.setPreviousPlace(place);
                    goTo(newPlace);
                }
            }));

            registrations.add(detailsView.getUpdateSchemaScopeButton().addClickHandler(new ClickHandler() {
                @Override
                public void onClick(ClickEvent event) {
                    CTLSchemaMetaInfoDto metaInfo = entity.getMetaInfo();

                    KaaAdmin.getDataSource().promoteScopeToTenant(metaInfo.getApplicationId(), metaInfo.getFqn(), new BusyAsyncCallback<CTLSchemaMetaInfoDto>() {
                        @Override
                        public void onFailureImpl(Throwable caught) {
                            Utils.handleException(caught, detailsView);
                        }

                        @Override
                        public void onSuccessImpl(CTLSchemaMetaInfoDto result) {
                            CtlSchemaPlace place = null;
                            if (CtlSchemaActivity.this.place.getScope() == CTLSchemaScopeDto.APPLICATION) {
                                place = new CtlSchemaPlace(result.getId(), version, result.getScope(),
                                        CtlSchemaActivity.this.place.getApplicationId(), result.getScope() == CTLSchemaScopeDto.APPLICATION, false);
                            } else if (CtlSchemaActivity.this.place.getScope() == CTLSchemaScopeDto.TENANT) {
                                place = new CtlSchemaPlace(result.getId(), version, result.getScope(),
                                        CtlSchemaActivity.this.place.getEcfId(), CtlSchemaActivity.this.place.getEcfVersionId(), CtlSchemaActivity.this.place.getEcfVersion(),
                                        result.getScope() == CTLSchemaScopeDto.TENANT, false);
                            }
                            goTo(place);
                        }
                    });
                }
            }));

            registrations.add(detailsView.getDeleteSchemaVersionButton().addClickHandler(new ClickHandler() {
                @Override
                public void onClick(ClickEvent event) {
                    final Integer version = detailsView.getVersion().getValue();
                    final String fqn = entity.getMetaInfo().getFqn();
                    ConfirmDialog.ConfirmListener listener = new ConfirmDialog.ConfirmListener() {
                        @Override
                        public void onNo() {
                        }

                        @Override
                        public void onYes() {
                            KaaAdmin.getDataSource().deleteCTLSchemaByFqnVersionTenantIdAndApplicationId(fqn, version,
                                    entity.getMetaInfo().getTenantId(), place.getApplicationId(), new BusyAsyncCallback<Void>() {
                                        @Override
                                        public void onSuccessImpl(Void result) {
                                            List<Integer> versions = entity.getMetaInfo().getVersions();
                                            versions.remove(version);
                                            if (versions.isEmpty()) {
                                                goTo(place.getPreviousPlace());
                                            } else {
                                                if (place.getScope() == CTLSchemaScopeDto.APPLICATION) {
                                                    goTo(new CtlSchemaPlace(place.getMetaInfoId(),
                                                            versions.get(versions.size() - 1), place.getScope(), place.getApplicationId(), true, false));
                                                } else if (place.getScope() == CTLSchemaScopeDto.TENANT) {
                                                    goTo(new CtlSchemaPlace(place.getMetaInfoId(), versions.get(versions.size() - 1),
                                                            place.getScope(), place.getEcfId(), place.getEcfVersionId(), place.getEcfVersion(), true, false));
                                                }
                                            }
                                        }

                                        @Override
                                        public void onFailureImpl(Throwable caught) {
                                            Utils.handleException(caught, detailsView);
                                        }
                                    });
                        }
                    };

                    ConfirmDialog dialog = new ConfirmDialog(listener, Utils.messages.deleteCommonTypeVersionTitle(),
                            Utils.messages.deleteCommonTypeVersionQuestion(fqn, version.toString()));
                    dialog.center();
                    dialog.show();
                }
            }));
        }
        super.bind(eventBus);
    }

    @Override
    protected void onEntityRetrieved() {
        if (create) {
            ConverterType converterType;

            if (place.getSchemaType() == SchemaType.CONFIGURATION) {
                converterType = ConverterType.CONFIGURATION_FORM_AVRO_CONVERTER;
            } else {
                converterType = ConverterType.FORM_AVRO_CONVERTER;
            }

            KaaAdmin.getDataSource().createNewCTLSchemaFormInstance(place.getMetaInfoId(),
                    place.getVersion(), place.getApplicationId(), converterType,
                    new BusyAsyncCallback<CtlSchemaFormDto>() {
                        @Override
                        public void onSuccessImpl(CtlSchemaFormDto result) {
                            entity = result;
                            if (place.getSchemaType() != null) {
                                entity.getSchema().setDisplayNameFieldOptional(false);
                            }
                            bindDetailsView(true);
                        }

                        @Override
                        public void onFailureImpl(Throwable caught) {
                            Utils.handleException(caught, detailsView);
                        }
                    });
        } else {
            if (entity == null) {
                goTo(place.getPreviousPlace());
            } else {
                bindDetailsView(false);
            }
        }
    }

    @Override
    protected void doSave(final EventBus eventBus) {
        onSave();

        editEntity(entity,
                new BusyAsyncCallback<CtlSchemaFormDto>() {
                    public void onSuccessImpl(CtlSchemaFormDto result) {
                        if (!create) {
                            if (place.getScope() == CTLSchemaScopeDto.APPLICATION) {
                                goTo(new CtlSchemaPlace(place.getMetaInfoId(), place.getVersion(), place.getScope(), place.getApplicationId(), true, false));
                            } else if (place.getScope() == CTLSchemaScopeDto.TENANT) {
                                goTo(new CtlSchemaPlace(place.getMetaInfoId(), place.getVersion(),
                                        place.getScope(), place.getEcfId(), place.getEcfVersionId(), place.getEcfVersion(), true, false));
                            }
                        } else if (place.getSchemaType() != null) {
                            if (place.getSchemaType() == SchemaType.ENDPOINT_PROFILE) {
                                goTo(new ProfileSchemasPlace(place.getApplicationId()));
                            } else if (place.getSchemaType() == SchemaType.CONFIGURATION) {
                                goTo(new ConfigurationSchemasPlace(place.getApplicationId()));
                            } else if (place.getSchemaType() == SchemaType.SERVER_PROFILE) {
                                goTo(new ServerProfileSchemasPlace(place.getApplicationId()));
                            } else if (place.getSchemaType() == SchemaType.NOTIFICATION) {
                                goTo(new NotificationSchemasPlace(place.getApplicationId()));
                            } else if (place.getSchemaType() == SchemaType.LOG_SCHEMA) {
                                goTo(new LogSchemasPlace(place.getApplicationId()));
                            } else if (place.getSchemaType() == SchemaType.EVENT_CLASS && place.getPreviousPlace() != null) {
                                EventClassPlace eventClassPlace = new EventClassPlace(place.getEcfId(), place.getEcfVersionId(), place.getEcfVersion(), "");
                                eventClassPlace.setCtlSchemaId(ctlSchemaID);
                                eventClassPlace.setNameEC(nameEC);
                                goTo(eventClassPlace);
                            }
                        } else if (place.getPreviousPlace() != null) {
                            goTo(place.getPreviousPlace());
                        }
                    }

                    public void onFailureImpl(Throwable caught) {
                        Utils.handleException(caught, detailsView);
                    }
                });
    }

    @Override
    public void loadFormData(String fileItemName,
                             final AsyncCallback<RecordField> callback) {
        KaaAdmin.getDataSource().generateCtlSchemaForm(fileItemName, place.getApplicationId(), new AsyncCallback<RecordField>() {
            @Override
            public void onSuccess(RecordField result) {
                if (place.getSchemaType() != null) {
                    result.setDisplayNameFieldOptional(false);
                }
                callback.onSuccess(result);
            }

            @Override
            public void onFailure(Throwable caught) {
                callback.onFailure(caught);
            }
        });
    }

    protected void editSchema(CtlSchemaFormDto entity, final AsyncCallback<CtlSchemaFormDto> callback) {
        if (create && place.getSchemaType() != null) {
            if (place.getSchemaType() == SchemaType.ENDPOINT_PROFILE) {
                KaaAdmin.getDataSource().createProfileSchemaFormCtlSchema(entity,
                        new BusyAsyncCallback<ProfileSchemaViewDto>() {
                            @Override
                            public void onFailureImpl(Throwable caught) {
                                callback.onFailure(caught);
                            }

                            @Override
                            public void onSuccessImpl(ProfileSchemaViewDto result) {
                                callback.onSuccess(null);
                            }
                        });
            } else if (place.getSchemaType() == SchemaType.CONFIGURATION) {
                KaaAdmin.getDataSource().createConfigurationSchemaFormCtlSchema(entity,
                        new BusyAsyncCallback<ConfigurationSchemaViewDto>() {
                            @Override
                            public void onFailureImpl(Throwable caught) {
                                callback.onFailure(caught);
                            }

                            @Override
                            public void onSuccessImpl(ConfigurationSchemaViewDto result) {
                                callback.onSuccess(null);
                            }
                        });
            } else if (place.getSchemaType() == SchemaType.SERVER_PROFILE) {
                KaaAdmin.getDataSource().createServerProfileSchemaFormCtlSchema(entity,
                        new BusyAsyncCallback<ServerProfileSchemaViewDto>() {
                            @Override
                            public void onFailureImpl(Throwable caught) {
                                callback.onFailure(caught);
                            }

                            @Override
                            public void onSuccessImpl(ServerProfileSchemaViewDto result) {
                                callback.onSuccess(null);
                            }
                        });
            } else if (place.getSchemaType() == SchemaType.NOTIFICATION) {
                KaaAdmin.getDataSource().createNotificationSchemaFormCtlSchema(entity,
                        new BusyAsyncCallback<NotificationSchemaViewDto>() {
                            @Override
                            public void onFailureImpl(Throwable caught) {
                                callback.onFailure(caught);
                            }

                            @Override
                            public void onSuccessImpl(NotificationSchemaViewDto notificationSchemaViewDto) {
                                callback.onSuccess(null);
                            }
                        });
            } else if (place.getSchemaType() == SchemaType.LOG_SCHEMA) {
                KaaAdmin.getDataSource().createLogSchemaFormCtlSchema(entity,
                        new BusyAsyncCallback<LogSchemaViewDto>() {
                            @Override
                            public void onFailureImpl(Throwable caught) {
                                callback.onFailure(caught);
                            }

                            @Override
                            public void onSuccessImpl(LogSchemaViewDto logSchemaViewDto) {
                                callback.onSuccess(null);
                            }
                        });
            } else if (place.getSchemaType() == SchemaType.EVENT_CLASS) {
                KaaAdmin.getDataSource().createEventClassFormCtlSchema(entity,
                        new BusyAsyncCallback<EventClassViewDto>() {
                            @Override
                            public void onFailureImpl(Throwable caught) {
                                callback.onFailure(caught);
                            }

                            @Override
                            public void onSuccessImpl(EventClassViewDto eventClassViewDto) {
                                ctlSchemaID = eventClassViewDto.getSchema().getCtlSchemaId();
                                nameEC = eventClassViewDto.getSchema().getName();
                                callback.onSuccess(null);
                            }
                        });
            }
        } else {
            KaaAdmin.getDataSource().editCTLSchemaForm(entity, ConverterType.FORM_AVRO_CONVERTER, callback);
        }
    }

}


text:
package com.restio.provider;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;

import com.restio.model.Request;

/**
 *
 * @author tham
 *
 */

@Provider
@Produces(MediaType.APPLICATION_JSON)
public class RequestJsonWriter implements MessageBodyWriter<Request>{

	@Override
	public long getSize(Request request, Class<?> clazz, Type type,
			Annotation[] annotations, MediaType mediaType) {
		return -1;
	}

	@Override
	public boolean isWriteable(Class<?> clazz, Type type, Annotation[] annotations,
			MediaType mediaType) {
		return clazz == Request.class;
	}

	@Override
	public void writeTo(Request request, Class<?> clazz, Type type,
			Annotation[] annotations, MediaType mediaType,
			MultivaluedMap<String, Object> multiValuedMap, OutputStream stream)
			throws IOException, WebApplicationException {
		DataOutputStream dataOutputStream = new DataOutputStream(stream);

		StringBuilder builder = new StringBuilder();
		builder.append("{");
		builder.append("\"name\"");
		builder.append(":");
		builder.append("\"Thamizh\"");
		builder.append("}");
		dataOutputStream.writeUTF(builder.toString());
	}
}


text:
package org.apache.hadoop.hive.accumulo.predicate;

import org.apache.hadoop.hive.accumulo.predicate.compare.PrimitiveComparison;

/**
 * Used when a {@link PrimitiveComparison} was specified but one with that name cannot be found
 */
public class NoSuchPrimitiveComparisonException extends Exception {

  private static final long serialVersionUID = 1L;

  public NoSuchPrimitiveComparisonException() {
    super();
  }

  public NoSuchPrimitiveComparisonException(String msg) {
    super(msg);
  }

  public NoSuchPrimitiveComparisonException(String msg, Throwable cause) {
    super(msg, cause);
  }
}


text:
using System;
using System.Collections.Generic;
using Mono.Cecil;
using Mono.Cecil.Cil;

public static class InstructionPatterns
{
    public static void CallDebugAssertInstructions(List<Instruction> instructions, string message)
    {
        // Load null onto the stack
        instructions.Add(Instruction.Create(OpCodes.Ldnull));

        // Compare the top 2 items on the stack, and put the result back on the stack
        instructions.Add(Instruction.Create(OpCodes.Ceq));

        // Loads constant int32 0 onto the stack
        instructions.Add(Instruction.Create(OpCodes.Ldc_I4_0));

        // Compare the top 2 items on the stack, and put the result back on the stack
        instructions.Add(Instruction.Create(OpCodes.Ceq));

        // Load assert message onto the stack
        instructions.Add(Instruction.Create(OpCodes.Ldstr, message));

        // Call Debug.Assert
        instructions.Add(Instruction.Create(OpCodes.Call, ReferenceFinder.DebugAssertMethod));
    }

    public static void DuplicateReturnValue(List<Instruction> instructions, TypeReference methodReturnType)
    {
        // Duplicate the stack (this should be the return value)
        instructions.Add(Instruction.Create(OpCodes.Dup));

        if (methodReturnType != null && methodReturnType.GetElementType().IsGenericParameter)
        {
            // Generic parameters must be boxed before access
            instructions.Add(Instruction.Create(OpCodes.Box, methodReturnType));
        }
    }

    public static void LoadArgumentOntoStack(List<Instruction> instructions, ParameterDefinition parameter)
    {
        // Load the argument onto the stack
        instructions.Add(Instruction.Create(OpCodes.Ldarg, parameter));

        var elementType = parameter.ParameterType.GetElementType();

        if (parameter.ParameterType.IsByReference)
        {
            if (elementType.IsGenericParameter)
            {
                // Loads an object reference onto the stack
                instructions.Add(Instruction.Create(OpCodes.Ldobj, elementType));

                // Box the type to an object
                instructions.Add(Instruction.Create(OpCodes.Box, elementType));
            }
            else
            {
                // Loads an object reference onto the stack
                instructions.Add(Instruction.Create(OpCodes.Ldind_Ref));
            }
        }
        else if (elementType.IsGenericParameter)
        {
            // Box the type to an object
            instructions.Add(Instruction.Create(OpCodes.Box, parameter.ParameterType));
        }
    }

    public static void LoadArgumentNullException(List<Instruction> instructions, string valueName, string errorString)
    {
        // Load the name of the argument onto the stack
        instructions.Add(Instruction.Create(OpCodes.Ldstr, valueName));

        // Load the exception text onto the stack
        instructions.Add(Instruction.Create(OpCodes.Ldstr, errorString));

        // Load the ArgumentNullException onto the stack
        instructions.Add(Instruction.Create(OpCodes.Newobj, ReferenceFinder.ArgumentNullExceptionWithMessageConstructor));
    }

    public static void LoadInvalidOperationException(List<Instruction> instructions, string errorString)
    {
        // Load the exception text onto the stack
        instructions.Add(Instruction.Create(OpCodes.Ldstr, errorString));

        // Load the InvalidOperationException onto the stack
        instructions.Add(Instruction.Create(OpCodes.Newobj, ReferenceFinder.InvalidOperationExceptionConstructor));
    }

    public static void IfNull(List<Instruction> instructions, Instruction returnInstruction, Action<List<Instruction>> trueBlock)
    {
        // Branch if value on stack is true, not null or non-zero
        instructions.Add(Instruction.Create(OpCodes.Brtrue_S, returnInstruction));

        trueBlock(instructions);
    }
}


text:
<?php

namespace pgn\tags;

use utils\Parser;

/**
 * Description of Event:
 * The Event tag value should be reasonably descriptive.
 * Abbreviations are to be avoided unless absolutely necessary.
 * A consistent event naming should be used to help facilitate database scanning.
 * If the name of the event is unknown, a single question mark should appear as the tag value.
 * @see pgn_standard.txt
 * @author Geraldo
 */
class Event extends Tag {

    public function getName() {
        $parsed = Parser::parseClassName(get_class());
        return $parsed['className'];
    }

}


text:
using System;
using System.Collections;
using System.Collections.Generic;

namespace Azure.Analytics.Synapse.Artifacts.Models
{
    /// <summary> Spark job definition. </summary>
    public partial class SparkJobDefinition : IDictionary<string, object>
    {
        /// <summary> Initializes a new instance of SparkJobDefinition. </summary>
        /// <param name="targetBigDataPool"> Big data pool reference. </param>
        /// <param name="jobProperties"> The properties of the Spark job. </param>
        public SparkJobDefinition(BigDataPoolReference targetBigDataPool, SparkJobProperties jobProperties)
        {
            if (targetBigDataPool == null)
            {
                throw new ArgumentNullException(nameof(targetBigDataPool));
            }
            if (jobProperties == null)
            {
                throw new ArgumentNullException(nameof(jobProperties));
            }

            TargetBigDataPool = targetBigDataPool;
            JobProperties = jobProperties;
            AdditionalProperties = new Dictionary<string, object>();
        }

        /// <summary> Initializes a new instance of SparkJobDefinition. </summary>
        /// <param name="description"> The description of the Spark job definition. </param>
        /// <param name="targetBigDataPool"> Big data pool reference. </param>
        /// <param name="requiredSparkVersion"> The required Spark version of the application. </param>
        /// <param name="language"> The language of the Spark application. </param>
        /// <param name="jobProperties"> The properties of the Spark job. </param>
        /// <param name="additionalProperties"> . </param>
        internal SparkJobDefinition(string description, BigDataPoolReference targetBigDataPool, string requiredSparkVersion, string language, SparkJobProperties jobProperties, IDictionary<string, object> additionalProperties)
        {
            Description = description;
            TargetBigDataPool = targetBigDataPool;
            RequiredSparkVersion = requiredSparkVersion;
            Language = language;
            JobProperties = jobProperties;
            AdditionalProperties = additionalProperties ?? new Dictionary<string, object>();
        }

        /// <summary> The description of the Spark job definition. </summary>
        public string Description { get; set; }
        /// <summary> Big data pool reference. </summary>
        public BigDataPoolReference TargetBigDataPool { get; set; }
        /// <summary> The required Spark version of the application. </summary>
        public string RequiredSparkVersion { get; set; }
        /// <summary> The language of the Spark application. </summary>
        public string Language { get; set; }
        /// <summary> The properties of the Spark job. </summary>
        public SparkJobProperties JobProperties { get; set; }
        internal IDictionary<string, object> AdditionalProperties { get; }
        /// <inheritdoc />
        public IEnumerator<KeyValuePair<string, object>> GetEnumerator() => AdditionalProperties.GetEnumerator();
        /// <inheritdoc />
        IEnumerator IEnumerable.GetEnumerator() => AdditionalProperties.GetEnumerator();
        /// <inheritdoc />
        public bool TryGetValue(string key, out object value) => AdditionalProperties.TryGetValue(key, out value);
        /// <inheritdoc />
        public bool ContainsKey(string key) => AdditionalProperties.ContainsKey(key);
        /// <inheritdoc />
        public ICollection<string> Keys => AdditionalProperties.Keys;
        /// <inheritdoc />
        public ICollection<object> Values => AdditionalProperties.Values;
        /// <inheritdoc />
        int ICollection<KeyValuePair<string, object>>.Count => AdditionalProperties.Count;
        /// <inheritdoc />
        public void Add(string key, object value) => AdditionalProperties.Add(key, value);
        /// <inheritdoc />
        public bool Remove(string key) => AdditionalProperties.Remove(key);
        /// <inheritdoc />
        bool ICollection<KeyValuePair<string, object>>.IsReadOnly => AdditionalProperties.IsReadOnly;
        /// <inheritdoc />
        void ICollection<KeyValuePair<string, object>>.Add(KeyValuePair<string, object> value) => AdditionalProperties.Add(value);
        /// <inheritdoc />
        bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> value) => AdditionalProperties.Remove(value);
        /// <inheritdoc />
        bool ICollection<KeyValuePair<string, object>>.Contains(KeyValuePair<string, object> value) => AdditionalProperties.Contains(value);
        /// <inheritdoc />
        void ICollection<KeyValuePair<string, object>>.CopyTo(KeyValuePair<string, object>[] destination, int offset) => AdditionalProperties.CopyTo(destination, offset);
        /// <inheritdoc />
        void ICollection<KeyValuePair<string, object>>.Clear() => AdditionalProperties.Clear();
        /// <inheritdoc />
        public object this[string key]
        {
            get => AdditionalProperties[key];
            set => AdditionalProperties[key] = value;
        }
    }
}


text:
<?php

/*
 * (c) Kévin Dunglas <dunglas@gmail.com>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Dunglas\ActionBundle\Tests\Fixtures\TestBundle\Action;

abstract class AbstractAction
{
}


text:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@color/colorUnderline"
              android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/colorPrimary">

        <TextView
            android:id="@+id/area_name_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textColor="#ffffff"
            android:textSize="25sp"/>
    </RelativeLayout>

    <ListView
        android:id="@+id/choose_area_listview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </ListView>
</LinearLayout>


text:
package ch.ifocusit.livingdoc.plugin.publish.confluence;

import ch.ifocusit.livingdoc.plugin.publish.PublishProvider;
import ch.ifocusit.livingdoc.plugin.publish.confluence.client.ConfluencePage;
import ch.ifocusit.livingdoc.plugin.publish.confluence.client.ConfluenceRestClient;
import ch.ifocusit.livingdoc.plugin.publish.confluence.client.NotFoundException;
import ch.ifocusit.livingdoc.plugin.publish.model.Page;

import static ch.ifocusit.livingdoc.plugin.utils.InputStreamUtils.fileInputStream;

/**
 * @author Julien Boz
 */
public class ConfluenceProvider implements PublishProvider {

    final ConfluenceRestClient client;

    public ConfluenceProvider(String endpoint, String username, String password) {
        client = new ConfluenceRestClient(endpoint, username, password);
    }

    @Override
    public boolean exists(final Page page) {
        try {
            client.getPageByTitle(page.getSpaceKey(), page.getTitle());
            return true;
        } catch (NotFoundException e) {
            return false;
        }
    }

    @Override
    public void update(final Page page) {
        String contentId = client.getPageByTitle(page.getSpaceKey(), page.getTitle());
        ConfluencePage existingPage = client.getPageWithContentAndVersionById(contentId);
        String oldContent = existingPage.getContent();

        // TODO check why content even upload
        if (!oldContent.equals(page.getContent())) {
            client.updatePage(contentId, page.getParentId(), page.getTitle(), page.getContent(), existingPage.getVersion() + 1);

            // TODO update attachement if possible
            // TODO remove attachement not present
            // remove all attachement
            client.getAttachments(contentId).forEach(attachment -> client.deleteAttachment(attachment.getId()));
            // add attachements
            page.getAttachements().forEach(attachement ->
                    client.addAttachment(contentId, attachement.getName(), fileInputStream(attachement.getFile()))
            );
        } else {
            // TODO log INFO
            System.out.println("Page with title=" + page.getTitle() + " did not change.");
        }
    }

    @Override
    public void insert(final Page page) {
        // post page to confluence.
        String contentId = client.addPageUnderAncestor(page.getSpaceKey(), page.getParentId(), page.getTitle(), page.getContent());

        // add attachements
        page.getAttachements().forEach(attachement ->
                client.addAttachment(contentId, attachement.getName(), fileInputStream(attachement.getFile()))
        );
    }

}


text:
import createError = require('dojo/errors/create');
import Request from './Request';

interface RoutingError extends Error {
	request: Request;
}

var RoutingError: {
	new (message: string, request?: Request): RoutingError;
	prototype: RoutingError;
};

function Ctor(message: string, request?: Request) {
	this.request = request;
}

RoutingError = createError('RoutingError', Ctor, Error, {});

export default RoutingError;


text:
package com.tvd12.ezyfox.binding.testing.objectbinding;

import com.tvd12.ezyfox.binding.EzyMarshaller;
import com.tvd12.ezyfox.binding.EzyReader;
import com.tvd12.ezyfox.binding.EzyUnmarshaller;
import com.tvd12.ezyfox.binding.EzyWriter;
import com.tvd12.ezyfox.binding.impl.EzyObjectReaderBuilder;
import com.tvd12.ezyfox.binding.impl.EzyObjectWriterBuilder;
import com.tvd12.ezyfox.binding.impl.EzySimpleMarshaller;
import com.tvd12.ezyfox.binding.impl.EzySimpleUnmarshaller;
import com.tvd12.ezyfox.entity.EzyObject;
import com.tvd12.ezyfox.reflect.EzyClass;
import org.testng.annotations.Test;

public class EzyObjectBinding4Test {

    @SuppressWarnings("unchecked")
    @Test
    public void test() {
        EzyObjectWriterBuilder.setDebug(true);
        EzyObjectReaderBuilder.setDebug(true);

        EzyMarshaller marshaller = new EzySimpleMarshaller();
        EzyUnmarshaller unmarshaller = new EzySimpleUnmarshaller();

        EzyObjectWriterBuilder writerBuilder
            = new EzyObjectWriterBuilder(new EzyClass(ClassD.class));
        EzyWriter<ClassD, EzyObject> writer = writerBuilder.build();
        EzyObject object = writer.write(marshaller, new ClassD());
        System.out.println(object);

        EzyObjectReaderBuilder readerBuilder
            = new EzyObjectReaderBuilder(new EzyClass(ClassD.class));
        EzyReader<EzyObject, ClassD> reader = readerBuilder.build();
        ClassD ClassD = reader.read(unmarshaller, object);
        System.out.println(ClassD);
    }
}


text:
<?php

/**
 * Base class that represents a query for the 'meta' table.
 *
 *
 *
 * @method     Oops_Db_MetaQuery orderByIdMeta($order = Criteria::ASC) Order by the id_meta column
 * @method     Oops_Db_MetaQuery orderByPage($order = Criteria::ASC) Order by the page column
 *
 * @method     Oops_Db_MetaQuery groupByIdMeta() Group by the id_meta column
 * @method     Oops_Db_MetaQuery groupByPage() Group by the page column
 *
 * @method     Oops_Db_MetaQuery leftJoin($relation) Adds a LEFT JOIN clause to the query
 * @method     Oops_Db_MetaQuery rightJoin($relation) Adds a RIGHT JOIN clause to the query
 * @method     Oops_Db_MetaQuery innerJoin($relation) Adds a INNER JOIN clause to the query
 *
 * @method     Oops_Db_Meta findOne(PropelPDO $con = null) Return the first Oops_Db_Meta matching the query
 * @method     Oops_Db_Meta findOneOrCreate(PropelPDO $con = null) Return the first Oops_Db_Meta matching the query, or a new Oops_Db_Meta object populated from the query conditions when no match is found
 *
 * @method     Oops_Db_Meta findOneByIdMeta(int $id_meta) Return the first Oops_Db_Meta filtered by the id_meta column
 * @method     Oops_Db_Meta findOneByPage(string $page) Return the first Oops_Db_Meta filtered by the page column
 *
 * @method     array findByIdMeta(int $id_meta) Return Oops_Db_Meta objects filtered by the id_meta column
 * @method     array findByPage(string $page) Return Oops_Db_Meta objects filtered by the page column
 *
 * @package    propel.generator.prestashop.om
 */
abstract class Oops_Db_Propel_MetaQuery extends ModelCriteria
{

	/**
	 * Initializes internal state of Oops_Db_Propel_MetaQuery object.
	 *
	 * @param     string $dbName The dabase name
	 * @param     string $modelName The phpName of a model, e.g. 'Book'
	 * @param     string $modelAlias The alias for the model in this query, e.g. 'b'
	 */
	public function __construct($dbName = 'prestashop', $modelName = 'Oops_Db_Meta', $modelAlias = null)
	{
		parent::__construct($dbName, $modelName, $modelAlias);
	}

	/**
	 * Returns a new Oops_Db_MetaQuery object.
	 *
	 * @param     string $modelAlias The alias of a model in the query
	 * @param     Criteria $criteria Optional Criteria to build the query from
	 *
	 * @return    Oops_Db_MetaQuery
	 */
	public static function create($modelAlias = null, $criteria = null)
	{
		if ($criteria instanceof Oops_Db_MetaQuery) {
			return $criteria;
		}
		$query = new Oops_Db_MetaQuery();
		if (null !== $modelAlias) {
			$query->setModelAlias($modelAlias);
		}
		if ($criteria instanceof Criteria) {
			$query->mergeWith($criteria);
		}
		return $query;
	}

	/**
	 * Find object by primary key.
	 * Propel uses the instance pool to skip the database if the object exists.
	 * Go fast if the query is untouched.
	 *
	 *
	 * $obj  = $c->findPk(12, $con);
	 *
	 *
	 * @param     mixed $key Primary key to use for the query
	 * @param     PropelPDO $con an optional connection object
	 *
	 * @return    Oops_Db_Meta|array|mixed the result, formatted by the current formatter
	 */
	public function findPk($key, $con = null)
	{
		if ($key === null) {
			return null;
		}
		if ((null !== ($obj = Oops_Db_MetaPeer::getInstanceFromPool((string) $key))) && !$this->formatter) {
			// the object is alredy in the instance pool
			return $obj;
		}
		if ($con === null) {
			$con = Propel::getConnection(Oops_Db_MetaPeer::DATABASE_NAME, Propel::CONNECTION_READ);
		}
		$this->basePreSelect($con);
		if ($this->formatter || $this->modelAlias || $this->with || $this->select
		 || $this->selectColumns || $this->asColumns || $this->selectModifiers
		 || $this->map || $this->having || $this->joins) {
			return $this->findPkComplex($key, $con);
		} else {
			return $this->findPkSimple($key, $con);
		}
	}

	/**
	 * Find object by primary key using raw SQL to go fast.
	 * Bypass doSelect() and the object formatter by using generated code.
	 *
	 * @param     mixed $key Primary key to use for the query
	 * @param     PropelPDO $con A connection object
	 *
	 * @return    Oops_Db_Meta A model object, or null if the key is not found
	 */
	protected function findPkSimple($key, $con)
	{
		$sql = 'SELECT `ID_META`, `PAGE` FROM `' . _DB_PREFIX_ . 'meta` WHERE `ID_META` = :p0';
		try {
			$stmt = $con->prepare($sql);
			$stmt->bindValue(':p0', $key, PDO::PARAM_INT);
			$stmt->execute();
		} catch (Exception $e) {
			Propel::log($e->getMessage(), Propel::LOG_ERR);
			throw new PropelException(sprintf('Unable to execute SELECT statement [%s]', $sql), $e);
		}
		$obj = null;
		if ($row = $stmt->fetch(PDO::FETCH_NUM)) {
			$obj = new Oops_Db_Meta();
			$obj->hydrate($row);
			Oops_Db_MetaPeer::addInstanceToPool($obj, (string) $row[0]);
		}
		$stmt->closeCursor();

		return $obj;
	}

	/**
	 * Find object by primary key.
	 *
	 * @param     mixed $key Primary key to use for the query
	 * @param     PropelPDO $con A connection object
	 *
	 * @return    Oops_Db_Meta|array|mixed the result, formatted by the current formatter
	 */
	protected function findPkComplex($key, $con)
	{
		// As the query uses a PK condition, no limit(1) is necessary.
		$criteria = $this->isKeepQuery() ? clone $this : $this;
		$stmt = $criteria
			->filterByPrimaryKey($key)
			->doSelect($con);
		return $criteria->getFormatter()->init($criteria)->formatOne($stmt);
	}

	/**
	 * Find objects by primary key
	 *
	 * $objs = $c->findPks(array(12, 56, 832), $con);
	 *
	 * @param     array $keys Primary keys to use for the query
	 * @param     PropelPDO $con an optional connection object
	 *
	 * @return    PropelObjectCollection|array|mixed the list of results, formatted by the current formatter
	 */
	public function findPks($keys, $con = null)
	{
		if ($con === null) {
			$con = Propel::getConnection($this->getDbName(), Propel::CONNECTION_READ);
		}
		$this->basePreSelect($con);
		$criteria = $this->isKeepQuery() ? clone $this : $this;
		$stmt = $criteria
			->filterByPrimaryKeys($keys)
			->doSelect($con);
		return $criteria->getFormatter()->init($criteria)->format($stmt);
	}

	/**
	 * Filter the query by primary key
	 *
	 * @param     mixed $key Primary key to use for the query
	 *
	 * @return    Oops_Db_MetaQuery The current query, for fluid interface
	 */
	public function filterByPrimaryKey($key)
	{
		return $this->addUsingAlias(Oops_Db_MetaPeer::ID_META, $key, Criteria::EQUAL);
	}

	/**
	 * Filter the query by a list of primary keys
	 *
	 * @param     array $keys The list of primary key to use for the query
	 *
	 * @return    Oops_Db_MetaQuery The current query, for fluid interface
	 */
	public function filterByPrimaryKeys($keys)
	{
		return $this->addUsingAlias(Oops_Db_MetaPeer::ID_META, $keys, Criteria::IN);
	}

	/**
	 * Filter the query on the id_meta column
	 *
	 * Example usage:
	 *
	 * $query->filterByIdMeta(1234); // WHERE id_meta = 1234
	 * $query->filterByIdMeta(array(12, 34)); // WHERE id_meta IN (12, 34)
	 * $query->filterByIdMeta(array('min' => 12)); // WHERE id_meta > 12
	 *
	 *
	 * @param     mixed $idMeta The value to use as filter.
	 *              Use scalar values for equality.
	 *              Use array values for in_array() equivalent.
	 *              Use associative array('min' => $minValue, 'max' => $maxValue) for intervals.
	 * @param     string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL
	 *
	 * @return    Oops_Db_MetaQuery The current query, for fluid interface
	 */
	public function filterByIdMeta($idMeta = null, $comparison = null)
	{
		if (is_array($idMeta) && null === $comparison) {
			$comparison = Criteria::IN;
		}
		return $this->addUsingAlias(Oops_Db_MetaPeer::ID_META, $idMeta, $comparison);
	}

	/**
	 * Filter the query on the page column
	 *
	 * Example usage:
	 *
	 * $query->filterByPage('fooValue');   // WHERE page = 'fooValue'
	 * $query->filterByPage('%fooValue%'); // WHERE page LIKE '%fooValue%'
	 *
	 *
	 * @param     string $page The value to use as filter.
	 *              Accepts wildcards (* and % trigger a LIKE)
	 * @param     string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL
	 *
	 * @return    Oops_Db_MetaQuery The current query, for fluid interface
	 */
	public function filterByPage($page = null, $comparison = null)
	{
		if (null === $comparison) {
			if (is_array($page)) {
				$comparison = Criteria::IN;
			} elseif (preg_match('/[\%\*]/', $page)) {
				$page = str_replace('*', '%', $page);
				$comparison = Criteria::LIKE;
			}
		}
		return $this->addUsingAlias(Oops_Db_MetaPeer::PAGE, $page, $comparison);
	}

	/**
	 * Exclude object from result
	 *
	 * @param     Oops_Db_Meta $meta Object to remove from the list of results
	 *
	 * @return    Oops_Db_MetaQuery The current query, for fluid interface
	 */
	public function prune($meta = null)
	{
		if ($meta) {
			$this->addUsingAlias(Oops_Db_MetaPeer::ID_META, $meta->getIdMeta(), Criteria::NOT_EQUAL);
		}

		return $this;
	}

} // Oops_Db_Propel_MetaQuery


text:
'''Single-layer autoencoder example using MNIST digit data.

This example shows one way to train a single-layer autoencoder model using the
handwritten MNIST digits.

This example also shows the use of command-line arguments.
'''

import click
import matplotlib.pyplot as plt
import theanets

from utils import load_mnist, plot_layers, plot_images

@click.command()
@click.option('--features', default=16, type=int, metavar='N',
              help='Train a model with NxN hidden features.')
def main(features):
    # load up the MNIST digit dataset.
    train, valid, _ = load_mnist()

    net = theanets.Autoencoder([784, features ** 2, 784])
    net.train(train, valid,
              train_batches=100,
              input_noise=0.1,
              weight_l2=0.0001,
              algo='rmsprop',
              momentum=0.9,
              min_improvement=0.1)

    plot_layers([net.find('hid1', 'w'), net.find('out', 'w')])
    plt.tight_layout()
    plt.show()

    v = valid[0][:100]
    plot_images(v, 121, 'Sample data')
    plot_images(net.predict(v), 122, 'Reconstructed data')
    plt.tight_layout()
    plt.show()

if __name__ == '__main__':
    main()


text:
bool GameScene::init()
{
	GameContext* gameContext = GameContext::getInstance();

	_loginLayer = LoginLayer::create();
	this->addChild(_loginLayer);

	return true;
}


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net.Appender;
using log4net.Core;
using Blade.Logging;

namespace Maui.Logging.Log4Net
{
   internal class LoggingSinkAppender : AppenderSkeleton
    {
        private ILoggingSink mySink;

        public LoggingSinkAppender( ILoggingSink sink )
        {
            mySink = sink;
        }

        protected override void Append( LoggingEvent loggingEvent )
        {
            var level = LogLevelConverter.FromLog4Net(loggingEvent.Level);
            var msg = RenderLoggingEvent( loggingEvent );
            mySink.Write( new LoggingEventAdapter( level, msg ) );
        }
    }
}


text:
.xworekr_text_box_1 {
	font: 14px/22px simsun; padding: 0px 6px; border: 1px solid rgb(231, 236, 240); height: 22px; text-align: center; text-transform: none; text-indent: 0px; letter-spacing: normal; overflow: hidden; text-decoration: none; margin-right: 5px; word-spacing: 0px; vertical-align: text-bottom; display: inline-block; white-space: nowrap; cursor: pointer; font-size-adjust: none; font-stretch: normal; background-color: white; -webkit-text-stroke-width: 0px;
}

.xworekr_text_box_1_noborder {
	font: 14px/22px simsun; padding: 0px 6px; height: 22px; text-align: center; text-transform: none; text-indent: 0px; letter-spacing: normal; overflow: hidden; text-decoration: none; margin-right: 5px; word-spacing: 0px; vertical-align: text-bottom; display: inline-block; white-space: nowrap; font-size-adjust: none; font-stretch: normal; background-color: white; -webkit-text-stroke-width: 0px;
}

.xworker_result_c-container {
  width: 538px;
  font-size: 13px;
  line-height: 1.54;
  word-wrap: break-word;
  word-break: break-all;
}


text:
/**
 * @jest-environment node
 */
'use strict';

const shell = require('shelljs');
const fs = require('fs');

const util = require('../fixtures/util');
const path = require("path");

const silent = !process.env.NOISY_TESTS;
shell.config.verbose = !silent;
shell.config.silent = silent;

const stageName = 'stage-cra';
const craPath = path.join('../node_modules/.bin/create-razzle-app');
const testPackage = 'razzle-example-basic';
const testRepo = 'https://github.com/fivethreeo/razzle-example-basic';

const directoryExists = (dirPath) => fs.existsSync(dirPath) && fs.lstatSync(dirPath).isDirectory();
const fileExists = (dirPath) => fs.existsSync(dirPath);

describe('create-razzle-app', () => {
  beforeAll(() => {
    util.teardownStage(stageName);
  });

  it('should create app from default template', () => {
    util.setupStage(stageName);
    const output = shell.exec(
      `${craPath} cra --verbose --no-install`
    );
    expect(directoryExists('cra/node_modules')).toBeFalsy();

    expect(fileExists('cra/yarn.lock')).toBeFalsy();

    expect(fileExists('cra/package.json')).toBeTruthy();

    expect(directoryExists('cra/src')).toBeTruthy();
    expect(fileExists('cra/src/index.js')).toBeTruthy();

    expect(output.code).toBe(0);
  });
  //
  // it('should create app from default template and install packages', () => {
  //   util.setupStage(stageName);
  //   const output = shell.exec(
  //     `${craPath} cra`--verbose
  //   );
  //   expect(directoryExists('cra/node_modules')).toBeTruthy();
  //
  //   expect(fileExists('cra/yarn.lock')).toBeTruthy();
  //
  //   expect(fileExists('cra/package.json')).toBeTruthy();
  //
  //   expect(directoryExists('cra/src')).toBeTruthy();
  //   expect(fileExists('cra/src/index.js')).toBeTruthy();
  //
  //   expect(output.code).toBe(0);
  // });

  it('should create app from official example', () => {
    util.setupStage(stageName);
    const output = shell.exec(
      `${craPath} cra --verbose --example basic --no-install`
    );
    expect(fileExists('cra/package.json')).toBeTruthy();

    expect(directoryExists('cra/src')).toBeTruthy();
    expect(fileExists('cra/src/index.js')).toBeTruthy();

    expect(output.code).toBe(0);
  });

  it('should create app from npm example', () => {
    util.setupStage(stageName);
    const output = shell.exec(
      `${craPath} cra --verbose --example ${testPackage} --no-install`
    );
    expect(fileExists('cra/package.json')).toBeTruthy();

    expect(directoryExists('cra/src')).toBeTruthy();
    expect(fileExists('cra/src/index.js')).toBeTruthy();

    expect(output.code).toBe(0);
  });

  it('should create app from npm example at tag', () => {
    util.setupStage(stageName);
    const output = shell.exec(
      `${craPath} cra --verbose --example ${testPackage}@latest --no-install`
    );
    expect(fileExists('cra/package.json')).toBeTruthy();

    expect(directoryExists('cra/src')).toBeTruthy();
    expect(fileExists('cra/src/index.js')).toBeTruthy();

    expect(output.code).toBe(0);
  });

  it('should create app from npm example in subdir', () => {
    util.setupStage(stageName);
    const output = shell.exec(
      `${craPath} cra --verbose --example ${testPackage}:subexample --no-install`
    );
    expect(fileExists('cra/package.json')).toBeTruthy();

    expect(directoryExists('cra/src')).toBeTruthy();
    expect(fileExists('cra/src/index.js')).toBeTruthy();

    expect(output.code).toBe(0);
  });

  it('should create app from github example', () => {
    util.setupStage(stageName);
    const output = shell.exec(
      `${craPath} cra --verbose --example ${testRepo} --no-install`
    );
    expect(fileExists('cra/package.json')).toBeTruthy();

    expect(directoryExists('cra/src')).toBeTruthy();
    expect(fileExists('cra/src/index.js')).toBeTruthy();

    expect(output.code).toBe(0);
  });

  it('should create app from github example at branch', () => {
    util.setupStage(stageName);
    const output = shell.exec(
      `${craPath} cra --verbose --example ${testRepo}@master --no-install`
    );
    expect(fileExists('cra/package.json')).toBeTruthy();

    expect(directoryExists('cra/src')).toBeTruthy();
    expect(fileExists('cra/src/index.js')).toBeTruthy();

    expect(output.code).toBe(0);
  });

  it('should create app from github example in subdir', () => {
    util.setupStage(stageName);
    const output = shell.exec(
      `${craPath} cra --verbose --example ${testRepo}:subexample --no-install`
    );
    expect(fileExists('cra/package.json')).toBeTruthy();

    expect(directoryExists('cra/src')).toBeTruthy();
    expect(fileExists('cra/src/index.js')).toBeTruthy();

    expect(output.code).toBe(0);
  });

  it('should create app from git example', () => {
    util.setupStage(stageName);
    const output = shell.exec(
      `${craPath} cra --verbose --example git+${testRepo}.git --no-install`
    );
    expect(fileExists('cra/package.json')).toBeTruthy();

    expect(directoryExists('cra/src')).toBeTruthy();
    expect(fileExists('cra/src/index.js')).toBeTruthy();

    expect(output.code).toBe(0);
  });

  it('should create app from git example at branch', () => {
    util.setupStage(stageName);
    const output = shell.exec(
      `${craPath} cra --verbose --example git+${testRepo}.git@master --no-install`
    );
    expect(fileExists('cra/package.json')).toBeTruthy();

    expect(directoryExists('cra/src')).toBeTruthy();
    expect(fileExists('cra/src/index.js')).toBeTruthy();

    expect(output.code).toBe(0);
  });

  it('should create app from git example in subdir', () => {
    util.setupStage(stageName);
    const output = shell.exec(
      `${craPath} cra --verbose --example git+${testRepo}.git:subexample --no-install`
    );
    expect(fileExists('cra/package.json')).toBeTruthy();

    expect(directoryExists('cra/src')).toBeTruthy();
    expect(fileExists('cra/src/index.js')).toBeTruthy();

    expect(output.code).toBe(0);
  });

  it('should create app from file example', () => {
    util.setupStage(stageName);
    const output = shell.exec(
      `${craPath} cra --verbose --example file:../examples/basic --no-install`
    );
    expect(fileExists('cra/package.json')).toBeTruthy();

    expect(directoryExists('cra/src')).toBeTruthy();
    expect(fileExists('cra/src/index.js')).toBeTruthy();

    expect(output.code).toBe(0);
  });

  it('should exit with an error code when no project name is supplied', () => {
    util.setupStage(stageName);
    const output = shell.exec(
      `${path.join(craPath)}`, {
      silent: true,
    });

    expect(output.code).toBe(1);
  });

  afterEach(() => {
    util.teardownStage(stageName);
  });
});


text:
package main

import (
	"fmt"
	"github.com/stretchr/testify/assert"
	"io"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
)

var (
	server *httptest.Server
	reader io.Reader //Ignore this for now
	url    string
)

func init() {
	http.HandleFunc("/filter", filterHandler)
	server = httptest.NewServer(http.DefaultServeMux)
	url = fmt.Sprintf("%s/filter", server.URL)
}

func TestHandler(t *testing.T) {
	assert := assert.New(t)
	testMessage := `Good morning @chris! (megusta) (coffee)`
	testResponse := `{"emoicons": ["megusta", "coffee"], "mentions": ["chris"]}`

	reader = strings.NewReader(testMessage)

	request, err := http.NewRequest("POST", url, reader)

	res, err := http.DefaultClient.Do(request)
	assert.Nil(err)
	if res.StatusCode != 200 {
		t.Errorf("Success expected: %d", res.StatusCode)
	}
	buf, err := ioutil.ReadAll(res.Body)
	assert.Nil(err)
	res.Body.Close()
	assert.JSONEq(testResponse, string(buf))
}


text:
OptimizationGuideKeyedService*
OptimizationGuideKeyedServiceFactory::GetForProfile(Profile* profile) {
  if (optimization_guide::features::IsOptimizationHintsEnabled()) {
    return static_cast<OptimizationGuideKeyedService*>(
        GetInstance()->GetServiceForBrowserContext(profile, true));
  }
  return nullptr;
}

// static
OptimizationGuideKeyedServiceFactory*
OptimizationGuideKeyedServiceFactory::GetInstance() {
  static base::NoDestructor<OptimizationGuideKeyedServiceFactory> factory;
  return factory.get();
}

OptimizationGuideKeyedServiceFactory::OptimizationGuideKeyedServiceFactory()
    : BrowserContextKeyedServiceFactory(
          "OptimizationGuideKeyedService",
          BrowserContextDependencyManager::GetInstance()) {}

OptimizationGuideKeyedServiceFactory::~OptimizationGuideKeyedServiceFactory() =
    default;

KeyedService* OptimizationGuideKeyedServiceFactory::BuildServiceInstanceFor(
    content::BrowserContext* context) const {
#if BUILDFLAG(IS_CHROMEOS_ASH)
  // Do not build the OptimizationGuideKeyedService if it's a sign-in profile
  // since it basically is an ephemeral profile anyway and we cannot provide
  // hints or models to it anyway. Additionally, sign in profiles do not go
  // through the standard profile initialization flow, so a lot of things that
  // are required are not available when the browser context for the signin
  // profile is created.
  Profile* profile = Profile::FromBrowserContext(context);
  if (chromeos::ProfileHelper::IsSigninProfile(profile))
    return nullptr;
#endif
  return new OptimizationGuideKeyedService(context);
}

content::BrowserContext*
OptimizationGuideKeyedServiceFactory::GetBrowserContextToUse(
    content::BrowserContext* context) const {
  return chrome::GetBrowserContextOwnInstanceInIncognito(context);
}

bool OptimizationGuideKeyedServiceFactory::ServiceIsCreatedWithBrowserContext()
    const {
  return optimization_guide::features::IsOptimizationHintsEnabled();
}

bool OptimizationGuideKeyedServiceFactory::ServiceIsNULLWhileTesting() const {
  return true;
}


text:
namespace threadutil {

// Holds references to a group of threads and exposes functionality to operate
// on all of them at once.
class ThreadGroup {
 public:
  // ThreadGroup's destructor makes sure that all threads are joined, else we
  // risk 'terminate called without an active exception' crashes.
  ~ThreadGroup() { JoinAll(); }

  // Adds a new thread to the group.
  template <class Function>
  inline void AddThread(Function&& f) {
    threads_.push_back(std::thread(std::forward<Function>(f)));
  };

  // Waits for all threads in the group to complete execution.
  inline void JoinAll() {
    for (auto& thread : threads_) {
      thread.join();
    }
    threads_.clear();
  };

 private:
  std::vector<std::thread> threads_;
};

}  // namespace threadutil

#endif