/// <summary> /// Directly enumerates the contents of BPlusTree from disk in read-only mode. /// </summary> /// <param name="options"> The options normally used to create the <see cref="BPlusTree{TKey, TValue}"/> instance </param> /// <returns> Yields the Key/Value pairs found in the file </returns> public static IEnumerable <KeyValuePair <TKey, TValue> > EnumerateFile(BPlusTreeOptions <TKey, TValue> options) { options = options.Clone(); options.CreateFile = CreatePolicy.Never; options.ReadOnly = true; using (INodeStorage store = options.CreateStorage()) { bool isnew; Node root; IStorageHandle hroot = store.OpenRoot(out isnew); if (isnew) { yield break; } NodeSerializer nodeReader = new NodeSerializer(options, new NodeHandleSerializer(store)); if (isnew || !store.TryGetNode(hroot, out root, nodeReader)) { throw new InvalidDataException(); } Stack <KeyValuePair <Node, int> > todo = new Stack <KeyValuePair <Node, int> >(); todo.Push(new KeyValuePair <Node, int>(root, 0)); while (todo.Count > 0) { KeyValuePair <Node, int> cur = todo.Pop(); if (cur.Value == cur.Key.Count) { continue; } todo.Push(new KeyValuePair <Node, int>(cur.Key, cur.Value + 1)); Node child; if (!store.TryGetNode(cur.Key[cur.Value].ChildNode.StoreHandle, out child, nodeReader)) { throw new InvalidDataException(); } if (child.IsLeaf) { for (int ix = 0; ix < child.Count; ix++) { var set = child[ix].ToKeyValuePair(); var enumerator = set.Value.GetEnumerator(); while (enumerator.MoveNext()) { yield return(new KeyValuePair <TKey, TValue>(set.Key, enumerator.Current.Key)); } } } else { todo.Push(new KeyValuePair <Node, int>(child, 0)); } } } }
private TooltipAttribute GetTooltipAttribute(SerializedProperty prop, bool inherit) { if (prop == null) { return(null); } // Remove the Selected.Data. part of the path because we already traversed it. var path = prop.propertyPath.Substring(14); FieldInfo fInfo = null; var type = _inspectorObject.Selected.Data.GetType(); foreach (var name in path.Split('.')) { var fields = NodeSerializer.GetSerializedFields(type); fInfo = fields.FirstOrDefault(x => x.Name == name); if (fInfo == null) { return(null); } type = fInfo.FieldType; } return(fInfo?.GetCustomAttribute <TooltipAttribute>()); }
private void SaveGraph() { _graphData.Nodes = new NodeData[_nodes.Count]; _graphData.Connections = new ConnectionData[_connections.Count]; var i = 0; foreach (var node in _nodes.Values) { _graphData.Nodes[i] = NodeSerializer.Serialize(node); i++; } i = 0; foreach (var connection in _connections.Values) { _graphData.Connections[i] = new ConnectionData { Guid = new GuidData(connection.Guid), In = new GuidData(connection.In.Guid), Out = new GuidData(connection.Out.Guid) }; i++; } }
public void BasicIncrementWorks() { var node = new Node(); IncrementPrefixExtensions.IncrementPrefix(ref node, true, "test", 0); Assert.Equal(NodeSerializer.Serialize(node), @"(0:1)t(0:1)te(0:1)tes(0:1)test(0:1)test (0:1)"); }
public override void WriteJson(JsonWriter writer, SharedVariable value, JsonSerializer serializer) { var obj = new JObject { { "Type", NodeSerializer.EvaluateType(value.GetType()) }, }; if (value.IsGlobal) { obj.Add("IsGlobal", true); obj.Add("IsShared", true); if (!string.IsNullOrEmpty(value.Name)) { obj.Add("Name", value.Name); } } else if (value.IsShared) { if (!string.IsNullOrEmpty(value.Name) && value.Name != "None") { obj.Add("Name", value.Name); obj.Add("IsShared", true); } else { WriteValue(obj, value, serializer); } } else { WriteValue(obj, value, serializer); } obj.WriteTo(writer); }
public void SerializeFourLevelTree() { var node = new Node { ratio = { aCount = 1, anCount = 11 }, SortedKids = new[] { new Node { c = 'b', ratio = { aCount = 5, anCount = 0 }, SortedKids = new[] { new Node { c = 'c', ratio = { aCount = 3, anCount = 4 }, SortedKids = new[] { new Node { c = 'd', ratio = { aCount = 0x100, anCount = 0x80 } }, }, }, new Node { c = 'u', ratio = { aCount = 2, anCount = 15 } }, }, }, } }; const string serializedNode = @"1;b;1;b5;;2;c3;4;1;d74;3k;;u2;f;;"; Assert.Equal(serializedNode, NodeSerializer.SerializeDense(node)); Assert.Equal(node, NodeDeserializer.DeserializeDense(serializedNode), NodeEqualityComparer.Instance); }
private void OnExportXML() { try { // Configure save file dialog box var dlg = new Microsoft.Win32.SaveFileDialog() { FileName = "Export", // Default file name DefaultExt = ".xml", // Default file extension Filter = "xml files|*.xml" // Filter files by extension }; // Show save file dialog box Nullable <bool> result = dlg.ShowDialog(); // Process save file dialog box results if (result.HasValue && result == true) { // write data this.CurrentFilePath = dlg.FileName; NodeSerializer.SaveNodeList(this.NManager.Nodes, this.CurrentFilePath); } } catch (Exception ex) { MessageBox.Show(ex.Message, "XML File Export Error", MessageBoxButton.OK, MessageBoxImage.Error); // MessageBox.Show(ex.StackTrace, "EXCEL File Export Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
public void SerializeFourLevelTree() { var node = new Node { ratio = { aCount = 1, anCount = 11 }, SortedKids = new[] { new Node { c = 'b', ratio = { aCount = 5, anCount = 0 }, SortedKids = new[] { new Node { c = 'c', ratio = { aCount = 3, anCount = 4 }, SortedKids = new[] { new Node { c = 'd', ratio = { aCount = 0x100, anCount = 0x80 } }, }, }, new Node { c = 'u', ratio = { aCount = 2, anCount = 15 } }, }, }, } }; const string serializedNode = @"(1:b)b(5:0)bc(3:4)bcd(100:80)bu(2:f)"; Assert.Equal(serializedNode, NodeSerializer.Serialize(node)); Assert.Equal(node, NodeSerializer.Deserialize(serializedNode), NodeEqualityComparer.Instance); }
/// <summary> /// Loads the Library from a file. /// </summary> public void loadLibrary() { this.libraryNodeViewerList.Clear(); if (!File.Exists(Path.Combine(WorkSpace.DIR, LIB_FILE_NAME))) { return; } NodeSerializer serializer = new NodeSerializer(); ClassManager cManager = Singleton <ClassManager> .Instance; JObject json = JObject.Parse(File.ReadAllText(Path.Combine(WorkSpace.DIR, LIB_FILE_NAME))); JToken arrayToken = new JObject(); json.TryGetValue("lib", out arrayToken); JArray array = arrayToken as JArray; if (array != null) { array .Select(n => serializer.deserializeNode(n as JObject, cManager)) .ForEach(addLibraryNode); } }
private void OnImportXML() { try { // Configure save file dialog box var dlg = new Microsoft.Win32.OpenFileDialog() { Filter = "xml files|*.xml" // Filter files by extension }; if (dlg.ShowDialog().Value) { if (File.Exists(dlg.FileName)) { // retrieve data this.CurrentFilePath = dlg.FileName; List <Node> nodes = NodeSerializer.RetrieveNodeList(this.CurrentFilePath); // attach data to the node manager foreach (Node n in nodes) { bool success = this.NManager.AddNode(n); } // publish to GUI this.OnRefreshTree(); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "XML File Import Error", MessageBoxButton.OK, MessageBoxImage.Error); // MessageBox.Show(ex.StackTrace, "XML File Import Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
public void Save(BehaviourGraph graph) { graph.Links.Clear(); if (graph == null) { return; } foreach (var edge in edges.ToList()) { var inputNode = edge.input.node as TaskNode; var outputNode = edge.output.node as TaskNode; graph.Links.Add(new NodeLink { SourceGuid = outputNode.GUID, TargetGuid = inputNode.GUID }); } graph.NodesData.Clear(); foreach (var node in nodes.ToList().Cast <TaskNode>()) { graph.NodesData.Add(new NodeData { GUID = node.GUID, Position = node.GetPosition().position, Type = node.Type, Data = node.Data }); } var id = 0; var entry = graph.NodesData.FirstOrDefault(x => x.Type == typeof(EntryTask)); if (entry != null) { AssignID(entry, ref id); } graph.Variables.Clear(); foreach (var variable in Properties) { graph.Variables.Add(new Variable { Name = variable.Name, Type = variable.Type, IsGlobal = variable.IsGlobal }); } graph.BehaviorSource.TaskData = NodeSerializer.Serialize(graph); EditorUtility.SetDirty(graph); AssetDatabase.SaveAssets(); }
public void IncrementWithSharedPrefixWorks() { var node = new Node(); IncrementPrefixExtensions.IncrementPrefix(ref node, true, "test", 0); IncrementPrefixExtensions.IncrementPrefix(ref node, true, "taste", 0); Assert.Equal(NodeSerializer.Serialize(node), @"(0:2)t(0:2)ta(0:1)tas(0:1)tast(0:1)taste(0:1)taste (0:1)te(0:1)tes(0:1)test(0:1)test (0:1)"); }
public void SingleNodeWorks() { var node = new Node { ratio = { aCount = 0x2468ad, anCount = 0x12345 } }; const string serializedNode = @"(2468ad:12345)"; Assert.Equal(serializedNode, NodeSerializer.Serialize(node)); Assert.Equal(node, NodeSerializer.Deserialize(serializedNode), NodeEqualityComparer.Instance); }
public IHashTrieNode?GetNodeById(ulong id) { var prefix = EntryPrefix.PersistentHashMap.BuildPrefix(id); var content = Get(prefix); if (content is null) { return(null); } return(NodeSerializer.FromBytes(content)); }
private static FieldInfo GetField(Type type, string name) { var fields = NodeSerializer.GetSerializedFields(type); var field = fields.FirstOrDefault(x => x.Name == name); if (field == null) { throw new MissingFieldException($"Couldn't find serialized field {name} on type {type}"); } return(field); }
public String Explore(String currentNode) { DbResourceLocator locator = new DbResourceLocator(); Node result = locator.Locate(currentNode); NodeSerializer ser = new NodeSerializer(); ser.Format = (int)NodeSerializer.Formats.XML; String s = ser.GetXml(result); s = Convert.ToBase64String(Encoding.UTF8.GetBytes(s)); return(s); }
private void LoadAttributes(FieldInfo info, GR2Writer writer) { var attrs = info.GetCustomAttributes(typeof(SerializationAttribute), true); if (attrs.Length > 0) { SerializationAttribute serialization = attrs[0] as SerializationAttribute; if (serialization.Section != SectionType.Invalid) { PreferredSection = serialization.Section; } DataArea = serialization.DataArea; if (serialization.Type != MemberType.Invalid) { Type = serialization.Type; } if (serialization.TypeSelector != null) { TypeSelector = Activator.CreateInstance(serialization.TypeSelector) as VariantTypeSelector; } if (serialization.SectionSelector != null) { SectionSelector = Activator.CreateInstance(serialization.SectionSelector) as SectionSelector; } if (serialization.Serializer != null) { Serializer = Activator.CreateInstance(serialization.Serializer) as NodeSerializer; } if (writer != null && serialization.Prototype != null) { WriteDefinition = writer.LookupStructDefinition(serialization.Prototype); } if (serialization.Name != null) { GrannyName = serialization.Name; } Prototype = serialization.Prototype; SerializationKind = serialization.Kind; ArraySize = serialization.ArraySize; MinVersion = serialization.MinVersion; MaxVersion = serialization.MaxVersion; } }
public NodeCacheBase(BPlusTreeOptions <TKey, TValue> options) { Options = options; LockFactory = Options.LockingFactory; Storage = Options.CreateStorage(); if (Options.UseStorageCache) { Storage = new StorageCache(Storage, Options.CacheKeepAliveMaximumHistory); } NodeSerializer = new NodeSerializer(Options, new NodeHandleSerializer(Storage)); _version = new NodeVersion(); }
public void TestNodeSerialization() { MemoryStream ms = new MemoryStream(); var f = new CoreEntityFactory(); var ns = new NodeSerializer(ms, f, new DummyContextFactory()); ns.Serialize(f.CreateNode()); ms.Position = 0; var doc = XDocument.Load(ms); Assert.Single(doc.Descendants("Node")); }
public void TestDeserializeNode() { MemoryStream ms = new MemoryStream(); var f = new CoreEntityFactory(); var ns = new NodeSerializer(ms, f, new DummyContextFactory()); var nodeOrig = f.CreateNode(); ns.Serialize(nodeOrig); ms.Position = 0; var node = ns.Deserialize(); Assert.Equal(nodeOrig.Id, node.Id); }
public void TestEdgesSerialization() { MemoryStream ms = new MemoryStream(); var f = new VirEntityFactory(); var ns = new NodeSerializer(ms, f, new DummyContextFactory()); var node = f.CreateNode(); node.ConnectTo(f.CreateNode(), Core.Interfaces.EdgeDirection.Both); ns.Serialize(node); ms.Position = 0; Assert.Single(XDocument.Load(ms).Descendants("Edge")); }
public bool TryGetNode(ulong id, out IHashTrieNode?trieNode) { if (_nodeCache.TryGetValue(id, out trieNode)) { return(true); } var rawNode = _dbContext.Get(EntryPrefix.PersistentHashMap.BuildPrefix(id)); trieNode = null; if (rawNode == null) { return(false); } trieNode = NodeSerializer.FromBytes(rawNode); return(true); }
public void CommitNodes() { RocksDbAtomicWrite tx = new RocksDbAtomicWrite(_dbContext); foreach (var item in _nodeCache) { tx.Put(EntryPrefix.PersistentHashMap.BuildPrefix(item.Key), NodeSerializer.ToBytes(item.Value)); Console.WriteLine("Adding node to DB : " + item.Key); } ulong nodesCnt = UInt64Utils.FromBytes(_dbContext.Get(EntryPrefix.NodesDownloadedTillNow.BuildPrefix())); nodesCnt += (ulong)_nodeCache.Count; tx.Put(EntryPrefix.NodesDownloadedTillNow.BuildPrefix(), UInt64Utils.ToBytes(nodesCnt)); tx.Commit(); _nodeCache.Clear(); }
/// <summary> /// Saves the library in a File. /// </summary> private void saveLibrary() { NodeSerializer serializer = new NodeSerializer(); JObject lib = new JObject(); JArray array = new JArray(); lib.Add("lib", array); this.libraryNodeViewerList .Select(n => (Node)n.DataContext) .Select(serializer.serializeNode) .ForEach(array.Add); File.WriteAllText(Path.Combine(WorkSpace.DIR, LIB_FILE_NAME), lib.ToString()); }
public void DeleteNode(Guid guid) { var nodeData = NodeSerializer.Serialize(_nodes[guid]); _history.Execute(() => { var node = _nodes[guid]; var sockets = new List <Socket>(); node.GetSockets(sockets, SocketType.Input); node.GetSockets(sockets, SocketType.Output); foreach (var socket in sockets) { _sockets.Remove(socket.Guid); } _nodes.Remove(guid); }, () => { CreateNode(nodeData); }, "delete node: " + nodeData.Type.FullName); }
public void TestEdgeDeserialization() { MemoryStream ms = new MemoryStream(); var f = new VirEntityFactory(); var ns = new NodeSerializer(ms, f, new DummyContextFactory()); var node = new Person(f); node.ConnectTo(f.CreateNode(), Core.Interfaces.EdgeDirection.Both); ns.Serialize(node); ms.Position = 0; var nOut = ns.Deserialize(); Assert.Single(nOut.Edges); Assert.IsAssignableFrom <IRemoteNode>(nOut.Edges.Single().GetOtherNode(nOut)); }
/// <summary> /// Performs a low-level scan of the storage file to yield all Key/Value pairs it was able to read from the file. /// </summary> /// <param name="options"> The options normally used to create the <see cref="BPlusTree{TKey, TValue}"/> instance </param> /// <param name="sharing"> <see cref="FileShare"/> options used to open the file </param> /// <returns> Yields the Key/Value pairs found in the file </returns> public static IEnumerable <KeyValuePair <TKey, TValue> > RecoveryScan(Options options, FileShare sharing) { options = options.Clone(); options.CreateFile = CreatePolicy.Never; string filename = options.FileName; if (String.IsNullOrEmpty(filename)) { throw new InvalidConfigurationValueException("FileName", "The FileName property was not specified."); } if (!File.Exists(filename)) { throw new InvalidConfigurationValueException("FileName", "The FileName specified does not exist."); } if (options.StorageType != StorageType.Disk) { throw new InvalidConfigurationValueException("StorageType", "The storage type is not set to 'Disk'."); } using (FragmentedFile file = new FragmentedFile(filename, options.FileBlockSize, 1, 1, FileAccess.Read, sharing, FileOptions.None)) { NodeSerializer nodeReader = new NodeSerializer(options, new NodeHandleSerializer(new Storage.BTreeFileStore.HandleSerializer())); foreach (KeyValuePair <long, Stream> block in file.ForeachBlock(true, false, IngoreDataInvalid)) { List <KeyValuePair <TKey, TValue> > found = new List <KeyValuePair <TKey, TValue> >(); try { foreach (KeyValuePair <TKey, TValue> entry in nodeReader.RecoverLeaf(block.Value)) { found.Add(entry); } } catch { /* Serialization error: Ignore and continue */ } foreach (KeyValuePair <TKey, TValue> entry in found) { yield return(entry); } } } }
public void TestHomeIndex(string tree) { var homeController = new HomeController(); var parseModel = new ParseModel { TreeRequest = tree }; var result = homeController.Index(parseModel) as ViewResult; parseModel = result?.Model as ParseModel; if (parseModel != null) { var node = NodeSerializer.Deserialize(parseModel.TreeResultJson); node = BalanceExample.Balance(node); Assert.True(BalanceExample.IsAvl(node)); } else { throw new Exception("Error"); } }
private void sendMenuItem_Click(object sender, RoutedEventArgs e) { Dictionary <Guid, INodeProxy> nodes = new Dictionary <Guid, INodeProxy>(); nodes.Add(NodeProxy.Id, NodeProxy); //send a basic XML representation of the node if (MapControl.SelectedNodes.Length > 0) { foreach (INodeProxy nodeProxy in MapControl.SelectedNodes) { if (!nodes.ContainsKey(nodeProxy.Id)) { nodes.Add(nodeProxy.Id, nodeProxy); } } } INodeProxy[] nodesArray = new INodeProxy[nodes.Values.Count]; nodes.Values.CopyTo(nodesArray, 0); HtmlPage.Window.Invoke("sendMessage", "'" + NodeSerializer.SerializeNode(nodesArray) + "'"); }
public void RootNodeWithKidsWorks() { var node = new Node { ratio = { aCount = 1, anCount = 11 }, SortedKids = new[] { new Node { c = 'b', ratio = { aCount = 5, anCount = 0 }, }, new Node { c = 'u', ratio = { aCount = 2, anCount = 15 }, }, } }; const string serializedNode = @"(1:b)b(5:0)u(2:f)"; Assert.Equal(serializedNode, NodeSerializer.Serialize(node)); Node deserialized = NodeSerializer.Deserialize(serializedNode); Assert.Equal(node, deserialized, NodeEqualityComparer.Instance); }