static public Library.Payloads.Models.Document ToPayload(PersistDocument document) { return(new Library.Payloads.Models.Document() { Id = document.Id, Created = document.Created, Modfied = document.Modfied, Content = document.Content }); }
public PersistDocumentMeta Add(PersistDocument document) { var catalogItem = new PersistDocumentMeta() { Id = document.Id }; this.Collection.Add(catalogItem); return catalogItem; }
public void Store(Session session, string schema, Library.Payloads.Models.Document document, out Guid newId) { try { var persistDocument = PersistDocument.FromPayload(document); if (persistDocument.Id == Guid.Empty) { persistDocument.Id = Guid.NewGuid(); } if (persistDocument.Created == DateTime.MinValue) { persistDocument.Created = DateTime.UtcNow; } if (persistDocument.Modfied == DateTime.MinValue) { persistDocument.Modfied = DateTime.UtcNow; } using (var txRef = core.Transactions.Begin(session)) { var schemaMeta = core.Schemas.VirtualPathToMeta(txRef.Transaction, schema, LockOperation.Write); if (schemaMeta == null || schemaMeta.Exists == false) { throw new LeafSQLSchemaDoesNotExistException(schema); } string documentCatalogDiskPath = Path.Combine(schemaMeta.DiskPath, Constants.DocumentCatalogFile); var documentCatalog = core.IO.GetJson <PersistDocumentCatalog>(txRef.Transaction, documentCatalogDiskPath, LockOperation.Write); documentCatalog.Add(persistDocument); core.IO.PutJson(txRef.Transaction, documentCatalogDiskPath, documentCatalog); string documentDiskPath = Path.Combine(schemaMeta.DiskPath, Helpers.GetDocumentModFilePath(persistDocument.Id)); core.IO.CreateDirectory(txRef.Transaction, Path.GetDirectoryName(documentDiskPath)); core.IO.PutJson(txRef.Transaction, documentDiskPath, persistDocument); core.Indexes.InsertDocumentIntoIndexes(txRef.Transaction, schemaMeta, persistDocument); newId = document.Id; txRef.Commit(); } } catch (Exception ex) { core.Log.Write(String.Format("Failed to store document for process {0}.", session.ProcessId), ex); throw; } }
private QueryResult FindDocuments(Transaction transaction, PersistSchema schemaMeta, Conditions conditions, int rowLimit, List <string> fieldList) { QueryResult results = new QueryResult(); try { conditions.MakeLowerCase(); if (fieldList.Count == 1 && fieldList[0] == "*") { fieldList = null; } if (fieldList?.Count() > 0) { foreach (var field in fieldList) { results.Columns.Add(new QueryColumn(field)); } } else { results.Columns.Add(new QueryColumn("Id")); results.Columns.Add(new QueryColumn("Created")); results.Columns.Add(new QueryColumn("Modfied")); results.Columns.Add(new QueryColumn("Content")); } bool hasFieldList = fieldList != null && fieldList.Count > 0; var indexSelectionGroups = core.Indexes.SelectIndexes(transaction, schemaMeta, conditions); string documentCatalogDiskPath = Path.Combine(schemaMeta.DiskPath, Constants.DocumentCatalogFile); if (indexSelectionGroups.Count == 0) //Full schema scan. Ouch! { var documentCatalog = core.IO.GetJson <PersistDocumentCatalog>(transaction, documentCatalogDiskPath, LockOperation.Read); foreach (var documentMeta in documentCatalog.Collection) { string documentDiskPath = Path.Combine(schemaMeta.DiskPath, Helpers.GetDocumentModFilePath(documentMeta.Id)); PersistDocument persistDocument = core.IO.GetJson <PersistDocument>(transaction, documentDiskPath, LockOperation.Read); JObject jsonContent = JObject.Parse(persistDocument.Content); if (conditions.IsMatch(jsonContent)) { QueryRow rowValues = new QueryRow(); if (rowLimit > 0 && results.Rows.Count > rowLimit) { break; } if (hasFieldList) { if (jsonContent == null) { jsonContent = JObject.Parse(persistDocument.Content); } foreach (string fieldName in fieldList) { if (fieldName == "#RID") { rowValues.Add(persistDocument.Id.ToString()); } else { JToken fieldToken = null; if (jsonContent.TryGetValue(fieldName, StringComparison.CurrentCultureIgnoreCase, out fieldToken)) { rowValues.Add(fieldToken.ToString()); } else { rowValues.Add(string.Empty); } } } } else { //If no fields "*" was specified as the select list, just return the content of each document and some metadata. rowValues.Add(persistDocument.Id.ToString()); rowValues.Add(persistDocument.Created.ToString()); rowValues.Add(persistDocument.Modfied.ToString()); rowValues.Add(persistDocument.Content); } results.Rows.Add(rowValues); } } } else //Indexed search! { HashSet <Guid> intersectedDocumentIds = new HashSet <Guid>(); foreach (var indexSelectionGroup in indexSelectionGroups) { if (indexSelectionGroup.ConditionGroupType == ConditionType.None) { } else if (indexSelectionGroup.ConditionGroupType == ConditionType.And) { } else if (indexSelectionGroup.ConditionGroupType == ConditionType.Or) { } foreach (var indexSelection in indexSelectionGroup) { var indexPageCatalog = core.IO.GetPBuf <PersistIndexPageCatalog>(transaction, indexSelection.Index.DiskPath, LockOperation.Read); //var targetedIndexConditions = (from o in indexSelectionGroup.Conditions // .Where(o => indexSelection.HandledKeyNames.Contains(o.Key)) select o).ToList(); //Going to have to loop though all of the nested conditions. var foundDocuments = core.Indexes.MatchDocuments(indexPageCatalog, indexSelection); } } //Now that we have elimiated all but the document IDs that we care about, all we // have to do is open each of them up and pull the content requeted in the field list. if (intersectedDocumentIds.Count > 0) { var documentCatalog = core.IO.GetJson <PersistDocumentCatalog>(transaction, documentCatalogDiskPath, LockOperation.Read); foreach (var intersectedDocumentId in intersectedDocumentIds) { var documentMeta = documentCatalog.GetById(intersectedDocumentId); string documentDiskPath = Path.Combine(schemaMeta.DiskPath, Helpers.GetDocumentModFilePath(documentMeta.Id)); PersistDocument persistDocument = core.IO.GetJson <PersistDocument>(transaction, documentDiskPath, LockOperation.Read); JObject jsonContent = JObject.Parse(persistDocument.Content); QueryRow rowValues = new QueryRow(); if (rowLimit > 0 && results.Rows.Count > rowLimit) { break; } bool fullAttributeMatch = true; foreach (var indexSelectionGroup in indexSelectionGroups) { //If we have any conditions that were not indexed, open the remainder // of the documents and do additonal document-level filtering. if (indexSelectionGroup.UnhandledKeys?.Count > 0) { var unhandledConditions = (from o in indexSelectionGroup.Conditions where indexSelectionGroup.UnhandledKeys.Contains(o.Key) select o).ToList(); foreach (Condition condition in unhandledConditions) { JToken jToken = null; if (jsonContent.TryGetValue(condition.Key, StringComparison.CurrentCultureIgnoreCase, out jToken)) { if (condition.IsMatch(jToken.ToString().ToLower()) == false) { fullAttributeMatch = false; break; } } } } } if (fullAttributeMatch) { if (hasFieldList) { if (jsonContent == null) { jsonContent = JObject.Parse(persistDocument.Content); } foreach (string fieldName in fieldList) { if (fieldName == "#RID") { rowValues.Add(persistDocument.Id.ToString()); } else { JToken fieldToken = null; if (jsonContent.TryGetValue(fieldName, StringComparison.CurrentCultureIgnoreCase, out fieldToken)) { rowValues.Add(fieldToken.ToString()); } else { rowValues.Add(string.Empty); } } } } else { //If no fields "*" was specified as the select list, just return the content of each document and some metadata. rowValues.Add(persistDocument.Id.ToString()); rowValues.Add(persistDocument.Created.ToString()); rowValues.Add(persistDocument.Modfied.ToString()); rowValues.Add(persistDocument.Content); } results.Rows.Add(rowValues); } } } } return(results); } catch (Exception ex) { throw new LeafSQLExecutionException(ex.Message); } }