view Main/jungle-network/operations/NetworkNodeOperation.cs @ 20:1f99e150f336

fix folder and add Object Mapper.
author Kazuma Takeda
date Thu, 15 Dec 2016 22:52:48 +0900
parents
children 1466993c104c
line wrap: on
line source

using UnityEngine;
using System.Collections;

namespace JungleDB {
	public class NetworkNodeOperation : NodeOperation {

		public int Position;
		public string Key;
		public byte[] Value;
		public int commandType;

		// when switch use static readonly so, use const. 
		// when A code compalie, case const use is fast.
		// case readonly use is bit slow.
		public const int NUM_PUT_ATTRIBUTE = 1;
		public const int NUM_APPEND_CHILD = 2;
		public const int NUM_DELETE_CHILD = 3;
		public const int NUM_DELETE_ATTRIBUTE = 4;

		public NetworkNodeOperation () {
			this.Position = -2;
			this.Key = null;
			this.Value = null;
			this.commandType = 0;
		}

		public NetworkNodeOperation(NodeOperation op) {
			this.Position = op.getPosition();
			this.Key = op.getKey();
			this.Value = op.getValue();
			this.commandType = getCommandType(op.getCommand());
		}

		public static int getCommandType (Command c) {
			switch(c) {
			case Command.PUT_ATTRIBUTE:
				return NUM_PUT_ATTRIBUTE;
			case Command.APPEND_CHILD:
				return NUM_APPEND_CHILD;
			case Command.DELETE_CHILD:
				return NUM_DELETE_CHILD;
			case Command.DELETE_ATTRIBUTE:
				return NUM_DELETE_ATTRIBUTE;
			default:
				break;
			}
			return 0;
		}

		public static Command getCommand (int num) {
			switch(num) {
			case NUM_PUT_ATTRIBUTE:
				return Command.PUT_ATTRIBUTE;
			case NUM_APPEND_CHILD:
				return Command.APPEND_CHILD;
			case NUM_DELETE_CHILD:
				return Command.DELETE_CHILD;
			case NUM_DELETE_ATTRIBUTE:
				return Command.DELETE_ATTRIBUTE;
			default:
				break;
			}
			return Command.DEFAULT;
		}

		public Command getCommand() {
			return getCommand(commandType);
		}

		public int getPosition () {
			return this.Position;
		}

		public string getKey () {
			return this.Key;
		}

		public byte[] getValue() {
			return this.Value;
		}

		public Either<Error, TreeNode> invoke (TreeNode target) {
			switch(getCommand(commandType)) {
			case Command.PUT_ATTRIBUTE:
				return target.getAttributes().put(this.Key, this.Value);
			case Command.APPEND_CHILD:
				return target.getChildren().addNewChildAt(this.Position);
			case Command.DELETE_CHILD:
				return target.getChildren().deleteChildAt(this.Position);
			case Command.DELETE_ATTRIBUTE:
				return target.getAttributes().delete(this.Key);
			}
			return null;
		}
	}
}