public RiakResult Read(IRiakCommand command) { bool done = false; do { MessageCode expectedCode = command.ExpectedCode; Type expectedType = MessageCodeTypeMapBuilder.GetTypeFor(expectedCode); int size = DoRead(expectedCode, expectedType); RpbResp response = DeserializeInstance(expectedType, size); command.OnSuccess(response); var streamingResponse = response as IRpbStreamingResp; if (streamingResponse == null) { done = true; } else { done = streamingResponse.done; } }while (done == false); return(RiakResult.Success()); }
public RiakResult <IEnumerable <RiakResult <TResult> > > PbcStreamRead <TResult>(Func <RiakResult <TResult>, bool> repeatRead, Action onFinish) where TResult : class, new() { var streamer = PbcStreamReadIterator(repeatRead, onFinish); return(RiakResult <IEnumerable <RiakResult <TResult> > > .Success(streamer)); }
public void AsyncListKeysReturnsTheCorrectNumberOfResults() { var bucket = Guid.NewGuid().ToString(); for (var i = 1; i < 11; i++) { var doc = new RiakObject(bucket, i.ToString(), new { value = i }); Client.Put(doc).IsSuccess.ShouldBeTrue(); } RiakResult <IEnumerable <string> > theResult = null; var resetEvnt = new AutoResetEvent(false); Client.Async.ListKeys(bucket, result => { theResult = result; resetEvnt.Set(); }); resetEvnt.WaitOne(); theResult.IsSuccess.ShouldBeTrue(); theResult.Value.ShouldNotBeNull(); theResult.Value.ToList().Count.ShouldEqual(10); }
public virtual string Save(TModel model) { var riakObjectId = new RiakObjectId(BucketName, model.ID); RiakResult <RiakObject> result = client.Get(riakObjectId); CheckResult(result, true); RiakObject objToUpdate = null; if (result.Value != null) { objToUpdate = result.Value; if (objToUpdate.Siblings.Count > 0) { // Provide a better sibling resolution strategy here in production objToUpdate = objToUpdate.Siblings[0]; } objToUpdate.SetObject <TModel>(model); } else { objToUpdate = new RiakObject(riakObjectId, model); } result = client.Put(objToUpdate); CheckResult(result); RiakObject value = result.Value; return(value.Key); }
public RiakResult <IEnumerable <RiakResult <TResult> > > PbcRepeatRead <TResult>(Func <RiakResult <TResult>, bool> repeatRead) where TResult : class, new() { var results = new List <RiakResult <TResult> >(); try { RiakResult <TResult> result; do { result = RiakResult <TResult> .Success(_socket.Read <TResult>()); results.Add(result); } while(repeatRead(result)); return(RiakResult <IEnumerable <RiakResult <TResult> > > .Success(results)); } catch (RiakException ex) { if (ex.NodeOffline) { Disconnect(); } return(RiakResult <IEnumerable <RiakResult <TResult> > > .Error(ResultCode.CommunicationError, ex.Message, ex.NodeOffline)); } catch (Exception ex) { Disconnect(); return(RiakResult <IEnumerable <RiakResult <TResult> > > .Error(ResultCode.CommunicationError, ex.Message, true)); } }
public void Can_Update_A_Hll() { string key = Guid.NewGuid().ToString(); SaveHll(key); var add_3 = new RiakString("add_3"); var adds = new HashSet <string> { add_3 }; var update = new UpdateHll.Builder(adds) .WithBucketType(BucketType) .WithBucket(Bucket) .WithKey(key) .WithReturnBody(true) .WithTimeout(TimeSpan.FromMilliseconds(20000)) .Build(); RiakResult rslt = client.Execute(update); Assert.IsTrue(rslt.IsSuccess, rslt.ErrorMessage); HllResponse response = update.Response; Assert.AreEqual(3, response.Cardinality); }
public void SearchForSetsContainingFootballString() { var cmd = new UpdateSet.Builder() .WithBucketType("sets") .WithBucket("people") .WithKey("ditka") .WithAdditions(new HashSet <string> { "football", "winning" }) .Build(); RiakResult rslt = client.Execute(cmd); CheckResult(rslt); cmd = new UpdateSet.Builder() .WithBucketType("sets") .WithBucket("people") .WithKey("dio") .WithAdditions(new HashSet <string> { "wailing", "rocking", "winning" }) .Build(); rslt = client.Execute(cmd); CheckResult(rslt); WaitForSearch(); DoSearch("hobbies", "set:football"); }
public override string Save(BlogPost model) { var mapOp = new UpdateMap.MapOperation(); mapOp.SetRegister(titleRegister, model.Title); mapOp.SetRegister(authorRegister, model.Author); mapOp.SetRegister(contentRegister, model.Content); mapOp.AddToSet(keywordsSet, model.Keywords); string datePostedSolrFormatted = model.DatePosted .ToUniversalTime() .ToString("yyyy-MM-dd'T'HH:mm:ss'Z'", CultureInfo.InvariantCulture); mapOp.SetRegister(datePostedRegister, datePostedSolrFormatted); mapOp.SetFlag(publishedFlag, model.Published); // NB: no key so Riak will generate it var cmd = new UpdateMap.Builder() .WithBucketType(BucketType) .WithBucket(this.bucket) .WithMapOperation(mapOp) .Build(); RiakResult rslt = client.Execute(cmd); CheckResult(rslt); MapResponse response = cmd.Response; return(response.Key); }
public void Query_Streaming_Matching_All_Data() { var qfmt = "SELECT * FROM GeoCheckin WHERE time >= {0} and time <= {1} and geohash = 'hash1' and user = '******'"; var q = string.Format( qfmt, DateTimeUtil.ToUnixTimeMillis(TwentyMinsAgo), DateTimeUtil.ToUnixTimeMillis(Now)); ushort i = 0; Action <QueryResponse> cb = (QueryResponse qr) => { i++; Assert.AreEqual(Columns.Length, qr.Columns.Count()); CollectionAssert.IsNotEmpty(qr.Value); }; var cmd = new Query.Builder() .WithTable("GeoCheckin") .WithQuery(q) .WithCallback(cb) .Build(); RiakResult rslt = client.Execute(cmd); Assert.IsTrue(rslt.IsSuccess, rslt.ErrorMessage); QueryResponse rsp = cmd.Response; Assert.IsFalse(rsp.NotFound); Assert.Greater(i, 0); }
public void Can_Fetch_Preflist() { RiakMinVersion(2, 1, 0); var fetch = new FetchPreflist.Builder() .WithBucketType(BucketType) .WithBucket(Bucket) .WithKey("key_1") .Build(); RiakResult rslt = client.Execute(fetch); if (rslt.IsSuccess) { PreflistResponse response = fetch.Response; Assert.IsNotNull(response); Assert.AreEqual(3, response.Value.Count()); } else { // TODO: remove this else case when this fix is in Riak: // https://github.com/basho/riak_kv/pull/1116 // https://github.com/basho/riak_core/issues/706 bool foundMessage = (rslt.ErrorMessage.IndexOf("Permission denied") >= 0 && rslt.ErrorMessage.IndexOf("riak_kv.get_preflist") >= 0) || (rslt.ErrorMessage.IndexOf("error:badarg") >= 0 && rslt.ErrorMessage.IndexOf("characters_to_binary") >= 0); Assert.True(foundMessage, rslt.ErrorMessage); } }
protected RiakString UpdateMap(TModel model, UpdateMap.MapOperation mapOperation, bool fetchFirst = false) { byte[] context = null; if (fetchFirst) { MapResponse response = FetchMap(model); context = response.Context; } var builder = new UpdateMap.Builder(mapOperation) .WithBucketType(BucketType) .WithBucket(Bucket); if (!string.IsNullOrEmpty(model.ID)) { builder.WithKey(model.ID); } UpdateMap cmd = builder.Build(); RiakResult rslt = client.Execute(cmd); CheckResult(rslt); return(cmd.Response.Key); }
public void Delete_One_Row() { var delete = new Delete.Builder() .WithTable(Table) .WithKey(KeyToDelete) .Build(); RiakResult rslt = client.Execute(delete); Assert.IsTrue(rslt.IsSuccess, rslt.ErrorMessage); Assert.IsFalse(delete.Response.NotFound); var get = new Get.Builder() .WithTable(Table) .WithKey(KeyToDelete) .Build(); rslt = client.Execute(get); Assert.IsTrue(rslt.IsSuccess, rslt.ErrorMessage); Assert.IsTrue(get.Response.NotFound); var store = new Store.Builder() .WithTable(Table) .WithRow(RowToRestore) .Build(); rslt = client.Execute(store); Assert.IsTrue(rslt.IsSuccess, rslt.ErrorMessage); }
public void Can_Remove_Data_From_A_Map() { string key = Guid.NewGuid().ToString(); MapResponse r = SaveMap(key); var mapOp = new UpdateMap.MapOperation(); mapOp.RemoveCounter("counter_1"); var update = new UpdateMap.Builder(mapOp) .WithBucketType(BucketType) .WithBucket(Bucket) .WithKey(key) .WithContext(r.Context) .WithReturnBody(true) .WithTimeout(TimeSpan.FromMilliseconds(20000)) .Build(); RiakResult rslt = client.Execute(update); Assert.IsTrue(rslt.IsSuccess, rslt.ErrorMessage); MapResponse response = update.Response; Assert.False(response.Value.Counters.ContainsKey("counter_1")); }
public override User Get(string key, bool notFoundOK = false) { FetchMap cmd = new FetchMap.Builder() .WithBucketType(BucketType) .WithBucket(Bucket) .WithKey(key) .Build(); RiakResult rslt = client.Execute(cmd); CheckResult(rslt); MapResponse response = cmd.Response; Map map = response.Value; string firstName = map.Registers.GetValue(firstNameRegister); string lastName = map.Registers.GetValue(lastNameRegister); var interests = map.Sets.GetValue(interestsSet).ToArray(); uint pageVisits = (uint)map.Counters.GetValue(pageVisitsCounter); bool accountStatus; map.Flags.TryGetValue(paidAccountFlag, out accountStatus); return(new User(firstName, lastName, interests, pageVisits, accountStatus)); }
public RiakResult Read(IRiakCommand command) { bool done = false; do { int size = ReadMessageSize(command.ExpectedCode); byte[] buffer = null; if (size > 0) { buffer = ReceiveAll(new byte[size]); } RiakResp response = command.DecodeResponse(buffer); command.OnSuccess(response); var streamingResponse = response as IRpbStreamingResp; if (streamingResponse == null) { done = true; } else { done = streamingResponse.done; } }while (done == false); return(RiakResult.Success()); }
public void SearchForCountersWithValueGreaterThan25() { var cmd = new UpdateCounter.Builder() .WithBucketType("counters") .WithBucket("people") .WithKey("christ_hitchens") .WithIncrement(10) .Build(); RiakResult rslt = client.Execute(cmd); CheckResult(rslt); cmd = new UpdateCounter.Builder() .WithBucketType("counters") .WithBucket("people") .WithKey("joan_rivers") .WithIncrement(25) .Build(); rslt = client.Execute(cmd); CheckResult(rslt); WaitForSearch(); DoSearch("scores", "counter:[20 TO *]"); }
protected void RiakMinVersion(ushort major, ushort minor, ushort build) { if (client == null) { throw new InvalidOperationException("Expected a client!"); } if (riakVersion == null) { var serverInfo = new FetchServerInfo(); RiakResult rslt = client.Execute(serverInfo); Assert.IsTrue(rslt.IsSuccess, rslt.ErrorMessage); var serverVersion = serverInfo.Response.Value.ServerVersion; if (!Version.TryParse(serverVersion, out riakVersion)) { Assert.Fail("Could not parse server version: {0}", serverVersion); } } if (!(riakVersion.Major >= major && riakVersion.Minor >= minor && riakVersion.Build >= build)) { Assert.Pass("Test requires a newer version of Riak. Skipping!"); } }
public RiakResult <TResult> PbcRead <TResult>() where TResult : class, new() { try { var result = socket.Read <TResult>(); return(RiakResult <TResult> .Success(result)); } catch (RiakException ex) { if (ex.NodeOffline) { Disconnect(); } if (ex.Message.Contains("Bucket cannot be zero-length") || ex.Message.Contains("Key cannot be zero-length")) { return(RiakResult <TResult> .FromException(ResultCode.InvalidRequest, ex, ex.NodeOffline)); } return(RiakResult <TResult> .FromException(ResultCode.CommunicationError, ex, ex.NodeOffline)); } catch (Exception ex) { Disconnect(); return(RiakResult <TResult> .FromException(ResultCode.CommunicationError, ex, true)); } }
private SetResponse SaveSet(string key = null) { var updateBuilder = new UpdateSet.Builder(DefaultAdds, null) .WithBucketType(BucketType) .WithBucket(Bucket) .WithTimeout(TimeSpan.FromMilliseconds(20000)); if (!string.IsNullOrEmpty(key)) { updateBuilder.WithKey(key); } UpdateSet cmd = updateBuilder.Build(); RiakResult rslt = client.Execute(cmd); Assert.IsTrue(rslt.IsSuccess, rslt.ErrorMessage); SetResponse response = cmd.Response; Keys.Add(response.Key); Assert.True(EnumerableUtil.NotNullOrEmpty(response.Context)); return(response); }
public void AsyncDeleteIsSuccessful() { var riakObject = new RiakObject(TestBucket, TestKey, TestJson, RiakConstants.ContentTypes.ApplicationJson); var riakObjectId = riakObject.ToRiakObjectId(); var putResult = Client.Put(riakObject); putResult.IsSuccess.ShouldBeTrue(); RiakResult theResult = null; var resetEvent = new AutoResetEvent(false); Client.Async.Delete(riakObjectId, result => { theResult = result; resetEvent.Set(); }); resetEvent.WaitOne(); theResult.IsSuccess.ShouldBeTrue(); var getResult = Client.Get(riakObjectId); getResult.IsSuccess.ShouldBeFalse(); getResult.ResultCode.ShouldEqual(ResultCode.NotFound); getResult.Value.ShouldBeNull(); }
public void Query_Table_Description() { var cmd = new Query.Builder() .WithTable("GeoCheckin") .WithQuery("DESCRIBE GeoCheckin") .Build(); RiakResult rslt = client.Execute(cmd); Assert.IsTrue(rslt.IsSuccess, rslt.ErrorMessage); QueryResponse rsp = cmd.Response; Assert.IsFalse(rsp.NotFound); CollectionAssert.IsNotEmpty(rsp.Columns); CollectionAssert.IsNotEmpty(rsp.Value); int columnCount = rsp.Columns.Count(); Assert.True(columnCount >= 5 && columnCount <= 8); Assert.AreEqual(Columns.Length, rsp.Value.Count()); foreach (Row row in rsp.Value) { Assert.AreEqual(columnCount, row.Cells.Count()); } }
public void Query_Create_Table() { string tableName = Guid.NewGuid().ToString(); string sqlFmt = string.Format( @"CREATE TABLE RTS-{0} (geohash varchar not null, user varchar not null, time timestamp not null, weather varchar not null, temperature double, data blob, PRIMARY KEY((geohash, user, quantum(time, 15, m)), geohash, user, time))", tableName); var cmd = new Query.Builder() .WithTable(tableName) .WithQuery(sqlFmt) .Build(); RiakResult rslt = client.Execute(cmd); Assert.IsTrue(rslt.IsSuccess, rslt.ErrorMessage); QueryResponse rsp = cmd.Response; Assert.IsFalse(rsp.NotFound); CollectionAssert.IsEmpty(rsp.Columns); CollectionAssert.IsEmpty(rsp.Value); }
public void SetUp() { var result = RiakResult <RiakRestResponse> .Success(new RiakRestResponse { StatusCode = System.Net.HttpStatusCode.NoContent }); Cluster.ConnectionMock.Setup(m => m.RestRequest(It.IsAny <RiakRestRequest>())).Returns(result); Response = Client.SetBucketProperties("foo", new RiakBucketProperties().SetAllowMultiple(true).SetRVal("one")); }
public void SetUp() { var result = RiakResult.Success(); Cluster.ConnectionMock.Setup(m => m.PbcWriteRead(It.IsAny <RpbSetBucketReq>(), MessageCode.SetBucketResp)).Returns(result); Response = Client.SetBucketProperties("foo", new RiakBucketProperties().SetAllowMultiple(true)); }
public RiakResult <IEnumerable <RiakResult <TResult> > > PbcWriteStreamRead <TRequest, TResult>(TRequest request, Func <RiakResult <TResult>, bool> repeatRead, Action onFinish) where TResult : new() { var streamer = PbcWriteStreamReadIterator(request, repeatRead, onFinish); return(RiakResult <IEnumerable <RiakResult <TResult> > > .Success(streamer)); }
static bool _test(RiakResult result) { #if DEBUG return(result?.IsSuccess ?? true); #else return(result.IsSuccess); #endif }
private static void CheckResult <T>(RiakResult <T> result) { if (!result.IsSuccess) { Console.Error.WriteLine("Error: {0}", result.ErrorMessage); Environment.Exit(1); } }
public static void IsSuccess(Query cmd, RiakResult result) { if (_test(result)) { return; } throw new QueryException(result, cmd); }
public void ThisTestShouldFail() { Func <RiakResult> alwaysFail = () => RiakResult.FromError(ResultCode.InvalidRequest, "Nope.", true); Func <RiakResult> alwaysThrow = () => { throw new ApplicationException("Whoopsie"); }; var failResult = alwaysFail.WaitUntil(2); alwaysThrow.WaitUntil(2); failResult.IsSuccess.ShouldBeFalse(); }
public static void IsSuccess(Store cmd, RiakResult result) { if (_test(result)) { return; } throw new StoreException(result); }
internal RiakIndexResult(bool includeTerms, RiakResult<RpbIndexResp> response) { if (includeTerms) { indexKeyTerms = response.Value.results.Select( pair => new RiakIndexKeyTerm(pair.value.FromRiakString(), pair.key.FromRiakString())); } else { indexKeyTerms = response.Value.keys.Select(key => new RiakIndexKeyTerm(key.FromRiakString())); } }