text:
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _index = _interopRequireDefault(require("../../../_lib/buildFormatLongFn/index.js"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// DIN 5008: https://de.wikipedia.org/wiki/Datumsformat#DIN_5008
var dateFormats = {
  full: 'EEEE, do MMMM y',
  // Méindeg, 7. Januar 2018
  long: 'do MMMM y',
  // 7. Januar 2018
  medium: 'do MMM y',
  // 7. Jan 2018
  short: 'dd.MM.yy' // 07.01.18

};
var timeFormats = {
  full: 'HH:mm:ss zzzz',
  long: 'HH:mm:ss z',
  medium: 'HH:mm:ss',
  short: 'HH:mm'
};
var dateTimeFormats = {
  full: "{{date}} 'um' {{time}}",
  long: "{{date}} 'um' {{time}}",
  medium: '{{date}} {{time}}',
  short: '{{date}} {{time}}'
};
var formatLong = {
  date: (0, _index.default)({
    formats: dateFormats,
    defaultWidth: 'full'
  }),
  time: (0, _index.default)({
    formats: timeFormats,
    defaultWidth: 'full'
  }),
  dateTime: (0, _index.default)({
    formats: dateTimeFormats,
    defaultWidth: 'full'
  })
};
var _default = formatLong;
exports.default = _default;
module.exports = exports.default;

redpajama_set_name: RedPajamaGithub


text:
<dc:title>GARNER<unicode value="8194"> v. <unicode value="8194"> JONES</dc:title>
<dc:language>en-US</dc:language>
<dc:creator opf:file-as="NYPL" opf:role="aut">NYPL</dc:creator>
<dc:publisher>NYPL</dc:publisher>
<dc:date opf:event="publication">2015-01-15</dc:date>
<dc:rights>Copyright ©2015 by Cornell Law and NYPL</dc:rights>


text:
package org.dolphin.secret.browser;

import android.media.MediaScannerConnection;
import android.os.Environment;
import android.util.Log;

import org.apache.commons.io.FileUtils;
import org.dolphin.arch.AndroidMainScheduler;
import org.dolphin.job.Job;
import org.dolphin.job.Operator;
import org.dolphin.job.schedulers.Schedulers;
import org.dolphin.job.tuple.FourTuple;
import org.dolphin.job.tuple.TwoTuple;
import org.dolphin.lib.util.ValueUtil;
import org.dolphin.secret.BuildConfig;
import org.dolphin.secret.SecretApplication;
import org.dolphin.secret.core.DeleteFileOperator;
import org.dolphin.secret.core.DeobscureOperator;
import org.dolphin.secret.core.EncodeLeakFileOperator;
import org.dolphin.secret.core.FileInfoContentCache;
import org.dolphin.secret.core.ObscureFileInfo;
import org.dolphin.secret.core.ObscureOperator;
import org.dolphin.secret.core.TraversalFolderOperator;
import org.dolphin.secret.picker.AndroidFileInfo;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by hanyanan on 2016/1/20.
 */
public class BrowserManager {
    public static final String TAG = "BrowserManager";
    public static final Comparator<ObscureFileInfo> fileInfoComparator = new Comparator<ObscureFileInfo>() {
        @Override
        public int compare(ObscureFileInfo lhs, ObscureFileInfo rhs) {
            return lhs.encodeTime > rhs.encodeTime ? 1 : -1;
        }
    };
    public static File sRootDir = new File(Environment.getExternalStorageDirectory(), "se");
    private static BrowserManager sInstance = null;
    private final List<FileChangeListener> imageFileChangeListeners = new ArrayList<FileChangeListener>();
    private final List<FileChangeListener> videoFileChangeListeners = new ArrayList<FileChangeListener>();
    private final List<FileChangeListener> audioFileChangeListeners = new ArrayList<FileChangeListener>();
    private final List<ObscureFileInfo> imageFileList = new ArrayList<ObscureFileInfo>();
    private final List<ObscureFileInfo> videoFileList = new ArrayList<ObscureFileInfo>();
    private final List<ObscureFileInfo> audioFileList = new ArrayList<ObscureFileInfo>();
    private File rootDir;
    private Job scannerJob = null;
    private Job obscureJob = null;

    private BrowserManager() {
        this.rootDir = sRootDir;
        checkEnvironment();
    }

    public synchronized static BrowserManager getInstance() {
        if (null == sInstance) {
            sInstance = new BrowserManager();
        }

        return sInstance;
    }

    private static void onTypedFileFound(List<ObscureFileInfo> newFiles, List<ObscureFileInfo> out,
                                         List<FileChangeListener> fileChangeListeners) {
        if (null == out || null == fileChangeListeners) {
            throw new NullPointerException("");
        }

        CopyOnWriteArrayList<ObscureFileInfo> copyOnWriteArrayList = null;
        CopyOnWriteArrayList<FileChangeListener> listeners = null;
        synchronized (out) {
            if (null == newFiles || newFiles.isEmpty()) {
                return;
            }
            out.addAll(newFiles);

            copyOnWriteArrayList = new CopyOnWriteArrayList<ObscureFileInfo>(out);
            listeners = new CopyOnWriteArrayList<FileChangeListener>(fileChangeListeners);
        }

        notifyFileChanged(copyOnWriteArrayList, listeners);
    }

    private static void onTypedFileRemoved(List<ObscureFileInfo> rejectFiles, List<ObscureFileInfo> out,
                                           List<FileChangeListener> fileChangeListeners) {
        if (null == out || null == fileChangeListeners) {
            throw new NullPointerException("");
        }

        CopyOnWriteArrayList<ObscureFileInfo> copyOnWriteArrayList = null;
        CopyOnWriteArrayList<FileChangeListener> listeners = null;
        synchronized (out) {
            if (null == rejectFiles || rejectFiles.isEmpty()) {
                return;
            }
            out.removeAll(rejectFiles);
            copyOnWriteArrayList = new CopyOnWriteArrayList<ObscureFileInfo>(out);
            listeners = new CopyOnWriteArrayList<FileChangeListener>(fileChangeListeners);
        }

        notifyFileChanged(copyOnWriteArrayList, listeners);
    }

    private static void notifyFileChanged(List<ObscureFileInfo> files, List<FileChangeListener> listeners) {
        for (FileChangeListener listener : listeners) {
            listener.onFileListChanged(files);
        }
    }

    private void checkEnvironment() {
        if (this.rootDir.exists()) {
            if (!this.rootDir.isDirectory()) {
                FileUtils.deleteQuietly(this.rootDir);
                checkEnvironment();
            }
        } else {
            if (!this.rootDir.mkdirs()) {
                // Double-check that some other thread or process hasn't made
                // the directory in the background
                if (!this.rootDir.isDirectory()) {
                    FileUtils.deleteQuietly(this.rootDir);
                    checkEnvironment();
                }
            }
        }
    }

    public File getRootDir() {
        return this.rootDir;
    }

    public synchronized void start() {
        imageFileList.clear();
        videoFileList.clear();
        audioFileList.clear();
        if (scannerJob != null && !scannerJob.isAborted()) {
            scannerJob.abort();
        }
        scannerJob = new Job(this.rootDir)
                .then(new TraversalFolderOperator())
                .workOn(Schedulers.io())
                .callbackOn(AndroidMainScheduler.INSTANCE)
                .error(new Job.Callback2() {
                    @Override
                    public void call(Throwable throwable, Object[] unexpectedResult) {
                        // TODO
                    }
                })
                .result(new Job.Callback1<FourTuple<List<ObscureFileInfo>, List<ObscureFileInfo>, List<ObscureFileInfo>, List<String>>>() {
                    @Override
                    public void call(FourTuple<List<ObscureFileInfo>, List<ObscureFileInfo>, List<ObscureFileInfo>, List<String>> result) {
                        onImageFileFound(null == result ? null : result.value1);
                        onVideoFileFound(null == result ? null : result.value2);
                        onAudioFileFound(null == result ? null : result.value3);
                        onLeakFileFound(null == result ? null : result.value4);
                    }
                })
                .work();
    }

    private void onImageFileFound(List<ObscureFileInfo> files) {
        onTypedFileFound(files, this.imageFileList, this.imageFileChangeListeners);
    }

    private void onVideoFileFound(List<ObscureFileInfo> files) {
        onTypedFileFound(files, this.videoFileList, this.videoFileChangeListeners);
    }

    private void onAudioFileFound(List<ObscureFileInfo> files) {
        onTypedFileFound(files, this.audioFileList, this.audioFileChangeListeners);
    }

    private void onImageFileRemoved(List<ObscureFileInfo> files) {
        onTypedFileRemoved(files, this.imageFileList, this.imageFileChangeListeners);
    }

    private void onVideoFileRemoved(List<ObscureFileInfo> files) {
        onTypedFileRemoved(files, this.videoFileList, this.videoFileChangeListeners);
    }

    private void onAudioFileRemoved(List<ObscureFileInfo> files) {
        onTypedFileRemoved(files, this.audioFileList, this.audioFileChangeListeners);
    }

    private void onLeakFileFound(List<String> leakedFileList) {
        if (null == leakedFileList || leakedFileList.isEmpty()) {
            return;
        }
        if (BuildConfig.DEBUG) {
            StringBuilder sb = new StringBuilder();
            for (String fileName : leakedFileList) {
                sb.append(fileName).append(" ");
            }
            Log.d(TAG, "Found leak file[" + sb.toString() + "]");
        }

        if (null != obscureJob) {
            obscureJob.abort();
            obscureJob = null;
        }
        obscureJob = new Job(leakedFileList);
        obscureJob.then(new EncodeLeakFileOperator(rootDir))
                .workOn(Schedulers.io())
                .callbackOn(AndroidMainScheduler.INSTANCE)
                .error(new Job.Callback2() {
                    @Override
                    public void call(Throwable throwable, Object[] unexpectedResult) {
                        // TODO
                    }
                })
                .result(new Job.Callback1<List<TwoTuple<ObscureFileInfo, FileInfoContentCache>>>() {
                    @Override
                    public void call(List<TwoTuple<ObscureFileInfo, FileInfoContentCache>> result) {
                        if (null == result || result.isEmpty()) {
                            return;
                        }
                        List<ObscureFileInfo> images = new ArrayList<ObscureFileInfo>();
                        List<ObscureFileInfo> video = new ArrayList<ObscureFileInfo>();
                        List<ObscureFileInfo> audio = new ArrayList<ObscureFileInfo>();

                        for (TwoTuple<ObscureFileInfo, FileInfoContentCache> tuple : result) {
                            CacheManager.getInstance().putCache(tuple.value1, tuple.value2);
                            if (tuple.value1.isPhotoType()) {
                                images.add(tuple.value1);
                            } else if (tuple.value1.isVideoType()) {
                                video.add(tuple.value1);
                            } else if (tuple.value1.isAudioType()) {
                                audio.add(tuple.value1);
                            }
                        }
                        onImageFileFound(images);
                        onVideoFileFound(video);
                        onAudioFileFound(audio);
                    }
                })
                .work();
    }

    void onObscureFileFound(ObscureFileInfo fileInfo) {
        if (null == fileInfo) return;
        List<ObscureFileInfo> files = new ArrayList<>();
        files.add(fileInfo);
        if (fileInfo.isPhotoType()) {
            onImageFileFound(files);
        } else if (fileInfo.isVideoType()) {
            onVideoFileFound(files);
        } else if (fileInfo.isAudioType()) {
            onAudioFileFound(files);
        }
    }

    void onFileRemoved(Iterable<ObscureFileInfo> fileInfos) {
        Iterator<ObscureFileInfo> infoIterator = null;
        if (null == fileInfos || null == (infoIterator = fileInfos.iterator())) {
            return;
        }
        ArrayList<ObscureFileInfo> images = new ArrayList<ObscureFileInfo>();
        ArrayList<ObscureFileInfo> audios = new ArrayList<ObscureFileInfo>();
        ArrayList<ObscureFileInfo> videos = new ArrayList<ObscureFileInfo>();
        while (infoIterator.hasNext()) {
            ObscureFileInfo fileInfo = infoIterator.next();
            if (fileInfo.isVideoType()) {
                videos.add(fileInfo);
            } else if (fileInfo.isPhotoType()) {
                images.add(fileInfo);
            } else if (fileInfo.isAudioType()) {
                audios.add(fileInfo);
            }
        }
        onImageFileRemoved(images);
        onVideoFileRemoved(videos);
        onAudioFileFound(audios);
    }

    /**
     * 导入新的文件,需要扫描整个目录完成之后才能继续进行, 该文件必须是未加密的文件
     * <br>
     * 操作步骤:
     * 1. 如果在扫描中,则会等待等待扫描结束再添加
     * 2. 删除已经存在的相同文件,则删除原有的
     * 3. 加密文件,添加到仓库中
     *
     * @param fileName
     */
    public void obscureFile(String fileName) {
        new Job(new File(this.rootDir, fileName))
                .then(new ObscureOperator())
                .workOn(Schedulers.io())
                .callbackOn(AndroidMainScheduler.INSTANCE)
                .error(new Job.Callback2() {
                    @Override
                    public void call(Throwable throwable, Object[] unexpectedResult) {
                        // TODO
                    }
                })
                .result(new Job.Callback1<TwoTuple<ObscureFileInfo, FileInfoContentCache>>() {
                    @Override
                    public void call(TwoTuple<ObscureFileInfo, FileInfoContentCache> result) {
                        CacheManager.getInstance().putCache(result.value1, result.value2);
                        onObscureFileFound(result.value1);
                    }
                })
                .work();
    }

    public synchronized void deleteFiles(Collection<ObscureFileInfo> fileInfos) {
        if (null == fileInfos || fileInfos.isEmpty()) {
            return;
        }

        for (ObscureFileInfo fileInfo : fileInfos) {
            new Job(fileInfo)
                    .then(new DeleteFileOperator(rootDir))
                    .workOn(Schedulers.computation())
                    .work();
            CacheManager.getInstance().remove(fileInfo);
            if (fileInfo.isPhotoType()) {
                this.imageFileList.remove(fileInfo);

            }
            if (fileInfo.isAudioType()) {
                this.audioFileList.remove(fileInfo);

            }
            if (fileInfo.isVideoType()) {
                this.videoFileList.remove(fileInfo);
            }
        }

        notifyFileChanged(this.imageFileList, this.imageFileChangeListeners);
        notifyFileChanged(this.audioFileList, this.audioFileChangeListeners);
        notifyFileChanged(this.videoFileList, this.videoFileChangeListeners);
    }

    public void importFiles(final List<AndroidFileInfo> fileList, final ImportFileListener callback) {
        for (final AndroidFileInfo fileEntry : fileList) {
            final File originalFile = new File(fileEntry.path);
            final File destFile = new File(getInstance().getRootDir(), originalFile.getName());
            new Job(fileEntry)
                    .workOn(Schedulers.io())
                    .callbackOn(AndroidMainScheduler.INSTANCE)
                    .then(new Operator<AndroidFileInfo, File>() {
                        @Override
                        public File operate(AndroidFileInfo input) throws Throwable {
                            FileUtils.moveFile(originalFile, destFile);
                            MediaScannerConnection.scanFile(SecretApplication.getInstance(),
                                    new String[]{originalFile.getAbsolutePath()}, null, null);
                            return destFile;
                        }
                    })
                    .then(ObscureOperator.INSTANCE)
                    .result(new Job.Callback1<TwoTuple<ObscureFileInfo, FileInfoContentCache>>() {
                        @Override
                        public void call(TwoTuple<ObscureFileInfo, FileInfoContentCache> result) {
                            if (null == result || null == result.value1) {
                                // do nothing
                            } else {
                                CacheManager.getInstance().putCache(result.value1, result.value2);
                                callback.onImportSuccess(fileEntry.path, result.value1);
                            }
                        }
                    })
                    .error(new Job.Callback2() {
                        @Override
                        public void call(Throwable throwable, Object[] unexpectedResult) {
                            callback.onImportFailed(fileEntry.path, throwable);
                        }
                    })
                    .work();
        }
    }

    public void exportFiles(final Collection<ObscureFileInfo> fileList) {
        if (ValueUtil.isEmpty(fileList)) {
            return;
        }
        onFileRemoved(fileList);

        for (final ObscureFileInfo fileInfo : fileList) {
            new Job(fileInfo)
                    .workOn(Schedulers.io())
                    .then(DeobscureOperator.DEFAULT)
                    .then(new Operator<File, Void>() {
                        @Override
                        public Void operate(File input) throws Throwable {
                            File exportFile = getTypedFileDirector(fileInfo);
                            FileUtils.moveFile(input, new File(exportFile, input.getName()));
                            MediaScannerConnection.scanFile(SecretApplication.getInstance(),
                                    new String[]{exportFile.getAbsolutePath()}, null, null);
                            return null;
                        }
                    })
                    .work();
        }
    }

    public List<ObscureFileInfo> getImageFileList() {
        return new CopyOnWriteArrayList<ObscureFileInfo>(imageFileList);
    }

    public List<ObscureFileInfo> getVideoFileList() {
        return new CopyOnWriteArrayList<ObscureFileInfo>(videoFileList);
    }

    public List<ObscureFileInfo> getAudioFileList() {
        return new CopyOnWriteArrayList<ObscureFileInfo>(audioFileList);
    }

    public synchronized void addImageFileChangeListener(FileChangeListener listener) {
        if (!imageFileChangeListeners.contains(listener)) {
            imageFileChangeListeners.add(listener);
        }
    }

    public synchronized void removeImageFileChangeListener(FileChangeListener listener) {
        if (imageFileChangeListeners.contains(listener)) {
            imageFileChangeListeners.remove(listener);
        }
    }

    public synchronized void addVideoFileChangeListener(FileChangeListener listener) {
        if (!videoFileChangeListeners.contains(listener)) {
            videoFileChangeListeners.add(listener);
        }
    }

    public synchronized void removeVideoFileChangeListener(FileChangeListener listener) {
        if (videoFileChangeListeners.contains(listener)) {
            videoFileChangeListeners.remove(listener);
        }
    }

    public synchronized void addAudioFileChangeListener(FileChangeListener listener) {
        if (!audioFileChangeListeners.contains(listener)) {
            audioFileChangeListeners.add(listener);
        }
    }

    public synchronized void removeAudioFileChangeListener(FileChangeListener listener) {
        if (audioFileChangeListeners.contains(listener)) {
            audioFileChangeListeners.remove(listener);
        }
    }

    public File getTypedFileDirector(ObscureFileInfo fileInfo) throws IOException {
        if (null == fileInfo) {
            return null;
        }

        File file = null;

        if (fileInfo.isPhotoType()) {
            file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        } else if (fileInfo.isVideoType()) {
            file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
        } else if (fileInfo.isAudioType()) {
            file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        } else {
            file = Environment.getExternalStorageDirectory();
        }

        if (!file.exists()) {
            // create director
            if (file.mkdir()) {
                return file;
            } else {
                // create failed
                throw new IOException("Create director " + file.getAbsolutePath() + " failed!");
            }
        }

        if (!file.isDirectory()) {
            throw new IOException("File " + file.getAbsolutePath() + " is not a director!");
        }

        return file;
    }

    public interface FileChangeListener {
        void onFileListChanged(List<ObscureFileInfo> files);
    }
}


text:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_creatqun2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.charmer.moving.friendchat.Creatqun2"
    android:background="#d0d0d0"
    >
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/qunname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="群名称:"
            android:textSize="30dp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="40dp"
            />
        <EditText
            android:id="@+id/edqun"
            android:layout_width="250dp"
            android:layout_height="30dp"
            android:layout_toRightOf="@id/qunname"
            android:layout_alignBottom="@id/qunname"
            android:textSize="25dp"
            android:background="@drawable/searchshape"
            />
    </RelativeLayout>
    <ImageView
        android:id="@+id/jiatu"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_centerInParent="true"
        android:src="@drawable/jiatu"
        />
       <TextView
           android:id="@+id/tiao"
           android:layout_width="match_parent"
           android:layout_height="30dp"
           android:layout_below="@id/jiatu"
           android:text="添加一张群图片"
           android:gravity="center"
           android:textSize="25dp"
           />
        <ImageView
            android:layout_marginTop="30dp"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_below="@id/tiao"
            android:id="@+id/chuangqun"
            />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/chuangqun"
        android:layout_alignBottom="@id/chuangqun"
        android:layout_marginLeft="30dp"
        android:text="取消创建"
        android:textSize="30dp"
        android:id="@+id/tv_cancelqun"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_createqun1"
        android:layout_toRightOf="@id/tv_cancelqun"
        android:layout_alignBottom="@id/tv_cancelqun"
        android:text="选择完成"
        android:textSize="30dp"
        android:layout_marginLeft="20dp"
        />
    <ImageView
        android:id="@+id/success"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_toRightOf="@id/tv_createqun1"
        android:layout_alignBottom="@id/tv_createqun1"
        android:layout_marginBottom="20dp"
        android:src="@drawable/confirm"
        android:layout_marginLeft="-10dp"
        android:visibility="gone"
        />
    <RelativeLayout
        android:layout_marginTop="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_createqun1"
        >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_createqun2"
        android:layout_centerHorizontal="true"
        android:text="完成创建"
        android:textSize="30dp"
        android:layout_marginLeft="20dp"
        android:enabled="false"
        />
    </RelativeLayout>
</RelativeLayout>


text:
package org.apache.hadoop.yarn.server.router.webapp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.ws.rs.core.Response;

import org.apache.hadoop.yarn.api.records.ApplicationId;
import org.apache.hadoop.yarn.conf.YarnConfiguration;
import org.apache.hadoop.yarn.exceptions.YarnException;
import org.apache.hadoop.yarn.server.federation.policies.FederationPolicyUtils;
import org.apache.hadoop.yarn.server.federation.policies.manager.UniformBroadcastPolicyManager;
import org.apache.hadoop.yarn.server.federation.store.impl.MemoryFederationStateStore;
import org.apache.hadoop.yarn.server.federation.store.records.GetApplicationHomeSubClusterRequest;
import org.apache.hadoop.yarn.server.federation.store.records.SubClusterId;
import org.apache.hadoop.yarn.server.federation.utils.FederationStateStoreFacade;
import org.apache.hadoop.yarn.server.federation.utils.FederationStateStoreTestUtil;
import org.apache.hadoop.yarn.server.resourcemanager.webapp.dao.ApplicationSubmissionContextInfo;
import org.apache.hadoop.yarn.server.resourcemanager.webapp.dao.AppsInfo;
import org.apache.hadoop.yarn.server.resourcemanager.webapp.dao.ClusterMetricsInfo;
import org.apache.hadoop.yarn.server.resourcemanager.webapp.dao.NewApplication;
import org.apache.hadoop.yarn.server.resourcemanager.webapp.dao.NodeInfo;
import org.apache.hadoop.yarn.server.resourcemanager.webapp.dao.NodesInfo;
import org.apache.hadoop.yarn.server.router.clientrm.PassThroughClientRequestInterceptor;
import org.apache.hadoop.yarn.server.router.clientrm.TestableFederationClientInterceptor;
import org.apache.hadoop.yarn.webapp.NotFoundException;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Extends the {@code BaseRouterWebServicesTest} and overrides methods in order
 * to use the {@code RouterWebServices} pipeline test cases for testing the
 * {@code FederationInterceptorREST} class. The tests for
 * {@code RouterWebServices} has been written cleverly so that it can be reused
 * to validate different request interceptor chains.
 * <p>
 * It tests the case with SubClusters down and the Router logic of retries. We
 * have 1 good SubCluster and 2 bad ones for all the tests.
 */
public class TestFederationInterceptorRESTRetry
    extends BaseRouterWebServicesTest {
  private static final Logger LOG =
      LoggerFactory.getLogger(TestFederationInterceptorRESTRetry.class);
  private static final int SERVICE_UNAVAILABLE = 503;
  private static final int ACCEPTED = 202;
  private static final int OK = 200;
  // running and registered
  private static SubClusterId good;
  // registered but not running
  private static SubClusterId bad1;
  private static SubClusterId bad2;
  private static List<SubClusterId> scs = new ArrayList<SubClusterId>();
  private TestableFederationInterceptorREST interceptor;
  private MemoryFederationStateStore stateStore;
  private FederationStateStoreTestUtil stateStoreUtil;
  private String user = "test-user";

  @Override
  public void setUp() {
    super.setUpConfig();
    interceptor = new TestableFederationInterceptorREST();

    stateStore = new MemoryFederationStateStore();
    stateStore.init(this.getConf());
    FederationStateStoreFacade.getInstance().reinitialize(stateStore,
        getConf());
    stateStoreUtil = new FederationStateStoreTestUtil(stateStore);

    interceptor.setConf(this.getConf());
    interceptor.init(user);

    // Create SubClusters
    good = SubClusterId.newInstance("1");
    bad1 = SubClusterId.newInstance("2");
    bad2 = SubClusterId.newInstance("3");
    scs.add(good);
    scs.add(bad1);
    scs.add(bad2);

    // The mock RM will not start in these SubClusters, this is done to simulate
    // a SubCluster down

    interceptor.registerBadSubCluster(bad1);
    interceptor.registerBadSubCluster(bad2);
  }

  @Override
  public void tearDown() {
    interceptor.shutdown();
    super.tearDown();
  }

  private void setupCluster(List<SubClusterId> scsToRegister)
      throws YarnException {

    try {
      // Clean up the StateStore before every test
      stateStoreUtil.deregisterAllSubClusters();

      for (SubClusterId sc : scsToRegister) {
        stateStoreUtil.registerSubCluster(sc);
      }
    } catch (YarnException e) {
      LOG.error(e.getMessage());
      Assert.fail();
    }
  }

  @Override
  protected YarnConfiguration createConfiguration() {
    YarnConfiguration conf = new YarnConfiguration();
    conf.setBoolean(YarnConfiguration.FEDERATION_ENABLED, true);

    conf.set(YarnConfiguration.ROUTER_WEBAPP_DEFAULT_INTERCEPTOR_CLASS,
        MockDefaultRequestInterceptorREST.class.getName());

    String mockPassThroughInterceptorClass =
        PassThroughClientRequestInterceptor.class.getName();

    // Create a request intercepter pipeline for testing. The last one in the
    // chain is the federation intercepter that calls the mock resource manager.
    // The others in the chain will simply forward it to the next one in the
    // chain
    conf.set(YarnConfiguration.ROUTER_CLIENTRM_INTERCEPTOR_CLASS_PIPELINE,
        mockPassThroughInterceptorClass + ","
            + TestableFederationClientInterceptor.class.getName());

    conf.set(YarnConfiguration.FEDERATION_POLICY_MANAGER,
        UniformBroadcastPolicyManager.class.getName());

    // Disable StateStoreFacade cache
    conf.setInt(YarnConfiguration.FEDERATION_CACHE_TIME_TO_LIVE_SECS, 0);

    return conf;
  }

  /**
   * This test validates the correctness of GetNewApplication in case the
   * cluster is composed of only 1 bad SubCluster.
   */
  @Test
  public void testGetNewApplicationOneBadSC()
      throws YarnException, IOException, InterruptedException {

    setupCluster(Arrays.asList(bad2));

    Response response = interceptor.createNewApplication(null);
    Assert.assertEquals(SERVICE_UNAVAILABLE, response.getStatus());
    Assert.assertEquals(FederationPolicyUtils.NO_ACTIVE_SUBCLUSTER_AVAILABLE,
        response.getEntity());
  }

  /**
   * This test validates the correctness of GetNewApplication in case the
   * cluster is composed of only 2 bad SubClusters.
   */
  @Test
  public void testGetNewApplicationTwoBadSCs()
      throws YarnException, IOException, InterruptedException {
    setupCluster(Arrays.asList(bad1, bad2));

    Response response = interceptor.createNewApplication(null);
    Assert.assertEquals(SERVICE_UNAVAILABLE, response.getStatus());
    Assert.assertEquals(FederationPolicyUtils.NO_ACTIVE_SUBCLUSTER_AVAILABLE,
        response.getEntity());
  }

  /**
   * This test validates the correctness of GetNewApplication in case the
   * cluster is composed of only 1 bad SubCluster and 1 good one.
   */
  @Test
  public void testGetNewApplicationOneBadOneGood()
      throws YarnException, IOException, InterruptedException {
    System.out.println("Test getNewApplication with one bad, one good SC");
    setupCluster(Arrays.asList(good, bad2));
    Response response = interceptor.createNewApplication(null);

    Assert.assertEquals(OK, response.getStatus());

    NewApplication newApp = (NewApplication) response.getEntity();
    ApplicationId appId = ApplicationId.fromString(newApp.getApplicationId());

    Assert.assertEquals(Integer.parseInt(good.getId()),
        appId.getClusterTimestamp());
  }

  /**
   * This test validates the correctness of SubmitApplication in case the
   * cluster is composed of only 1 bad SubCluster.
   */
  @Test
  public void testSubmitApplicationOneBadSC()
      throws YarnException, IOException, InterruptedException {

    setupCluster(Arrays.asList(bad2));

    ApplicationId appId =
        ApplicationId.newInstance(System.currentTimeMillis(), 1);
    ApplicationSubmissionContextInfo context =
        new ApplicationSubmissionContextInfo();
    context.setApplicationId(appId.toString());

    Response response = interceptor.submitApplication(context, null);
    Assert.assertEquals(SERVICE_UNAVAILABLE, response.getStatus());
    Assert.assertEquals(FederationPolicyUtils.NO_ACTIVE_SUBCLUSTER_AVAILABLE,
        response.getEntity());
  }

  /**
   * This test validates the correctness of SubmitApplication in case the
   * cluster is composed of only 2 bad SubClusters.
   */
  @Test
  public void testSubmitApplicationTwoBadSCs()
      throws YarnException, IOException, InterruptedException {
    setupCluster(Arrays.asList(bad1, bad2));

    ApplicationId appId =
        ApplicationId.newInstance(System.currentTimeMillis(), 1);
    ApplicationSubmissionContextInfo context =
        new ApplicationSubmissionContextInfo();
    context.setApplicationId(appId.toString());

    Response response = interceptor.submitApplication(context, null);
    Assert.assertEquals(SERVICE_UNAVAILABLE, response.getStatus());
    Assert.assertEquals(FederationPolicyUtils.NO_ACTIVE_SUBCLUSTER_AVAILABLE,
        response.getEntity());
  }

  /**
   * This test validates the correctness of SubmitApplication in case the
   * cluster is composed of only 1 bad SubCluster and a good one.
   */
  @Test
  public void testSubmitApplicationOneBadOneGood()
      throws YarnException, IOException, InterruptedException {
    System.out.println("Test submitApplication with one bad, one good SC");
    setupCluster(Arrays.asList(good, bad2));

    ApplicationId appId =
        ApplicationId.newInstance(System.currentTimeMillis(), 1);
    ApplicationSubmissionContextInfo context =
        new ApplicationSubmissionContextInfo();
    context.setApplicationId(appId.toString());
    Response response = interceptor.submitApplication(context, null);

    Assert.assertEquals(ACCEPTED, response.getStatus());

    Assert.assertEquals(good,
        stateStore
            .getApplicationHomeSubCluster(
                GetApplicationHomeSubClusterRequest.newInstance(appId))
            .getApplicationHomeSubCluster().getHomeSubCluster());
  }

  /**
   * This test validates the correctness of GetApps in case the cluster is
   * composed of only 1 bad SubCluster.
   */
  @Test
  public void testGetAppsOneBadSC()
      throws YarnException, IOException, InterruptedException {

    setupCluster(Arrays.asList(bad2));

    AppsInfo response = interceptor.getApps(null, null, null, null, null, null,
        null, null, null, null, null, null, null, null);
    Assert.assertNull(response);
  }

  /**
   * This test validates the correctness of GetApps in case the cluster is
   * composed of only 2 bad SubClusters.
   */
  @Test
  public void testGetAppsTwoBadSCs()
      throws YarnException, IOException, InterruptedException {
    setupCluster(Arrays.asList(bad1, bad2));

    AppsInfo response = interceptor.getApps(null, null, null, null, null, null,
        null, null, null, null, null, null, null, null);
    Assert.assertNull(response);
  }

  /**
   * This test validates the correctness of GetApps in case the cluster is
   * composed of only 1 bad SubCluster and a good one.
   */
  @Test
  public void testGetAppsOneBadOneGood()
      throws YarnException, IOException, InterruptedException {
    setupCluster(Arrays.asList(good, bad2));

    AppsInfo response = interceptor.getApps(null, null, null, null, null, null,
        null, null, null, null, null, null, null, null);
    Assert.assertNotNull(response);
    Assert.assertEquals(1, response.getApps().size());
  }

  /**
   * This test validates the correctness of GetNode in case the cluster is
   * composed of only 1 bad SubCluster.
   */
  @Test
  public void testGetNodeOneBadSC()
      throws YarnException, IOException, InterruptedException {

    setupCluster(Arrays.asList(bad2));
    try {
      interceptor.getNode("testGetNodeOneBadSC");
      Assert.fail();
    } catch (NotFoundException e) {
      Assert.assertTrue(
          e.getMessage().contains("nodeId, testGetNodeOneBadSC, is not found"));
    }
  }

  /**
   * This test validates the correctness of GetNode in case the cluster is
   * composed of only 2 bad SubClusters.
   */
  @Test
  public void testGetNodeTwoBadSCs()
      throws YarnException, IOException, InterruptedException {
    setupCluster(Arrays.asList(bad1, bad2));

    try {
      interceptor.getNode("testGetNodeTwoBadSCs");
      Assert.fail();
    } catch (NotFoundException e) {
      Assert.assertTrue(e.getMessage()
          .contains("nodeId, testGetNodeTwoBadSCs, is not found"));
    }
  }

  /**
   * This test validates the correctness of GetNode in case the cluster is
   * composed of only 1 bad SubCluster and a good one.
   */
  @Test
  public void testGetNodeOneBadOneGood()
      throws YarnException, IOException, InterruptedException {
    setupCluster(Arrays.asList(good, bad2));

    NodeInfo response = interceptor.getNode(null);
    Assert.assertNotNull(response);
    // Check if the only node came from Good SubCluster
    Assert.assertEquals(good.getId(),
        Long.toString(response.getLastHealthUpdate()));
  }

  /**
   * This test validates the correctness of GetNodes in case the cluster is
   * composed of only 1 bad SubCluster.
   */
  @Test
  public void testGetNodesOneBadSC()
      throws YarnException, IOException, InterruptedException {

    setupCluster(Arrays.asList(bad2));

    NodesInfo response = interceptor.getNodes(null);
    Assert.assertNotNull(response);
    Assert.assertEquals(0, response.getNodes().size());
    // The remove duplicate operations is tested in TestRouterWebServiceUtil
  }

  /**
   * This test validates the correctness of GetNodes in case the cluster is
   * composed of only 2 bad SubClusters.
   */
  @Test
  public void testGetNodesTwoBadSCs()
      throws YarnException, IOException, InterruptedException {
    setupCluster(Arrays.asList(bad1, bad2));

    NodesInfo response = interceptor.getNodes(null);
    Assert.assertNotNull(response);
    Assert.assertEquals(0, response.getNodes().size());
    // The remove duplicate operations is tested in TestRouterWebServiceUtil
  }

  /**
   * This test validates the correctness of GetNodes in case the cluster is
   * composed of only 1 bad SubCluster and a good one.
   */
  @Test
  public void testGetNodesOneBadOneGood()
      throws YarnException, IOException, InterruptedException {
    setupCluster(Arrays.asList(good, bad2));

    NodesInfo response = interceptor.getNodes(null);
    Assert.assertNotNull(response);
    Assert.assertEquals(1, response.getNodes().size());
    // Check if the only node came from Good SubCluster
    Assert.assertEquals(good.getId(),
        Long.toString(response.getNodes().get(0).getLastHealthUpdate()));
    // The remove duplicate operations is tested in TestRouterWebServiceUtil
  }

  /**
   * This test validates the correctness of GetNodes in case the cluster is
   * composed of only 1 bad SubCluster. The excepted result would be a
   * ClusterMetricsInfo with all its values set to 0.
   */
  @Test
  public void testGetClusterMetricsOneBadSC()
      throws YarnException, IOException, InterruptedException {
    setupCluster(Arrays.asList(bad2));

    ClusterMetricsInfo response = interceptor.getClusterMetricsInfo();
    Assert.assertNotNull(response);
    // check if we got an empty metrics
    checkEmptyMetrics(response);
  }

  /**
   * This test validates the correctness of GetClusterMetrics in case the
   * cluster is composed of only 2 bad SubClusters. The excepted result would be
   * a ClusterMetricsInfo with all its values set to 0.
   */
  @Test
  public void testGetClusterMetricsTwoBadSCs()
      throws YarnException, IOException, InterruptedException {
    setupCluster(Arrays.asList(bad1, bad2));

    ClusterMetricsInfo response = interceptor.getClusterMetricsInfo();
    Assert.assertNotNull(response);
    // check if we got an empty metrics
    Assert.assertEquals(0, response.getAppsSubmitted());
  }

  /**
   * This test validates the correctness of GetClusterMetrics in case the
   * cluster is composed of only 1 bad SubCluster and a good one. The good
   * SubCluster provided a ClusterMetricsInfo with appsSubmitted set to its
   * SubClusterId. The expected result would be appSubmitted equals to its
   * SubClusterId. SubClusterId in this case is an integer.
   */
  @Test
  public void testGetClusterMetricsOneBadOneGood()
      throws YarnException, IOException, InterruptedException {
    setupCluster(Arrays.asList(good, bad2));

    ClusterMetricsInfo response = interceptor.getClusterMetricsInfo();
    Assert.assertNotNull(response);
    checkMetricsFromGoodSC(response);
    // The merge operations is tested in TestRouterWebServiceUtil
  }

  private void checkMetricsFromGoodSC(ClusterMetricsInfo response) {
    Assert.assertEquals(Integer.parseInt(good.getId()),
        response.getAppsSubmitted());
    Assert.assertEquals(Integer.parseInt(good.getId()),
        response.getAppsCompleted());
    Assert.assertEquals(Integer.parseInt(good.getId()),
        response.getAppsPending());
    Assert.assertEquals(Integer.parseInt(good.getId()),
        response.getAppsRunning());
    Assert.assertEquals(Integer.parseInt(good.getId()),
        response.getAppsFailed());
    Assert.assertEquals(Integer.parseInt(good.getId()),
        response.getAppsKilled());
  }

  private void checkEmptyMetrics(ClusterMetricsInfo response) {
    Assert.assertEquals(0, response.getAppsSubmitted());
    Assert.assertEquals(0, response.getAppsCompleted());
    Assert.assertEquals(0, response.getAppsPending());
    Assert.assertEquals(0, response.getAppsRunning());
    Assert.assertEquals(0, response.getAppsFailed());
    Assert.assertEquals(0, response.getAppsKilled());

    Assert.assertEquals(0, response.getReservedMB());
    Assert.assertEquals(0, response.getAvailableMB());
    Assert.assertEquals(0, response.getAllocatedMB());

    Assert.assertEquals(0, response.getReservedVirtualCores());
    Assert.assertEquals(0, response.getAvailableVirtualCores());
    Assert.assertEquals(0, response.getAllocatedVirtualCores());

    Assert.assertEquals(0, response.getContainersAllocated());
    Assert.assertEquals(0, response.getReservedContainers());
    Assert.assertEquals(0, response.getPendingContainers());

    Assert.assertEquals(0, response.getTotalMB());
    Assert.assertEquals(0, response.getTotalVirtualCores());
    Assert.assertEquals(0, response.getTotalNodes());
    Assert.assertEquals(0, response.getLostNodes());
    Assert.assertEquals(0, response.getUnhealthyNodes());
    Assert.assertEquals(0, response.getDecommissioningNodes());
    Assert.assertEquals(0, response.getDecommissionedNodes());
    Assert.assertEquals(0, response.getRebootedNodes());
    Assert.assertEquals(0, response.getActiveNodes());
    Assert.assertEquals(0, response.getShutdownNodes());
  }
}


text:
<?php

/**
 * PluginOAuthServerRequestTokensTable
 *
 * @package    tiDoctrineOAuthServerPlugin
 * @author     Emeric Kasbarian <emeric@tillid.fr>
 */

class PluginOAuthServerRequestTokensTable extends Doctrine_Table
{
    /**
     * Returns an instance of this class.
     *
     * @return object PluginOAuthServerRequestTokensTable
     */
    public static function getInstance()
    {
        return Doctrine_Core::getTable('PluginOAuthServerRequestTokens');
    }
}


text:
package org.juniversal.translator.csharp;

import org.eclipse.jdt.core.dom.*;

import static org.juniversal.translator.core.ASTUtil.*;

public class ClassInstanceCreationWriter extends CSharpASTNodeWriter<ClassInstanceCreation> {
    public ClassInstanceCreationWriter(CSharpSourceFileWriter cSharpASTWriters) {
        super(cSharpASTWriters);
    }

    @Override public void write(ClassInstanceCreation classInstanceCreation) {
        //TODO: Handle type arguments

        // TODO: Support inner class creation via object.new
        if (classInstanceCreation.getExpression() != null)
            throw sourceNotSupported("Inner classes not yet supported");

        if (classInstanceCreation.getAnonymousClassDeclaration() != null) {
            writeAnonymousInnerClassFunction(classInstanceCreation);
        } else {
            matchAndWrite("new");

            copySpaceAndComments();
            writeNode(classInstanceCreation.getType());

            copySpaceAndComments();
            matchAndWrite("(");

            writeCommaDelimitedNodes(classInstanceCreation.arguments());

            if (classInstanceCreation.getAnonymousClassDeclaration() != null) {
                throw sourceNotSupported("Anonymous classes aren't yet supported");
            }

            copySpaceAndComments();
            matchAndWrite(")");
        }
    }

    private void writeAnonymousInnerClassFunction(ClassInstanceCreation classInstanceCreation) {
        Type type = classInstanceCreation.getType();

        if (! isFunctionalInterfaceImplementation(getSourceFileWriter(), type))
            throw sourceNotSupported("Anonymous inner classes are only supported when they implement a functional interface (an interface with a single abstract method, no constants, and the @FunctionalInterface annotation).  Change to use a functional interface if you just want a single method/function or use a static (non-anonymous) inner class for a full class.");

        MethodDeclaration functionalMethod = (MethodDeclaration) classInstanceCreation.getAnonymousClassDeclaration().bodyDeclarations().get(0);

        write("(");
        forEach(functionalMethod.parameters(), (SingleVariableDeclaration parameter, boolean first) -> {
            if (! first)
                write(", ");
            write(parameter.getName().getIdentifier());
        });
        write(") => ");

        setPosition(functionalMethod.getBody().getStartPosition());
        writeNode(functionalMethod.getBody());

        setPositionToEndOfNode(classInstanceCreation);
    }
}


text:
namespace E04_Telephony
{
    public interface ICallable
    {
        string Call(string phoneNumber);
    }
}


text:
namespace autofill {

Suggestion::Suggestion() = default;
Suggestion::Suggestion(const Suggestion& other) = default;
Suggestion::Suggestion(Suggestion&& other) = default;

Suggestion::Suggestion(base::string16 value) : value(std::move(value)) {}

Suggestion::Suggestion(base::StringPiece value,
                       base::StringPiece label,
                       std::string icon,
                       int frontend_id)
    : frontend_id(frontend_id),
      value(base::UTF8ToUTF16(value)),
      label(base::UTF8ToUTF16(label)),
      icon(std::move(icon)) {}

Suggestion& Suggestion::operator=(const Suggestion& other) = default;
Suggestion& Suggestion::operator=(Suggestion&& other) = default;

Suggestion::~Suggestion() = default;

}  // namespace autofill


text:
using System;

namespace OfficeDevPnP.Core.Framework.Provisioning.Model
{
    /// <summary>
    /// Represents a Field XML Markup that is used to define information about a field
    /// </summary>
    public partial class FieldRef
    {
    }
}


text:
"""An executor responsible for operations on sequences."""

import abc
import asyncio

from tensorflow_federated.proto.v0 import computation_pb2 as pb
from tensorflow_federated.python.common_libs import py_typecheck
from tensorflow_federated.python.common_libs import structure
from tensorflow_federated.python.common_libs import tracing
from tensorflow_federated.python.core.impl.compiler import intrinsic_defs
from tensorflow_federated.python.core.impl.executors import executor_base
from tensorflow_federated.python.core.impl.executors import executor_value_base
from tensorflow_federated.python.core.impl.types import computation_types
from tensorflow_federated.python.core.impl.types import type_analysis
from tensorflow_federated.python.core.impl.types import type_conversions
from tensorflow_federated.python.core.impl.types import type_serialization
from tensorflow_federated.python.core.impl.types import typed_object

class _AsyncFromSyncIterator:
  """Wraps a regular sync iterator as an asynchronous one."""

  def __init__(self, sync_iter):
    self._iter = sync_iter

  async def __anext__(self):
    try:
      return next(self._iter)
    except StopIteration:
      raise StopAsyncIteration  # pylint: disable=raise-missing-from

class _SyncFromAsyncIterable:
  """Wraps an asynchronous iteble as a regular sync iterable and generator."""

  def __init__(self, async_iterable):
    self._async_iterable = async_iterable
    self._event_loop = asyncio.new_event_loop()

  def __call__(self):
    return self.__iter__()

  def __iter__(self):

    def _generate(event_loop, async_iter):
      try:
        while True:
          # TODO(b/175888145): Whiel everything works so far with the full
          # stack on top of this, there is some lignering doubt about whether
          # asyncio will break here. If we see breakage, possibly reuse the
          # pattern from thread delegating executor, with a dedicated thread.
          yield event_loop.run_until_complete(async_iter.__anext__())
      except StopAsyncIteration:
        return

    return _generate(self._event_loop, self._async_iterable.__aiter__())

class _Sequence(typed_object.TypedObject, metaclass=abc.ABCMeta):
  """An internal representation of a sequence within this executor."""

  def __init__(self, type_spec: computation_types.SequenceType):
    py_typecheck.check_type(type_spec, computation_types.SequenceType)
    self._type_signature = type_spec

  @property
  def type_signature(self):
    return self._type_signature

  @abc.abstractmethod
  async def compute(self):
    raise NotImplementedError

  @abc.abstractmethod
  def __aiter__(self):
    raise NotImplementedError

class _SequenceFromPayload(_Sequence):
  """An internal representation of a sequence created from a payload."""

  def __init__(self, payload, type_spec: computation_types.SequenceType):
    """Constructs a representation for sequence-typed `payload`.

    Args:
      payload: The Python object that contains the payload.
      type_spec: An instance of `computation_types.SequenceType`.
    """
    _Sequence.__init__(self, type_spec)
    if isinstance(payload, type_conversions.TF_DATASET_REPRESENTATION_TYPES):
      self._payload = payload
      return
    try:
      iter(payload)
      self._payload = payload
    except Exception as e:
      raise NotImplementedError(
          'Unrecognized type of payload: {}: returned {} from iter()'.format(
              py_typecheck.type_string(type(payload)), str(e))) from e

  async def compute(self):
    return self._payload

  def __aiter__(self):
    return _AsyncFromSyncIterator(iter(self._payload))

class _SequenceFromMap(_Sequence):
  """An internal representation of a sequence created from a map op."""

  def __init__(self, source: _Sequence,
               map_fn: executor_value_base.ExecutorValue,
               target_executor: executor_base.Executor,
               type_spec: computation_types.SequenceType):
    """Constructs a representation of a mapped sequence.

    Args:
      source: The source sequence.
      map_fn: A mapping function embedded in the target executor.
      target_executor: The target executor.
      type_spec: The type of the mapped sequence.
    """
    py_typecheck.check_type(source, _Sequence)
    py_typecheck.check_type(map_fn, executor_value_base.ExecutorValue)
    py_typecheck.check_type(target_executor, executor_base.Executor)
    py_typecheck.check_type(type_spec, computation_types.SequenceType)
    _Sequence.__init__(self, type_spec)
    self._source = source
    self._map_fn = map_fn
    self._target_executor = target_executor

  async def compute(self):
    return _SyncFromAsyncIterable(self)

  def __aiter__(self):
    return _AsyncMapIterator(self._source, self._map_fn, self._target_executor)

class _AsyncMapIterator:
  """An async iterator based on a mapping function."""

  def __init__(self, source: _Sequence,
               map_fn: executor_value_base.ExecutorValue,
               target_executor: executor_base.Executor):
    self._source_aiter = source.__aiter__()
    self._source_element_type = source.type_signature.element
    self._map_fn = map_fn
    self._target_executor = target_executor

  async def __anext__(self):
    next_element = await self._source_aiter.__anext__()
    element_val = await _delegate(next_element, self._source_element_type,
                                  self._target_executor)
    mapped_val = await self._target_executor.create_call(
        self._map_fn, element_val)
    return await mapped_val.compute()

class _SequenceOp(typed_object.TypedObject, metaclass=abc.ABCMeta):
  """An internal representation of a sequence op used by this executor."""

  def __init__(self, type_spec: computation_types.SequenceType):
    self._type_signature = type_spec

  @property
  def type_signature(self):
    return self._type_signature

  @abc.abstractmethod
  async def execute(self, target_executor, arg):
    raise NotImplementedError

class _SequenceMapOp(_SequenceOp):
  """Implements a SEQUENCE_MAP intrinsic."""

  async def execute(self, target_executor: executor_base.Executor, arg):
    arg_type = self._type_signature.parameter
    seq, map_fn = await asyncio.gather(
        *
        [_to_sequence(arg[1]),
         _delegate(arg[0], arg_type[0], target_executor)])
    result_type = self._type_signature.result
    return _SequenceFromMap(seq, map_fn, target_executor, result_type)

class _SequenceReduceOp(_SequenceOp):
  """Implements a SEQUENCE_REDUCE intrinsic."""

  async def execute(self, target_executor: executor_base.Executor, arg):
    arg_type = self._type_signature.parameter
    seq = await _to_sequence(arg[0])
    accumulator, op = await asyncio.gather(
        *
        [_delegate(arg[idx], arg_type[idx], target_executor) for idx in [1, 2]])
    element_type = seq.type_signature.element
    async for x in seq:
      el = await target_executor.create_value(x, element_type)
      arg = await target_executor.create_struct([accumulator, el])
      accumulator = await target_executor.create_call(op, arg)
    return accumulator

async def _to_sequence(sequence_val):
  """Returns an instance of `_Sequence` for given internal representation.

  Args:
    sequence_val: An internal sequence representation, either `_Sequence` or a
      value embedded in a target executor.

  Returns:
    An instance of `_Sequenc`.
  """
  if isinstance(sequence_val, _Sequence):
    return sequence_val
  py_typecheck.check_type(sequence_val, executor_value_base.ExecutorValue)
  return _SequenceFromPayload(await sequence_val.compute(),
                              sequence_val.type_signature)

async def _delegate(val, type_spec: computation_types.Type,
                    target_executor: executor_base.Executor):
  """Delegates value representation to target executor.

  Args:
    val: A value representation to delegate.
    type_spec: The TFF type.
    target_executor: The target executor to delegate.

  Returns:
    An instance of `executor_value_base.ExecutorValue` owned by the target
    executor.
  """
  py_typecheck.check_type(target_executor, executor_base.Executor)
  if val is None:
    return None
  if isinstance(val, executor_value_base.ExecutorValue):
    return val
  if isinstance(val, _Sequence):
    return await target_executor.create_value(await val.compute(), type_spec)
  if isinstance(val, structure.Struct):
    if len(val) != len(type_spec):
      raise ValueError('Found {} elements and {} types in a struct {}.'.format(
          len(val), len(type_spec), str(val)))
    elements = structure.iter_elements(val)
    element_types = structure.iter_elements(type_spec)
    names = []
    coros = []
    for (el_name, el), (el_type_name, el_type) in zip(elements, element_types):
      if el_name != el_type_name:
        raise ValueError(
            'Element name mismatch between value ({}) and type ({}).'.format(
                str(val), str(type_spec)))
      names.append(el_name)
      coros.append(_delegate(el, el_type, target_executor))
    flat_targets = await asyncio.gather(*coros)
    reassembled_struct = structure.Struct(list(zip(names, flat_targets)))
    return await target_executor.create_struct(reassembled_struct)
  return await target_executor.create_value(val, type_spec)

class SequenceExecutorValue(executor_value_base.ExecutorValue):
  """A representation of a value owned and managed by the `SequenceExecutor`."""

  _VALID_INTERNAL_REPRESENTATION_TYPES = (_Sequence, _SequenceOp,
                                          executor_value_base.ExecutorValue,
                                          structure.Struct)

  def __init__(self, value, type_spec):
    """Creates an instance of `SequenceExecutorValue` to represent `value`.

    The following kinds of representations are supported as the input:

    * An instance of `_Sequence` to represent sequences.

    * An instance of `_SequenceOp` for an instrinsic among one of the
      `SequenceExecutor._SUPPORTED_INTRINSIC_TO_SEQUENCE_OP` to represent the
      supported sequence intrinsics.

    * An instance of `ExecutorValue` (constructed and managed by a downstream
      executor).

    * An instance of `structure.Struct` with any of the above, possibly nested.

    Args:
      value: The internal representation of the value (see above).
      type_spec: The TFF type of the value.
    """
    py_typecheck.check_type(type_spec, computation_types.Type)
    py_typecheck.check_type(
        value, SequenceExecutorValue._VALID_INTERNAL_REPRESENTATION_TYPES)
    self._type_signature = type_spec
    self._value = value

  @property
  def internal_representation(self):
    return self._value

  @property
  def type_signature(self):
    return self._type_signature

  @tracing.trace
  async def compute(self):

    async def _comp(x):
      if isinstance(x, (_Sequence, executor_value_base.ExecutorValue)):
        return await x.compute()
      if isinstance(x, structure.Struct):
        return structure.pack_sequence_as(
            x, await asyncio.gather(*[_comp(y) for y in structure.flatten(x)]))
      raise NotImplementedError('Unable to compute a value of type {}.'.format(
          py_typecheck.type_string(type(x))))

    return await _comp(self._value)

class SequenceExecutor(executor_base.Executor):
  """The sequence executor is responsible for operations on sequences.

  NOTE: This executor is not fully implemented yet.

  This executor will understand TFF sequence types, sequence intrinsics, and
  potentially the `data` construct. It is intended for use as a building block
  for use in executor stacks that need to rely on sequence operators.

  This executor directly executes operations on sequences, as opposed to
  compiling or delegating them to another executor. Other executors may support
  operations on sequences, and they may handle them in a different, more
  efficient manner. This generic implementation is intended as a general-purpose
  building block that could be added at any level of the executor stack.

  Currently supported constructs:
  - tff.sequence_map
  - tff.sequence_reduce
  """

  def __init__(self, target_executor: executor_base.Executor):
    """Creates a new instance of this executor.

    Args:
      target_executor: Downstream executor that this executor delegates to.

    Raises:
      TypeError: if arguments are of the wrong types.
    """
    py_typecheck.check_type(target_executor, executor_base.Executor)
    self._target_executor = target_executor

  _SUPPORTED_INTRINSIC_TO_SEQUENCE_OP = {
      intrinsic_defs.SEQUENCE_MAP.uri: _SequenceMapOp,
      intrinsic_defs.SEQUENCE_REDUCE.uri: _SequenceReduceOp
  }

  @tracing.trace(span=True)
  async def create_value(self, value, type_spec=None):
    """Creates a value in this executor.

    The following kinds of `value` are supported as the input:

    * An instance of TFF computation proto containing one of the supported
      sequence intrinsics as its sole body.

    * An instance of eager TF dataset.

    * Anything that is supported by the target executor (as a pass-through).

    * A nested structure of any of the above.

    Args:
      value: The input for which to create a value.
      type_spec: An optional TFF type (required if `value` is not an instance of
        `typed_object.TypedObject`, otherwise it can be `None`).

    Returns:
      An instance of `SequenceExecutorValue` that represents the embedded value.
    """
    if type_spec is None:
      py_typecheck.check_type(value, typed_object.TypedObject)
      type_spec = value.type_signature
    else:
      type_spec = computation_types.to_type(type_spec)
    if isinstance(type_spec, computation_types.SequenceType):
      return SequenceExecutorValue(
          _SequenceFromPayload(value, type_spec), type_spec)
    if isinstance(value, pb.Computation):
      value_type = type_serialization.deserialize_type(value.type)
      value_type.check_equivalent_to(type_spec)
      which_computation = value.WhichOneof('computation')
      # NOTE: If not a supported type of intrinsic, we let it fall through and
      # be handled by embedding in the target executor (below).
      if which_computation == 'intrinsic':
        intrinsic_def = intrinsic_defs.uri_to_intrinsic_def(value.intrinsic.uri)
        if intrinsic_def is None:
          raise ValueError('Encountered an unrecognized intrinsic "{}".'.format(
              value.intrinsic.uri))
        op_type = SequenceExecutor._SUPPORTED_INTRINSIC_TO_SEQUENCE_OP.get(
            intrinsic_def.uri)
        if op_type is not None:
          type_analysis.check_concrete_instance_of(type_spec,
                                                   intrinsic_def.type_signature)
          op = op_type(type_spec)
          return SequenceExecutorValue(op, type_spec)
    if isinstance(type_spec, computation_types.StructType):
      if not isinstance(value, structure.Struct):
        value = structure.from_container(value)
      elements = structure.flatten(value)
      element_types = structure.flatten(type_spec)
      flat_embedded_vals = await asyncio.gather(*[
          self.create_value(el, el_type)
          for el, el_type in zip(elements, element_types)
      ])
      embedded_struct = structure.pack_sequence_as(value, flat_embedded_vals)
      return await self.create_struct(embedded_struct)
    target_value = await self._target_executor.create_value(value, type_spec)
    return SequenceExecutorValue(target_value, type_spec)

  @tracing.trace
  async def create_call(self, comp, arg=None):
    py_typecheck.check_type(comp, SequenceExecutorValue)
    py_typecheck.check_type(comp.type_signature, computation_types.FunctionType)
    fn = comp.internal_representation
    if isinstance(fn, executor_value_base.ExecutorValue):
      if arg is not None:
        arg = await _delegate(arg.internal_representation, arg.type_signature,
                              self._target_executor)
      target_result = await self._target_executor.create_call(fn, arg)
      return SequenceExecutorValue(target_result, target_result.type_signature)
    if isinstance(fn, _SequenceOp):
      py_typecheck.check_type(arg, SequenceExecutorValue)
      comp.type_signature.parameter.check_assignable_from(arg.type_signature)
      result = await fn.execute(self._target_executor,
                                arg.internal_representation)
      result_type = comp.type_signature.result
      return SequenceExecutorValue(result, result_type)
    raise NotImplementedError(
        'Unsupported functional representation of type {} (possibly indicating '
        'a mismatch between structures supported by create_cvalue() and '
        'create_call()).'.format(py_typecheck.type_string(type(fn))))

  @tracing.trace
  async def create_struct(self, elements):
    elements = structure.iter_elements(structure.from_container(elements))
    val_elements = []
    type_elements = []
    for k, v in elements:
      py_typecheck.check_type(v, SequenceExecutorValue)
      val_elements.append((k, v.internal_representation))
      type_elements.append((k, v.type_signature))
    return SequenceExecutorValue(
        structure.Struct(val_elements),
        computation_types.StructType(type_elements))

  @tracing.trace
  async def create_selection(self, source, index):
    py_typecheck.check_type(source, SequenceExecutorValue)
    py_typecheck.check_type(source.type_signature, computation_types.StructType)
    if isinstance(source.internal_representation,
                  executor_value_base.ExecutorValue):
      target_val = await self._target_executor.create_selection(
          source.internal_representation, index)
      return SequenceExecutorValue(target_val, target_val.type_signature)
    py_typecheck.check_type(source.internal_representation, structure.Struct)
    py_typecheck.check_type(index, int)
    return SequenceExecutorValue(source.internal_representation[index],
                                 source.type_signature[index])

  def close(self):
    self._target_executor.close()


text:
using System.Web.Http;
using IOC.Ninject;

namespace WebApi
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            GlobalConfiguration.Configure(WebApiConfig.Register);

            GlobalConfiguration.Configuration.DependencyResolver = new WebApiNinjectDependencyResolver();
        }
    }
}


text:
require 'test_helper'

class KeywordSetControllerTest < ActionController::TestCase
  # test "the truth" do
  #   assert true
  # end
end


text:
/**
 *      @file   mactable.c
 *      @brief  MAC address table management.
 */

#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <sys/queue.h>

#include "lagopus_apis.h"
#include "lagopus/port.h"
#include "lagopus/bridge.h"
#include "pktbuf.h"
#include "packet.h"
#include <pthread.h>

#if defined HYBRID && defined PIPELINER
#include "pipeline.h"
#endif /* HYBRID && PIPELINER */

#define NR_MAX_ENTRIES 1024  /**< max number that can be registered
                                  in the bbq. */

/**
 * Struct mac entry args for get all entries from mactable.
 */
struct macentry_args {
  unsigned int num;  /**< number of entries. */
  unsigned int no;  /**< current entry's index. */
  struct macentry *entries;  /**< mac entries. */
};

/**
 * Convert type of mac address.
 * @param[in] inteth MAC address.
 */
static inline uint64_t
array_to_uint64(const uint8_t ethaddr[]) {
  uint64_t inteth = (uint64_t)ethaddr[5] << 40 |
                    (uint64_t)ethaddr[4] << 32 |
                    (uint64_t)ethaddr[3] << 24 |
                    (uint64_t)ethaddr[2] << 16 |
                    (uint64_t)ethaddr[1] << 8 |
                    (uint64_t)ethaddr[0];
  return inteth;
}

/**
 * Get local data.
 * @param[in] mactable MAC address table object.
 */
static struct local_data *
get_local_data (struct mactable *mactable) {
  uint32_t wid = 0;
#if defined PIPELINER
  wid = pipeline_worker_id;
#elif defined HAVE_DPDK
  if (is_rawsocket_only_mode() == false) {
    wid = dpdk_get_worker_id();
    if (wid < 0 || wid >= UPDATER_LOCALDATA_MAX_NUM || wid == UINT32_MAX) {
      wid = 0;
    }
  }
#endif
  return &mactable->local[wid];
}

/**
 * Create mac entry.
 * mac entry is struct macentry object.
 * @param[in] inteth MAC address.
 * @param[in] portid In port number.
 * @param[in] address_type Type(static or dynamic) of mac address learning.
 */
static struct macentry *
macentry_alloc(uint64_t inteth, uint32_t portid,
               uint16_t address_type) {
  struct macentry *entry;

  entry = calloc(1, sizeof(struct macentry));
  if (entry != NULL) {
    entry->inteth = inteth;
    entry->portid = portid;
    entry->address_type = address_type;
  }

  return entry;
}

/**
 * Free mac entry for mactable.
 * @param[in] macentry MAC address entry.
 */
static void
macentry_free(struct macentry *macentry) {
  free(macentry);
}

/**
 * Free mac entry for bbq.
 * @param[in] data MAC address entry.
 */
static void
free_bbq_entry(void **data) {
  if (likely(data != NULL && *data != NULL)) {
    struct macentry *entry = *data;
    free(entry);
  }
}

/**
 * Copy mac entry when get all entreis from mactable.
 */
static bool
copy_macentry(void *key, void *val, lagopus_hashentry_t he, void *arg) {
  bool result = false;
  struct macentry_args *ma = (struct macentry_args *)arg;
  struct macentry *entries = (struct macentry *)ma->entries;
  struct macentry *entry = (struct macentry *)val;

  if (val != NULL && ma->no < ma->num) {
    entries[ma->no].inteth = entry->inteth;
    entries[ma->no].portid = entry->portid;
    entries[ma->no].update_time = entry->update_time;
    entries[ma->no].address_type = entry->address_type;
    ma->no++;
    result = true;
  }

  return result;
}

/**
 * Add mac entry to bbq.
 * @param[in] mactable MAC address table.
 * @param[in] inteth MAC address
 * @param[in] portid In port number.
 * @param[in] address_type Setting address type.
 * @param[in] now Update time.
 */
static inline lagopus_result_t
add_entry_bbq(struct local_data *local, uint64_t inteth,
              uint32_t portid, uint16_t address_type) {
  lagopus_result_t rv = LAGOPUS_RESULT_OK;
  struct macentry *entry;

  entry = macentry_alloc(inteth, portid, address_type);
  if (entry) {
    lagopus_bbq_put(&local->bbq, &entry, struct macentry *, 0);
  } else {
    rv = LAGOPUS_RESULT_ANY_FAILURES;
  }

  return rv;
}

/**
 * Add mac entry to mactable for writing.
 * @param[in] mactable MAC address table.
 * @param[in] write_table MAC address table for writing.
 * @param[in] inteth MAC address
 * @param[in] portid In port number.
 * @param[in] address_type Setting address type.
 * @param[in] now Update time.
 */
static lagopus_result_t
add_entry_write_table(struct mactable *mactable, lagopus_hashmap_t *write_table,
                      uint64_t inteth, uint32_t portid, uint16_t address_type,
                      struct timespec now) {
  lagopus_result_t rv;
  struct macentry *entry;
  struct macentry *dentry;
  struct macentry *find_entry;

  if (mactable == NULL || write_table == NULL) {
    rv = LAGOPUS_RESULT_INVALID_ARGS;
    goto out;
  }

  /* allcate new entry. */
  entry = macentry_alloc(inteth, portid, address_type);
  if (entry == NULL) {
    rv = LAGOPUS_RESULT_NO_MEMORY;
    goto out;
  }
  entry->update_time = now;
  dentry = entry;

  /* if same key(inteth) entry was already registered,
     update entry in hashmap_add(4th argument means allow overwrite). */
  rv = lagopus_hashmap_find_no_lock(write_table,
                                    (void *)inteth,
                                    (void **)&find_entry);
  if (rv == LAGOPUS_RESULT_NOT_FOUND) {
    /* new entry */
    if (entry->address_type == MACTABLE_SETTYPE_DYNAMIC) {
      TAILQ_INSERT_TAIL(&mactable->macentry_list, entry, next);
    }

    mactable->nentries++;
  } else if (find_entry != NULL && rv == LAGOPUS_RESULT_OK) {
    /* update entry */
    if (find_entry->address_type == MACTABLE_SETTYPE_DYNAMIC) {
      TAILQ_REMOVE(&mactable->macentry_list, find_entry, next);
      TAILQ_INSERT_TAIL(&mactable->macentry_list, entry, next);
    } else {
      dentry->address_type = MACTABLE_SETTYPE_STATIC;
    }
  } else {
    lagopus_msg_error("lagopus hashmap find failed\n");
    goto out;
  }

  rv = lagopus_hashmap_add_no_lock(write_table, (void *)inteth,
                                   (void **)&dentry, true);

out:
  return rv;
}

/**
 * Write entry to local cache.
 * @param[in] h Hashamap for local cache.
 * @param[in] inteth MAC address
 * @param[in] portid Port number.
 * @param[in] address_type Type(static or dynamic) of mac address learning.
 */
static lagopus_result_t
add_entry_local_cache(lagopus_hashmap_t *h, uint64_t inteth,
                  uint32_t portid, uint16_t address_type) {
  lagopus_result_t rv = LAGOPUS_RESULT_ANY_FAILURES;
  struct macentry *entry;

  entry = macentry_alloc(inteth, portid, address_type);
  rv = lagopus_hashmap_add_no_lock(h, (void *)inteth, (void **)&entry, true);

  return rv;
}

/**
 * Get output port and address type by mac address from mac address table
 * (local cache or reading mac address table).
 * @param[in] h Target hashmap(localcache or reading mactable).
 * @param[in] inteth MAC address.
 * @param[out] port Number of output port.
 */
static inline lagopus_result_t
lookup(lagopus_hashmap_t *h, uint64_t inteth,
       uint32_t *port, uint16_t *address_type) {
  lagopus_result_t rv = LAGOPUS_RESULT_ANY_FAILURES;
  struct macentry *dentry;

  rv = lagopus_hashmap_find_no_lock(h, (void *)inteth, (void **)&dentry);
  if (rv == LAGOPUS_RESULT_OK) {
    *port = dentry->portid;
    *address_type = dentry->address_type;
  }

  return rv;
}

/**
 *
 */
static lagopus_result_t
check_eth_history (struct local_data *local, uint64_t inteth, bool switched) {
  lagopus_result_t rv;
  int i;

  /* mactable were switched, clear history. */
  if (unlikely(switched)) {
    /* clear history to reset when mactables are switched. */
    for (i = 0; i < MACTABLE_HISTORY_MAX_NUM; i++) {
      local->eth_history[i] = 0;
    }
    local->history_index = 0;
  } else {
    /* check history for entry decimation. */
    for (i = 0; i < MACTABLE_HISTORY_MAX_NUM; i++) {
      /* start history_index - 1. */
      uint16_t index =
        (local->history_index + MACTABLE_HISTORY_MAX_NUM - (i + 1)) % MACTABLE_HISTORY_MAX_NUM;
      if (local->eth_history[index] == inteth) {
        /* don't write to bbq. */
        return LAGOPUS_RESULT_ALREADY_EXISTS;
      }
    }
  }

  local->eth_history[local->history_index] = inteth;
  local->history_index = (local->history_index + 1) % 10;

  return LAGOPUS_RESULT_NOT_FOUND;
}

/**
 * Reset referred flag, check and return result if switched.
 * @param[in] mactable MAC address table.
 * @param[in] local Local data for each worker.
 */
static bool
check_referred(struct mactable *mactable, struct local_data *local) {
  uint32_t referred = __sync_val_compare_and_swap(&local->referred_table,
                                                  mactable->read_table^1,
                                                  mactable->read_table);
  if (referred == local->referred_table) {
    return false;
  } else {
    return true;
  }
}

/**
 * Learning mac address to mac address table(write to local cache or bbq).
 * When writing to the bbq, thinning the duplicate entry.
 * @param[in] mactable MAC address table.
 * @param[in] portid Target port no.
 * @param[in] ethaddr Target mac address.
 * @param[in] address_type Type(static or dynamic) of mac address learning.
 */
static lagopus_result_t
learn_port(struct mactable *mactable,
           uint32_t portid,
           const uint8_t ethaddr[],
           uint16_t address_type) {
  uint64_t inteth = array_to_uint64(ethaddr);
  lagopus_result_t rv = LAGOPUS_RESULT_OK;
  lagopus_hashmap_t *h;
  struct macentry *entry, *dentry;
  struct local_data *local;
  int i;
  bool switched;
  uint32_t read_table, referred;

  if (mactable == NULL) {
    rv = LAGOPUS_RESULT_INVALID_ARGS;
    return rv;
  }

  /* get local data. */
  local = get_local_data(mactable);

  /* check reference index. */
  switched = check_referred(mactable, local);
  if (switched) {
    /* clear local cache. */
    lagopus_hashmap_clear(&local->localcache, true);
  }

  /* find entry, then write to cache if not found. */
  rv = lagopus_hashmap_find_no_lock(&local->localcache,
                                    (void *)inteth, (void **)&dentry);
  if (rv == LAGOPUS_RESULT_NOT_FOUND) {
    rv = add_entry_local_cache(&local->localcache, inteth,
                               portid, address_type);
    if (rv != LAGOPUS_RESULT_OK) {
      /* failed lookup from local cache. */
      lagopus_msg_warning("local cache operation failed(%d).\n", rv);
      goto out;
    }
  }

  /* thinning out the entry to be added to the queue, *
   * to reduce the load on the 'updater'.             */
  if (check_eth_history(local, inteth, switched) == LAGOPUS_RESULT_NOT_FOUND) {
    /* 'updater' updates the update_time in macentry. *
     * therefore, 'worker' add an entry to the bbq,   *
     * to notify that there was reference.            */
    rv = add_entry_bbq(local, inteth, portid, address_type);
  }

out:
  return rv;
}

/**
 * Get output port by the mac address
 * in mac address table(local cache or reading mac address table).
 * @param[in] mactable MAC address table.
 * @param[in] ethaddr MAC address.
 */
static uint32_t
lookup_port(struct mactable *mactable,
            const uint8_t ethaddr[]) {
  lagopus_result_t rv = LAGOPUS_RESULT_OK;
  lagopus_hashmap_t *h;
  uint64_t inteth = array_to_uint64(ethaddr);
  uint32_t port;
  uint16_t addr_type;
  struct local_data *local;
  bool switched = false;
  uint32_t read_table, referred;

  if (mactable == NULL) {
    rv = LAGOPUS_RESULT_INVALID_ARGS;
    return OFPP_ALL;
  }

  /* get local data. */
  local = get_local_data(mactable);

  /*
    "local->referring" is a flag that indicates that it's in operation.
    Before performing the operation on mactable, it must be turned on(1).
    Then, after the operation ends, it must be turned off(0).
    Only worker's operation(learning and lookup) to change this flag.
   */
  __sync_add_and_fetch(&local->referring, 1);

  /* check reference index. */
  switched = check_referred(mactable, local);
  if (switched) {
    /* clear local cache. */
    lagopus_hashmap_clear(&local->localcache, true);
  } else {
    /* check local cache. */
    rv = lookup(&local->localcache, inteth, &port, &addr_type);
    if (rv == LAGOPUS_RESULT_OK) {
      goto out;
    }
  }

  /* lookup from read mactable */
  read_table = __sync_add_and_fetch(&mactable->read_table, 0);
  h = &mactable->hashmap[read_table];
  rv = lookup(h, inteth, &port, &addr_type);
  if (rv == LAGOPUS_RESULT_OK) {
    /* write to local cache(no entry in local cache). */
    add_entry_local_cache(&local->localcache, inteth, port, addr_type);
  } else {
    port = OFPP_ALL;
  }

out:
  if (port != OFPP_ALL &&
      check_eth_history(local, inteth, switched) == LAGOPUS_RESULT_NOT_FOUND) {
    /* 'updater' updates the update_time in macentry. *
     * therefore, 'worker' add an entry to the bbq,   *
     * to notify that there was reference.            */
    rv = add_entry_bbq(local, inteth, port, addr_type);
  }

  __sync_sub_and_fetch(&local->referring, 1);
  return port;
}

/**
 * Age out(= remove old entries).
 * This function is called from mactable_update().
 * Utilizing the diffrence between the current time and update time
 * that the entry has.
 * Deletable entry(address_type is MACTABLE_SETTYPE_DYNAMIC) is registered
 * in the macentry_list.
 * @param[in] mactable MAC address table object.
 */
static lagopus_result_t
age_out_write_table(struct mactable *mactable, lagopus_hashmap_t *write_table) {
  lagopus_result_t rv = LAGOPUS_RESULT_OK;
  struct macentry *entry;
  struct timespec now = get_current_time();

  /* check the update_time from the top of the macentry_list */
  while ((entry = TAILQ_FIRST(&mactable->macentry_list)) != NULL) {

    if (now.tv_sec - entry->update_time.tv_sec >= mactable->ageing_time) {
      lagopus_msg_info("mactable_age_out: expired mac entry.\n");

      /* this entry is expired. */
      /* remove mac entry from macentry_list. */
      TAILQ_REMOVE(&mactable->macentry_list, entry, next);

      /* remove mac entry from hashmap. */
      rv = lagopus_hashmap_delete_no_lock(write_table,
                                          (void *)entry->inteth,
                                          (void **)&entry,
                                          true);
      mactable->nentries--;

      if (rv != LAGOPUS_RESULT_OK) {
        /* remove fialure from hashmap. */
        break;
      }
    } else {
      /* nothing to do. */
      break;
    }
  }

  return rv;
}

/**
 * Initialize mactable object.
 * This function must be called when the bridge object creation.
 * @param[in] mactable MAC address table object.
 */
lagopus_result_t
mactable_init(struct mactable *mactable) {
  lagopus_result_t rv = LAGOPUS_RESULT_OK;
  int i, j;

  /* create read-write lock. */
  rv = lagopus_rwlock_create(&mactable->lock);
  if (rv != LAGOPUS_RESULT_OK) {
    return rv;
  }

  /* set mac table configuration */
  mactable->maxentries = 8192;
  mactable->ageing_time = 300;

  mactable->nentries = 0;
  TAILQ_INIT(&mactable->macentry_list);

  for (i = 0; i < UPDATER_LOCALDATA_MAX_NUM; i++) {
    struct local_data *local = &mactable->local[i];
    /* initialize localcache */
    lagopus_hashmap_create(&local->localcache,
                           LAGOPUS_HASHMAP_TYPE_ONE_WORD,
                           macentry_free);

    /* create mac entry queue */
    rv = lagopus_bbq_create(&local->bbq, struct macentry *,
                            NR_MAX_ENTRIES, free_bbq_entry);
    if (rv != LAGOPUS_RESULT_OK) {
      lagopus_perror(rv);
      return rv;
    }

    for (j = 0; j < MACTABLE_HISTORY_MAX_NUM; j++) {
      local->eth_history[j] = 0;
    }
    local->history_index = 0;
    __sync_lock_test_and_set(&local->referring, 0);
    __sync_lock_release(&local->referring);
  }

  /* init read_tbl flag */
  __sync_lock_test_and_set(&mactable->read_table, 0);
  __sync_lock_release(&mactable->read_table);

  /* create hashmap for mactable */
  for (i = 0; i < 2; i++) {
    rv = lagopus_hashmap_create(&mactable->hashmap[i],
                                LAGOPUS_HASHMAP_TYPE_ONE_WORD,
                                macentry_free);
  }

  return rv;
}

/**
 * Finalize mactable object.
 * This function must be called when the bridge object discarded.
 * @param[in] mactable MAC address table object.
 */
lagopus_result_t
mactable_fini(struct mactable *mactable) {
  lagopus_result_t rv = LAGOPUS_RESULT_OK;
  int i;

  /* destroy hashmap for mactable. */
  for (i = 0; i < 2; i++) {
    lagopus_hashmap_destroy(&mactable->hashmap[i], true);
  }

  for (i = 0; i < UPDATER_LOCALDATA_MAX_NUM; i++) {
    /* destroy local cache. */
    lagopus_hashmap_destroy(&mactable->local[i].localcache, true);

    /* destroy bbq. */
    lagopus_bbq_shutdown(&mactable->local[i].bbq, true);
    lagopus_bbq_destroy(&mactable->local[i].bbq, true);
  }

  /* destroy read-write lock. */
  lagopus_rwlock_destroy(&mactable->lock);

  return rv;
}

/**
 * Update mac address table by timer('updater').
 * Entry data are written to the writing mac table by only 'updater'.
 * They are merged read mac table and bbq.
 * @param[in] mactable MAC address table object.
 */
lagopus_result_t
mactable_update(struct mactable *mactable) {
  lagopus_result_t rv = LAGOPUS_RESULT_OK;
  lagopus_hashmap_t *rh, *wh;
  int i, j, cnt;
  struct macentry_args ma;
  struct macentry **ep = NULL;
  bool is_empty = true;
  unsigned int bbq_size = 0;
  uint32_t read_table;
  size_t get_num = 0, entry_num = 0, tmp_num = 0;

  /*
   * update mactable entry from new entries
   * in queue and read table.
   */
  read_table = __sync_add_and_fetch(&mactable->read_table, 0);

  /* check referred */
  for (cnt = 0; cnt < UPDATER_LOCALDATA_MAX_NUM; cnt++) {
    /*
     * Here, let's describe how to manage double mactables.
     *
     * We have 2 types of threads.
     * - Updater:
     * The updater is a maintainer of mactables. All mac entries should be
     * wrriten in one of mactables by the updater.
     * Currently the updater has 2 mactables. It looks like double buffering.
     * One of mactable is only for reading, and the another is for writing new
     * mac entries. At some point, the updater replaces mactable, then all
     * readers will refer latest mactables.
     * - Worker:
     * The worker refers one of mactable to process packets. The worker needs
     * to check which mactable is currently valid before processing packets.
     *
     * The updater has a below value to specify which mactable is valid.
     * - read_table:
     * The index value of mactable that stores newest mac entries. This value
     * is managed by the updater.
     *
     * Also, the worker has below values.
     * - referred
     * The index value of mactable that the worker is curretly referring.
     * - referring:
     * The status value indicates whether the worker is in a critical section,
     * or not.
     *  0: not in critical section.
     *  1: in critical section.
     *
     * The updater should know correct timing to write new macentiries,
     * and replace mactable. To know it, check below conditions.
     * - 'referred' and 'read_table' have different values, and the workers is
     *   in critical section.
     * If one of the workers is in above condition, just give up writing and
     * replacing. The next time the updater is invoked by timer, the updater
     * will try same things.
     *
     * Here is more detaled description why the updater needs to check like
     * above. If 'referred' and 'read_table' are different, obiously it means
     * old table is still referred by the worker. Even if values are different,
     * if worker 'is not' in a critical section, next time the worker will go
     * in the critical section, reffered value will be updated to latest one.
     * So the updater can start writing.
     *
     * Anyway, only the case that above condition is true in all the workers,
     * the updater cannot start writing.
     */
    uint32_t referred =
      __sync_add_and_fetch(&mactable->local[cnt].referred_table, 0);
    if (referred != read_table) {
      uint16_t referring =
        __sync_add_and_fetch(&mactable->local[cnt].referring, 0);
      if (referring == 1) {
        return LAGOPUS_RESULT_OK;
      }
    }
  }

  /* get tables */
  wh = &mactable->hashmap[read_table^1];
  rh = &mactable->hashmap[read_table];

  /* clear write table. */
  lagopus_hashmap_clear(wh, true);
  while ((mactable->macentry_list).tqh_first != NULL) {
    TAILQ_REMOVE(&mactable->macentry_list,
                 mactable->macentry_list.tqh_first, next);
    mactable->nentries = 0;
  }

  /* get entries from read table. */
  ma.num = (unsigned int)lagopus_hashmap_size(rh);
  ma.no = 0;
  ma.entries = malloc(sizeof(struct macentry) * ma.num);
  rv = lagopus_hashmap_iterate(rh, copy_macentry, &ma);

  /* check entry num. */
  entry_num = ma.num;
  if (ma.num > mactable->maxentries) {
    entry_num = mactable->maxentries;
    lagopus_msg_warning("mactable is full, drop read table entries(%d)\n",
                        ma.num - mactable->maxentries);
  }

  /* write entries to write table. */
  for (j = 0; j < entry_num; j++) {
    add_entry_write_table(mactable, wh,
                          ma.entries[j].inteth,
                          ma.entries[j].portid,
                          ma.entries[j].address_type,
                          ma.entries[j].update_time);
  }

  /* get entries from queue. */
  for (cnt = 0; cnt < UPDATER_LOCALDATA_MAX_NUM; cnt++) {
    get_num = 0;
    rv = lagopus_bbq_is_empty(&mactable->local[cnt].bbq, &is_empty);
    if (rv != LAGOPUS_RESULT_OK) {
      lagopus_msg_error("bbq_is_empty failed[%d].\n", (int)rv);
      if (ma.entries) {
        free(ma.entries);
      }
      return rv;
    }
    if (!is_empty) {
      bbq_size = lagopus_bbq_size(&mactable->local[cnt].bbq);
      ep = malloc(sizeof(struct macentry *) * bbq_size);
      lagopus_bbq_get_n(&mactable->local[cnt].bbq, ep, bbq_size, 0,
                        struct macentry *, 0, &get_num);
    }

    tmp_num = mactable->maxentries - entry_num;
    entry_num = get_num;
    if (get_num > tmp_num) {
      entry_num = tmp_num;
      lagopus_msg_warning("mactable is full, drop bbq entries(%d)\n",
                          get_num - tmp_num);
    }
    /* write entries to write table */
    for (i = 0; i < entry_num; i++) {
      struct timespec now = get_current_time();
      add_entry_write_table(mactable, wh,
                            ep[i]->inteth, ep[i]->portid,
                            ep[i]->address_type, now);
      free(ep[i]);
    }
  }

  /* age out */
  age_out_write_table(mactable, wh);

  /* free temporary data. */
  if (ma.entries) {
    free(ma.entries);
  }
  if (ep) {
    free(ep);
  }

  /*
   * switch table (write table <-> read table).
   */
  __sync_val_compare_and_swap(&mactable->read_table, read_table, read_table^1);

  return rv;
}

/**
 * Learning mac address and input port number when packet handling.
 * This function is called from l3 routing function in interface.c.
 * @param[in] pkt receive packet.
 */
void
mactable_port_learning(struct lagopus_packet *pkt) {
  learn_port(&pkt->in_port->bridge->mactable,
             pkt->in_port->ofp_port.port_no,
             pkt->eth->ether_shost,
             MACTABLE_SETTYPE_DYNAMIC);
}

/**
 * Look up output port in mac address table when packet handling.
 * This function is called from l3 routing function in interface.c.
 * @param[in] pkt receive packet.
 */
void
mactable_port_lookup(struct lagopus_packet *pkt) {
  uint32_t port;
  port = lookup_port(&pkt->in_port->bridge->mactable,
                     pkt->eth->ether_dhost);
  pkt->output_port = port;
}

/**
 * Delete all mac entries from mac address table by a request from datastore.
 * @param[in] mactable MAC address table object.
 */
lagopus_result_t
mactable_entry_clear(struct mactable *mactable) {
  return LAGOPUS_RESULT_OK;
}

/**
 * Delete a mac entry from mac address table by a request from datastore.
 * @param[in] mactable MAC address table object.
 * @param[in] ethaddr MAC address.
 */
lagopus_result_t
mactable_entry_delete(struct mactable *mactable, const uint8_t ethaddr[]) {
  return LAGOPUS_RESULT_OK;
}

/**
 * Add or modify a mac entry to mac address table by a request from datastore.
 * @param[in] mactable MAC address table object.
 * @param[in] ethaddr MAC address.
 * @param[in] portid Port number.
 */
lagopus_result_t
mactable_entry_update(struct mactable *mactable,
                      const uint8_t ethaddr[],
                      uint32_t portid) {
  struct local_data *local;
  local = get_local_data(mactable);
  return add_entry_bbq(local, array_to_uint64(ethaddr),
                       portid, MACTABLE_SETTYPE_STATIC);
}

/**
 * Get number of max entries by a request from datastore.
 * @param[in] mactable MAC address table object.
 */
uint32_t
mactable_max_entries_get(struct mactable *mactable) {
  uint32_t ret;
  int cstate;

  lagopus_rwlock_reader_enter_critical(&mactable->lock, &cstate);
  ret = mactable->maxentries;
  (void)lagopus_rwlock_leave_critical(&mactable->lock, cstate);

  return ret;
}

/**
 * Get ageing time by a request from datastore.
 * @param[in] mactable MAC address table object.
 */
uint32_t
mactable_ageing_time_get(struct mactable *mactable) {
  uint32_t ret;
  int cstate;

  lagopus_rwlock_reader_enter_critical(&mactable->lock, &cstate);
  ret =  mactable->ageing_time;
  (void)lagopus_rwlock_leave_critical(&mactable->lock, cstate);

  return ret;
}

/**
 * Set number of max entries by a request from datastore.
 * @param[in] mactable MAC address table object.
 * @param[in] max_entries Number of max entries.
 */
void
mactable_max_entries_set(struct mactable *mactable, uint32_t max_entries) {
  int cstate;

  lagopus_rwlock_writer_enter_critical(&mactable->lock, &cstate);
  mactable->maxentries = max_entries;
  (void)lagopus_rwlock_leave_critical(&mactable->lock, cstate);
}

/**
 * Set ageing time by a request from datastore.
 * @param[in] mactable MAC address table object.
 * @param[in] ageing_time Ageing time.
 */
void
mactable_ageing_time_set(struct mactable *mactable, uint32_t ageing_time) {
  int cstate;

  lagopus_rwlock_writer_enter_critical(&mactable->lock, &cstate);
  mactable->ageing_time = ageing_time;
  (void)lagopus_rwlock_leave_critical(&mactable->lock, cstate);

}