Permalink
Browse files

NCBC-157: NRE thrown on deleted keys in non-stale views

Change-Id: I87bbee6672aecdb1c108dd0fca146ebdadda309d
Reviewed-on: http://review.couchbase.org/22593
Reviewed-by: Matt Ingenthron <matt@couchbase.com>
Tested-by: John C. Zablocki <john@couchbase.com>
  • Loading branch information...
1 parent 6397aea commit 610f5d4a116c2056c2fa9c23f2bf75a1515d0d5e @johnzablocki johnzablocki committed with John C. Zablocki Nov 16, 2012
@@ -7,6 +7,7 @@
using Enyim.Caching.Memcached;
using System.Net;
using Newtonsoft.Json;
+using Couchbase.Operations;
namespace Couchbase.Tests
{
@@ -41,6 +42,46 @@ public void When_Should_Lookup_By_Id_Is_False_Document_Is_Deserialized_By_Proper
Assert.That(view.Count(), Is.GreaterThan(0), "View count was 0");
}
+
+ [Test]
+ public void When_Iterating_Over_A_Non_Stale_View_Deleted_Keys_Return_Null()
+ {
+ var json = "{ \"name\" : \"New Britain\", \"state\" : \"CT\", \"type\" : \"city\", \"loc\" : [-72.4714, 41.4030] }";
+ var key = "city_CT_New_Britain";
+
+ var storeResult = _Client.ExecuteStore(StoreMode.Set, key, json, PersistTo.One);
+ StoreAssertPass(storeResult);
+
+ //force view to have new doc indexed
+ var view = _Client.GetView<City>("cities", "by_name", true).Stale(StaleMode.False);
+
+ var viewContainsNewDoc = false;
+ foreach (var item in view)
+ {
+ if (item.Id == key)
+ {
+ viewContainsNewDoc = true;
+ break;
+ }
+ }
+
+ Assert.That(viewContainsNewDoc, Is.True, "View did not contain new doc");
+
+ var removeResult = _Client.ExecuteRemove(key);
+ Assert.That(removeResult.Success, Is.True, "Remove failed");
+
+ var getResult = _Client.ExecuteGet(key);
+ GetAssertFail(getResult);
+
+ view = _Client.GetView<City>("cities", "by_name", true).Stale(StaleMode.AllowStale);
+ var nullItemCount = 0;
+ foreach (var item in view)
+ {
+ if (item == null) ++nullItemCount;
+ }
+
+ Assert.That(nullItemCount, Is.EqualTo(1), "Null item count was not 1");
+ }
}
}
@@ -4,6 +4,8 @@
using System.Text;
using NUnit.Framework;
using Couchbase.Extensions;
+using Enyim.Caching.Memcached;
+using Couchbase.Operations;
namespace Couchbase.Tests
{
@@ -74,6 +76,46 @@ public void When_Querying_Spatial_View_With_Bounding_Box_Rows_Are_Limited()
Assert.That(hasAtLeastOneRecord, Is.True, "No records found");
}
+
+ [Test]
+ public void When_Iterating_Over_A_Non_Stale_Spatial_View_Deleted_Keys_Return_Null()
+ {
+ var json = "{ \"name\" : \"New Britain\", \"state\" : \"CT\", \"type\" : \"city\", \"loc\" : [-72.4714, 41.4030] }";
+ var key = "city_CT_New_Britain";
+
+ var storeResult = _Client.ExecuteStore(StoreMode.Set, key, json, PersistTo.One);
+ StoreAssertPass(storeResult);
+
+ //force view to have new doc indexed
+ var view = _Client.GetSpatialView<City>("cities", "by_location", true).Stale(StaleMode.False);
+
+ var viewContainsNewDoc = false;
+ foreach (var item in view)
+ {
+ if (item.Id == key)
+ {
+ viewContainsNewDoc = true;
+ break;
+ }
+ }
+
+ Assert.That(viewContainsNewDoc, Is.True, "View did not contain new doc");
+
+ var removeResult = _Client.ExecuteRemove(key);
+ Assert.That(removeResult.Success, Is.True, "Remove failed");
+
+ var getResult = _Client.ExecuteGet(key);
+ GetAssertFail(getResult);
+
+ view = _Client.GetSpatialView<City>("cities", "by_location", true).Stale(StaleMode.AllowStale);
+ var nullItemCount = 0;
+ foreach (var item in view)
+ {
+ if (item == null) ++nullItemCount;
+ }
+
+ Assert.That(nullItemCount, Is.EqualTo(1), "Null item count was not 1");
+ }
}
}
@@ -77,6 +77,7 @@
<Compile Include="CouchbaseSpatialViewBase.cs" />
<Compile Include="CouchbaseViewHandler.cs" />
<Compile Include="Extensions\CouchbaseClientExtensions.cs" />
+ <Compile Include="GenericViewRowTransformer.cs" />
<Compile Include="Helpers\DocHelper.cs" />
<Compile Include="Helpers\JsonHelper.cs" />
<Compile Include="ISpatialView`1.cs" />
@@ -21,18 +21,7 @@ public override IEnumerator<T> GetEnumerator()
{
return ViewHandler.TransformResults<T>((jr) =>
{
- if (_shouldLookupDocById)
- {
- var key = Json.ParseValue(jr, "id") as string;
- var json = ViewHandler.Client.Get<string>(key);
- var jsonWithId = DocHelper.InsertId(json, key);//_id is omitted from the Json return by Get
- return JsonConvert.DeserializeObject<T>(jsonWithId);
- }
- else
- {
- var jObject = Json.ParseValue(jr, "value");
- return JsonConvert.DeserializeObject<T>(jObject);
- }
+ return GenericViewRowTransformer<T>.TransformRow(jr, ViewHandler.Client, _shouldLookupDocById);
}, BuildParams());
}
}
@@ -33,18 +33,7 @@ public override IEnumerator<T> GetEnumerator()
{
return TransformResults<T>((jr) =>
{
- if (_shouldLookupDocById)
- {
- var key = Json.ParseValue(jr, "id") as string;
- var json = ViewHandler.Client.Get<string>(key);
- var jsonWithId = DocHelper.InsertId(json, key);//_id is omitted from the Json return by Get
- return JsonConvert.DeserializeObject<T>(jsonWithId);
- }
- else
- {
- var jObject = Json.ParseValue(jr, "value");
- return JsonConvert.DeserializeObject<T>(jObject);
- }
+ return GenericViewRowTransformer<T>.TransformRow(jr, ViewHandler.Client, _shouldLookupDocById);
});
}
@@ -0,0 +1,51 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Newtonsoft.Json;
+using Couchbase.Helpers;
+
+namespace Couchbase
+{
+ internal static class GenericViewRowTransformer<T>
+ {
+ public static T TransformRow(JsonReader reader, ICouchbaseClient client, bool shouldLookupById)
+ {
+ if (shouldLookupById)
+ {
+ var key = Json.ParseValue(reader, "id") as string;
+ var json = client.Get<string>(key);
+ if (string.IsNullOrEmpty(json)) return default(T);
+
+ var jsonWithId = DocHelper.InsertId(json, key);//_id is omitted from the Json return by Get
+ return JsonConvert.DeserializeObject<T>(jsonWithId);
+ }
+ else
+ {
+ var jObject = Json.ParseValue(reader, "value");
+ return JsonConvert.DeserializeObject<T>(jObject);
+ }
+ }
+ }
+}
+
+#region [ License information ]
+/* ************************************************************
+ *
+ * @author Couchbase <info@couchbase.com>
+ * @copyright 2012 Couchbase, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * ************************************************************/
+#endregion

0 comments on commit 610f5d4

Please sign in to comment.