text:
package org.apache.hyracks.storage.am.lsm.common.impls;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.apache.hyracks.api.exceptions.HyracksDataException;
import org.apache.hyracks.api.io.FileReference;
import org.apache.hyracks.api.replication.IIOReplicationManager;
import org.apache.hyracks.storage.am.lsm.common.api.IVirtualBufferCache;
import org.apache.hyracks.storage.common.buffercache.ICacheMemoryAllocator;
import org.apache.hyracks.storage.common.buffercache.ICachedPage;
import org.apache.hyracks.storage.common.file.BufferedFileHandle;
import org.apache.hyracks.storage.common.file.IFileMapManager;
import org.apache.hyracks.storage.common.file.TransientFileMapManager;

public class VirtualBufferCache implements IVirtualBufferCache {
    private static final int OVERFLOW_PADDING = 8;

    private final ICacheMemoryAllocator allocator;
    private final IFileMapManager fileMapManager;
    private final int pageSize;
    private final int numPages;

    private final CacheBucket[] buckets;
    private final ArrayList<VirtualPage> pages;

    private volatile int nextFree;

    private boolean open;

    public VirtualBufferCache(ICacheMemoryAllocator allocator, int pageSize, int numPages) {
        this.allocator = allocator;
        this.fileMapManager = new TransientFileMapManager();
        this.pageSize = pageSize;
        this.numPages = 2 * (numPages / 2) + 1;

        buckets = new CacheBucket[this.numPages];
        pages = new ArrayList<VirtualPage>();
        nextFree = 0;
        open = false;
    }

    @Override
    public void createFile(FileReference fileRef) throws HyracksDataException {
        synchronized (fileMapManager) {
            if (fileMapManager.isMapped(fileRef)) {
                throw new HyracksDataException("File " + fileRef + " is already mapped");
            }
            fileMapManager.registerFile(fileRef);
        }
    }

    @Override
    public void openFile(int fileId) throws HyracksDataException {
    }

    @Override
    public void closeFile(int fileId) throws HyracksDataException {
    }

    @Override
    public void deleteFile(int fileId, boolean flushDirtyPages) throws HyracksDataException {
        synchronized (fileMapManager) {
            if (!fileMapManager.isMapped(fileId)) {
                throw new HyracksDataException("File with id " + fileId + " is not mapped");
            }
            fileMapManager.unregisterFile(fileId);
        }

        for (int i = 0; i < buckets.length; i++) {
            CacheBucket bucket = buckets[i];
            bucket.bucketLock.lock();
            try {
                VirtualPage prev = null;
                VirtualPage curr = bucket.cachedPage;
                while (curr != null) {
                    if (BufferedFileHandle.getFileId(curr.dpid) == fileId) {
                        if (prev == null) {
                            bucket.cachedPage = curr.next;
                            curr.reset();
                            curr = bucket.cachedPage;
                        } else {
                            prev.next = curr.next;
                            curr.reset();
                            curr = prev.next;
                        }
                    } else {
                        prev = curr;
                        curr = curr.next;
                    }
                }
            } finally {
                bucket.bucketLock.unlock();
            }
        }
        defragPageList();
    }

    private void defragPageList() {
        synchronized (pages) {
            int start = 0;
            int end = nextFree - 1;
            while (start < end) {
                VirtualPage lastUsed = pages.get(end);
                while (end > 0 && lastUsed.dpid == -1) {
                    --end;
                    lastUsed = pages.get(end);
                }

                if (end == 0) {
                    nextFree = lastUsed.dpid == -1 ? 0 : 1;
                    break;
                }

                VirtualPage firstUnused = pages.get(start);
                while (start < end && firstUnused.dpid != -1) {
                    ++start;
                    firstUnused = pages.get(start);
                }

                if (start >= end) {
                    break;
                }

                Collections.swap(pages, start, end);
                nextFree = end;
                --end;
                ++start;
            }
        }
    }

    @Override
    public ICachedPage tryPin(long dpid) throws HyracksDataException {
        return pin(dpid, false);
    }

    @Override
    public ICachedPage pin(long dpid, boolean newPage) throws HyracksDataException {
        VirtualPage page = null;
        int hash = hash(dpid);
        CacheBucket bucket = buckets[hash];

        bucket.bucketLock.lock();
        try {
            page = bucket.cachedPage;
            while (page != null) {
                if (page.dpid == dpid) {
                    return page;
                }
                page = page.next;
            }

            if (!newPage) {
                throw new HyracksDataException("Page " + BufferedFileHandle.getPageId(dpid)
                        + " does not exist in file "
                        + fileMapManager.lookupFileName(BufferedFileHandle.getFileId(dpid)));
            }

            page = getOrAllocPage(dpid);
            page.next = bucket.cachedPage;
            bucket.cachedPage = page;
        } finally {
            bucket.bucketLock.unlock();
        }

        return page;
    }

    private int hash(long dpid) {
        int hashValue = (int) dpid ^ (Integer.reverse((int) (dpid >>> 32)) >>> 1);
        return hashValue % buckets.length;
    }

    private VirtualPage getOrAllocPage(long dpid) {
        VirtualPage page = null;
        synchronized (pages) {
            if (nextFree >= pages.size()) {
                page = new VirtualPage(allocator.allocate(pageSize, 1)[0]);
                pages.add(page);
            } else {
                page = pages.get(nextFree);
            }
            ++nextFree;
            page.dpid = dpid;
        }
        return page;
    }

    @Override
    public void unpin(ICachedPage page) throws HyracksDataException {
    }

    @Override
    public void flushDirtyPage(ICachedPage page) throws HyracksDataException {
    }

    @Override
    public void force(int fileId, boolean metadata) throws HyracksDataException {
    }

    @Override
    public int getPageSize() {
        return pageSize;
    }

    @Override
    public int getNumPages() {
        return numPages;
    }

    @Override
    public void open() throws HyracksDataException {
        if (open) {
            throw new HyracksDataException("Failed to open virtual buffercache since it is already open.");
        }
        pages.trimToSize();
        pages.ensureCapacity(numPages + OVERFLOW_PADDING);
        ByteBuffer[] buffers = allocator.allocate(pageSize, numPages);
        for (int i = 0; i < numPages; i++) {
            pages.add(new VirtualPage(buffers[i]));
            buckets[i] = new CacheBucket();
        }
        nextFree = 0;
        open = true;
    }

    @Override
    public void reset() {
        for (int i = 0; i < numPages; i++) {
            pages.get(i).reset();
            buckets[i].cachedPage = null;
        }
        int excess = pages.size() - numPages;
        if (excess > 0) {
            for (int i = numPages + excess - 1; i >= numPages; i--) {
                pages.remove(i);
            }
        }
        nextFree = 0;
    }

    @Override
    public void close() throws HyracksDataException {
        if (!open) {
            throw new HyracksDataException("Failed to close virtual buffercache since it is already closed.");
        }

        pages.clear();
        for (int i = 0; i < numPages; i++) {
            buckets[i].cachedPage = null;
        }
        open = false;
    }

    public String dumpState() {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("Page size = %d\n", pageSize));
        sb.append(String.format("Capacity = %d\n", numPages));
        sb.append(String.format("Allocated pages = %d\n", pages.size()));
        sb.append(String.format("Next free page = %d\n", nextFree));
        return sb.toString();
    }

    @Override
    public IFileMapManager getFileMapProvider() {
        return fileMapManager;
    }

    @Override
    public boolean isFull() {
        return nextFree >= numPages;
    }

    private static class CacheBucket {
        private final ReentrantLock bucketLock;
        private VirtualPage cachedPage;

        public CacheBucket() {
            this.bucketLock = new ReentrantLock();
        }
    }

    private class VirtualPage implements ICachedPage {
        final ByteBuffer buffer;
        final ReadWriteLock latch;
        volatile long dpid;
        VirtualPage next;

        public VirtualPage(ByteBuffer buffer) {
            this.buffer = buffer;
            latch = new ReentrantReadWriteLock(true);
            dpid = -1;
            next = null;
        }

        public void reset() {
            dpid = -1;
            next = null;
        }

        @Override
        public ByteBuffer getBuffer() {
            return buffer;
        }

        @Override
        public void acquireReadLatch() {
            latch.readLock().lock();
        }

        @Override
        public void releaseReadLatch() {
            latch.readLock().unlock();
        }

        @Override
        public void acquireWriteLatch() {
            latch.writeLock().lock();
        }

        @Override
        public void releaseWriteLatch(boolean markDirty) {
            latch.writeLock().unlock();
        }

    }

    //These 4 methods aren't applicable here.
    @Override
    public int createMemFile() throws HyracksDataException {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void deleteMemFile(int fileId) throws HyracksDataException {
        // TODO Auto-generated method stub

    }

    @Override
    public ICachedPage pinVirtual(long vpid) throws HyracksDataException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public ICachedPage unpinVirtual(long vpid, long dpid) throws HyracksDataException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int getFileReferenceCount(int fileId) {
        return 0;
    }

    @Override
    public boolean isReplicationEnabled() {
        return false;
    }

    @Override
    public IIOReplicationManager getIIOReplicationManager() {
        return null;
    }
}


text:
"""Tests for type_utils."""

import numpy as np
import pytest
import tensorflow as tf
from tensorflow_datasets.core.utils import type_utils

@pytest.mark.parametrize('input_args,expected_output', [
    (np.int64, np.int64),
    (tf.int64, np.int64),
    (np.float64, np.float64),
    (tf.float64, np.float64),
    (tf.string, np.str_),
])
def test_tree_parallel_map(input_args, expected_output):
  assert type_utils.cast_to_numpy(input_args) == expected_output

redpajama_set_name: RedPajamaGithub


text:
export interface Entity {
  changed?: number;
  cid?: number;
  created?: number;
  language?: string;
  picture?: number;
  name?: string;
  uid?: number;
  status?: number;
}

export interface CreatedEntity {
  uri: string;
}


text:
var Strategies = require('./strategies');
var DIFF_TYPE = require('./diffType');

var EMPTY_STRATEGY = function () {};

/**
 * @param {Array<diff_match_patch.Diff>} diffs
 * @param {Boolean} forNewText True if the text in blockMap is the new text.
 * @param {DraftJS.BlockMap} blockMap The BlockMap of the ContentState to decorate
 * @return {Strategies} Three strategies that identify ranges of text for each type of diff.
 * Only two of them will actually be relevant (equal and insert for
 * new text, or equal and delete for old text).
 */
function diffDecoratorStrategies(diffs, forNewText, blockMap) {
    var absoluteRanges = diffToAbsoluteRanges(diffs, forNewText);

    var modifiedMapping = mapRangesToBlocks(absoluteRanges.modified, blockMap);
    var equalMapping = mapRangesToBlocks(absoluteRanges.equal, blockMap);

    var modifiedStrategy = strategyFromMapping(modifiedMapping, blockMap);
    var equalStrategy = strategyFromMapping(equalMapping, blockMap);

    if (forNewText) {
        return new Strategies({
            delete: EMPTY_STRATEGY,
            equal: equalStrategy,
            insert: modifiedStrategy
        });
    } else {
        return new Strategies({
            delete: modifiedStrategy,
            equal: equalStrategy,
            insert: EMPTY_STRATEGY
        });
    }
}

/**
 * Returns the absolute ranges for equal and modified (insert or delete) texts.
 * @param {Array<diff_match_patch.Diff>} diffs
 * @param {Boolean} forNewText
 * @returns {Object<Array<Range>>} Two list of ranges (equal and modified)
 */
function diffToAbsoluteRanges(diffs, forNewText) {
    var absoluteRanges = {
        equal: [],
        modified: []
    };
    var typeToIgnore = forNewText ? DIFF_TYPE.DELETE : DIFF_TYPE.INSERT;

    var charIndex = 0;
    diffs.forEach(function (diff) {
        var diffType = diff[0];
        var diffText = diff[1];

        if (diffType === typeToIgnore) {
            return;
        }

        var range = {
            start: charIndex,
            end: charIndex + diffText.length
        };
        if (diffType === DIFF_TYPE.EQUAL) {
            absoluteRanges.equal.push(range);
        } else {
            absoluteRanges.modified.push(range);
        }
        // Progress in the text
        charIndex += diffText.length;
    });

    return absoluteRanges;
}

/**
 * @param {Array<Range>} absoluteRanges The ranges for the whole text
 * @param {DraftJS.BlockMap} blockMap The BlockMap of the ContentState to decorate
 * @returns {Immutable.Map<BlockKey, Array<Range>>} Ranges are relative to each block.
 */
function mapRangesToBlocks(absoluteRanges, blockMap) {
    var blockStartIndex = 0;
    return blockMap.map(function (block) {
        var ranges = findRangesBetween(absoluteRanges,
                                       blockStartIndex,
                                       blockStartIndex + block.getLength());
        blockStartIndex += block.getLength() + 1; // Account for possible '\n'
        return ranges;
    });
}

/**
 * @param {Array<Range>} ranges
 * @param {Number} start
 * @param {Number} end
 * @returns {Array<Range>} All the ranges that overlapped
 * start-end, cropped and re-indexed to be relative to start.
 */
function findRangesBetween(ranges, start, end) {
    var res = [];
    ranges.forEach(function (range) {
        if (range.start < end && range.end > start) {
            // Crop the range
            var intersectionStart = Math.max(range.start, start);
            var intersectionEnd = Math.min(range.end, end);
            // Push relative range
            res.push({
                start: intersectionStart - start,
                end: intersectionEnd - start
            });
        }
    });
    return res;
}

/**
 * @returns {Immutable.Map<BlockKey, Array<Range>>} mappedRanges
 * @returns {DraftJS.BlockMap} blockMap
 * @returns {DraftJS.DecoratorStrategy} A strategy applying to the
 * ranges provided for each block. Once the block's content change, the
 * block will not be decorated anymore.
 */
function strategyFromMapping(mappedRanges, blockMap) {
    // Save the original blockMap's content for later comparison
    return function (contentBlock, callback) {
        var key = contentBlock.getKey();
        var ranges = mappedRanges.get(key);
        if (!ranges) {
            return;
        }
        var oldContent = blockMap.get(key).getText();
        var newContent = contentBlock.getText();
        // If the content is still the same
        if (oldContent === newContent) {
            ranges.forEach(function (range) {
                callback(range.start, range.end);
            });
        }
    };
}

module.exports = diffDecoratorStrategies;


text:
package libcontainer

import (
	"bufio"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"path"
	"regexp"
	"strconv"
	"strings"
	"time"

	"github.com/google/cadvisor/container"
	info "github.com/google/cadvisor/info/v1"
	"golang.org/x/sys/unix"

	"bytes"

	"github.com/opencontainers/runc/libcontainer"
	"github.com/opencontainers/runc/libcontainer/cgroups"
	"k8s.io/klog"
)

type Handler struct {
	cgroupManager   cgroups.Manager
	rootFs          string
	pid             int
	includedMetrics container.MetricSet
	pidMetricsCache map[int]*info.CpuSchedstat
}

var whitelistedUlimits = [...]string{"max_open_files"}

func NewHandler(cgroupManager cgroups.Manager, rootFs string, pid int, includedMetrics container.MetricSet) *Handler {
	return &Handler{
		cgroupManager:   cgroupManager,
		rootFs:          rootFs,
		pid:             pid,
		includedMetrics: includedMetrics,
		pidMetricsCache: make(map[int]*info.CpuSchedstat),
	}
}

// Get cgroup and networking stats of the specified container
func (h *Handler) GetStats() (*info.ContainerStats, error) {
	cgroupStats, err := h.cgroupManager.GetStats()
	if err != nil {
		return nil, err
	}
	libcontainerStats := &libcontainer.Stats{
		CgroupStats: cgroupStats,
	}
	stats := newContainerStats(libcontainerStats, h.includedMetrics)

	if h.includedMetrics.Has(container.ProcessSchedulerMetrics) {
		pids, err := h.cgroupManager.GetAllPids()
		if err != nil {
			klog.V(4).Infof("Could not get PIDs for container %d: %v", h.pid, err)
		} else {
			stats.Cpu.Schedstat, err = schedulerStatsFromProcs(h.rootFs, pids, h.pidMetricsCache)
			if err != nil {
				klog.V(4).Infof("Unable to get Process Scheduler Stats: %v", err)
			}
		}
	}

	// If we know the pid then get network stats from /proc/<pid>/net/dev
	if h.pid == 0 {
		return stats, nil
	}
	if h.includedMetrics.Has(container.NetworkUsageMetrics) {
		netStats, err := networkStatsFromProc(h.rootFs, h.pid)
		if err != nil {
			klog.V(4).Infof("Unable to get network stats from pid %d: %v", h.pid, err)
		} else {
			stats.Network.Interfaces = append(stats.Network.Interfaces, netStats...)
		}
	}
	if h.includedMetrics.Has(container.NetworkTcpUsageMetrics) {
		t, err := tcpStatsFromProc(h.rootFs, h.pid, "net/tcp")
		if err != nil {
			klog.V(4).Infof("Unable to get tcp stats from pid %d: %v", h.pid, err)
		} else {
			stats.Network.Tcp = t
		}

		t6, err := tcpStatsFromProc(h.rootFs, h.pid, "net/tcp6")
		if err != nil {
			klog.V(4).Infof("Unable to get tcp6 stats from pid %d: %v", h.pid, err)
		} else {
			stats.Network.Tcp6 = t6
		}

	}
	if h.includedMetrics.Has(container.NetworkAdvancedTcpUsageMetrics) {
		ta, err := advancedTcpStatsFromProc(h.rootFs, h.pid, "net/netstat", "net/snmp")
		if err != nil {
			klog.V(4).Infof("Unable to get advanced tcp stats from pid %d: %v", h.pid, err)
		} else {
			stats.Network.TcpAdvanced = ta
		}
	}
	if h.includedMetrics.Has(container.NetworkUdpUsageMetrics) {
		u, err := udpStatsFromProc(h.rootFs, h.pid, "net/udp")
		if err != nil {
			klog.V(4).Infof("Unable to get udp stats from pid %d: %v", h.pid, err)
		} else {
			stats.Network.Udp = u
		}

		u6, err := udpStatsFromProc(h.rootFs, h.pid, "net/udp6")
		if err != nil {
			klog.V(4).Infof("Unable to get udp6 stats from pid %d: %v", h.pid, err)
		} else {
			stats.Network.Udp6 = u6
		}
	}
	if h.includedMetrics.Has(container.ProcessMetrics) {
		paths := h.cgroupManager.GetPaths()
		path, ok := paths["cpu"]
		if !ok {
			klog.V(4).Infof("Could not find cgroups CPU for container %d", h.pid)
		} else {
			stats.Processes, err = processStatsFromProcs(h.rootFs, path, h.pid)
			if err != nil {
				klog.V(4).Infof("Unable to get Process Stats: %v", err)
			}
		}

		// if include processes metrics, just set threads metrics if exist, and has no relationship with cpu path
		setThreadsStats(cgroupStats, stats)
	}

	// For backwards compatibility.
	if len(stats.Network.Interfaces) > 0 {
		stats.Network.InterfaceStats = stats.Network.Interfaces[0]
	}

	return stats, nil
}

func parseUlimit(value string) (int64, error) {
	num, err := strconv.ParseInt(value, 10, 64)
	if err != nil {
		if strings.EqualFold(value, "unlimited") {
			// -1 implies unlimited except for priority and nice; man limits.conf
			num = -1
		} else {
			// Value is not a number or "unlimited"; return an error
			return 0, fmt.Errorf("unable to parse limit: %s", value)
		}
	}
	return num, nil
}

func isUlimitWhitelisted(name string) bool {
	for _, whitelist := range whitelistedUlimits {
		if name == whitelist {
			return true
		}
	}
	return false
}

func processLimitsFile(fileData string) []info.UlimitSpec {
	limits := strings.Split(fileData, "\n")
	ulimits := make([]info.UlimitSpec, 0, len(limits))
	for _, lim := range limits {
		// Skip any headers/footers
		if strings.HasPrefix(lim, "Max") {

			// Line format: Max open files            16384                16384                files
			fields := regexp.MustCompile("[\\s]{2,}").Split(lim, -1)
			name := strings.Replace(strings.ToLower(strings.TrimSpace(fields[0])), " ", "_", -1)

			found := isUlimitWhitelisted(name)
			if !found {
				continue
			}

			soft := strings.TrimSpace(fields[1])
			soft_num, soft_err := parseUlimit(soft)

			hard := strings.TrimSpace(fields[2])
			hard_num, hard_err := parseUlimit(hard)

			// Omit metric if there were any parsing errors
			if soft_err == nil && hard_err == nil {
				ulimitSpec := info.UlimitSpec{
					Name:      name,
					SoftLimit: int64(soft_num),
					HardLimit: int64(hard_num),
				}
				ulimits = append(ulimits, ulimitSpec)
			}
		}
	}
	return ulimits
}

func processRootProcUlimits(rootFs string, rootPid int) []info.UlimitSpec {
	filePath := path.Join(rootFs, "/proc", strconv.Itoa(rootPid), "limits")
	out, err := ioutil.ReadFile(filePath)
	if err != nil {
		klog.V(4).Infof("error while listing directory %q to read ulimits: %v", filePath, err)
		return []info.UlimitSpec{}
	}
	return processLimitsFile(string(out))
}

func processStatsFromProcs(rootFs string, cgroupPath string, rootPid int) (info.ProcessStats, error) {
	var fdCount, socketCount uint64
	filePath := path.Join(cgroupPath, "cgroup.procs")
	out, err := ioutil.ReadFile(filePath)
	if err != nil {
		return info.ProcessStats{}, fmt.Errorf("couldn't open cpu cgroup procs file %v : %v", filePath, err)
	}

	pids := strings.Split(string(out), "\n")

	// EOL is also treated as a new line while reading "cgroup.procs" file with ioutil.ReadFile.
	// The last value is an empty string "". Ex: pids = ["22", "1223", ""]
	// Trim the last value
	if len(pids) != 0 && pids[len(pids)-1] == "" {
		pids = pids[:len(pids)-1]
	}

	for _, pid := range pids {
		dirPath := path.Join(rootFs, "/proc", pid, "fd")
		fds, err := ioutil.ReadDir(dirPath)
		if err != nil {
			klog.V(4).Infof("error while listing directory %q to measure fd count: %v", dirPath, err)
			continue
		}
		fdCount += uint64(len(fds))
		for _, fd := range fds {
			fdPath := path.Join(dirPath, fd.Name())
			linkName, err := os.Readlink(fdPath)
			if err != nil {
				klog.V(4).Infof("error while reading %q link: %v", fdPath, err)
				continue
			}
			if strings.HasPrefix(linkName, "socket") {
				socketCount++
			}
		}
	}
	ulimits := processRootProcUlimits(rootFs, rootPid)

	processStats := info.ProcessStats{
		ProcessCount: uint64(len(pids)),
		FdCount:      fdCount,
		SocketCount:  socketCount,
		Ulimits:      ulimits,
	}

	return processStats, nil
}

func schedulerStatsFromProcs(rootFs string, pids []int, pidMetricsCache map[int]*info.CpuSchedstat) (info.CpuSchedstat, error) {
	for _, pid := range pids {
		f, err := os.Open(path.Join(rootFs, "proc", strconv.Itoa(pid), "schedstat"))
		if err != nil {
			return info.CpuSchedstat{}, fmt.Errorf("couldn't open scheduler statistics for process %d: %v", pid, err)
		}
		defer f.Close()
		contents, err := ioutil.ReadAll(f)
		if err != nil {
			return info.CpuSchedstat{}, fmt.Errorf("couldn't read scheduler statistics for process %d: %v", pid, err)
		}
		rawMetrics := bytes.Split(bytes.TrimRight(contents, "\n"), []byte(" "))
		if len(rawMetrics) != 3 {
			return info.CpuSchedstat{}, fmt.Errorf("unexpected number of metrics in schedstat file for process %d", pid)
		}
		cacheEntry, ok := pidMetricsCache[pid]
		if !ok {
			cacheEntry = &info.CpuSchedstat{}
			pidMetricsCache[pid] = cacheEntry
		}
		for i, rawMetric := range rawMetrics {
			metric, err := strconv.ParseUint(string(rawMetric), 10, 64)
			if err != nil {
				return info.CpuSchedstat{}, fmt.Errorf("parsing error while reading scheduler statistics for process: %d: %v", pid, err)
			}
			switch i {
			case 0:
				cacheEntry.RunTime = metric
			case 1:
				cacheEntry.RunqueueTime = metric
			case 2:
				cacheEntry.RunPeriods = metric
			}
		}
	}
	schedstats := info.CpuSchedstat{}
	for _, v := range pidMetricsCache {
		schedstats.RunPeriods += v.RunPeriods
		schedstats.RunqueueTime += v.RunqueueTime
		schedstats.RunTime += v.RunTime
	}
	return schedstats, nil
}

func networkStatsFromProc(rootFs string, pid int) ([]info.InterfaceStats, error) {
	netStatsFile := path.Join(rootFs, "proc", strconv.Itoa(pid), "/net/dev")

	ifaceStats, err := scanInterfaceStats(netStatsFile)
	if err != nil {
		return []info.InterfaceStats{}, fmt.Errorf("couldn't read network stats: %v", err)
	}

	return ifaceStats, nil
}

var (
	ignoredDevicePrefixes = []string{"lo", "veth", "docker"}
)

func isIgnoredDevice(ifName string) bool {
	for _, prefix := range ignoredDevicePrefixes {
		if strings.HasPrefix(strings.ToLower(ifName), prefix) {
			return true
		}
	}
	return false
}

func scanInterfaceStats(netStatsFile string) ([]info.InterfaceStats, error) {
	file, err := os.Open(netStatsFile)
	if err != nil {
		return nil, fmt.Errorf("failure opening %s: %v", netStatsFile, err)
	}
	defer file.Close()

	scanner := bufio.NewScanner(file)

	// Discard header lines
	for i := 0; i < 2; i++ {
		if b := scanner.Scan(); !b {
			return nil, scanner.Err()
		}
	}

	stats := []info.InterfaceStats{}
	for scanner.Scan() {
		line := scanner.Text()
		line = strings.Replace(line, ":", "", -1)

		fields := strings.Fields(line)
		// If the format of the  line is invalid then don't trust any of the stats
		// in this file.
		if len(fields) != 17 {
			return nil, fmt.Errorf("invalid interface stats line: %v", line)
		}

		devName := fields[0]
		if isIgnoredDevice(devName) {
			continue
		}

		i := info.InterfaceStats{
			Name: devName,
		}

		statFields := append(fields[1:5], fields[9:13]...)
		statPointers := []*uint64{
			&i.RxBytes, &i.RxPackets, &i.RxErrors, &i.RxDropped,
			&i.TxBytes, &i.TxPackets, &i.TxErrors, &i.TxDropped,
		}

		err := setInterfaceStatValues(statFields, statPointers)
		if err != nil {
			return nil, fmt.Errorf("cannot parse interface stats (%v): %v", err, line)
		}

		stats = append(stats, i)
	}

	return stats, nil
}

func setInterfaceStatValues(fields []string, pointers []*uint64) error {
	for i, v := range fields {
		val, err := strconv.ParseUint(v, 10, 64)
		if err != nil {
			return err
		}
		*pointers[i] = val
	}
	return nil
}

func tcpStatsFromProc(rootFs string, pid int, file string) (info.TcpStat, error) {
	tcpStatsFile := path.Join(rootFs, "proc", strconv.Itoa(pid), file)

	tcpStats, err := scanTcpStats(tcpStatsFile)
	if err != nil {
		return tcpStats, fmt.Errorf("couldn't read tcp stats: %v", err)
	}

	return tcpStats, nil
}

func advancedTcpStatsFromProc(rootFs string, pid int, file1, file2 string) (info.TcpAdvancedStat, error) {
	var advancedStats info.TcpAdvancedStat
	var err error

	netstatFile := path.Join(rootFs, "proc", strconv.Itoa(pid), file1)
	err = scanAdvancedTcpStats(&advancedStats, netstatFile)
	if err != nil {
		return advancedStats, err
	}

	snmpFile := path.Join(rootFs, "proc", strconv.Itoa(pid), file2)
	err = scanAdvancedTcpStats(&advancedStats, snmpFile)
	if err != nil {
		return advancedStats, err
	}

	return advancedStats, nil
}

func scanAdvancedTcpStats(advancedStats *info.TcpAdvancedStat, advancedTcpStatsFile string) error {
	data, err := ioutil.ReadFile(advancedTcpStatsFile)
	if err != nil {
		return fmt.Errorf("failure opening %s: %v", advancedTcpStatsFile, err)
	}

	reader := strings.NewReader(string(data))
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanLines)

	advancedTcpStats := make(map[string]interface{})
	for scanner.Scan() {
		nameParts := strings.Split(scanner.Text(), " ")
		scanner.Scan()
		valueParts := strings.Split(scanner.Text(), " ")
		// Remove trailing :. and ignore non-tcp
		protocol := nameParts[0][:len(nameParts[0])-1]
		if protocol != "TcpExt" && protocol != "Tcp" {
			continue
		}
		if len(nameParts) != len(valueParts) {
			return fmt.Errorf("mismatch field count mismatch in %s: %s",
				advancedTcpStatsFile, protocol)
		}
		for i := 1; i < len(nameParts); i++ {
			if strings.Contains(valueParts[i], "-") {
				vInt64, err := strconv.ParseInt(valueParts[i], 10, 64)
				if err != nil {
					return fmt.Errorf("decode value: %s to int64 error: %s", valueParts[i], err)
				}
				advancedTcpStats[nameParts[i]] = vInt64
			} else {
				vUint64, err := strconv.ParseUint(valueParts[i], 10, 64)
				if err != nil {
					return fmt.Errorf("decode value: %s to uint64 error: %s", valueParts[i], err)
				}
				advancedTcpStats[nameParts[i]] = vUint64
			}
		}
	}

	b, err := json.Marshal(advancedTcpStats)
	if err != nil {
		return err
	}

	err = json.Unmarshal(b, advancedStats)
	if err != nil {
		return err
	}

	return scanner.Err()

}

func scanTcpStats(tcpStatsFile string) (info.TcpStat, error) {

	var stats info.TcpStat

	data, err := ioutil.ReadFile(tcpStatsFile)
	if err != nil {
		return stats, fmt.Errorf("failure opening %s: %v", tcpStatsFile, err)
	}

	tcpStateMap := map[string]uint64{
		"01": 0, //ESTABLISHED
		"02": 0, //SYN_SENT
		"03": 0, //SYN_RECV
		"04": 0, //FIN_WAIT1
		"05": 0, //FIN_WAIT2
		"06": 0, //TIME_WAIT
		"07": 0, //CLOSE
		"08": 0, //CLOSE_WAIT
		"09": 0, //LAST_ACK
		"0A": 0, //LISTEN
		"0B": 0, //CLOSING
	}

	reader := strings.NewReader(string(data))
	scanner := bufio.NewScanner(reader)

	scanner.Split(bufio.ScanLines)

	// Discard header line
	if b := scanner.Scan(); !b {
		return stats, scanner.Err()
	}

	for scanner.Scan() {
		line := scanner.Text()

		state := strings.Fields(line)
		// TCP state is the 4th field.
		// Format: sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt  uid timeout inode
		tcpState := state[3]
		_, ok := tcpStateMap[tcpState]
		if !ok {
			return stats, fmt.Errorf("invalid TCP stats line: %v", line)
		}
		tcpStateMap[tcpState]++
	}

	stats = info.TcpStat{
		Established: tcpStateMap["01"],
		SynSent:     tcpStateMap["02"],
		SynRecv:     tcpStateMap["03"],
		FinWait1:    tcpStateMap["04"],
		FinWait2:    tcpStateMap["05"],
		TimeWait:    tcpStateMap["06"],
		Close:       tcpStateMap["07"],
		CloseWait:   tcpStateMap["08"],
		LastAck:     tcpStateMap["09"],
		Listen:      tcpStateMap["0A"],
		Closing:     tcpStateMap["0B"],
	}

	return stats, nil
}

func udpStatsFromProc(rootFs string, pid int, file string) (info.UdpStat, error) {
	var err error
	var udpStats info.UdpStat

	udpStatsFile := path.Join(rootFs, "proc", strconv.Itoa(pid), file)

	r, err := os.Open(udpStatsFile)
	if err != nil {
		return udpStats, fmt.Errorf("failure opening %s: %v", udpStatsFile, err)
	}

	udpStats, err = scanUdpStats(r)
	if err != nil {
		return udpStats, fmt.Errorf("couldn't read udp stats: %v", err)
	}

	return udpStats, nil
}

func scanUdpStats(r io.Reader) (info.UdpStat, error) {
	var stats info.UdpStat

	scanner := bufio.NewScanner(r)
	scanner.Split(bufio.ScanLines)

	// Discard header line
	if b := scanner.Scan(); !b {
		return stats, scanner.Err()
	}

	listening := uint64(0)
	dropped := uint64(0)
	rxQueued := uint64(0)
	txQueued := uint64(0)

	for scanner.Scan() {
		line := scanner.Text()
		// Format: sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt  uid timeout inode ref pointer drops

		listening++

		fs := strings.Fields(line)
		if len(fs) != 13 {
			continue
		}

		rx, tx := uint64(0), uint64(0)
		fmt.Sscanf(fs[4], "%X:%X", &rx, &tx)
		rxQueued += rx
		txQueued += tx

		d, err := strconv.Atoi(string(fs[12]))
		if err != nil {
			continue
		}
		dropped += uint64(d)
	}

	stats = info.UdpStat{
		Listen:   listening,
		Dropped:  dropped,
		RxQueued: rxQueued,
		TxQueued: txQueued,
	}

	return stats, nil
}

func (h *Handler) GetProcesses() ([]int, error) {
	pids, err := h.cgroupManager.GetPids()
	if err != nil {
		return nil, err
	}
	return pids, nil
}

func minUint32(x, y uint32) uint32 {
	if x < y {
		return x
	}
	return y
}

// var to allow unit tests to stub it out
var numCpusFunc = getNumberOnlineCPUs

// Convert libcontainer stats to info.ContainerStats.
func setCpuStats(s *cgroups.Stats, ret *info.ContainerStats, withPerCPU bool) {
	ret.Cpu.Usage.User = s.CpuStats.CpuUsage.UsageInUsermode
	ret.Cpu.Usage.System = s.CpuStats.CpuUsage.UsageInKernelmode
	ret.Cpu.Usage.Total = s.CpuStats.CpuUsage.TotalUsage
	ret.Cpu.CFS.Periods = s.CpuStats.ThrottlingData.Periods
	ret.Cpu.CFS.ThrottledPeriods = s.CpuStats.ThrottlingData.ThrottledPeriods
	ret.Cpu.CFS.ThrottledTime = s.CpuStats.ThrottlingData.ThrottledTime

	if !withPerCPU {
		return
	}
	if len(s.CpuStats.CpuUsage.PercpuUsage) == 0 {
		// libcontainer's 'GetStats' can leave 'PercpuUsage' nil if it skipped the
		// cpuacct subsystem.
		return
	}

	numPossible := uint32(len(s.CpuStats.CpuUsage.PercpuUsage))
	// Note that as of https://patchwork.kernel.org/patch/8607101/ (kernel v4.7),
	// the percpu usage information includes extra zero values for all additional
	// possible CPUs. This is to allow statistic collection after CPU-hotplug.
	// We intentionally ignore these extra zeroes.
	numActual, err := numCpusFunc()
	if err != nil {
		klog.Errorf("unable to determine number of actual cpus; defaulting to maximum possible number: errno %v", err)
		numActual = numPossible
	}
	if numActual > numPossible {
		// The real number of cores should never be greater than the number of
		// datapoints reported in cpu usage.
		klog.Errorf("PercpuUsage had %v cpus, but the actual number is %v; ignoring extra CPUs", numPossible, numActual)
	}
	numActual = minUint32(numPossible, numActual)
	ret.Cpu.Usage.PerCpu = make([]uint64, numActual)

	for i := uint32(0); i < numActual; i++ {
		ret.Cpu.Usage.PerCpu[i] = s.CpuStats.CpuUsage.PercpuUsage[i]
	}

}

func getNumberOnlineCPUs() (uint32, error) {
	var availableCPUs unix.CPUSet
	if err := unix.SchedGetaffinity(0, &availableCPUs); err != nil {
		return 0, err
	}
	return uint32(availableCPUs.Count()), nil
}

func setDiskIoStats(s *cgroups.Stats, ret *info.ContainerStats) {
	ret.DiskIo.IoServiceBytes = DiskStatsCopy(s.BlkioStats.IoServiceBytesRecursive)
	ret.DiskIo.IoServiced = DiskStatsCopy(s.BlkioStats.IoServicedRecursive)
	ret.DiskIo.IoQueued = DiskStatsCopy(s.BlkioStats.IoQueuedRecursive)
	ret.DiskIo.Sectors = DiskStatsCopy(s.BlkioStats.SectorsRecursive)
	ret.DiskIo.IoServiceTime = DiskStatsCopy(s.BlkioStats.IoServiceTimeRecursive)
	ret.DiskIo.IoWaitTime = DiskStatsCopy(s.BlkioStats.IoWaitTimeRecursive)
	ret.DiskIo.IoMerged = DiskStatsCopy(s.BlkioStats.IoMergedRecursive)
	ret.DiskIo.IoTime = DiskStatsCopy(s.BlkioStats.IoTimeRecursive)
}

func setMemoryStats(s *cgroups.Stats, ret *info.ContainerStats) {
	ret.Memory.Usage = s.MemoryStats.Usage.Usage
	ret.Memory.MaxUsage = s.MemoryStats.Usage.MaxUsage
	ret.Memory.Failcnt = s.MemoryStats.Usage.Failcnt

	if s.MemoryStats.UseHierarchy {
		ret.Memory.Cache = s.MemoryStats.Stats["total_cache"]
		ret.Memory.RSS = s.MemoryStats.Stats["total_rss"]
		ret.Memory.Swap = s.MemoryStats.Stats["total_swap"]
		ret.Memory.MappedFile = s.MemoryStats.Stats["total_mapped_file"]
	} else {
		ret.Memory.Cache = s.MemoryStats.Stats["cache"]
		ret.Memory.RSS = s.MemoryStats.Stats["rss"]
		ret.Memory.Swap = s.MemoryStats.Stats["swap"]
		ret.Memory.MappedFile = s.MemoryStats.Stats["mapped_file"]
	}
	if v, ok := s.MemoryStats.Stats["pgfault"]; ok {
		ret.Memory.ContainerData.Pgfault = v
		ret.Memory.HierarchicalData.Pgfault = v
	}
	if v, ok := s.MemoryStats.Stats["pgmajfault"]; ok {
		ret.Memory.ContainerData.Pgmajfault = v
		ret.Memory.HierarchicalData.Pgmajfault = v
	}

	workingSet := ret.Memory.Usage
	if v, ok := s.MemoryStats.Stats["total_inactive_file"]; ok {
		if workingSet < v {
			workingSet = 0
		} else {
			workingSet -= v
		}
	}
	ret.Memory.WorkingSet = workingSet
}

func setHugepageStats(s *cgroups.Stats, ret *info.ContainerStats) {
	ret.Hugetlb = make(map[string]info.HugetlbStats)
	for k, v := range s.HugetlbStats {
		ret.Hugetlb[k] = info.HugetlbStats{
			Usage:    v.Usage,
			MaxUsage: v.MaxUsage,
			Failcnt:  v.Failcnt,
		}
	}
}

func setNetworkStats(libcontainerStats *libcontainer.Stats, ret *info.ContainerStats) {
	ret.Network.Interfaces = make([]info.InterfaceStats, len(libcontainerStats.Interfaces))
	for i := range libcontainerStats.Interfaces {
		ret.Network.Interfaces[i] = info.InterfaceStats{
			Name:      libcontainerStats.Interfaces[i].Name,
			RxBytes:   libcontainerStats.Interfaces[i].RxBytes,
			RxPackets: libcontainerStats.Interfaces[i].RxPackets,
			RxErrors:  libcontainerStats.Interfaces[i].RxErrors,
			RxDropped: libcontainerStats.Interfaces[i].RxDropped,
			TxBytes:   libcontainerStats.Interfaces[i].TxBytes,
			TxPackets: libcontainerStats.Interfaces[i].TxPackets,
			TxErrors:  libcontainerStats.Interfaces[i].TxErrors,
			TxDropped: libcontainerStats.Interfaces[i].TxDropped,
		}
	}

	// Add to base struct for backwards compatibility.
	if len(ret.Network.Interfaces) > 0 {
		ret.Network.InterfaceStats = ret.Network.Interfaces[0]
	}
}

// read from pids path not cpu
func setThreadsStats(s *cgroups.Stats, ret *info.ContainerStats) {
	if s != nil {
		ret.Processes.ThreadsCurrent = s.PidsStats.Current
		ret.Processes.ThreadsMax = s.PidsStats.Limit
	}

}

func newContainerStats(libcontainerStats *libcontainer.Stats, includedMetrics container.MetricSet) *info.ContainerStats {
	ret := &info.ContainerStats{
		Timestamp: time.Now(),
	}

	if s := libcontainerStats.CgroupStats; s != nil {
		setCpuStats(s, ret, includedMetrics.Has(container.PerCpuUsageMetrics))
		if includedMetrics.Has(container.DiskIOMetrics) {
			setDiskIoStats(s, ret)
		}
		setMemoryStats(s, ret)
		if includedMetrics.Has(container.HugetlbUsageMetrics) {
			setHugepageStats(s, ret)
		}
	}
	if len(libcontainerStats.Interfaces) > 0 {
		setNetworkStats(libcontainerStats, ret)
	}
	return ret
}


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>
AlreadyLinkedViewException (Jena Framework)
</TITLE>

<META NAME="date" CONTENT="2010-06-01">

<LINK REL ="stylesheet" TYPE="text/css" HREF="../../../../../stylesheet.css" TITLE="Style">

      Overview
      Package
    Class
      Use
      Tree
      Deprecated
      Index
      Help

 PREV CLASS
 NEXT CLASS

  FRAMES
 NO FRAMES

  All Classes

  SUMMARY: NESTED | FIELD | CONSTR | METHOD

DETAIL: FIELD | CONSTR | METHOD

com.hp.hpl.jena.enhanced

Class AlreadyLinkedViewException

java.lang.Object
  java.lang.Throwable
      java.lang.Exception
          java.lang.RuntimeException
              com.hp.hpl.jena.shared.JenaException
                  com.hp.hpl.jena.enhanced.AlreadyLinkedViewException

All Implemented Interfaces:
java.io.Serializable

public class AlreadyLinkedViewException
extends JenaException

Exception to throw when adding a view to a Polymorphic discovers that
    the view to add is already on a ring.

Author:

kers

See Also:
Serialized Form

Constructor Summary

AlreadyLinkedViewException(Polymorphic<?> other)

          The polymorphic other has already been linked into
        a sibling ring and hence cannot be linked into a different one.

Method Summary

Methods inherited from class java.lang.Throwable

fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString

Methods inherited from class java.lang.Object

equals, getClass, hashCode, notify, notifyAll, wait, wait, wait

Constructor Detail

AlreadyLinkedViewException

public AlreadyLinkedViewException(Polymorphic<?> other)

The polymorphic other has already been linked into
        a sibling ring and hence cannot be linked into a different one.

      Overview
      Package
    Class
      Use
      Tree
      Deprecated
      Index
      Help

 PREV CLASS
 NEXT CLASS

  FRAMES
 NO FRAMES

  All Classes

  SUMMARY: NESTED | FIELD | CONSTR | METHOD

DETAIL: FIELD | CONSTR | METHOD

Copyright © 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Hewlett-Packard Development Company, LP


text:
using Marvin.JsonPatch.Exceptions;
using System;

namespace Marvin.JsonPatch.Internal
{
    internal static class ErrorReporter
    {
        public static Action<JsonPatchError> Default = (e) => throw new JsonPatchException(e, 422);
    }
}


text:
var util = require('util')

var parse = require('./parse')
var encode = require('./encode')
var constants = require('./constants')

module.exports = DNSMessage

var SECTIONS = ['question', 'answer', 'authority', 'additional']

// A DNS message.  This is an easy-to-understand object representation of
// standard DNS queries and responses.
//
// Attributes:
// * id                  - a number representing the unique query ID
// * type                - "request" or "response"
// * response            - Number (server response code)
// * opcode              - "query", "iquery", "status", "unassigned", "notify", "update"
// * authoritative       - Boolean
// * truncated           - Boolean
// * recursion_desired   - Boolean
// * recursion_available - Boolean
// * authenticated       - Boolean
// * checking_disabled   - Boolean
//
// Optional attributes:
// * question (optional) - Array of the question section
// * answer (optional) - Array of the answer section
// * authority (optional) - Array of the authority section
// * additional (optional) - Array of the additional section
//
// Methods:
// * toString() - return a human-readable representation of this message
// * toJSON() - Return a JSON-friendly represenation of this message
// * toBinary() - Return a buffer of the encoded message
function DNSMessage (body) {
  var self = this

  this.id = null
  this.type                = null
  this.responseCode        = null
  this.opcode              = null
  this.authoritative       = null
  this.truncated           = null
  this.recursion_desired   = null
  this.recursion_available = null
  this.authenticated       = null
  this.checking_disabled   = null

  if(Buffer.isBuffer(body))
    this.parse(body)
  else if(typeof body != 'object')
    throw new Error('Must provide a buffer or object argument with message contents')
  else {
    Object.keys(body).forEach(function(key) { self[key] = body[key] })
    SECTIONS.forEach(function(section) {
      if(self[section])
        self[section].forEach(function(record, i) {
          self[section][i] = new DNSRecord(record)
        })
    })
  }

  // EDNS processing. For now, just remove those records.
  SECTIONS.forEach(function(section) {
    if(self[section]) {
      self[section] = self[section].filter(function(record) { return ! record.edns })
      if(self[section].length == 0)
        delete self[section]
    }
  })
}

DNSMessage.prototype.parse = function(body) {
  var self = this

  self.id = parse.id(body)

  var qr = parse.qr(body)
  self.type = (qr == 0) ? 'request' : 'response'

  self.responseCode = parse.rcode(body)

  var opcode_names = ['query', 'iquery', 'status', null, 'notify', 'update']
  var opcode = parse.opcode(body)
  self.opcode = opcode_names[opcode] || null

  self.authoritative       = !! parse.aa(body)
  self.truncated           = !! parse.tc(body)
  self.recursion_desired   = !! parse.rd(body)
  self.recursion_available = !! parse.ra(body)
  self.authenticated       = !! parse.ad(body)
  self.checking_disabled   = !! parse.cd(body)

  var sections_cache = parse.sections(body)

  SECTIONS.forEach(function(section) {
    var count = parse.record_count(body, section)
    if(count) {
      self[section] = []
      for(var i = 0; i < count; i++)
        self[section].push(new DNSRecord(body, section, i, sections_cache))
    }
  })
}

DNSMessage.prototype.toBinary = function() {
  // The encoder is picky, so make sure it gets a valid message.
  var msg = JSON.parse(JSON.stringify(this))

  SECTIONS.forEach(function(section) {
    if(section == 'question')
      return

    msg[section] = msg[section] || []
    msg[section].forEach(function(record) {
      if(record.class != 'IN')
        return

      // Make sure records promising data have data.
      if(record.class == 'IN' && record.type == 'A')
        record.data = record.data || '0.0.0.0'

      // Convert SOA email addresses back to the dotted notation.
      if(record.class == 'IN' && record.type == 'SOA')
        record.data.rname = record.data.rname.replace(/@/g, '.')

      // Normalize TXT records.
      if(record.type == 'TXT' && typeof record.data == 'string')
        record.data = [record.data]
    })
  })

  var state = new encode.State
  state.message(msg)
  return state.toBinary()
}

DNSMessage.prototype.toString = function() {
  var self = this

  var info = [ util.format('ID                 : %d', self.id)
             , util.format("Type               : %s", self.type)
             , util.format("Opcode             : %s", self.opcode)
             , util.format("Authoritative      : %s", self.authoritative)
             , util.format("Truncated          : %s", self.truncated)
             , util.format("Recursion Desired  : %s", self.recursion_desired)
             , util.format("Recursion Available: %s", self.recursion_available)
             , util.format("Response Code      : %d", self.responseCode)
             ]

  SECTIONS.forEach(function(section) {
    if(self[section]) {
      info.push(util.format(';; %s SECTION:', section.toUpperCase()))
      self[section].forEach(function(record) {
        info.push(record.toString())
      })
    }
  })

  return info.join('\n')
}

// An individual record from a DNS message
//
// Attributes:
// * name  - Host name
// * type  - Query type ('A', 'NS', 'CNAME', etc. or 'Unknown')
// * class - Network class ('IN', 'None' 'Unknown')
// * ttl   - Time to live for the data in the record
// * data  - The record data value, or null if not applicable
function DNSRecord (body, section_name, record_num, sections_cache) {
  var self = this

  this.name = null
  this.type = null
  this.class = null

  // Leave these undefined for more consice and clear JSON serialization.
  //this.ttl  = null
  //this.data = null

  if(Buffer.isBuffer(body))
    this.parse(body, section_name, record_num, sections_cache || body)
  else if(typeof body != 'object')
    throw new Error('Must provide a buffer or object argument with message contents')
  else
    Object.keys(body).forEach(function(key) { self[key] = body[key] })
}

DNSRecord.prototype.parse = function(body, section_name, record_num, sections) {
  var self = this

  self.name = parse.record_name(sections, section_name, record_num)

  var type = parse.record_type(sections, section_name, record_num)
  self.type = constants.type_to_label(type)
  if(! self.type)
    throw new Error('Record '+record_num+' in section "'+section_name+'" has unknown type: ' + type)

  if(section_name != 'additional' || self.type != 'OPT' || self.name != '') {
    // Normal record
    var clas = parse.record_class(sections, section_name, record_num)
    self.class = constants.class_to_label(clas)
    if(! self.class)
      throw new Error('Record '+record_num+' in section "'+section_name+'" has unknown class: ' + type)

    if(section_name == 'question')
      return
    else
      self.ttl  = parse.record_ttl(sections, section_name, record_num)
  } else {
    // EDNS record
    self.edns = true
    delete self.name
    delete self.class
    //self.edns = parse.record_edns(sections, section_name, record_num)
  }

  var rdata = parse.record_data(sections, section_name, record_num)
  switch (self.kind()) {
    case 'IN A':
      if(rdata.length != 4)
        throw new Error('Bad IN A data: ' + JSON.stringify(self))
      self.data = inet_ntoa(rdata)
      break
    case 'IN AAAA':
      if(rdata.length != 16)
        throw new Error('Bad IN AAAA data: ' + JSON.stringify(self))
      self.data = inet_ntoa6(rdata)
      break
    case 'IN NS':
    case 'IN CNAME':
    case 'IN PTR':
      self.data = parse.uncompress(body, rdata)
      break
    case 'IN TXT':
      self.data = parse.txt(body, rdata)
      if(self.data.length === 0)
        self.data = ''
      else if(self.data.length === 1)
        self.data = self.data[0]
      break
    case 'IN MX':
      self.data = parse.mx(body, rdata)
      break
    case 'IN SRV':
      self.data = parse.srv(body, rdata)
      break
    case 'IN SOA':
      self.data = parse.soa(body, rdata)
      self.data.rname = self.data.rname.replace(/\./, '@')
      break
    case 'IN DS':
      self.data = { 'key_tag'    : rdata[0] << 8 | rdata[1]
                  , 'algorithm'  : rdata[2]
                  , 'digest_type': rdata[3]
                  , 'digest'     : rdata.slice(4).toJSON() // Convert to a list of numbers.
                  }
      break
    case 'NONE A':
      self.data = []
      break
    case 'EDNS':
      self.data = rdata
      break
    default:
      throw new Error('Unknown record '+self.kind()+': ' + JSON.stringify(self))
  }
}

DNSRecord.prototype.kind = function() {
  return this.edns
          ? 'EDNS'
          : this.class + ' ' + this.type
}

DNSRecord.prototype.toString = function() {
  var self = this
  return [ width(23, self.name)
         , width( 7, self.ttl || '')
         , width( 7, self.class)
         , width( 7, self.type)
         , self.type == 'MX' && self.data
            ? (width(3, self.data[0]) + ' ' + self.data[1])
           : Buffer.isBuffer(self.data)
            ? self.data.toString('hex')
            : self.data || ''
         ].join(' ')
}

//
// Utilities
//

function width(str_len, str) {
  str = '' + str
  do {
    var needed = str_len - str.length
    if(needed > 0)
      str = ' ' + str
  } while(needed > 0)

  return str
}

function inet_ntoa(buf) {
  return buf[0] + '.' + buf[1] + '.' + buf[2] + '.' + buf[3]
}

function inet_ntoa6(buf) {
  var result = []
  for(var i = 0; i < 16; i += 2)
    result.push(buf.slice(i, i+2).toString('hex'))
  return result.join(':')
}


text:
@implementation QBSubCategory

@synthesize icon = icon_;
@synthesize name = name_;
@synthesize id = id_;
@synthesize screenAdress = screenAdress_;

@end


text:
<?php
/**
 * Created by JetBrains PhpStorm.
 * User: bikashrai
 * Date: 7/15/13
 * Time: 10:29 AM
 * To change this template use File | Settings | File Templates.
 */
namespace Album\Model;

use Zend\Db\TableGateway\TableGateway;

class AlbumTable
{
    protected $tableGateway;

    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }

    public function fetchAll()
    {
        $resultSet = $this->tableGateway->select();
        return $resultSet;
    }

    public function getAlbum($id)
    {
        $id  = (int) $id;
        $rowset = $this->tableGateway->select(array('id' => $id));
        $row = $rowset->current();
        if (!$row) {
            throw new \Exception("Could not find row $id");
        }
        return $row;
    }

    public function saveAlbum(Album $album)
    {
        $data = array(
            'artist' => $album->artist,
            'title'  => $album->title,
        );

        $id = (int)$album->id;
        if ($id == 0) {
            $this->tableGateway->insert($data);
            $id = $this->tableGateway->getLastInsertValue(); //Add this line
        } else {
            if ($this->getAlbum($id)) {
                $this->tableGateway->update($data, array('id' => $id));
            } else {
                throw new \Exception('Form id does not exist');
            }
        }

        return $id; // Add Return
    }

    public function deleteAlbum($id)
    {
        $this->tableGateway->delete(array('id' => $id));
    }
}


text:
package com.dumbster.pop.action;

import com.dumbster.pop.POPState;
import com.dumbster.pop.Response;
import com.dumbster.smtp.MailStore;

/**
 * If this were a real POP3 server, this command would undelete any
 * messages marked for deletion. That's way too complicated. We're
 * just gonna lie and say, sure, they're undeleted.
 */
public class Reset implements Action {
    @Override
    public String getCommand() {
        return "RSET";
    }

    @Override
    public Response response(POPState popState, MailStore mailStore) {
        return new Response(Response.OK, "Dumbster doesn't really undelete", POPState.TRANSACTION);
    }
}


text:
import React from 'react';
import bind from 'react-autobind';
import Game from 'Game/index.js'

class WalkerSprite extends React.Component {
  constructor(props) {
    super(props);
    bind(this);
    this.walkerSprite = this.props.sprite;
    this.initializeSprite();
  }

  componentDidMount() {
    Game.stage.addChild(this.walkerSprite);
  }

  initializeSprite() {
    this.walkerSprite.x = this.props.position.x;
    this.walkerSprite.y = this.props.position.y;
    this.walkerSprite.anchor.set(0.5, 1);
    this.walkerSprite.animationSpeed = 0.1;
    this.walkerSprite.scale.x = 3;
    this.walkerSprite.scale.y = 3;
    this.walkerSprite.class = 'walker';

    const randomFrame = Math.floor(Math.random() * 6);
    this.walkerSprite.gotoAndPlay(randomFrame);
  }

  setPosition(x, y) {
    this.walkerSprite.x = x;
    this.walkerSprite.y = y;
  }

  updateAnimation(isWalking) {
    const currentDoorFrame = this.props.currentDoorFrame;
    const frames = this.props.frames;
    if (!isWalking && this.walkerSprite.textures === frames.walkingFrames) {
      this.walkerSprite.textures = frames.idleFrames;
      this.walkerSprite.loop = true;
    } else if (isWalking && this.walkerSprite.textures === frames.idleFrames) {
      this.walkerSprite.textures = frames.walkingFrames;
      this.walkerSprite.loop = true;
    }
    this.walkerSprite.play();
  }

  render() {
    const isWalking = this.props.isWalking;
    const x = this.props.position.x;
    const y = this.props.position.y;
    this.setPosition(x, y);
    this.updateAnimation(isWalking);
    return null;
  }
}

export default WalkerSprite;


text:
/**
 * Support and FAQ: visit Atmel Support
 */

#ifndef _data_visualizer_h_
#define _data_visualizer_h_

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>

// Symbol Definitions

#define FIELD_FORMAT_SIGNED       1       // data field is signed integer
#define FIELD_FORMAT_UNSIGNED     2       // data field is unsigned integer
#define FIELD_FORMAT_FLOAT        3       // data field is floating point

#define STREAM_NAME_LEN     25      // length of stream name (incl. null term.)
#define FIELD_UNIT_LEN      20      // length of field.units (incl. null)
#define FIELD_NAME_LEN      11      // length of field name (incl. null)

#define ADV_PKT_HEADER_1  0xFF    // first byte of header sequence
#define ADV_PKT_HEADER_2  0x5A    // second byte of header sequence
#define ADV_PKT_END       0x06    // 8-bit end-of-packet value

#define ADV_PKT_CONFIG_START      0x01    // config start packet
#define ADV_PKT_CONFIG_END        0x02    // config start packet
#define ADV_PKT_CONFIG_STREAM     0x03    // stream config packet
#define ADV_PKT_CONFIG_FIELD      0x04    // data field config packet
#define ADV_PKT_DATA              0x05    // data packet

// Data Structure Definitions

COMPILER_PACK_SET(1)        // pack structures (1-byte align all fields)

//! \brief Atmel Data Visualizer start-of-configuration packet
//
// This packet type is sent to indicate the beginning of a series of
// configuration packets.  After this packet is sent, one more
// stream configuration packet (adv_config_stream_t) will be sent for
// each logical stream to be displayed, and one or more data field
// configuration packets (adv_data_field_t) will be sent for each
// stream.
//
typedef struct {
	uint8_t         header1;        // header bytes - always 0xFF5A
	uint8_t         header2;        // header bytes - always 0xFF5A
	uint16_t        length;         // packet length (bytes)
	uint8_t         type;           // packet type
	uint8_t         num_streams;    // total number of data streams to follow
	uint8_t         crc;            // 8-bit checksum
	uint8_t         mark;           // 1-byte end-of-packet marker
} adv_config_start_t;

//! \brief Atmel Data Visualizer data stream configuration packet
//
// This packet type is used during configuration of the Atmel Data Visualizer
// to describe a logical data stream that will be used.
//
// The packet includes a name string that will be used in the
// ADV display tool to label the display of the stream.
//
// Each logical stream contains one or more data fields.  The number
// of data fields to be used for this stream is specified in this packet.
// Each data field must be separately described in subsequent
// adv_config_field_t packets, which must immediately follow the transmission
// of this stream configuration packet.
//
typedef struct {
	uint8_t         header1;        // header bytes - always 0xFF5A
	uint8_t         header2;        // header bytes - always 0xFF5A
	uint16_t        length;         // packet length (bytes)
	uint8_t         type;           // packet type
	uint8_t         stream_num;     // stream number (to match later data pkts)
	uint32_t        tick_res;       // timer tick resolution (Hz)
	uint8_t         num_fields;     // number of data fields per data packet
	char            stream_name[STREAM_NAME_LEN];
	                                // stream name (w/ null terminator)
	uint8_t         crc;            // 8-bit checksum
	uint8_t         mark;           // 1-byte end-of-packet marker
} adv_config_stream_t;

//! \brief Atmel Data Visualizer data field configuration packet
//
// This packet type is used during configuration of the Atmel Data Visualizer
// to describe a single data field that will be transmitted inside subsequent
// data packets.  The packet includes strings that will be used in the
// ADV display tool to label the display of the corresponding data element.
// The minimum and maximum values to be displayed are also provided, along
// with information about the numeric representation format.
//
typedef struct {
	uint8_t         header1;        // header bytes - always 0xFF5A
	uint8_t         header2;        // header bytes - always 0xFF5A
	uint16_t        length;         // packet length (bytes)
	uint8_t         type;           // packet type
	uint8_t         stream_num;     // stream number
	uint8_t         field_num;      // field number
	                                // stream name (w/ null terminator)
	uint8_t         field_length;   // length of data field (bytes)
	uint8_t         format;         // signed / unsigned / float
	int32_t         min;            // minimum expected value
	int32_t         max;            // maximum expected value
	char            units [FIELD_UNIT_LEN];
	                                // data units label (w/ null terminator)
	char            name [FIELD_NAME_LEN];
	                                // data field name (w/ null terminator)
	uint8_t         crc;            // 8-bit checksum
	uint8_t         mark;       // 1-byte end-of-packet marker
} adv_config_field_t;

//! \brief Atmel Data Visualizer end-of-configuration packet
//
// This packet type is sent to indicate the completion of a series of
// configuration packets.  After this packet is sent, the application
// may begin transmitting regular stream data packets for display.
//
typedef struct
	{
	uint8_t         header1;        // header bytes - always 0xFF5A
	uint8_t         header2;        // header bytes - always 0xFF5A
	uint16_t        length;         // packet length (bytes)
	uint8_t         type;           // packet type
	uint8_t         num_streams;    // total number of data streams
	uint8_t         crc;            // 8-bit checksum
	uint8_t         mark;           // 1-byte end-of-packet marker
} adv_config_end_t;

/*! \brief Atmel Data Visualizer data packet components
 *
 * A complete data packet consists of an adv_data_start_t structure, followed
 * by one or more adv_data_field_t structures containing the actual data
 * values, followed by an adv_data_end_t structure.
 */

typedef struct {
	uint8_t         header1;        // header bytes - always 0xFF5A
	uint8_t         header2;        // header bytes - always 0xFF5A
	uint16_t        length;         // packet length (bytes)
	uint8_t         type;           // packet type
	uint8_t         stream_num;     // stream number (to match later data pkts)
	uint32_t        time_stamp;     // time stamp (tick count)
} adv_data_start_t;

typedef struct {
	int32_t         value;          // data field value
} adv_data_field_t;

typedef struct {
	uint8_t         crc;            // 8-bit checksum
	uint8_t         mark;           // 1-byte end-of-packet marker
} adv_data_end_t;

// @}

COMPILER_PACK_RESET()              // reset to default structure packing

/*! \brief Write Atmel Data Visualizer data buffer via USB or serial port.
 *
 * This application uses a serial or USB connection to transmit sensor data.
 * This routine takes the specified input buffer and writes each byte to
 * the appropriate output device.
 *
 * \param   buffer    The address of a buffer containing the data to transmit
 * \param   num_bytes The number of bytes to transmit
 *
 * \return  Nothing.
 */
void adv_write_buf(uint8_t * buf, int num_bytes);

/*! \brief  Send Atmel Data Visualizer data packet with 1 data field
 *
 * This routine constructs a data visualizer data packet with one data value
 * field and then transmits it.  All multi-byte transmitted values
 * use little endian byte order.
 *
 * \param   stream_num  The ID number of the visualizer data stream
 * \param   timestamp   A 32-bit timestamp value, in microseconds
 * \param   value       The data value to include in the transmitted packet
 *
 * \return  Nothing.
 */
void adv_data_send_1(uint8_t stream_num, uint32_t timestamp, int32_t value);

/*! \brief  Send Atmel Data Visualizer data packet w/ 3 data fields
 *
 * This routine constructs a data visualizer data packet with three data
 * value fields and then transmits it.  All multi-byte transmitted values
 * use little endian byte order.
 *
 * \param   stream_num  The ID number of the visualizer data stream
 * \param   timestamp   A 32-bit timestamp value, in microseconds
 * \param   value0      Data field 0 value
 * \param   value1      Data field 1 value
 * \param   value2      Data field 2 value
 *
 * \return  Nothing.
 */
void adv_data_send_3(uint8_t stream_num, uint32_t timestamp, int32_t value0,
		int32_t value1, int32_t value2);

//! @
#endif


text:
from __future__ import print_function

import bisect

from rdkit import DataStructs
from rdkit.DataStructs.TopNContainer import TopNContainer

class GenericPicker(object):
  _picks = None

  def MakePicks(self, force=False):
    raise NotImplementedError("GenericPicker is a virtual base class")

  def __len__(self):
    if self._picks is None:
      self.MakePicks()
    return len(self._picks)

  def __getitem__(self, which):
    if self._picks is None:
      self.MakePicks()
    return self._picks[which]

class TopNOverallPicker(GenericPicker):
  """  A class for picking the top N overall best matches across a library

  Connect to a database and build molecules:
  >>> from rdkit import Chem
  >>> from rdkit import RDConfig
  >>> import os.path
  >>> from rdkit.Dbase.DbConnection import DbConnect
  >>> dbName = RDConfig.RDTestDatabase
  >>> conn = DbConnect(dbName,'simple_mols1')
  >>> [x.upper() for x in conn.GetColumnNames()]
  ['SMILES', 'ID']
  >>> mols = []
  >>> for smi,id in conn.GetData():
  ...   mol = Chem.MolFromSmiles(str(smi))
  ...   mol.SetProp('_Name',str(id))
  ...   mols.append(mol)
  >>> len(mols)
  12

  Calculate fingerprints:
  >>> probefps = []
  >>> for mol in mols:
  ...   fp = Chem.RDKFingerprint(mol)
  ...   fp._id = mol.GetProp('_Name')
  ...   probefps.append(fp)

  Start by finding the top matches for a single probe.  This ether should pull
  other ethers from the db:
  >>> mol = Chem.MolFromSmiles('COC')
  >>> probeFp = Chem.RDKFingerprint(mol)
  >>> picker = TopNOverallPicker(numToPick=2,probeFps=[probeFp],dataSet=probefps)
  >>> len(picker)
  2
  >>> fp,score = picker[0]
  >>> id = fp._id
  >>> str(id)
  'ether-1'
  >>> score
  1.0

  The results come back in order:
  >>> fp,score = picker[1]
  >>> id = fp._id
  >>> str(id)
  'ether-2'

  Now find the top matches for 2 probes.  We'll get one ether and one acid:
  >>> fps = []
  >>> fps.append(Chem.RDKFingerprint(Chem.MolFromSmiles('COC')))
  >>> fps.append(Chem.RDKFingerprint(Chem.MolFromSmiles('CC(=O)O')))
  >>> picker = TopNOverallPicker(numToPick=3,probeFps=fps,dataSet=probefps)
  >>> len(picker)
  3
  >>> fp,score = picker[0]
  >>> id = fp._id
  >>> str(id)
  'acid-1'
  >>> fp,score = picker[1]
  >>> id = fp._id
  >>> str(id)
  'ether-1'
  >>> score
  1.0
  >>> fp,score = picker[2]
  >>> id = fp._id
  >>> str(id)
  'acid-2'

  """

  def __init__(self, numToPick=10, probeFps=None, dataSet=None,
               simMetric=DataStructs.TanimotoSimilarity):
    """

      dataSet should be a sequence of BitVectors

    """
    self.numToPick = numToPick
    self.probes = probeFps
    self.data = dataSet
    self.simMetric = simMetric
    self._picks = None

  def MakePicks(self, force=False):
    if self._picks is not None and not force:
      return
    picks = TopNContainer(self.numToPick)
    for fp in self.data:
      origFp = fp
      bestScore = -1.0
      for probeFp in self.probes:
        score = DataStructs.FingerprintSimilarity(origFp, probeFp, self.simMetric)
        bestScore = max(score, bestScore)
      picks.Insert(bestScore, fp)
    self._picks = []
    for score, pt in picks:
      self._picks.append((pt, score))
    self._picks.reverse()

class SpreadPicker(GenericPicker):
  """  A class for picking the best matches across a library

  Connect to a database:
  >>> from rdkit import Chem
  >>> from rdkit import RDConfig
  >>> import os.path
  >>> from rdkit.Dbase.DbConnection import DbConnect
  >>> dbName = RDConfig.RDTestDatabase
  >>> conn = DbConnect(dbName,'simple_mols1')
  >>> [x.upper() for x in conn.GetColumnNames()]
  ['SMILES', 'ID']
  >>> mols = []
  >>> for smi,id in conn.GetData():
  ...   mol = Chem.MolFromSmiles(str(smi))
  ...   mol.SetProp('_Name',str(id))
  ...   mols.append(mol)
  >>> len(mols)
  12

  Calculate fingerprints:
  >>> probefps = []
  >>> for mol in mols:
  ...   fp = Chem.RDKFingerprint(mol)
  ...   fp._id = mol.GetProp('_Name')
  ...   probefps.append(fp)

  Start by finding the top matches for a single probe.  This ether should pull
  other ethers from the db:
  >>> mol = Chem.MolFromSmiles('COC')
  >>> probeFp = Chem.RDKFingerprint(mol)
  >>> picker = SpreadPicker(numToPick=2,probeFps=[probeFp],dataSet=probefps)
  >>> len(picker)
  2
  >>> fp,score = picker[0]
  >>> id = fp._id
  >>> str(id)
  'ether-1'
  >>> score
  1.0

  The results come back in order:
  >>> fp,score = picker[1]
  >>> id = fp._id
  >>> str(id)
  'ether-2'

  Now find the top matches for 2 probes.  We'll get one ether and one acid:
  >>> fps = []
  >>> fps.append(Chem.RDKFingerprint(Chem.MolFromSmiles('COC')))
  >>> fps.append(Chem.RDKFingerprint(Chem.MolFromSmiles('CC(=O)O')))
  >>> picker = SpreadPicker(numToPick=3,probeFps=fps,dataSet=probefps)
  >>> len(picker)
  3
  >>> fp,score = picker[0]
  >>> id = fp._id
  >>> str(id)
  'ether-1'
  >>> score
  1.0
  >>> fp,score = picker[1]
  >>> id = fp._id
  >>> str(id)
  'acid-1'
  >>> score
  1.0
  >>> fp,score = picker[2]
  >>> id = fp._id
  >>> str(id)
  'ether-2'

  """

  def __init__(self, numToPick=10, probeFps=None, dataSet=None,
               simMetric=DataStructs.TanimotoSimilarity, expectPickles=True, onlyNames=False):
    """

      dataSet should be a sequence of BitVectors or, if expectPickles
      is False, a set of strings that can be converted to bit vectors

    """
    self.numToPick = numToPick
    self.probes = probeFps
    self.data = dataSet
    self.simMetric = simMetric
    self.expectPickles = expectPickles
    self.onlyNames = onlyNames

    self._picks = None

  def MakePicks(self, force=False, silent=False):
    if self._picks is not None and not force:
      return

    # start by getting the NxM score matrix
    #  (N=num probes, M=num fps)
    nProbes = len(self.probes)
    scores = [None] * nProbes
    for i in range(nProbes):
      scores[i] = []
    j = 0
    fps = []
    for origFp in self.data:
      for i in range(nProbes):
        score = DataStructs.FingerprintSimilarity(self.probes[i], origFp, self.simMetric)
        bisect.insort(scores[i], (score, j))
        if len(scores[i]) >= self.numToPick:
          del scores[self.numToPick:]
      if self.onlyNames and hasattr(origFp, '_fieldsFromDb'):
        fps.append(origFp._fieldsFromDb[0])
      else:
        fps.append(origFp)
      j += 1
      if not silent and not j % 1000:
        print('scored %d fps' % j)

    # now go probe by probe and select the current top entry until we are finished:
    nPicked = 0
    self._picks = []
    taken = [0] * len(fps)
    while nPicked < self.numToPick:
      rowIdx = nPicked % len(scores)
      row = scores[rowIdx]
      score, idx = row.pop()
      # make sure we haven't taken this one already (from another row):
      while taken[idx] and len(row):
        score, idx = row.pop()
      if not taken[idx]:
        fp = fps[idx]
        self._picks.append((fp, score))
        taken[idx] = 1
        nPicked += 1

# ------------------------------------
#
#  doctest boilerplate
#
def _runDoctests(verbose=None):  # pragma: nocover
  import sys
  import doctest
  failed, _ = doctest.testmod(optionflags=doctest.ELLIPSIS, verbose=verbose)
  sys.exit(failed)

if __name__ == '__main__':  # pragma: nocover
  _runDoctests()