package jp.ac.u_ryukyu.ie.cr.jungle.test;

import jp.ac.u_ryukyu.ie.cr.jungle.JungleTree;
import jp.ac.u_ryukyu.ie.cr.jungle.JungleTreeEditor;
import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.DefaultNodePath;

import java.nio.ByteBuffer;


public class JungleWriteThread extends Thread {

    JungleTree tree;
    boolean loop = true;
    private int writeCount;

    public JungleWriteThread(JungleTree tree) {
        this.tree = tree;
    }

    public void set(boolean flag) {
        loop = flag;
    }

    public void get() {
        System.out.println("writeCount = " + writeCount);
    }

    @Override
    public void run() {
        while (loop) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            JungleTreeEditor editor = tree.getTreeEditor();
            editor = editor.putAttribute(new DefaultNodePath(), "key", ByteBuffer.wrap(String.valueOf(0).getBytes())).b();
            if (editor.success().isA())
                System.out.println("faild");
            else
                writeCount++;
        }
    }
}
