protected override bool TryResolve(string id, RavenJObject metadata, RavenJObject document, JsonDocument existingDoc,
		                                Func<string, JsonDocument> getDocument, out RavenJObject metadataToSave,
		                                out RavenJObject documentToSave)
		{
			var existingDocumentIsInConflict = existingDoc.Metadata[Constants.RavenReplicationConflict] != null;
			var existingDocumentIsDeleted = existingDoc.Metadata[Constants.RavenDeleteMarker] != null
			                                && existingDoc.Metadata[Constants.RavenDeleteMarker].Value<bool>();

			metadataToSave = null;
			documentToSave = null;

			if (existingDocumentIsInConflict && existingDocumentIsDeleted == false)
			{
				var conflictIds =
					existingDoc.DataAsJson.Value<RavenJArray>("Conflicts")
					           .Select(x => x.Value<string>())
					           .ToArray();

				if (conflictIds.Length == 0)
					return false;

				if (conflictIds
					    .Select(getDocument)
					    .Where(x => x != null)
						.All(doc => Historian.IsDirectChildOfCurrent(metadata, doc.Metadata)) == false)
					return false;

				metadataToSave = metadata;
				documentToSave = document;

				return true;
			}

			return false;
		}
Esempio n. 2
0
        public JsonDocument ProcessReadVetoes(JsonDocument document, TransactionInformation transactionInformation, ReadOperation operation)
        {
            if (document == null)
                return document;
            foreach (var readTrigger in triggers)
            {
                var readVetoResult = readTrigger.AllowRead(document.Key, document.DataAsJson ?? document.Projection, document.Metadata, operation, transactionInformation);
                switch (readVetoResult.Veto)
                {
                    case ReadVetoResult.ReadAllow.Allow:
                        break;
                    case ReadVetoResult.ReadAllow.Deny:
                        return new JsonDocument
                        {
                            DataAsJson = new JObject(),
                            Metadata = new JObject(
                                new JProperty("Raven-Read-Veto", new JObject(new JProperty("Reason", readVetoResult.Reason),
                                                                             new JProperty("Trigger", readTrigger.ToString())
                                                                    ))
                                )
                        };
                    case ReadVetoResult.ReadAllow.Ignore:
                        return null;
                    default:
                        throw new ArgumentOutOfRangeException(readVetoResult.Veto.ToString());
                }
            }

            return document;
        }
Esempio n. 3
0
        public DocumentViewModel(JsonDocument inner)
        {
            this.inner = inner;
            Id = inner.Metadata.IfPresent<string>("@id");
            LastModified = inner.LastModified ?? DateTime.MinValue;
			ClrType = inner.Metadata.IfPresent<string>(Raven.Abstractions.Data.Constants.RavenClrType);
            CollectionType = DetermineCollectionType();
        }
 public UpdateCascadeOperationExecutor(DocumentDatabase db, UpdateCascadeSetting setting, UpdateCascadeOperation operation, JsonDocument referencedDoc)
 {
     this.db = db;
     this.services = Services.GetServices(db);
     this.setting = setting;
     this.operation = operation;
     this.referencedDoc = referencedDoc;
 }
Esempio n. 5
0
        public JsonDocument ExecuteReadTriggers(JsonDocument document, TransactionInformation transactionInformation, ReadOperation operation)
        {
            if(disableReadTriggers.Value)
                return document;

            return ExecuteReadTriggersOnRead(ProcessReadVetoes(document, transactionInformation, operation),
                                             transactionInformation, operation);
        }
	    private static bool RemoveConstraintFromUniqueConstraintDocument(JsonDocument uniqueConstraintsDocument, string escapedUniqueValue)
        {
            if (!uniqueConstraintsDocument.DataAsJson.ContainsKey("Constraints"))
                return false;

            var constraints = (RavenJObject)uniqueConstraintsDocument.DataAsJson["Constraints"];

            return constraints.Remove(escapedUniqueValue);
        }
Esempio n. 7
0
        public void op_Add_JsonObject()
        {
            var expected = new JsonObject();

            var document = new JsonDocument
                               {
                                   expected
                               };

            var actual = document.First();

            Assert.Equal(expected, actual);
        }
Esempio n. 8
0
        public void opIndexer_int()
        {
            var expected = new JsonObject();

            var document = new JsonDocument
                               {
                                   expected
                               };

            var actual = document[0];

            Assert.Equal(expected, actual);
        }
Esempio n. 9
0
		public EditDocumentViewModel(IEventAggregator events, IKeyboardShortcutBinder keys)
		{
			metadata = new Dictionary<string, RavenJToken>();

			Id = "";
			document = new JsonDocument();
			JsonData = InitialJsonData();
			JsonMetadata = "{}";
			events.Subscribe(this);
			this.keys = keys;

			keys.Register<SaveDocument>(Key.S, ModifierKeys.Control, x => x.Execute(this), this);
		}
        private static bool ShouldRemoveUniqueConstraintDocument(JsonDocument uniqueConstraintsDocument)
        {
            if (!uniqueConstraintsDocument.DataAsJson.ContainsKey("Constraints"))
                return true;

            if (uniqueConstraintsDocument.DataAsJson.Keys.Count == 0)
                return true;

            var constraints = (RavenJObject)uniqueConstraintsDocument.DataAsJson["Constraints"];

            if (constraints.Keys.Count == 0)
                return true;

            return false;
        }
Esempio n. 11
0
		public void GetDocumentAfterAnEtagWhileAddingDocsFromMultipleThreadsEnumeratesAllDocs()
		{
			var numberOfDocsAdded = 0;
			var threads = new List<Thread>();
			try
			{
				for (var i = 0; i < 10; i++)
				{
                    var thread = new Thread(() =>
                    {
                    	var cmds = new List<ICommandData>();
                         for (var k = 0; k < 100; k++)
                         {
							var newId = Interlocked.Increment(ref numberOfDocsAdded);
                            cmds.Add(new PutCommandData
                            {
                            	Document = new RavenJObject(),
								Metadata = new RavenJObject(),
								Key = newId.ToString()
                            });
                        };
                    	db.Batch(cmds);
                    });
					threads.Add(thread);
					thread.Start();
				}

				var docs = new List<string>();
				var lastEtag = Guid.Empty;
				var total = 0;
			    var stop = false;
			    do
			    {
					var etag = lastEtag;
			        var jsonDocuments = new JsonDocument[0];
			        db.TransactionalStorage.Batch(actions =>
			        {
			            jsonDocuments = actions.Documents.GetDocumentsAfter(etag).Where(x => x != null).ToArray();
			        });
					docs.AddRange(jsonDocuments.Select(x=>x.Key));
			        total += jsonDocuments.Length;
			    	if (jsonDocuments.Length > 0)
			    		lastEtag = jsonDocuments.Last().Etag.Value;
			    	if (stop)
                        break;
                    if (threads.All(x => !x.IsAlive))
                        stop = true;
			    } while (true);

                Assert.Equal(numberOfDocsAdded, total);
			}
			finally
			{
				foreach (var thread in threads)
				{
					thread.Join();
				}
			}
		}
Esempio n. 12
0
        public void op_WriteJson_JsonWriter_whenEmptyObject(string expected)
        {
            var document = new JsonDocument
                               {
                                   new JsonObject()
                               };

            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream))
                {
                    document.WriteJson(writer);
                }

                using (var reader = new StreamReader(stream))
                {
                    var actual = reader.ReadToEnd();

                    Assert.Equal(expected, actual);
                }
            }
        }
Esempio n. 13
0
		public IEnumerable<Tuple<JsonDocument, int>> DocumentsById(int startId, int endId)
		{
			Api.JetSetCurrentIndex(session, Documents, "by_id");
			Api.MakeKey(session, Documents, startId, MakeKeyGrbit.NewKey);
			// this sholdn't really happen, it means that the doc is missing
			// probably deleted before we can get it?
			if (Api.TrySeek(session, Documents, SeekGrbit.SeekGE) == false)
			{
				logger.DebugFormat("Document with id {0} or higher was not found", startId);
				yield break;
			}
			do
			{
				var id = Api.RetrieveColumnAsInt32(session, Documents, tableColumnsCache.DocumentsColumns["id"],
												   RetrieveColumnGrbit.RetrieveFromIndex).Value;
				if (id > endId)
					break;

				var data = Api.RetrieveColumn(session, Documents, tableColumnsCache.DocumentsColumns["data"]);
				logger.DebugFormat("Document with id '{0}' was found, doc length: {1}", id, data.Length);
				var etag = new Guid(Api.RetrieveColumn(session, Documents, tableColumnsCache.DocumentsColumns["etag"]));
				var metadata = Api.RetrieveColumn(session, Documents, tableColumnsCache.DocumentsColumns["metadata"]).ToJObject();
				var key = Api.RetrieveColumnAsString(session, Documents, tableColumnsCache.DocumentsColumns["key"], Encoding.Unicode);

				data = documentCodecs.Aggregate(data, (bytes, codec) => codec.Decode(key, metadata, bytes));

				var doc = new JsonDocument
				{
					Key = key,
					DataAsJson = data.ToJObject(),
					Etag = etag,
					Metadata = metadata
				};
				yield return new Tuple<JsonDocument, int>(doc, id);
			} while (Api.TryMoveNext(session, Documents));
		}
 private bool UpdateDocumentWithProgressReport(JsonDocument referencingDocument, ReferencingCollectionOperation collectionOperation)
 {
     if (UpdateDocument(referencingDocument))
     {
         collectionOperation.UpdatedDocumentCount++;
         if (collectionOperation.UpdatedDocumentCount % 200 == 0)
         {
             log.Trace("Updating progress for operation {0}. {1} {2} documents have been updated so far", operation.Id, collectionOperation.UpdatedDocumentCount, collectionOperation.ReferencingEntityName);
             SaveOperationSilentlyIgnoringError(); // updating progress is not so important
         }
         return true;
     }
     else
     {
         return false;
     }
 }
Esempio n. 15
0
        public void prop_Count_get()
        {
            var document = new JsonDocument();
            Assert.Equal(0, document.Count);

            document.Add(new JsonObject());

            Assert.Equal(1, document.Count);
        }
Esempio n. 16
0
        private static async Task <JsonDocument> GetJsonDocumentAsync(this HttpWebRequest request, CancellationToken token = default)
        {
            using WebResponse response = await request.GetResponseAsync().IgnoreNonSuccess();

            using Stream responseStream = response.GetResponseStream();
            return(await JsonDocument.ParseAsync(responseStream, default, token));
        private bool UpdateDocument(JsonDocument referencingDocument)
        {
            var referencingEntityName = referencingDocument.Metadata.Value<string>(Constants.RavenEntityName);
            ReferencingCollectionSetting referencingCollectionSetting = null;
            if (!this.setting.ReferencingCollections.TryGetValue(referencingEntityName, out referencingCollectionSetting))
            {
                log.Debug("{0} document doesn't need to be cascade updated because it doesn't belong to any document collection that hold denormalized references to {1}. Operation {2} ", referencingDocument.Key, setting.ReferencedEntityName, operation.Id);
                return false;
            }

            var denormalizedReferences = referencingDocument.DataAsJson.GetObjectsAtPath(referencingCollectionSetting.ReferencingPropertyPath);
            bool shouldUpdate = false;
            foreach (var reference in denormalizedReferences)
            {
                Guid? referencedEtag = reference.Value<Guid?>("Etag");
                var referencedDocId = reference.Value<string>("Id");

                if (referencedDocId == referencedDoc.Key && (referencedEtag == null ||
                    Buffers.Compare(referencedEtag.Value.ToByteArray(), referencedDoc.Etag.Value.ToByteArray()) < 0))
                {
                    shouldUpdate = true;
                    foreach (var property in setting.DenormalizedReferencePropertyNames)
                    {
                        reference[property] = referencedDoc.DataAsJson[property].CloneToken();
                    }
                    reference["Etag"] = RavenJToken.FromObject(referencedDoc.Etag.Value);
                }
            }
            if (shouldUpdate)
            {
                log.Debug("{0} document has been cascade updated in memory beacause it references {1} document and its referencing Etag is prior to the referenced document one {2}", referencingDocument.Key, referencedDoc.Key, referencedDoc.Etag);
            }
            else
            {
                log.Debug("{0} document has not been cascade updated in memory beacause it does not references {1} document or its referencing Etag is subsequent to the referenced document one {2}", referencingDocument.Key, referencedDoc.Key, referencedDoc.Etag);
            }
            return shouldUpdate;
        }
        private static bool IsDirectChildOfCurrentDocument(JsonDocument existingDoc, RavenJObject metadata)
        {
			var version = new RavenJObject
        	{
        		{ReplicationConstants.RavenReplicationSource, existingDoc.Metadata[ReplicationConstants.RavenReplicationSource]},
        		{ReplicationConstants.RavenReplicationVersion, existingDoc.Metadata[ReplicationConstants.RavenReplicationVersion]},
        	};

			var history = metadata[ReplicationConstants.RavenReplicationHistory];
			if (history == null) // no history, not a parent
				return false;

			return history.Values().Contains(version, new RavenJTokenEqualityComparer());
        }
Esempio n. 19
0
        public IEnumerable<Tuple<JsonDocument, int>> DocumentsById(Reference<bool> hasMoreWork, int startId, int endId,
            int limit)
        {
            Api.JetSetCurrentIndex(session, Documents, "by_id");
            Api.MakeKey(session, Documents, startId, MakeKeyGrbit.NewKey);
            // this sholdn't really happen, it means that the doc is missing
            // probably deleted before we can get it?
            if (Api.TrySeek(session, Documents, SeekGrbit.SeekGE) == false)
            {
                logger.DebugFormat("Document with id {0} or higher was not found", startId);
                yield break;
            }
            var count = 0;
            do
            {
                if ((++count) > limit)
                {
                    hasMoreWork.Value = true;
                    yield break;
                }
                var id = Api.RetrieveColumnAsInt32(session, Documents, documentsColumns["id"],
                                                   RetrieveColumnGrbit.RetrieveFromIndex).Value;
                if (id > endId)
                    break;

                var data = Api.RetrieveColumn(session, Documents, documentsColumns["data"]);
                logger.DebugFormat("Document with id '{0}' was found, doc length: {1}", id, data.Length);
                var json = Api.RetrieveColumnAsString(session, Documents, documentsColumns["metadata"],
                                                      Encoding.Unicode);
                var doc = new JsonDocument
                {
                    Key = Api.RetrieveColumnAsString(session, Documents, documentsColumns["key"], Encoding.Unicode),
                    Data = data,
                    Etag = new Guid(Api.RetrieveColumn(session, Documents, documentsColumns["etag"])),
                    Metadata = JObject.Parse(json)
                };
                yield return new Tuple<JsonDocument, int>(doc, id);
            } while (Api.TryMoveNext(session, Documents));
            hasMoreWork.Value = false;
        }
Esempio n. 20
0
	    private long GetMaxFromDocument(JsonDocument document, long minMax)
		{
			long max;
			if (document.DataAsJson.ContainsKey("ServerHi")) // convert from hi to max
			{
				var hi = document.DataAsJson.Value<long>("ServerHi");
				max = ((hi - 1) * capacity);
				document.DataAsJson.Remove("ServerHi");
				document.DataAsJson["Max"] = max;
			}
			max = document.DataAsJson.Value<long>("Max");
			return Math.Max(max, minMax);
		}
Esempio n. 21
0
        public void op_WriteJson_JsonWriter_whenArray(string expected)
        {
            var document = new JsonDocument
                               {
                                   new JsonObject
                                       {
                                           new JsonPair("one", new JsonNumber("1"))
                                       },
                                   new JsonObject
                                       {
                                           new JsonPair("two", new JsonNumber("2"))
                                       }
                               };

            using (var stream = new MemoryStream())
            {
                using (var writer = new JsonWriter(stream))
                {
                    document.WriteJson(writer);
                }

                using (var reader = new StreamReader(stream))
                {
                    var actual = reader.ReadToEnd();

                    Assert.Equal(expected, actual);
                }
            }
        }
 private static bool IsDirectChildOfCurrentDocument(JsonDocument existingDoc, RavenJObject metadata)
 {
     return RavenJToken.DeepEquals(existingDoc.Metadata[ReplicationConstants.RavenReplicationVersion],
                              metadata[ReplicationConstants.RavenReplicationParentVersion]) && 
            RavenJToken.DeepEquals(existingDoc.Metadata[ReplicationConstants.RavenReplicationSource],
                              metadata[ReplicationConstants.RavenReplicationParentSource]);
 }
        public bool TryStartOperation(UpdateCascadeOperation operation, JsonDocument referencedDoc)
        {
            var services = Services.GetServices(this.db);

            if (services.IsShutDownInProgress)
            {
                log.Warn("Tried to start operation {0} while shuting down", operation.Id);
                return false;
            }

            RunningOperation ro = null;
            UpdateCascadeSetting setting;
            if (!services.SettingsCache.TryGetValue(operation.UpdateCascadeSettingId, out setting))
            {
                log.Error("Tried to add and run the operation {0}. But there is no corresponding setting {1}", operation.Id, operation.UpdateCascadeSettingId);
                return false;
            }

            lock (runningOperations)
            {
                if (runningOperations.TryGetValue(operation.ReferencedDocId, out ro))
                {
                    // the operation might be already here. This shouldn't occur
                    if (operation.Id == ro.Operation.Id)
                    {
                        log.Warn("Tried to start an operation that is already started. Operation Id = {0}", operation.Id);
                        return false;
                    }
                    // the operation might refer to an older entity. This is unprobable, I think
                    if (Buffers.Compare(operation.ReferencedDocEtag.ToByteArray(), ro.Operation.ReferencedDocEtag.ToByteArray()) < 0)
                    {
                        log.Warn("Tried to start an operation that refers to an entity which is older than the referenced by a running operation. Older operation id: {0}, existing operation id: {1}", operation.Id, ro.Operation.Id);
                        return false;
                    }

                    log.Warn("The same referenced entity {0} has been updated while a previous update cascade operation of that entity is in progress, that might indicate that the document is updated so often that referencing entities cannot be updated at time. Update cascade bundle is not recomended in this scenario", operation.ReferencedDocId);

                    // the same referenced entity has been updated while a previous update cascade operation of that entity is in progress
                    // we need to cancel that operation and span a new one.
                    var tokenSource = ro.TokenSource;
                    if (tokenSource != null) tokenSource.Cancel();
                    try
                    {
                        var task = ro.ExecutorTask;
                        if (task!= null) task.Wait();
                    }
                    catch (AggregateException ex)
                    {
                        ex.Handle(x => x is TaskCanceledException);
                    }
                }
                var runningOperation = new RunningOperation
                {
                    Operation = operation,
                    TokenSource =  new CancellationTokenSource(),
                    Executor = new UpdateCascadeOperationExecutor(db, setting, operation, referencedDoc),
                };
                runningOperations[operation.ReferencedDocId] = runningOperation;
                log.Trace("Starting operation: {0}", operation.Id);
                runningOperation.ExecutorTask = runningOperation.Executor.ExecuteAsync(runningOperation.TokenSource.Token);
                runningOperation.ExecutorTask.ContinueWith(t =>
                {
                    if (!services.IsShutDownInProgress)
                    {
                        lock (runningOperations)
                        {
                            RunningOperation rop;
                            if (runningOperations.TryGetValue(operation.ReferencedDocId, out rop))
                            {
                                if (rop.Operation.Id == operation.Id)
                                {
                                    runningOperations.Remove(operation.ReferencedDocId);
                                }
                            }
                            t.Dispose();
                        }
                    }

                }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);

                return true;
            }
        }
Esempio n. 24
0
        public void UpsertAssets(string p_targetDb) // DEV1 or DEV2
        {
            // AllAssets gSheet location: https://docs.google.com/spreadsheets/d/1gkZlvD5epmBV8zi-l0BbLaEtwScvhHvHOYFZ4Ah_MA4/edit#gid=898941432
            string gApiKey = Utils.Configuration["Google:GoogleApiKeyKey"];

            if (String.IsNullOrEmpty(Utils.Configuration["Google:GoogleApiKeyKey"]))
            {
                throw new SqException("GoogleApiKeyKey is missing.");
            }

            string range          = "BaseAssets"; // gets all data from the tab-page. range = "BaseAssets!A:A" is also possible
            string?baseAssetsJson = Utils.DownloadStringWithRetryAsync($"https://sheets.googleapis.com/v4/spreadsheets/1gkZlvD5epmBV8zi-l0BbLaEtwScvhHvHOYFZ4Ah_MA4/values/{range}?key={gApiKey}").TurnAsyncToSyncTask();

            range = "StockAssets";
            string?stockAssetsJson = Utils.DownloadStringWithRetryAsync($"https://sheets.googleapis.com/v4/spreadsheets/1gkZlvD5epmBV8zi-l0BbLaEtwScvhHvHOYFZ4Ah_MA4/values/{range}?key={gApiKey}").TurnAsyncToSyncTask();

            range = "CompanyAssets";
            string?companyAssetsJson = Utils.DownloadStringWithRetryAsync($"https://sheets.googleapis.com/v4/spreadsheets/1gkZlvD5epmBV8zi-l0BbLaEtwScvhHvHOYFZ4Ah_MA4/values/{range}?key={gApiKey}").TurnAsyncToSyncTask();

            if (baseAssetsJson == null || stockAssetsJson == null || companyAssetsJson == null)
            {
                throw new SqException("DownloadStringWithRetryAsync() failed.");
            }

            StringBuilder sbCash       = new StringBuilder("\"C\":[\n");
            bool          isFirstCash  = true;
            StringBuilder sbCpair      = new StringBuilder("\"D\":[\n");
            bool          isFirstCpair = true;
            StringBuilder sbReEst      = new StringBuilder("\"R\":[\n");
            bool          isFirstReEst = true;
            StringBuilder sbNav        = new StringBuilder("\"N\":[\n");
            bool          isFirstNav   = true;
            StringBuilder sbPortf      = new StringBuilder("\"P\":[\n");
            bool          isFirstPortf = true;

            StringBuilder sbComp       = new StringBuilder("\"A\":[\n"); // companies should come first, because stocks refer to companies
            bool          isFirstComp  = true;
            StringBuilder sbStock      = new StringBuilder("\"S\":[\n");
            bool          isFirstStock = true;


            // https://marcroussy.com/2020/08/17/deserialization-with-system-text-json/     // POCO: Plain Old Class Object
            using (JsonDocument baseDoc = JsonDocument.Parse(baseAssetsJson))
            {
                JsonElement baseValues      = baseDoc.RootElement.GetProperty("values");
                bool        wasHeaderParsed = false;
                foreach (JsonElement row in baseValues.EnumerateArray())
                {
                    if (!wasHeaderParsed)
                    {
                        wasHeaderParsed = true;
                    }
                    else
                    {
                        JsonElement[] rowArr = row.EnumerateArray().ToArray();
                        if (rowArr.Length == 0)
                        {
                            continue;                    // skip empty gSheet rows in JSON: "[],"
                        }
                        if (rowArr[0].ToString() == "C") // CurrencyCash
                        {
                            if (isFirstCash)
                            {
                                isFirstCash = false;
                            }
                            else
                            {
                                sbCash.Append(",");
                            }
                            sbCash.Append($"[{rowArr[1]},\"{rowArr[2]}\",\"{rowArr[3]}\",\"{rowArr[4]}\",\"{rowArr[5]}\"]");
                        }
                        if (rowArr[0].ToString() == "D")    // CurrencyPair
                        {
                            if (isFirstCpair)
                            {
                                isFirstCpair = false;
                            }
                            else
                            {
                                sbCpair.Append(",");
                            }
                            sbCpair.Append($"[{rowArr[1]},\"{rowArr[2]}\",\"{rowArr[3]}\",\"{rowArr[4]}\",\"{rowArr[5]}\",\"{rowArr[7]}\"]");
                        }
                        if (rowArr[0].ToString() == "R")    // RealEstate
                        {
                            if (isFirstReEst)
                            {
                                isFirstReEst = false;
                            }
                            else
                            {
                                sbReEst.Append(",");
                            }
                            sbReEst.Append($"[{rowArr[1]},\"{rowArr[2]}\",\"{rowArr[3]}\",\"{rowArr[4]}\",\"{rowArr[5]}\",\"{rowArr[8]}\"]");
                        }
                        if (rowArr[0].ToString() == "N")    // BrokerNav
                        {
                            if (isFirstNav)
                            {
                                isFirstNav = false;
                            }
                            else
                            {
                                sbNav.Append(",");
                            }
                            sbNav.Append($"[{rowArr[1]},\"{rowArr[2]}\",\"{rowArr[3]}\",\"{rowArr[4]}\",\"{rowArr[5]}\",\"{rowArr[8]}\"]");
                        }
                        if (rowArr[0].ToString() == "P")    // Portfolio
                        {
                            if (isFirstPortf)
                            {
                                isFirstPortf = false;
                            }
                            else
                            {
                                sbPortf.Append(",");
                            }
                            sbPortf.Append($"[{rowArr[1]},\"{rowArr[2]}\",\"{rowArr[3]}\",\"{rowArr[4]}\",\"{rowArr[5]}\",\"{rowArr[8]}\"]");
                        }
                    }
                }

                using (JsonDocument companyDoc = JsonDocument.Parse(companyAssetsJson))
                {
                    wasHeaderParsed = false;
                    foreach (JsonElement row in companyDoc.RootElement.GetProperty("values").EnumerateArray())
                    {
                        if (!wasHeaderParsed)
                        {
                            wasHeaderParsed = true;
                        }
                        else
                        {
                            JsonElement[] rowArr = row.EnumerateArray().ToArray();
                            if (rowArr.Length == 0)
                            {
                                continue;                    // skip empty gSheet rows in JSON: "[],"
                            }
                            if (rowArr[0].ToString() == "A") // Stock
                            {
                                if (isFirstComp)
                                {
                                    isFirstComp = false;
                                }
                                else
                                {
                                    sbComp.Append(",");
                                }
                                sbComp.Append($"[{rowArr[1]},\"{rowArr[2]}\",\"{rowArr[3]}\",\"{Get(rowArr, 4)}\",\"{Get(rowArr, 5)}\",\"{Get(rowArr, 7)}\",\"{Get(rowArr, 8)}\",\"{Get(rowArr, 9)}\",\"{Get(rowArr, 10)}\",\"{Get(rowArr, 11)}\"]");
                            }
                        }
                    }
                }

                using (JsonDocument stockDoc = JsonDocument.Parse(stockAssetsJson))
                {
                    wasHeaderParsed = false;
                    foreach (JsonElement row in stockDoc.RootElement.GetProperty("values").EnumerateArray())
                    {
                        if (!wasHeaderParsed)
                        {
                            wasHeaderParsed = true;
                        }
                        else
                        {
                            JsonElement[] rowArr = row.EnumerateArray().ToArray();
                            if (rowArr.Length == 0)
                            {
                                continue;                    // skip empty gSheet rows in JSON: "[],"
                            }
                            if (rowArr[0].ToString() == "S") // Stock
                            {
                                if (isFirstStock)
                                {
                                    isFirstStock = false;
                                }
                                else
                                {
                                    sbStock.Append(",");
                                }
                                sbStock.Append($"[{rowArr[1]},\"{rowArr[2]}\",\"{rowArr[3]}\",\"{Get(rowArr, 4)}\",\"{Get(rowArr, 5)}\",\"{Get(rowArr, 7)}\",\"{Get(rowArr, 8)}\",\"{Get(rowArr, 9)}\",\"{Get(rowArr, 10)}\",\"{Get(rowArr, 11)}\",\"{Get(rowArr, 12)}\",\"{Get(rowArr, 13)}\",\"{Get(rowArr, 14)}\",\"{Get(rowArr, 15)}\",\"{Get(rowArr, 16)}\"]");
                            }
                        }
                    }
                }

                sbCash.Append("],\n");
                sbCpair.Append("],\n");
                sbReEst.Append("],\n");
                sbNav.Append("],\n");
                sbPortf.Append("],\n");
                sbComp.Append("],\n");
                sbStock.Append("]");
                StringBuilder sb = new StringBuilder("{");
                sb.Append(sbCash).Append(sbCpair).Append(sbReEst).Append(sbNav).Append(sbPortf).Append(sbComp).Append(sbStock).Append("}");
                m_redisDb.HashSet("memDb", "Assets", new RedisValue(sb.ToString()));

                // At the moment, the NAV's and StockAsset table's "Srv.LoadPrHist(Span)" column is not mirrored from gSheet to RedisDb automatically.
                // We add these manually to RedisDb now. Not bad, because adding them manually forces us to think about whether we really need that extra stock consuming RAM and YF downloads.
            }
        }
Esempio n. 25
0
		public void Initialize(JsonDocument doc)
		{
			document = doc;

			UpdateDocumentFromJsonDocument();
		}
Esempio n. 26
0
        public void op_GetEnumerator()
        {
            var expected = new JsonObject();

            IEnumerable document = new JsonDocument
                                       {
                                           expected
                                       };

            foreach (var actual in document)
            {
                Assert.Equal(expected, actual);
            }
        }
Esempio n. 27
0
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(JsonDocument obj)
 {
     return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
 }
Esempio n. 28
0
        private JsonDocument ExecuteReadTriggersOnRead(JsonDocument resultingDocument, TransactionInformation transactionInformation, ReadOperation operation)
        {
            if (resultingDocument == null)
                return null;

            foreach (var readTrigger in triggers)
            {
                readTrigger.OnRead(resultingDocument.Key, resultingDocument.DataAsJson, resultingDocument.Metadata, operation, transactionInformation);
            }
            return resultingDocument;
        }
Esempio n. 29
0
		public JsonDocument GetWithTransformer(string key, string transformer, TransactionInformation transactionInformation, Dictionary<string, RavenJToken> queryInputs)
		{
			JsonDocument result = null;
			TransactionalStorage.Batch(
			actions =>
			{
				var docRetriever = new DocumentRetriever(actions, ReadTriggers, queryInputs);
				using (new CurrentTransformationScope(docRetriever))
				{
					var document = Get(key, transactionInformation);
					if (document == null)
						return;

					var storedTransformer = IndexDefinitionStorage.GetTransfomer(transformer);
					if (storedTransformer == null)
						throw new InvalidOperationException("No transfomer with the name: " + transformer);

					var transformed = storedTransformer.TransformResultsDefinition(new[] { new DynamicJsonObject(document.ToJson()) })
									 .Select(x => JsonExtensions.ToJObject(x))
									 .ToArray();

					if (transformed.Length == 0)
						return;

					result = new JsonDocument
					{
						Etag = document.Etag,
						NonAuthoritativeInformation = document.NonAuthoritativeInformation,
						LastModified = document.LastModified,
					};
					result.DataAsJson = new RavenJObject { { "$values", new RavenJArray(transformed) } };
				}
			});
			return result;
		}
Esempio n. 30
0
		public bool FilterDocuments(JsonDocument document, string destinationId)
		{
			if (document.Key.StartsWith("Raven/", StringComparison.InvariantCultureIgnoreCase)) // don't replicate system docs
			{
				if (document.Key.StartsWith("Raven/Hilo/", StringComparison.InvariantCultureIgnoreCase) == false) // except for hilo documents
					return false;
			}
			if (document.Metadata.ContainsKey(Constants.NotForReplication) && document.Metadata.Value<bool>(Constants.NotForReplication)) // not explicitly marked to skip
				return false;
			if (document.Metadata[ReplicationConstants.RavenReplicationConflict] != null) // don't replicate conflicted documents, that just propagate the conflict
				return false;

			if (document.Metadata.Value<string>(ReplicationConstants.RavenReplicationSource) == destinationId) // prevent replicating back to source
				return false;

			switch (ReplicationOptionsBehavior)
			{
				case TransitiveReplicationOptions.None:
					var value = document.Metadata.Value<string>(ReplicationConstants.RavenReplicationSource);
					var replicateDoc = value == null || (value == CurrentDatabaseId);
					return replicateDoc;
			}
			return true;

		}
Esempio n. 31
0
		public bool FilterDocuments(JsonDocument document)
		{
			if(document.Key.StartsWith("Raven/") ) // don't replicate system docs
				return false;
			if(document.Metadata.ContainsKey(Constants.NotForReplication) && document.Metadata.Value<bool>(Constants.NotForReplication) ) // not explicitly marked to skip
				return false;
			if (document.Metadata[ReplicationConstants.RavenReplicationConflict] != null) // don't replicate conflicted documents, that just propgate the conflict
				return false;

			switch (ReplicationOptionsBehavior)
			{
				case TransitiveReplicationOptions.None:
					return document.Metadata.Value<string>(ReplicationConstants.RavenReplicationSource) == null ||
						(document.Metadata.Value<string>(ReplicationConstants.RavenReplicationSource) == CurrentDatabaseId);
			}
			return true;

		}
Esempio n. 32
0
        public void op_ToString(string expected)
        {
            var document = new JsonDocument
                               {
                                   new JsonObject
                                       {
                                           new JsonPair("one", new JsonNumber("1"))
                                       },
                                   new JsonObject
                                       {
                                           new JsonPair("two", new JsonNumber("2"))
                                       }
                               };

            var actual = document.ToString();

            Assert.Equal(expected, actual);
        }