view src/alice/jungle/codesegment/LogUpdateCodeSegment.java @ 49:a89c3539bff2

modified LogUpdateCodeSegment
author one
date Fri, 12 Jul 2013 19:56:12 +0900
parents 686057add8a4
children 459b50f1a6ee
line wrap: on
line source

package alice.jungle.codesegment;

import java.io.IOException;

import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.DefaultTreeOperationLog;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
import jungle.test.bbs.JungleManager;
import alice.codesegment.CodeSegment;
import alice.datasegment.CommandType;
import alice.datasegment.Receiver;
import alice.jungle.datasegment.store.operations.DefaultTreeOperationLogContainer;
import alice.jungle.transaction.NetworkDefaultJungleTreeEditor;

public class LogUpdateCodeSegment extends CodeSegment {
	
	public Receiver arg1 = ids.create(CommandType.PEEK);
	public Receiver host = ids.create(CommandType.PEEK);
	public String location = "local";
	
	public LogUpdateCodeSegment() {
		host.setKey("host",0);
	}
	
	public LogUpdateCodeSegment(String _location) {
		location = _location;
		host.setKey("local","host",0);
	}
	
	public void run() {
		int index = arg1.index;
		String h = host.asString();
		DefaultTreeOperationLogContainer container = arg1.asClass(DefaultTreeOperationLogContainer.class);
		if(updaterIsMe(h, container)) {
			LogUpdateCodeSegment updateCS = new LogUpdateCodeSegment();
			updateCS.arg1.setKey(location, "log", index+1);
			if(location.equals("local") && !iAmRootNode(h)) {
				ods.put("parent", "log", container);
			}
			return;
		}
		DefaultTreeOperationLog log = null;
		try {
			log = container.convert();
		} catch (IOException e) {
			e.printStackTrace();
		}
		JungleTree tree = JungleManager.getJungle().getTreeByName("boards");
		JungleTreeEditor editor = tree.getTreeEditor();
		Either<Error, JungleTreeEditor> either = JungleManager.edit(editor, log);
		if(either.isA()) {
			throw new IllegalStateException();
		}
		editor = either.b();
		either = editor.success();
		if(either.isA()) {
			throw new IllegalStateException();			
		}
		if(location.equals("local")) {
			NetworkDefaultJungleTreeEditor nEditor = (NetworkDefaultJungleTreeEditor) editor;
			container.setRevision(Long.parseLong(nEditor.getRevision()));
			container.setUpdaterName(nEditor.getUpdaterName());
		}
		ods.put("log", container);
		LogUpdateCodeSegment updateCS = new LogUpdateCodeSegment();
		updateCS.arg1.setKey(location, "log", index+1);
	}
	
	private boolean updaterIsMe(String host, DefaultTreeOperationLogContainer container) {
		return host.equals(container.getServerName());
	}
	
	private boolean iAmRootNode(String h) {
		return h.equals("node0");
	}
	
	
	
}