public void LookupIn_MultiCommands_ReturnsCorrectCount() { var key = "LookupIn_MultiCommands_ReturnsCorrectCount"; _bucket.Upsert(key, new { foo = "bar", bar = "foo" }); var builder = _bucket.LookupIn <dynamic>(key).Get("foo").Get("bar"); var result = (DocumentFragment <dynamic>)builder.Execute(); Assert.AreEqual(2, result.Value.Count); }
/// <summary> /// Determines whether the <see cref="T:Couchbase.Collections.CouchbaseDictionary`2" /> contains an element with the specified key. /// </summary> /// <param name="key">The key to locate in the <see cref="T:Couchbase.Collections.CouchbaseDictionary`2" />.</param> /// <returns> /// true if the <see cref="T:Couchbase.Collections.CouchbaseDictionary`2" /> contains an element with the key; otherwise, false. /// </returns> public bool ContainsKey(TKey key) { var exists = Bucket.LookupIn <IDictionary <TKey, TValue> >(Key). Exists(key.ToString()). Execute(); if (!exists.Success) { if (exists.Exception != null) { throw exists.Exception; } } return(exists.Success); }
public static void LookupInChaining(IBucket bucket, string id) { // tag::LookupInExample[] var builder = bucket.LookupIn <dynamic>(id). Get("type"). Get("name"). Get("owner"). Exists("notfound"); // end::LookupInExample[] // tag::LookupInExampleExecute[] var fragment = builder.Execute(); // end::LookupInExampleExecute[] // tag::LookupInExampleResult[] if (fragment.OpStatus("type") == ResponseStatus.Success) { string format = "Path='{0}' Value='{1}'"; Console.WriteLine(format, "type", fragment.Content("type")); } // end::LookupInExampleResult[] GetDisplay(fragment, "type"); GetDisplay(fragment, "name"); GetDisplay(fragment, "owner"); }
public static void LookupInChaining(IBucket bucket, string id) { var builder = bucket.LookupIn <dynamic>(id). Get("type"). Get("name"). Get("owner"). Exists("notfound"); dynamic fragment = builder.Execute(); try { if (fragment.OpStatus("type") == ResponseStatus.Success) { string format = "Path='{0}' Value='{1}'"; Console.WriteLine(format, "type", fragment.Content("type")); GetDisplay(fragment, "type"); GetDisplay(fragment, "name"); GetDisplay(fragment, "owner"); } } catch (Exception e) { Console.WriteLine(e); } }
// end::GetExample[] // tag::ExistsExample[] public static void ExistsExample(IBucket bucket, string path, string id) { var builder = bucket.LookupIn <dynamic>(id). Exists(path). Execute(); var found = builder.Content(path); Console.WriteLine(found); }
// tag::GetExample[] public static void GetExample(IBucket bucket, string path, string id) { var builder = bucket.LookupIn <dynamic>(id). Get(path). Execute(); var fragment = builder.Content(path); Console.WriteLine(fragment); }
public static void ErrorExample(IBucket bucket, string id) { var builder = bucket.LookupIn(id). Get("type"). Get("somepaththatdoesntexist"). Get("owner"); var fragment = builder.Execute <dynamic>(); Console.WriteLine("Generic error: {0}{1}Specific Error: {2}", fragment.Status, Environment.NewLine, fragment.OpStatus(1)); Console.WriteLine("Generic error: {0}{1}Specific Error: {2}", fragment.Status, Environment.NewLine, fragment.OpStatus("somepaththatdoesntexist")); }
public override string GetJobParameter(string id, string name) { if (id == null) { throw new ArgumentNullException(nameof(id)); } if (name == null) { throw new ArgumentNullException(nameof(name)); } IDocumentFragment <Documents.Job> result = bucket.LookupIn <Documents.Job>(id) .Get($"parameters.{name}") .Execute(); return(result.Success ? result.Content($"parameters.{name}").ToString() : null); }
public async Task <ITransactionDocument <T> > Get <T>(IBucket bucket, string key) { if (TryGetStagedWrite(key, out var doc)) { // document has been mutated within transaction, return this version return(await Task.FromResult((ITransactionDocument <T>) doc).ConfigureAwait(false)); } if (TryGetStagedRemove(key)) { // document has been removed within transaction, return nothing return(null); } var result = await bucket.LookupIn <T>(key) .Get(AtrIdFieldName, SubdocPathFlags.Xattr) .Get(AtrBucketNameFieldName, SubdocPathFlags.Xattr) .Get(StagedVersionFieldName, SubdocPathFlags.Xattr) .Get(StagedDataFieldName, SubdocPathFlags.Xattr) .Get(string.Empty) // get document body .WithTimeout(_config.KeyValueTimeout) .ExecuteAsync() .ConfigureAwait(false); if (!result.Success) { //TODO: key not found return(null); } // create TransactionDocument return(new TransactionDocument <T>( bucket, result.Id, result.Cas, result.Content <T>(4), // document body TransactionDocumentStatus.Normal, new TransactionLinks <T>( result.Content <string>(0), // atrId result.Content <string>(1), // atrBucketName result.Content <string>(2), // stagedVersion result.Content <T>(3) // stagedContent ) )); }
private static Product GetFragmentProduct(IBucket bucket, string key, IEnumerable <string> paths) { var builder = bucket.LookupIn <dynamic>(key); foreach (var path in paths) { builder = builder.Get(path); } var doc = builder.Execute(); var result = new Product(); result.Id = doc.Content <string>("id"); result.Type = doc.Content <string>("type"); result.ProductId = doc.Content <int>("productId"); result.MarketInfo = new Dictionary <string, MarketInfo>(); result.MarketInfo["fr"] = doc.Content <MarketInfo>("marketInfo.fr"); return(result); }
public void PrepareTest() { var a = _bucket.Get <dynamic>("a"); var b = _bucket.Get <dynamic>("b"); var c = _bucket.Exists("c"); Assert.AreEqual(ResponseStatus.KeyNotFound, a.Status); Assert.AreEqual(ResponseStatus.KeyNotFound, b.Status); Assert.IsTrue(c); a = _bucket.Insert("a", new { }); Assert.IsTrue(a.Success); var increment = _bucket.Increment("counter"); Assert.IsTrue(increment.Success); var aMeta = _bucket.MutateIn <dynamic>("a"). Insert("tx", new { ts = increment.Value, md = new[] { b.Id }, value = new { name = "jeff" } }, SubdocPathFlags.Xattr); var execute = aMeta.Execute(); Assert.IsTrue(execute.Success); a = _bucket.Get <dynamic>("a"); Assert.AreEqual("", a.Value); b = _bucket.Insert("b", new { }); Assert.IsTrue(b.Success); var bMeta = _bucket.MutateIn <dynamic>("b"). Insert("tx", new { ts = increment.Value, md = new[] { a.Id }, value = new { name = "mike" } }, SubdocPathFlags.Xattr); execute = bMeta.Execute(); Assert.IsTrue(execute.Success); b = _bucket.Get <dynamic>("b"); Assert.AreEqual("", b.Value); dynamic aTx = _bucket.LookupIn <dynamic>("a").Get("tx", SubdocPathFlags.Xattr).Execute().Content("tx"); dynamic bTx = _bucket.LookupIn <dynamic>("b").Get("tx", SubdocPathFlags.Xattr).Execute().Content("tx"); //ts Assert.AreEqual(increment.Value, aTx.ts.Value); Assert.AreEqual(increment.Value, bTx.ts.Value); //md Assert.AreEqual(a.Id, bTx.md[0].Value); Assert.AreEqual(b.Id, aTx.md[0].Value); //value Assert.AreEqual("jeff", aTx.value.name.Value); Assert.AreEqual("mike", bTx.value.name.Value); }
public HttpResponseMessage FindHotel(string description = null, string location = null) { var query = new ConjunctionQuery( new TermQuery("hotel").Field("type") ); if (!string.IsNullOrEmpty(description) && description != "*") { query.And(new DisjunctionQuery( new PhraseQuery(description).Field("name"), new PhraseQuery(description).Field("description") )); } if (!string.IsNullOrEmpty(location) && location != "*") { query.And(new DisjunctionQuery( new PhraseQuery(location).Field("address"), new PhraseQuery(location).Field("city"), new PhraseQuery(location).Field("state"), new PhraseQuery(location).Field("country") )); } var search = new SearchQuery(); search.Index = "hotel"; search.Query = query; search.Limit(100); var queryJson = query.Export().ToString(Formatting.None); var hotels = new List <dynamic>(); var result = _bucket.Query(search); foreach (var row in result) { var fragment = _bucket.LookupIn <dynamic>(row.Id) .Get("name") .Get("description") .Get("address") .Get("city") .Get("state") .Get("country") .Execute(); var address = string.Join(", ", new[] { fragment.Content <string>("address"), fragment.Content <string>("city"), fragment.Content <string>("state"), fragment.Content <string>("country") }.Where(x => !string.IsNullOrEmpty(x))); hotels.Add(new { name = fragment.Content <string>("name"), description = fragment.Content <string>("description"), address = address }); } return(Request.CreateResponse(new Result(hotels, queryJson))); }
/// <summary> /// Gets a subdocument for a document by property name /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="childName"></param> /// <returns></returns> public async Task <IDocumentFragment <T> > GetChildAsync <T>(string key, string childName) { return(await _bucket.LookupIn <T>(key.ToLower()) .Get(childName) .ExecuteAsync()); }