本文整理汇总了C#中RavenJArray.Add方法的典型用法代码示例。如果您正苦于以下问题:C# RavenJArray.Add方法的具体用法?C# RavenJArray.Add怎么用?C# RavenJArray.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RavenJArray
的用法示例。
在下文中一共展示了RavenJArray.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: OnPut
public override void OnPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
{
if (key.StartsWith("Raven/", StringComparison.OrdinalIgnoreCase) && // we don't deal with system documents
key.StartsWith("Raven/Hilo/", StringComparison.OrdinalIgnoreCase) == false) // except for hilos
return;
using (Database.DisableAllTriggersForCurrentThread())
{
var documentMetadata = GetDocumentMetadata(key);
if (documentMetadata != null)
{
RavenJArray history = new RavenJArray(ReplicationData.GetHistory(documentMetadata));
metadata[Constants.RavenReplicationHistory] = history;
if (documentMetadata.ContainsKey(Constants.RavenReplicationVersion) &&
documentMetadata.ContainsKey(Constants.RavenReplicationSource))
{
history.Add(new RavenJObject
{
{Constants.RavenReplicationVersion, documentMetadata[Constants.RavenReplicationVersion]},
{Constants.RavenReplicationSource, documentMetadata[Constants.RavenReplicationSource]}
});
}
while (history.Length > Constants.ChangeHistoryLength)
{
history.RemoveAt(0);
}
}
metadata[Constants.RavenReplicationVersion] = RavenJToken.FromObject(HiLo.NextId());
metadata[Constants.RavenReplicationSource] = RavenJToken.FromObject(Database.TransactionalStorage.Id);
}
}
示例2: OnPut
public override void OnPut(string key, Stream data, RavenJObject metadata)
{
if (key.StartsWith("Raven/")) // we don't deal with system attachment
return;
using (Database.DisableAllTriggersForCurrentThread())
{
var attachmentMetadata = GetAttachmentMetadata(key);
if (attachmentMetadata != null)
{
RavenJArray history = new RavenJArray(metadata.Value<RavenJArray>(Constants.RavenReplicationHistory));
metadata[Constants.RavenReplicationHistory] = history;
if (attachmentMetadata.ContainsKey(Constants.RavenReplicationVersion) &&
attachmentMetadata.ContainsKey(Constants.RavenReplicationSource))
{
history.Add(new RavenJObject
{
{Constants.RavenReplicationVersion, attachmentMetadata[Constants.RavenReplicationVersion]},
{Constants.RavenReplicationSource, attachmentMetadata[Constants.RavenReplicationSource]}
});
}
if (history.Length > Constants.ChangeHistoryLength)
{
history.RemoveAt(0);
}
}
metadata[Constants.RavenReplicationVersion] = RavenJToken.FromObject(HiLo.NextId());
metadata[Constants.RavenReplicationSource] = RavenJToken.FromObject(Database.TransactionalStorage.Id);
}
}
示例3: OnPut
public override void OnPut(string key, Stream data, RavenJObject metadata)
{
using (Database.DisableAllTriggersForCurrentThread())
{
metadata.Remove(Constants.RavenReplicationConflict);// you can't put conflicts
var oldVersion = Database.Attachments.GetStatic(key);
if (oldVersion == null)
return;
if (oldVersion.Metadata[Constants.RavenReplicationConflict] == null)
return;
var history = new RavenJArray(ReplicationData.GetHistory(metadata));
metadata[Constants.RavenReplicationHistory] = history;
var ravenJTokenEqualityComparer = new RavenJTokenEqualityComparer();
// this is a conflict document, holding document keys in the
// values of the properties
var conflictData = oldVersion.Data().ToJObject();
var conflicts = conflictData.Value<RavenJArray>("Conflicts");
if (conflicts == null)
return;
foreach (var prop in conflicts)
{
var id = prop.Value<string>();
Attachment attachment = Database.Attachments.GetStatic(id);
if(attachment == null)
continue;
Database.Attachments.DeleteStatic(id, null);
// add the conflict history to the mix, so we make sure that we mark that we resolved the conflict
var conflictHistory = new RavenJArray(ReplicationData.GetHistory(attachment.Metadata));
conflictHistory.Add(new RavenJObject
{
{Constants.RavenReplicationVersion, attachment.Metadata[Constants.RavenReplicationVersion]},
{Constants.RavenReplicationSource, attachment.Metadata[Constants.RavenReplicationSource]}
});
foreach (var item in conflictHistory)
{
if (history.Any(x => ravenJTokenEqualityComparer.Equals(x, item)))
continue;
history.Add(item);
}
}
}
}
示例4: OnPut
public override void OnPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
{
using (Database.DisableAllTriggersForCurrentThread())
{
metadata.Remove(Constants.RavenReplicationConflict);// you can't put conflicts
var oldVersion = Database.Get(key, transactionInformation);
if (oldVersion == null)
return;
if (oldVersion.Metadata[Constants.RavenReplicationConflict] == null)
return;
RavenJArray history = new RavenJArray(ReplicationData.GetHistory(metadata));
metadata[Constants.RavenReplicationHistory] = history;
var ravenJTokenEqualityComparer = new RavenJTokenEqualityComparer();
// this is a conflict document, holding document keys in the
// values of the properties
var conflicts = oldVersion.DataAsJson.Value<RavenJArray>("Conflicts");
if(conflicts == null)
return;
foreach (var prop in conflicts)
{
RavenJObject deletedMetadata;
Database.Delete(prop.Value<string>(), null, transactionInformation, out deletedMetadata);
// add the conflict history to the mix, so we make sure that we mark that we resolved the conflict
var conflictHistory = new RavenJArray(ReplicationData.GetHistory(deletedMetadata));
conflictHistory.Add(new RavenJObject
{
{Constants.RavenReplicationVersion, deletedMetadata[Constants.RavenReplicationVersion]},
{Constants.RavenReplicationSource, deletedMetadata[Constants.RavenReplicationSource]}
});
foreach (var item in conflictHistory)
{
if(history.Any(x=>ravenJTokenEqualityComparer.Equals(x, item)))
continue;
history.Add(item);
}
}
}
}
示例5: Execute
public override IEnumerable<Row> Execute(IEnumerable<Row> rows)
{
int count = 0;
foreach (var commentsForPosts in rows.Partition(Constants.BatchSize))
{
var cmds = new List<ICommandData>();
foreach (var commentsForPost in commentsForPosts.GroupBy(row => row["PostId"]))
{
var comments = new RavenJArray();
foreach (var row in commentsForPost)
{
comments.Add(new RavenJObject
{
{"Score", new RavenJValue(row["Score"])},
{"CreationDate", new RavenJValue(row["CreationDate"])},
{"Text", new RavenJValue(row["Text"])},
{"UserId", new RavenJValue(row["UserId"])}
});
}
cmds.Add(new PatchCommandData
{
Key = "posts/" + commentsForPost.Key,
Patches = new[]
{
new PatchRequest
{
Name = "Comments",
Type = PatchCommandType.Set,
Value = comments
},
}
});
}
count++;
WriteCommandsTo("Comments #" + count.ToString("00000") + ".json", cmds);
}
yield break;
}
示例6: PerformBulkOperation
private RavenJArray PerformBulkOperation(string index, IndexQuery indexQuery, bool allowStale, Func<string, TransactionInformation, object> batchOperation)
{
var array = new RavenJArray();
var bulkIndexQuery = new IndexQuery
{
Query = indexQuery.Query,
Start = indexQuery.Start,
Cutoff = indexQuery.Cutoff,
PageSize = int.MaxValue,
FieldsToFetch = new[] { Constants.DocumentIdFieldName },
SortedFields = indexQuery.SortedFields
};
bool stale;
var queryResults = database.QueryDocumentIds(index, bulkIndexQuery, out stale);
if (stale && allowStale == false)
{
throw new InvalidOperationException(
"Bulk operation cancelled because the index is stale and allowStale is false");
}
const int batchSize = 1024;
using (var enumerator = queryResults.GetEnumerator())
{
while (true)
{
var batchCount = 0;
database.TransactionalStorage.Batch(actions =>
{
while (batchCount < batchSize && enumerator.MoveNext())
{
batchCount++;
var result = batchOperation(enumerator.Current, transactionInformation);
array.Add(RavenJObject.FromObject(result, JsonExtensions.CreateDefaultJsonSerializer()));
}
});
if (batchCount < batchSize) break;
}
}
return array;
}
示例7: MinimizeToken
public static RavenJToken MinimizeToken(RavenJToken obj, int depth = 0)
{
switch (obj.Type)
{
case JTokenType.Array:
var array = new RavenJArray();
foreach (var item in ((RavenJArray)obj))
{
array.Add(MinimizeToken(item, depth + 1));
}
return array;
case JTokenType.Object:
var ravenJObject = ((RavenJObject)obj);
if (ravenJObject.ContainsKey(Constants.Metadata) == false)
{
// this might be a wrapper object, let check for first level arrays
if (depth == 0)
{
var newRootObj = new RavenJObject();
foreach (var prop in ravenJObject)
{
newRootObj[prop.Key] = prop.Value.Type == JTokenType.Array ?
MinimizeToken(prop.Value, depth + 1) :
prop.Value;
}
return newRootObj;
}
return obj;
}
var newObj = new RavenJObject();
newObj[Constants.Metadata] = ravenJObject[Constants.Metadata];
return newObj;
default:
return obj;
}
}
示例8: OnPut
public override void OnPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
{
using (Database.DisableAllTriggersForCurrentThread())
{
if (metadata.Remove(Constants.RavenReplicationConflictSkipResolution))
{
if (key.IndexOf("/conflicts/", StringComparison.OrdinalIgnoreCase) == -1)
{
metadata["@Http-Status-Code"] = 409;
metadata["@Http-Status-Description"] = "Conflict";
}
return;
}
metadata.Remove(Constants.RavenReplicationConflict);// you can't put conflicts
var oldVersion = Database.Documents.Get(key, transactionInformation);
if (oldVersion == null)
return;
if (oldVersion.Metadata[Constants.RavenReplicationConflict] == null)
return;
var history = new RavenJArray();
metadata[Constants.RavenReplicationHistory] = history;
// this is a conflict document, holding document keys in the
// values of the properties
var conflicts = oldVersion.DataAsJson.Value<RavenJArray>("Conflicts");
if(conflicts == null)
return;
var list = new List<RavenJArray>
{
new RavenJArray(ReplicationData.GetHistory(metadata)) // first item to interleave
};
foreach (var prop in conflicts)
{
RavenJObject deletedMetadata;
Database.Documents.Delete(prop.Value<string>(), null, transactionInformation, out deletedMetadata);
if (deletedMetadata != null)
{
var conflictHistory = new RavenJArray(ReplicationData.GetHistory(deletedMetadata));
conflictHistory.Add(new RavenJObject
{
{Constants.RavenReplicationVersion, deletedMetadata[Constants.RavenReplicationVersion]},
{Constants.RavenReplicationSource, deletedMetadata[Constants.RavenReplicationSource]}
});
list.Add(conflictHistory);
}
}
int index = 0;
bool added = true;
while (added) // interleave the history from all conflicts
{
added = false;
foreach (var deletedMetadata in list)
{
// add the conflict history to the mix, so we make sure that we mark that we resolved the conflict
if (index < deletedMetadata.Length)
{
history.Add(deletedMetadata[index]);
added = true;
}
}
index++;
}
while (history.Length > Constants.ChangeHistoryLength)
{
history.RemoveAt(0);
}
}
}
示例9: FlushBatch
private static void FlushBatch(string instanceUrl, List<RavenJObject> batch)
{
var sw = Stopwatch.StartNew();
long size;
using (var webClient = new WebClient())
{
webClient.Headers.Add("Content-Type", "application/json; charset=utf-8");
webClient.UseDefaultCredentials = true;
webClient.Credentials = CredentialCache.DefaultNetworkCredentials;
using (var stream = new MemoryStream())
{
using (var streamWriter = new StreamWriter(stream, Encoding.UTF8))
using (var jsonTextWriter = new JsonTextWriter(streamWriter))
{
var commands = new RavenJArray();
foreach (var doc in batch)
{
var metadata = doc.Value<RavenJObject>("@metadata");
doc.Remove("@metadata");
commands.Add(new RavenJObject
{
{"Method", "PUT"},
{"Document", doc},
{"Metadata", metadata},
{"Key", metadata.Value<string>("@id")}
});
}
commands.WriteTo(jsonTextWriter);
jsonTextWriter.Flush();
streamWriter.Flush();
stream.Flush();
size = stream.Length;
using (var netStream = webClient.OpenWrite(instanceUrl + "bulk_docs", "POST"))
{
stream.WriteTo(netStream);
netStream.Flush();
}
}
}
}
Console.WriteLine("Wrote {0} documents [{1:#,#} kb] in {2:#,#} ms",
batch.Count, Math.Round((double)size / 1024, 2), sw.ElapsedMilliseconds);
batch.Clear();
}
示例10: GetDocumentsWithIdStartingWith
public RavenJArray GetDocumentsWithIdStartingWith(string idPrefix, int start, int pageSize)
{
var list = new RavenJArray();
TransactionalStorage.Batch(actions =>
{
var documents = actions.Documents.GetDocumentsWithIdStartingWith(idPrefix, start)
.Take(pageSize);
var documentRetriever = new DocumentRetriever(actions, ReadTriggers);
foreach (var doc in documents)
{
DocumentRetriever.EnsureIdInMetadata(doc);
var document = documentRetriever
.ExecuteReadTriggers(doc, null, ReadOperation.Load);
if (document == null)
continue;
list.Add(document.ToJson());
}
});
return list;
}
示例11: GetDocuments
public RavenJArray GetDocuments(int start, int pageSize, Guid? etag)
{
var list = new RavenJArray();
TransactionalStorage.Batch(actions =>
{
IEnumerable<JsonDocument> documents;
if (etag == null)
documents = actions.Documents.GetDocumentsByReverseUpdateOrder(start);
else
documents = actions.Documents.GetDocumentsAfter(etag.Value);
var documentRetriever = new DocumentRetriever(actions, ReadTriggers);
foreach (var doc in documents.Take(pageSize))
{
DocumentRetriever.EnsureIdInMetadata(doc);
var document = documentRetriever
.ExecuteReadTriggers(doc, null, ReadOperation.Load);
if (document == null)
continue;
list.Add(document.ToJson());
}
});
return list;
}
示例12: GetDocumentsWithIdStartingWith
public RavenJArray GetDocumentsWithIdStartingWith(string idPrefix, string matches, int start, int pageSize)
{
if (idPrefix == null)
throw new ArgumentNullException("idPrefix");
idPrefix = idPrefix.Trim();
var list = new RavenJArray();
TransactionalStorage.Batch(actions =>
{
while (true)
{
int docCount = 0;
var documents = actions.Documents.GetDocumentsWithIdStartingWith(idPrefix, start, pageSize);
var documentRetriever = new DocumentRetriever(actions, ReadTriggers);
foreach (var doc in documents)
{
docCount++;
if (WildcardMatcher.Matches(matches, doc.Key.Substring(idPrefix.Length)) == false)
continue;
DocumentRetriever.EnsureIdInMetadata(doc);
var document = documentRetriever
.ExecuteReadTriggers(doc, null, ReadOperation.Load);
if (document == null)
continue;
list.Add(document.ToJson());
}
if (list.Length != 0 || docCount == 0)
break;
start += docCount;
}
});
return list;
}
示例13: ToRavenJToken
private RavenJToken ToRavenJToken(JsInstance v)
{
switch (v.Class)
{
case JsInstance.TYPE_OBJECT:
case JsInstance.CLASS_OBJECT:
return ToRavenJObject((JsObject) v);
case JsInstance.CLASS_DATE:
var dt = (DateTime) v.Value;
return new RavenJValue(dt);
case JsInstance.TYPE_NUMBER:
case JsInstance.CLASS_NUMBER:
var num = (double) v.Value;
KeyValuePair<RavenJValue, object> property;
if (propertiesByValue.TryGetValue(v, out property))
{
var originalValue = property.Key;
if (originalValue.Type == JTokenType.Float)
return new RavenJValue(num);
if (originalValue.Type == JTokenType.Integer)
{
// If the current value is exactly as the original value, we can return the original value before we made the JS conversion,
// which will convert a Int64 to jsFloat.
var originalJsValue = property.Value;
if (originalJsValue is double && Math.Abs(num - (double)originalJsValue) < double.Epsilon)
return originalValue;
return new RavenJValue((long) num);
}
}
// If we don't have the type, assume that if the number ending with ".0" it actually an integer.
var integer = Math.Truncate(num);
if (Math.Abs(num - integer) < double.Epsilon)
return new RavenJValue((long) integer);
return new RavenJValue(num);
case JsInstance.TYPE_STRING:
case JsInstance.CLASS_STRING:
{
const string ravenDataByteArrayToBase64 = "raven-data:byte[];base64,";
var value = v.Value as string;
if (value != null && value.StartsWith(ravenDataByteArrayToBase64))
{
value = value.Remove(0, ravenDataByteArrayToBase64.Length);
var byteArray = Convert.FromBase64String(value);
return new RavenJValue(byteArray);
}
return new RavenJValue(v.Value);
}
case JsInstance.TYPE_BOOLEAN:
case JsInstance.CLASS_BOOLEAN:
return new RavenJValue(v.Value);
case JsInstance.CLASS_NULL:
case JsInstance.TYPE_NULL:
return RavenJValue.Null;
case JsInstance.CLASS_UNDEFINED:
case JsInstance.TYPE_UNDEFINED:
return RavenJValue.Null;
case JsInstance.CLASS_ARRAY:
var jsArray = ((JsArray) v);
var rja = new RavenJArray();
for (int i = 0; i < jsArray.Length; i++)
{
var jsInstance = jsArray.get(i);
var ravenJToken = ToRavenJToken(jsInstance);
if (ravenJToken == null)
continue;
rja.Add(ravenJToken);
}
return rja;
case JsInstance.CLASS_REGEXP:
case JsInstance.CLASS_ERROR:
case JsInstance.CLASS_ARGUMENTS:
case JsInstance.CLASS_DESCRIPTOR:
case JsInstance.CLASS_FUNCTION:
return null;
default:
throw new NotSupportedException(v.Class);
}
}
示例14: ToRavenJToken
private RavenJToken ToRavenJToken(JsValue v, string propertyKey)
{
if (v.IsBoolean())
return new RavenJValue(v.AsBoolean());
if (v.IsString())
{
const string RavenDataByteArrayToBase64 = "raven-data:byte[];base64,";
var value = v.AsString();
if (value != null && value.StartsWith(RavenDataByteArrayToBase64))
{
value = value.Remove(0, RavenDataByteArrayToBase64.Length);
var byteArray = Convert.FromBase64String(value);
return new RavenJValue(byteArray);
}
return new RavenJValue(value);
}
if (v.IsNumber())
{
var num = v.AsNumber();
KeyValuePair<RavenJValue, JsValue> property;
if (propertiesByValue.TryGetValue(propertyKey, out property))
{
var originalValue = property.Key;
if (originalValue.Type == JTokenType.Float)
return new RavenJValue(num);
if (originalValue.Type == JTokenType.Integer)
{
// If the current value is exactly as the original value, we can return the original value before we made the JS conversion,
// which will convert a Int64 to jsFloat.
var originalJsValue = property.Value;
if (originalJsValue.IsNumber() && Math.Abs(num - originalJsValue.AsNumber()) < double.Epsilon)
return originalValue;
return new RavenJValue((long)num);
}
}
// If we don't have the type, assume that if the number ending with ".0" it actually an integer.
var integer = Math.Truncate(num);
if (Math.Abs(num - integer) < double.Epsilon)
return new RavenJValue((long)integer);
return new RavenJValue(num);
}
if (v.IsNull())
return RavenJValue.Null;
if (v.IsUndefined())
return RavenJValue.Null;
if (v.IsArray())
{
var jsArray = v.AsArray();
var rja = new RavenJArray();
foreach (var property in jsArray.Properties)
{
if (property.Key == "length")
continue;
var jsInstance = property.Value.Value;
if (!jsInstance.HasValue)
continue;
var ravenJToken = ToRavenJToken(jsInstance.Value, CreatePropertyKey(property.Key, propertyKey));
if (ravenJToken == null)
continue;
rja.Add(ravenJToken);
}
return rja;
}
if (v.IsObject())
return ToRavenJObject(v, propertyKey);
if (v.IsRegExp())
return null;
throw new NotSupportedException(v.Type.ToString());
}
示例15: GetTransformers
public async Task<RavenJArray> GetTransformers(RavenConnectionStringOptions src, int start)
{
if (isTransformersSupported() == false)
return new RavenJArray();
var transformers = await Store.AsyncDatabaseCommands.GetTransformersAsync(start, Options.BatchSize);
var result = new RavenJArray();
foreach (var transformer in transformers)
{
result.Add(new RavenJObject
{
{ "name", transformer.Name },
{ "definition", RavenJObject.FromObject(transformer) }
});
}
return result;
}