protected final ArrayList<PageNode> buildNodes() { ArrayList<PageNode> nodes = new ArrayList<PageNode>(); for (NodeBuilder node : this.nodes) { nodes.add(node.build()); } return nodes; }
// ignored due to: https://github.com/hazelcast/hazelcast/issues/5035 @Ignore @Test public void testMapLoaderLoadUpdatingIndex() throws Exception { final int nodeCount = 3; String mapName = randomString(); SampleIndexableObjectMapLoader loader = new SampleIndexableObjectMapLoader(); Config config = createMapConfig(mapName, loader); NodeBuilder nodeBuilder = new NodeBuilder(nodeCount, config).build(); HazelcastInstance node = nodeBuilder.getRandomNode(); IMap<Integer, SampleIndexableObject> map = node.getMap(mapName); for (int i = 0; i < 10; i++) { map.put(i, new SampleIndexableObject("My-" + i, i)); } final SqlPredicate predicate = new SqlPredicate("name='My-5'"); assertPredicateResultCorrect(map, predicate); map.destroy(); loader.preloadValues = true; node = nodeBuilder.getRandomNode(); map = node.getMap(mapName); assertLoadAllKeysCount(loader, 1); assertPredicateResultCorrect(map, predicate); }
@Override public void init() { super.init(); nodeBuilder = new NodeBuilder(); nodeBuilder.setContext(getContext()); nodeBuilder.setOrder(getOrder()); nodeBuilder.setTree(getTree()); }
/** * Loads this configuration from stream<br> * Note: Closes the stream when finished * * @param stream to read from */ public void loadFromStream(InputStream stream) throws IOException { try { InputStreamReader reader = new InputStreamReader(stream); StringBuilder builder = new StringBuilder(); BufferedReader input = new BufferedReader(reader); try { String line, trimmedLine; HeaderBuilder header = new HeaderBuilder(); NodeBuilder node = new NodeBuilder(this.getIndent()); StringBuilder mainHeader = new StringBuilder(); int indent; while ((line = input.readLine()) != null) { line = StringUtil.ampToColor(line); indent = StringUtil.getSuccessiveCharCount(line, ' '); trimmedLine = line.substring(indent); // Prevent new name convention errors if (trimmedLine.equals("*:")) { trimmedLine = "'*':"; line = StringUtil.getFilledString(" ", indent) + trimmedLine; } // Handle a main header line if (trimmedLine.startsWith(MAIN_HEADER_PREFIX)) { mainHeader.append('\n').append(trimmedLine.substring(MAIN_HEADER_PREFIX.length())); continue; } // Handle a header line if (header.handle(trimmedLine)) { continue; } // Handle a node line node.handle(trimmedLine, indent); // Apply the header to a node if available if (header.hasHeader()) { this.setHeader(node.getPath(), header.getHeader()); header.clear(); } builder.append(line).append('\n'); } // Set main header if (mainHeader.length() > 0) { this.setHeader(mainHeader.toString()); } } finally { input.close(); } try { this.getSource().loadFromString(builder.toString()); } catch (InvalidConfigurationException e) { throw new IOException("YAML file is corrupt", e); } } catch (FileNotFoundException ex) { // Ignored } }
/** * At the highest level, we adhere to the classic b-tree insertion algorithm: * * <p>1. Add to the appropriate leaf 2. Split the leaf if necessary, add the median to the parent * 3. Split the parent if necessary, etc. * * <p>There is one important difference: we don't actually modify the original tree, but copy each * node that we modify. Note that every node on the path to the key being inserted or updated will * be modified; this implies that at a minimum, the root node will be modified for every update, * so every root is a "snapshot" of a tree that can be iterated or sliced without fear of * concurrent modifications. * * <p>The NodeBuilder class handles the details of buffering the copied contents of the original * tree and adding in our changes. Since NodeBuilder maintains parent/child references, it also * handles parent-splitting (easy enough, since any node affected by the split will already be * copied into a NodeBuilder). * * <p>One other difference from the simple algorithm is that we perform modifications in bulk; we * assume @param source has been sorted, e.g. by BTree.update, so the update of each key resumes * where the previous left off. */ public <V> Object[] update( Object[] btree, Comparator<V> comparator, Iterable<V> source, UpdateFunction<V> updateF) { assert updateF != null; NodeBuilder current = rootBuilder; current.reset(btree, POSITIVE_INFINITY, updateF, comparator); for (V key : source) { while (true) { if (updateF.abortEarly()) { rootBuilder.clear(); return null; } NodeBuilder next = current.update(key); if (next == null) break; // we were in a subtree from a previous key that didn't contain this new key; // retry against the correct subtree current = next; } } // finish copying any remaining keys from the original btree while (true) { NodeBuilder next = current.finish(); if (next == null) break; current = next; } // updating with POSITIVE_INFINITY means that current should be back to the root assert current.isRoot(); Object[] r = current.toNode(); current.clear(); return r; }
public String create() { StringBuilder builder = new StringBuilder(); builder.append("digraph{\r\nrankdir=BT\r\nnode [shape=record]\r\n"); Set<ConnectionBuilder> connections = new HashSet<ConnectionBuilder>(); for (NodeBuilder node : nodes) { node.create(builder); connections.addAll(node.getConnections()); } for (ConnectionBuilder connection : connections) { connection.create(builder); } builder.append("}"); return builder.toString(); }
public <V> Object[] build(Iterable<V> source, UpdateFunction<V> updateF, int size) { assert updateF != null; NodeBuilder current = rootBuilder; // we descend only to avoid wasting memory; in update() we will often descend into existing // trees // so here we want to descend also, so we don't have lg max(N) depth in both directions while ((size >>= FAN_SHIFT) > 0) current = current.ensureChild(); current.reset(EMPTY_LEAF, POSITIVE_INFINITY, updateF, null); for (V key : source) current.addNewKey(key); current = current.ascendToRoot(); Object[] r = current.toNode(); current.clear(); return r; }
@Override protected void initChildren() { List<OrderItem> orderItems = FacadeContext.getOrderItemFacade().findBy(getOrder(), OrderItemType.first); for (OrderItem orderItem : orderItems) { AOrderNode node = nodeBuilder.build( OrderItemNode.class, MapUtils.putAll( new HashMap<String, Object>(), new Object[] {"orderItem", orderItem})); add(node); } orderItems = FacadeContext.getOrderItemFacade().findBy(getOrder(), OrderItemType.common); for (OrderItem orderItem : orderItems) { if (orderItem.getSource() == null) { AOrderNode node = nodeBuilder.build( OrderItemNode.class, MapUtils.putAll( new HashMap<String, Object>(), new Object[] {"orderItem", orderItem})); add(node); } } if (getOrder() instanceof Order) { add( nodeBuilder.build( TemplatesNode.class, MapUtils.putAll( new HashMap<String, Object>(), new Object[] {"nodeBuilder", nodeBuilder}))); add(nodeBuilder.build(AGTFacadeNode.class, MapUtils.EMPTY_MAP)); add(nodeBuilder.build(ZFacadeNode.class, MapUtils.EMPTY_MAP)); } else if (getOrder() instanceof TemplateOrder) { add(nodeBuilder.build(TemplateFacadeNode.class, MapUtils.EMPTY_MAP)); } }
/** * Writes this configuration to stream<br> * Note: Closes the stream when finished * * @param stream to write to */ public void saveToStream(OutputStream stream) throws IOException { // Get rid of newline characters in text - Bukkit bug prevents proper saving for (String key : this.getSource().getKeys(true)) { Object value = this.getSource().get(key); if (value instanceof String) { String text = (String) value; if (text.contains("\n")) { this.getSource().set(key, Arrays.asList(text.split("\n", -1))); } } } BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stream)); try { // Write the top header writeHeader(true, writer, this.getHeader(), 0); // Write other headers and the nodes IntHashMap<String> anchorData = new IntHashMap<String>(); int indent; int anchStart, anchEnd, anchId = -1, anchDepth = 0, anchIndent = 0; boolean wasAnchor; int refStart, refEnd, refId; StringBuilder refData = new StringBuilder(); NodeBuilder node = new NodeBuilder(this.getIndent()); for (String line : this.getSource().saveToString().split("\n", -1)) { line = StringUtil.colorToAmp(line); indent = StringUtil.getSuccessiveCharCount(line, ' '); line = line.substring(indent); wasAnchor = false; // ===== Logic start ===== // Get rid of the unneeded '-characters around certain common names if (line.equals("'*':")) { line = "*:"; } // Handle a node if (node.handle(line, indent)) { // Store old anchor data if (anchId >= 0 && node.getDepth() <= anchDepth) { anchorData.put(anchId, refData.toString()); refData.setLength(0); anchId = refId = -1; } // Saving a new node: Write the node header writeHeader(false, writer, this.getHeader(node.getPath()), indent); // Check if the value denotes a reference refStart = line.indexOf("*id", node.getName().length()); refEnd = line.indexOf(' ', refStart); if (refEnd == -1) { refEnd = line.length(); } if (refStart > 0 && refEnd > refStart) { // This is a reference pointer: get id refId = ParseUtil.parseInt(line.substring(refStart + 3, refEnd), -1); if (refId >= 0) { // Obtain the reference data String data = anchorData.get(refId); if (data != null) { // Replace the line with the new data line = StringUtil.trimEnd(line.substring(0, refStart)) + " " + data; } } } // Check if the value denotes a data anchor anchStart = line.indexOf("&id", node.getName().length()); anchEnd = line.indexOf(' ', anchStart); if (anchEnd == -1) { anchEnd = line.length(); } if (anchStart > 0 && anchEnd > anchStart) { // This is a reference node anchor: get id anchId = ParseUtil.parseInt(line.substring(anchStart + 3, anchEnd), -1); anchDepth = node.getDepth(); anchIndent = indent; if (anchId >= 0) { // Fix whitespace after anchor identifier anchEnd += StringUtil.getSuccessiveCharCount(line.substring(anchEnd), ' '); // Store the data of this anchor refData.append(line.substring(anchEnd)); // Remove the variable reference from saved data line = StringUtil.replace(line, anchStart, anchEnd, ""); } wasAnchor = true; } } if (!wasAnchor && anchId >= 0) { // Not an anchor: append anchor data refData .append('\n') .append(StringUtil.getFilledString(" ", indent - anchIndent)) .append(line); } // Write the data if (LogicUtil.containsChar('\n', line)) { for (String part : line.split("\n", -1)) { StreamUtil.writeIndent(writer, indent); writer.write(part); writer.newLine(); } } else { StreamUtil.writeIndent(writer, indent); writer.write(line); writer.newLine(); } } } finally { writer.close(); } }