Mercurial > hg > Members > tatsuki > bench > jungle-core
view src/main/java/jp/ac/u_ryukyu/ie/cr/tatsuki/jungle/store/index/PutIndexEditor.java @ 140:99bda30ea72c
temporarily stored
author | one |
---|---|
date | Fri, 07 Nov 2014 02:20:31 +0900 |
parents | ec166c8ff079 |
children | afbe19c98f53 72f454eb04ec |
line wrap: on
line source
package jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.store.index; import fj.Ord; import fj.data.List; import fj.data.Option; import fj.data.TreeMap; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.TreeEditor; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.TreeOperationLog; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.IndexJungleTreeEditor; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.TransactionManager; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Pair; import java.nio.ByteBuffer; public class PutIndexEditor implements IndexEditor { String key; String attribute; NodePath path; TransactionManager txManager; TreeNode root; TreeEditor editor; TreeOperationLog log; public PutIndexEditor(String key, ByteBuffer attribute,NodePath path) { this.key = key; this.attribute = new String(attribute.array()); this.path = path; } @Override public Either<Error, IndexJungleTreeEditor> edit(TreeNode root,TransactionManager txManager,TreeEditor editor,TreeOperationLog log,TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index) { NodePath newPath = path.pop().right(); TreeNode target = getTarget(root, newPath); TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> newIndex = editIndex(target, path, key, attribute,index); IndexJungleTreeEditor newEditor = new IndexJungleTreeEditor(root,txManager,editor,log, newIndex); Either<Error, IndexJungleTreeEditor> either = DefaultEither.newB(newEditor); return either; } public TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> editIndex(TreeNode target, NodePath path, String key, String attribute,TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> index){ Option<TreeMap<String, List<Pair<TreeNode, NodePath>>>> innerIndexOp = index.get(key); Pair<TreeNode, NodePath> pathNode = new Pair<TreeNode, NodePath>(target,path); if (attribute != null) { if (innerIndexOp.isNone()) { TreeMap<String, List<Pair<TreeNode, NodePath>>> innerIndex = TreeMap.empty(Ord.stringOrd); List<Pair<TreeNode, NodePath>> list = List.nil(); list = list.cons(pathNode); innerIndex = innerIndex.set(attribute, list); TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> newIndex = index.set(key, innerIndex); return newIndex; } else { TreeMap<String, List<Pair<TreeNode, NodePath>>> innerIndex = innerIndexOp.some(); Option<List<Pair<TreeNode, NodePath>>> opList = innerIndex.get(attribute); if (opList.isNone()) { List<Pair<TreeNode, NodePath>> list = List.nil(); list = list.cons(pathNode); innerIndex = innerIndex.set(attribute, list); } else { List<Pair<TreeNode, NodePath>> list = opList.some(); list = list.cons(pathNode); innerIndex = innerIndex.set(attribute, list); } TreeMap<String, TreeMap<String, List<Pair<TreeNode, NodePath>>>> newIndex = index.set(key, innerIndex); return newIndex; } } return index; } public TreeNode getTarget(TreeNode node , NodePath path){ Pair<Integer, NodePath> pathNode = path.pop(); Either<Error, TreeNode> either = node.getChildren().at(pathNode.left()); if (either.isA()) return node; TreeNode child = either.b(); if (pathNode.right().size() == 0) return child; TreeNode target = getTarget(child,pathNode.right()); return target; } }