public static List <NodeBlock> SelectAllNodeBlocks() { var result = new List <NodeBlock>(); var lastNodeId = DbControl.FetchLastId(DbControl.NodePath); for (var nodeId = 1; nodeId < lastNodeId; ++nodeId) { var candidateNodeBlock = DbReader.ReadNodeBlock(nodeId); if (candidateNodeBlock.Used) { result.Add(candidateNodeBlock); } } return(result); }
public static HashSet <NodeBlock> SelectNodeBlocksByLabelAndProperties(string label, Dictionary <string, object> props) { if (label == null && props.Count == 0) { return(new HashSet <NodeBlock>(SelectAllNodeBlocks())); } var labelId = 0; if (label != null) { bool ok = DbControl.LabelInvertedIndex.TryGetValue(label, out labelId); if (!ok) { return(new HashSet <NodeBlock>()); } } if (label != null && props.Count == 0) { var result = new HashSet <NodeBlock>(); foreach (var nodeBlock in SelectAllNodeBlocks()) { if (nodeBlock.LabelId == labelId) { result.Add(nodeBlock); } } return(result); } var rawProps = new Dictionary <int, object>(); var fromPropNameIdToGoodNodeBlocks = new Dictionary <int, HashSet <NodeBlock> >(); foreach (var keyValuePair in props) { bool ok = DbControl.PropertyNameInvertedIndex.TryGetValue(keyValuePair.Key, out var propertyNameId); if (!ok) { return(new HashSet <NodeBlock>()); } fromPropNameIdToGoodNodeBlocks[propertyNameId] = new HashSet <NodeBlock>(); rawProps[propertyNameId] = keyValuePair.Value; } var propertyNameIds = new HashSet <int>(fromPropNameIdToGoodNodeBlocks.Keys); var lastPropertyId = DbControl.FetchLastId(DbControl.NodePropertyPath); for (var propertyId = 1; propertyId < lastPropertyId; ++propertyId) { var currentPropertyBlock = new NodePropertyBlock(DbReader.ReadPropertyBlock(DbControl.NodePropertyPath, propertyId)); if (!currentPropertyBlock.Used) { continue; } if (!propertyNameIds.Contains(currentPropertyBlock.PropertyNameId)) { continue; } switch (currentPropertyBlock.PropertyType) { case PropertyType.Int: if (BitConverter.ToInt32(currentPropertyBlock.Value, 0) != (int)rawProps[currentPropertyBlock.PropertyNameId]) { continue; } break; case PropertyType.String: if (!(DbReader.ReadGenericStringBlock(DbControl.StringPath, BitConverter.ToInt32(currentPropertyBlock.Value, 0)).Data) .Equals((string)rawProps[currentPropertyBlock.PropertyNameId])) { continue; } break; case PropertyType.Bool: if (BitConverter.ToBoolean(currentPropertyBlock.Value, 3) != (bool)rawProps[currentPropertyBlock.PropertyNameId]) { continue; } break; case PropertyType.Float: if (BitConverter.ToSingle(currentPropertyBlock.Value, 0) != (float)rawProps[currentPropertyBlock.PropertyNameId]) { continue; } break; default: throw new NotSupportedException(); } var currentNodeBlock = DbReader.ReadNodeBlock(currentPropertyBlock.NodeId); if (labelId != 0 && currentNodeBlock.LabelId != labelId) { continue; } fromPropNameIdToGoodNodeBlocks[currentPropertyBlock.PropertyNameId].Add(currentNodeBlock); } var listOfSets = new List <HashSet <NodeBlock> >(fromPropNameIdToGoodNodeBlocks.Values); var outputNodes = listOfSets.Aggregate( (h, e) => { h.IntersectWith(e); return(h); } ); return(outputNodes); }
/// <summary> /// Create storage files if missing /// </summary> public static void InitializeIO() { if (initializedIOFlag) { return; } try { if (!Directory.Exists(DbPath)) { Directory.CreateDirectory(DbPath); } foreach (var filePath in DbControl.DbFilePaths) { FileStreamDictionary[filePath] = new FileStream(Path.Combine(DbControl.DbPath, filePath), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, 5 * 1024 * 1024); } // Create new empty IdStorage if not present with next free id. // Else initialize .storage.db -> ID mapping if (!File.Exists(Path.Combine(DbPath, IdStoragePath))) { idFileStream = new FileStream(Path.Combine(DbPath, IdStoragePath), FileMode.Create, FileAccess.ReadWrite, FileShare.Read); foreach (var filePath in DbFilePaths) { idFileStream.Write(BitConverter.GetBytes(1), 0, 4); IdStorageDictionary[filePath] = 1; } idFileStream.Flush(); } else { idFileStream = new FileStream(Path.Combine(DbPath, IdStoragePath), FileMode.Open, FileAccess.ReadWrite, FileShare.Read); foreach (var filePath in DbFilePaths) { var blockNumber = IdStoreOrderNumber[filePath]; var storedIdBytes = new byte[4]; idFileStream.Seek(blockNumber * 4, SeekOrigin.Begin); idFileStream.Read(storedIdBytes, 0, 4); IdStorageDictionary[filePath] = BitConverter.ToInt32(storedIdBytes, 0); } } // Initialize Inverted Indexes for (var i = 1; i < FetchLastId(LabelPath); ++i) { var labelBlock = new LabelBlock(DbReader.ReadGenericStringBlock(LabelPath, i)); LabelInvertedIndex[labelBlock.Data] = labelBlock.Id; } for (var i = 1; i < FetchLastId(PropertyNamePath); ++i) { var propertyNameBlock = new PropertyNameBlock(DbReader.ReadGenericStringBlock(PropertyNamePath, i)); PropertyNameInvertedIndex[propertyNameBlock.Data] = propertyNameBlock.Id; } } catch (Exception ex) { TraceSource.TraceEvent(TraceEventType.Error, 1, $"Database Initialization Falied: {ex}"); } finally { initializedIOFlag = true; } }