本文整理汇总了C#中Couchbase.Lite.Status.GetCode方法的典型用法代码示例。如果您正苦于以下问题:C# Status.GetCode方法的具体用法?C# Status.GetCode怎么用?C# Status.GetCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Couchbase.Lite.Status
的用法示例。
在下文中一共展示了Status.GetCode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestPusherDeletedDoc
/// <exception cref="System.Exception"></exception>
public virtual void TestPusherDeletedDoc()
{
CountDownLatch replicationDoneSignal = new CountDownLatch(1);
Uri remote = GetReplicationURL();
string docIdTimestamp = System.Convert.ToString(Runtime.CurrentTimeMillis());
// Create some documents:
IDictionary<string, object> documentProperties = new Dictionary<string, object>();
string doc1Id = string.Format("doc1-%s", docIdTimestamp);
documentProperties.Put("_id", doc1Id);
documentProperties.Put("foo", 1);
documentProperties.Put("bar", false);
Body body = new Body(documentProperties);
RevisionInternal rev1 = new RevisionInternal(body, database);
Status status = new Status();
rev1 = database.PutRevision(rev1, null, false, status);
NUnit.Framework.Assert.AreEqual(Status.Created, status.GetCode());
documentProperties.Put("_rev", rev1.GetRevId());
documentProperties.Put("UPDATED", true);
documentProperties.Put("_deleted", true);
RevisionInternal rev2 = database.PutRevision(new RevisionInternal(documentProperties
, database), rev1.GetRevId(), false, status);
NUnit.Framework.Assert.IsTrue(status.GetCode() >= 200 && status.GetCode() < 300);
Replication repl = database.CreatePushReplication(remote);
((Pusher)repl).SetCreateTarget(true);
RunReplication(repl);
// make sure doc1 is deleted
Uri replicationUrlTrailing = new Uri(string.Format("%s/", remote.ToExternalForm()
));
Uri pathToDoc = new Uri(replicationUrlTrailing, doc1Id);
Log.D(Tag, "Send http request to " + pathToDoc);
CountDownLatch httpRequestDoneSignal = new CountDownLatch(1);
BackgroundTask getDocTask = new _BackgroundTask_216(pathToDoc, httpRequestDoneSignal
);
getDocTask.Execute();
Log.D(Tag, "Waiting for http request to finish");
try
{
httpRequestDoneSignal.Await(300, TimeUnit.Seconds);
Log.D(Tag, "http request finished");
}
catch (Exception e)
{
Sharpen.Runtime.PrintStackTrace(e);
}
Log.D(Tag, "testPusherDeletedDoc() finished");
}
示例2: TestPusher
/// <exception cref="System.Exception"></exception>
public virtual void TestPusher()
{
CountDownLatch replicationDoneSignal = new CountDownLatch(1);
Uri remote = GetReplicationURL();
string docIdTimestamp = System.Convert.ToString(Runtime.CurrentTimeMillis());
// Create some documents:
IDictionary<string, object> documentProperties = new Dictionary<string, object>();
string doc1Id = string.Format("doc1-%s", docIdTimestamp);
documentProperties.Put("_id", doc1Id);
documentProperties.Put("foo", 1);
documentProperties.Put("bar", false);
Body body = new Body(documentProperties);
RevisionInternal rev1 = new RevisionInternal(body, database);
Status status = new Status();
rev1 = database.PutRevision(rev1, null, false, status);
NUnit.Framework.Assert.AreEqual(Status.Created, status.GetCode());
documentProperties.Put("_rev", rev1.GetRevId());
documentProperties.Put("UPDATED", true);
RevisionInternal rev2 = database.PutRevision(new RevisionInternal(documentProperties
, database), rev1.GetRevId(), false, status);
NUnit.Framework.Assert.AreEqual(Status.Created, status.GetCode());
documentProperties = new Dictionary<string, object>();
string doc2Id = string.Format("doc2-%s", docIdTimestamp);
documentProperties.Put("_id", doc2Id);
documentProperties.Put("baz", 666);
documentProperties.Put("fnord", true);
database.PutRevision(new RevisionInternal(documentProperties, database), null, false
, status);
NUnit.Framework.Assert.AreEqual(Status.Created, status.GetCode());
bool continuous = false;
Replication repl = database.CreatePushReplication(remote);
repl.SetContinuous(continuous);
repl.SetCreateTarget(true);
// Check the replication's properties:
NUnit.Framework.Assert.AreEqual(database, repl.GetLocalDatabase());
NUnit.Framework.Assert.AreEqual(remote, repl.GetRemoteUrl());
NUnit.Framework.Assert.IsFalse(repl.IsPull());
NUnit.Framework.Assert.IsFalse(repl.IsContinuous());
NUnit.Framework.Assert.IsTrue(repl.ShouldCreateTarget());
NUnit.Framework.Assert.IsNull(repl.GetFilter());
NUnit.Framework.Assert.IsNull(repl.GetFilterParams());
// TODO: CAssertNil(r1.doc_ids);
// TODO: CAssertNil(r1.headers);
// Check that the replication hasn't started running:
NUnit.Framework.Assert.IsFalse(repl.IsRunning());
NUnit.Framework.Assert.AreEqual(Replication.ReplicationStatus.ReplicationStopped,
repl.GetStatus());
NUnit.Framework.Assert.AreEqual(0, repl.GetCompletedChangesCount());
NUnit.Framework.Assert.AreEqual(0, repl.GetChangesCount());
NUnit.Framework.Assert.IsNull(repl.GetLastError());
RunReplication(repl);
// make sure doc1 is there
// TODO: make sure doc2 is there (refactoring needed)
Uri replicationUrlTrailing = new Uri(string.Format("%s/", remote.ToExternalForm()
));
Uri pathToDoc = new Uri(replicationUrlTrailing, doc1Id);
Log.D(Tag, "Send http request to " + pathToDoc);
CountDownLatch httpRequestDoneSignal = new CountDownLatch(1);
BackgroundTask getDocTask = new _BackgroundTask_122(pathToDoc, doc1Id, httpRequestDoneSignal
);
//Closes the connection.
getDocTask.Execute();
Log.D(Tag, "Waiting for http request to finish");
try
{
httpRequestDoneSignal.Await(300, TimeUnit.Seconds);
Log.D(Tag, "http request finished");
}
catch (Exception e)
{
Sharpen.Runtime.PrintStackTrace(e);
}
Log.D(Tag, "testPusher() finished");
}
示例3: CouchbaseLiteException
public CouchbaseLiteException (Exception innerException, Status status) : this(innerException, status.GetCode()) { Code = status.GetCode(); }
示例4: TestCRUDOperations
public void TestCRUDOperations()
{
database.Changed += (sender, e) => {
var changes = e.Changes.ToList();
foreach (DocumentChange change in changes)
{
var rev = change.AddedRevision;
Assert.IsNotNull(rev);
Assert.IsNotNull(rev.GetDocId());
Assert.IsNotNull(rev.GetRevId());
Assert.AreEqual(rev.GetDocId(), rev.GetProperties()["_id"]);
Assert.AreEqual(rev.GetDocId(), rev.GetProperties()["_rev"]);
}
};
var privateUUID = database.PrivateUUID();
var publicUUID = database.PublicUUID();
Log.V(Tag, "DB private UUID = '" + privateUUID + "', public UUID = '" +
publicUUID + "'");
Assert.IsTrue(privateUUID.Length >= 20);
Assert.IsTrue(publicUUID.Length >= 20);
//create a document
var documentProperties = new Dictionary<string, object>();
documentProperties["foo"] = 1;
documentProperties["bar"] = false;
documentProperties["baz"] = "touch";
var body = new Body(documentProperties);
var rev1 = new RevisionInternal(body, database);
var status = new Status();
rev1 = database.PutRevision(rev1, null, false, status);
Log.V(Tag, "Created " + rev1);
Assert.IsTrue(rev1.GetDocId().Length >= 10);
Assert.IsTrue(rev1.GetRevId().StartsWith("1-"));
//read it back
var readRev = database.GetDocumentWithIDAndRev(rev1.GetDocId(), null,
DocumentContentOptions.None);
Assert.IsNotNull(readRev);
var userReadRevProps = UserProperties(readRev.GetProperties());
var userBodyProps = UserProperties(body.GetProperties());
Assert.AreEqual(userReadRevProps.Count, userBodyProps.Count);
foreach(var key in userReadRevProps.Keys)
{
Assert.AreEqual(userReadRevProps[key], userBodyProps[key]);
}
//now update it
documentProperties = (Dictionary<string, object>)readRev.GetProperties();
documentProperties["status"] = "updated!";
body = new Body(documentProperties);
var rev2 = new RevisionInternal(body, database);
var rev2input = rev2;
rev2 = database.PutRevision(rev2, rev1.GetRevId(), false, status);
Log.V(Tag, "Updated " + rev1);
Assert.AreEqual(rev1.GetDocId(), rev2.GetDocId());
Assert.IsTrue(rev2.GetRevId().StartsWith("2-"));
//read it back
readRev = database.GetDocumentWithIDAndRev(rev2.GetDocId(), null,
DocumentContentOptions.None);
Assert.IsNotNull(readRev);
Assert.AreEqual(UserProperties(readRev.GetProperties()), UserProperties
(body.GetProperties()));
// Try to update the first rev, which should fail:
var gotExpectedError = false;
try
{
database.PutRevision(rev2input, rev1.GetRevId(), false, status);
}
catch (CouchbaseLiteException e)
{
gotExpectedError = e.GetCBLStatus().GetCode() == StatusCode.Conflict;
}
Assert.IsTrue(gotExpectedError);
// Check the changes feed, with and without filters:
var changeRevisions = database.ChangesSince(0, null, null);
Log.V(Tag, "Changes = " + changeRevisions);
Assert.AreEqual(1, changeRevisions.Count);
changeRevisions = database.ChangesSince(0, null,
(revision, items) => "updated!".Equals (revision.Properties.Get("status")));
Assert.AreEqual(1, changeRevisions.Count);
changeRevisions = database.ChangesSince(0, null,
(revision, items) => "not updated!".Equals (revision.Properties.Get("status")));
Assert.AreEqual(0, changeRevisions.Count);
// Delete it:
var revD = new RevisionInternal(rev2.GetDocId(), null, true, database);
RevisionInternal revResult = null;
gotExpectedError = false;
try
{
//.........这里部分代码省略.........
示例5: TestPusher
public void TestPusher()
{
var remote = GetReplicationURL();
var docIdTimestamp = Convert.ToString(Runtime.CurrentTimeMillis());
// Create some documents:
var documentProperties = new Dictionary<string, object>();
var doc1Id = string.Format("doc1-{0}", docIdTimestamp);
documentProperties["_id"] = doc1Id;
documentProperties["foo"] = 1;
documentProperties["bar"] = false;
var body = new Body(documentProperties);
var rev1 = new RevisionInternal(body, database);
var status = new Status();
rev1 = database.PutRevision(rev1, null, false, status);
Assert.AreEqual(StatusCode.Created, status.GetCode());
documentProperties.Put("_rev", rev1.GetRevId());
documentProperties["UPDATED"] = true;
database.PutRevision(new RevisionInternal(documentProperties, database), rev1.GetRevId(), false, status);
Assert.AreEqual(StatusCode.Created, status.GetCode());
documentProperties = new Dictionary<string, object>();
var doc2Id = string.Format("doc2-{0}", docIdTimestamp);
documentProperties["_id"] = doc2Id;
documentProperties["baz"] = 666;
documentProperties["fnord"] = true;
database.PutRevision(new RevisionInternal(documentProperties, database), null, false, status);
Assert.AreEqual(StatusCode.Created, status.GetCode());
var doc2 = database.GetDocument(doc2Id);
var doc2UnsavedRev = doc2.CreateRevision();
var attachmentStream = GetAsset("attachment.png");
doc2UnsavedRev.SetAttachment("attachment.png", "image/png", attachmentStream);
var doc2Rev = doc2UnsavedRev.Save();
Assert.IsNotNull(doc2Rev);
const bool continuous = false;
var repl = database.CreatePushReplication(remote);
repl.Continuous = continuous;
if (!IsSyncGateway(remote)) {
repl.CreateTarget = true;
}
// Check the replication's properties:
Assert.AreEqual(database, repl.LocalDatabase);
Assert.AreEqual(remote, repl.RemoteUrl);
Assert.IsFalse(repl.IsPull);
Assert.IsFalse(repl.Continuous);
Assert.IsNull(repl.Filter);
Assert.IsNull(repl.FilterParams);
Assert.IsNull(repl.DocIds);
// TODO: CAssertNil(r1.headers); still not null!
// Check that the replication hasn't started running:
Assert.IsFalse(repl.IsRunning);
Assert.AreEqual((int)repl.Status, (int)ReplicationStatus.Stopped);
Assert.AreEqual(0, repl.CompletedChangesCount);
Assert.AreEqual(0, repl.ChangesCount);
Assert.IsNull(repl.LastError);
RunReplication(repl);
// TODO: Verify the foloowing 2 asserts. ChangesCount and CompletedChangesCount
// should already be reset when the replicator stopped.
Assert.IsTrue(repl.ChangesCount >= 2);
Assert.IsTrue(repl.CompletedChangesCount >= 2);
Assert.IsNull(repl.LastError);
VerifyRemoteDocExists(remote, doc1Id);
// Add doc3
documentProperties = new Dictionary<string, object>();
var doc3Id = string.Format("doc3-{0}", docIdTimestamp);
var doc3 = database.GetDocument(doc3Id);
documentProperties["bat"] = 677;
doc3.PutProperties(documentProperties);
// re-run push replication
var repl2 = database.CreatePushReplication(remote);
repl2.Continuous = continuous;
if (!IsSyncGateway(remote))
{
repl2.CreateTarget = true;
}
var repl2CheckedpointId = repl2.RemoteCheckpointDocID();
RunReplication(repl2);
Assert.IsNull(repl2.LastError);
// make sure trhe doc has been added
VerifyRemoteDocExists(remote, doc3Id);
Assert.AreEqual(repl2.LastSequence, database.LastSequenceWithCheckpointId(repl2CheckedpointId));
System.Threading.Thread.Sleep(2000);
//.........这里部分代码省略.........
示例6: LoadRevisionBody
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
internal RevisionInternal LoadRevisionBody(RevisionInternal rev, DocumentContentOptions contentOptions)
{
if (rev.GetBody() != null && contentOptions == DocumentContentOptions.None && rev.GetSequence() != 0)
{
return rev;
}
if ((rev.GetDocId() == null) || (rev.GetRevId() == null))
{
Log.E(Database.Tag, "Error loading revision body");
throw new CouchbaseLiteException(StatusCode.PreconditionFailed);
}
Cursor cursor = null;
var result = new Status(StatusCode.NotFound);
try
{
// TODO: on ios this query is:
// TODO: "SELECT sequence, json FROM revs WHERE [email protected] AND [email protected] LIMIT 1"
var sql = "SELECT sequence, json FROM revs, docs WHERE revid=? AND docs.docid=? AND revs.doc_id=docs.doc_id LIMIT 1";
var args = new [] { rev.GetRevId(), rev.GetDocId() };
cursor = StorageEngine.RawQuery(sql, CommandBehavior.SequentialAccess, args);
if (cursor.MoveToNext())
{
result.SetCode(StatusCode.Ok);
rev.SetSequence(cursor.GetLong(0));
ExpandStoredJSONIntoRevisionWithAttachments(cursor.GetBlob(1), rev, contentOptions);
}
}
catch (SQLException e)
{
Log.E(Tag, "Error loading revision body", e);
throw new CouchbaseLiteException(StatusCode.InternalServerError);
}
finally
{
if (cursor != null)
{
cursor.Close();
}
}
if (result.GetCode() == StatusCode.NotFound)
{
throw new CouchbaseLiteException(result.GetCode());
}
return rev;
}
示例7: TestPusher
public virtual void TestPusher()
{
var replicationDoneSignal = new CountDownLatch(1);
var remote = GetReplicationURL();
var docIdTimestamp = Convert.ToString(Runtime.CurrentTimeMillis());
// Create some documents:
var documentProperties = new Dictionary<string, object>();
var doc1Id = string.Format("doc1-{0}", docIdTimestamp);
documentProperties["_id"] = doc1Id;
documentProperties["foo"] = 1;
documentProperties["bar"] = false;
var body = new Body(documentProperties);
var rev1 = new RevisionInternal(body, database);
var status = new Status();
rev1 = database.PutRevision(rev1, null, false, status);
Assert.AreEqual(StatusCode.Created, status.GetCode());
documentProperties.Put("_rev", rev1.GetRevId());
documentProperties["UPDATED"] = true;
var rev2 = database.PutRevision(new RevisionInternal(documentProperties, database), rev1.GetRevId(), false, status);
Assert.AreEqual(StatusCode.Created, status.GetCode());
documentProperties = new Dictionary<string, object>();
var doc2Id = string.Format("doc2-{0}", docIdTimestamp);
documentProperties["_id"] = doc2Id;
documentProperties["baz"] = 666;
documentProperties["fnord"] = true;
database.PutRevision(new RevisionInternal(documentProperties, database), null, false, status);
Assert.AreEqual(StatusCode.Created, status.GetCode());
var continuous = false;
var repl = database.CreatePushReplication(remote);
repl.Continuous = continuous;
//repl.CreateTarget = false;
// Check the replication's properties:
Assert.AreEqual(database, repl.LocalDatabase);
Assert.AreEqual(remote, repl.RemoteUrl);
Assert.IsFalse(repl.IsPull);
Assert.IsFalse(repl.Continuous);
//Assert.IsTrue(repl.CreateTarget);
Assert.IsNull(repl.Filter);
Assert.IsNull(repl.FilterParams);
// TODO: CAssertNil(r1.doc_ids);
// TODO: CAssertNil(r1.headers);
// Check that the replication hasn't started running:
Assert.IsFalse(repl.IsRunning);
Assert.AreEqual((int)repl.Status, (int)ReplicationStatus.Stopped);
Assert.AreEqual(0, repl.CompletedChangesCount);
Assert.AreEqual(0, repl.ChangesCount);
Assert.IsNull(repl.LastError);
RunReplication(repl);
// make sure doc1 is there
// TODO: make sure doc2 is there (refactoring needed)
var replicationUrlTrailing = new Uri(string.Format("{0}/", remote));
var pathToDoc = new Uri(replicationUrlTrailing, doc1Id);
Log.D(Tag, "Send http request to " + pathToDoc);
var httpRequestDoneSignal = new CountDownLatch(1);
var getDocTask = Task.Factory.StartNew(()=>
{
var httpclient = new HttpClient();
HttpResponseMessage response;
string responseString = null;
try
{
var responseTask = httpclient.GetAsync(pathToDoc.ToString());
responseTask.Wait(TimeSpan.FromSeconds(10));
response = responseTask.Result;
var statusLine = response.StatusCode;
NUnit.Framework.Assert.IsTrue(statusLine == HttpStatusCode.OK);
if (statusLine == HttpStatusCode.OK)
{
var responseStringTask = response.Content.ReadAsStringAsync();
responseStringTask.Wait(TimeSpan.FromSeconds(10));
responseString = responseStringTask.Result;
NUnit.Framework.Assert.IsTrue(responseString.Contains(doc1Id));
Log.D(ReplicationTest.Tag, "result: " + responseString);
}
else
{
var statusReason = response.ReasonPhrase;
response.Dispose();
throw new IOException(statusReason);
}
}
catch (ProtocolViolationException e)
{
NUnit.Framework.Assert.IsNull(e, "Got ClientProtocolException: " + e.Message);
}
catch (IOException e)
{
NUnit.Framework.Assert.IsNull(e, "Got IOException: " + e.Message);
}
httpRequestDoneSignal.CountDown();
});
//Closes the connection.
//.........这里部分代码省略.........
示例8: TestValidations
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
public virtual void TestValidations()
{
Validator validator = new _Validator_19(this);
database.SetValidation("hoopy", validator);
// POST a valid new document:
IDictionary<string, object> props = new Dictionary<string, object>();
props.Put("name", "Zaphod Beeblebrox");
props.Put("towel", "velvet");
RevisionInternal rev = new RevisionInternal(props, database);
Status status = new Status();
validationCalled = false;
rev = database.PutRevision(rev, null, false, status);
NUnit.Framework.Assert.IsTrue(validationCalled);
NUnit.Framework.Assert.AreEqual(Status.Created, status.GetCode());
// PUT a valid update:
props.Put("head_count", 3);
rev.SetProperties(props);
validationCalled = false;
rev = database.PutRevision(rev, rev.GetRevId(), false, status);
NUnit.Framework.Assert.IsTrue(validationCalled);
NUnit.Framework.Assert.AreEqual(Status.Created, status.GetCode());
// PUT an invalid update:
Sharpen.Collections.Remove(props, "towel");
rev.SetProperties(props);
validationCalled = false;
bool gotExpectedError = false;
try
{
rev = database.PutRevision(rev, rev.GetRevId(), false, status);
}
catch (CouchbaseLiteException e)
{
gotExpectedError = (e.GetCBLStatus().GetCode() == Status.Forbidden);
}
NUnit.Framework.Assert.IsTrue(validationCalled);
NUnit.Framework.Assert.IsTrue(gotExpectedError);
// POST an invalid new document:
props = new Dictionary<string, object>();
props.Put("name", "Vogon");
props.Put("poetry", true);
rev = new RevisionInternal(props, database);
validationCalled = false;
gotExpectedError = false;
try
{
rev = database.PutRevision(rev, null, false, status);
}
catch (CouchbaseLiteException e)
{
gotExpectedError = (e.GetCBLStatus().GetCode() == Status.Forbidden);
}
NUnit.Framework.Assert.IsTrue(validationCalled);
NUnit.Framework.Assert.IsTrue(gotExpectedError);
// PUT a valid new document with an ID:
props = new Dictionary<string, object>();
props.Put("_id", "ford");
props.Put("name", "Ford Prefect");
props.Put("towel", "terrycloth");
rev = new RevisionInternal(props, database);
validationCalled = false;
rev = database.PutRevision(rev, null, false, status);
NUnit.Framework.Assert.IsTrue(validationCalled);
NUnit.Framework.Assert.AreEqual("ford", rev.GetDocId());
// DELETE a document:
rev = new RevisionInternal(rev.GetDocId(), rev.GetRevId(), true, database);
NUnit.Framework.Assert.IsTrue(rev.IsDeleted());
validationCalled = false;
rev = database.PutRevision(rev, rev.GetRevId(), false, status);
NUnit.Framework.Assert.IsTrue(validationCalled);
// PUT an invalid new document:
props = new Dictionary<string, object>();
props.Put("_id", "petunias");
props.Put("name", "Pot of Petunias");
rev = new RevisionInternal(props, database);
validationCalled = false;
gotExpectedError = false;
try
{
rev = database.PutRevision(rev, null, false, status);
}
catch (CouchbaseLiteException e)
{
gotExpectedError = (e.GetCBLStatus().GetCode() == Status.Forbidden);
}
NUnit.Framework.Assert.IsTrue(validationCalled);
NUnit.Framework.Assert.IsTrue(gotExpectedError);
}
示例9: UpdateIndex
//.........这里部分代码省略.........
// because this is the
// one with the highest revid, hence the "winning" revision
// of a conflict.
lastDocID = docID;
// Reconstitute the document as a dictionary:
sequence = cursor.GetLong(1);
string docId = cursor.GetString(2);
if (docId.StartsWith("_design/"))
{
// design docs don't get indexed!
keepGoing = cursor.MoveToNext();
continue;
}
string revId = cursor.GetString(3);
byte[] json = cursor.GetBlob(4);
bool noAttachments = cursor.GetInt(5) > 0;
while ((keepGoing = cursor.MoveToNext()) && cursor.GetLong(0) == docID)
{
}
// Skip rows with the same doc_id -- these are losing conflicts.
if (lastSequence > 0)
{
// Find conflicts with documents from previous indexings.
string[] selectArgs2 = new string[] { System.Convert.ToString(docID), System.Convert.ToString
(lastSequence) };
Cursor cursor2 = database.GetDatabase().RawQuery("SELECT revid, sequence FROM revs "
+ "WHERE doc_id=? AND sequence<=? AND current!=0 AND deleted=0 " + "ORDER BY revID DESC "
+ "LIMIT 1", selectArgs2);
if (cursor2.MoveToNext())
{
string oldRevId = cursor2.GetString(0);
// This is the revision that used to be the 'winner'.
// Remove its emitted rows:
long oldSequence = cursor2.GetLong(1);
string[] args = new string[] { Sharpen.Extensions.ToString(GetViewId()), System.Convert.ToString
(oldSequence) };
database.GetDatabase().ExecSQL("DELETE FROM maps WHERE view_id=? AND sequence=?",
args);
if (RevisionInternal.CBLCompareRevIDs(oldRevId, revId) > 0)
{
// It still 'wins' the conflict, so it's the one that
// should be mapped [again], not the current revision!
revId = oldRevId;
sequence = oldSequence;
string[] selectArgs3 = new string[] { System.Convert.ToString(sequence) };
json = Utils.ByteArrayResultForQuery(database.GetDatabase(), "SELECT json FROM revs WHERE sequence=?"
, selectArgs3);
}
}
}
// Get the document properties, to pass to the map function:
EnumSet<Database.TDContentOptions> contentOptions = EnumSet.NoneOf<Database.TDContentOptions
>();
if (noAttachments)
{
contentOptions.AddItem(Database.TDContentOptions.TDNoAttachments);
}
IDictionary<string, object> properties = database.DocumentPropertiesFromJSON(json
, docId, revId, false, sequence, contentOptions);
if (properties != null)
{
// Call the user-defined map() to emit new key/value
// pairs from this revision:
emitBlock.SetSequence(sequence);
mapBlock.Map(properties, emitBlock);
}
}
}
// Finally, record the last revision sequence number that was
// indexed:
ContentValues updateValues = new ContentValues();
updateValues.Put("lastSequence", dbMaxSequence);
string[] whereArgs_1 = new string[] { Sharpen.Extensions.ToString(GetViewId()) };
database.GetDatabase().Update("views", updateValues, "view_id=?", whereArgs_1);
// FIXME actually count number added :)
Log.V(Log.TagView, "Finished re-indexing view: %s " + " up to sequence %s" + " (deleted %s added ?)"
, name, dbMaxSequence, deleted);
result.SetCode(Status.Ok);
}
catch (SQLException e)
{
throw new CouchbaseLiteException(e, new Status(Status.DbError));
}
finally
{
if (cursor != null)
{
cursor.Close();
}
if (!result.IsSuccessful())
{
Log.W(Log.TagView, "Failed to rebuild view %s. Result code: %d", name, result.GetCode
());
}
if (database != null)
{
database.EndTransaction(result.IsSuccessful());
}
}
}
示例10: TestPusherDeletedDoc
public virtual void TestPusherDeletedDoc()
{
Assert.Fail(); // TODO.ZJG: Needs debugging, overflows stack.
CountDownLatch replicationDoneSignal = new CountDownLatch(1);
Uri remote = GetReplicationURL();
string docIdTimestamp = System.Convert.ToString(Runtime.CurrentTimeMillis());
// Create some documentsConvert
IDictionary<string, object> documentProperties = new Dictionary<string, object>();
string doc1Id = string.Format("doc1-{0}", docIdTimestamp);
documentProperties["_id"] = doc1Id;
documentProperties["foo"] = 1;
documentProperties["bar"] = false;
Body body = new Body(documentProperties);
RevisionInternal rev1 = new RevisionInternal(body, database);
Status status = new Status();
rev1 = database.PutRevision(rev1, null, false, status);
NUnit.Framework.Assert.AreEqual(StatusCode.Created, status.GetCode());
documentProperties["_rev"] = rev1.GetRevId();
documentProperties["UPDATED"] = true;
documentProperties["_deleted"] = true;
RevisionInternal rev2 = database.PutRevision(new RevisionInternal(documentProperties
, database), rev1.GetRevId(), false, status);
NUnit.Framework.Assert.IsTrue((int)status.GetCode() >= 200 && (int)status.GetCode() < 300);
var repl = database.CreatePushReplication(remote);
((Pusher)repl).CreateTarget = true;
RunReplication(repl);
// make sure doc1 is deleted
Uri replicationUrlTrailing = new Uri(string.Format("{0}/", remote.ToString()
));
Uri pathToDoc = new Uri(replicationUrlTrailing, doc1Id);
Log.D(Tag, "Send http request to " + pathToDoc);
CountDownLatch httpRequestDoneSignal = new CountDownLatch(1);
var getDocTask = Task.Factory.StartNew(()=>
{
var httpclient = new HttpClient();
HttpResponseMessage response;
string responseString = null;
try
{
var responseTask = httpclient.GetAsync(pathToDoc.ToString());
responseTask.Wait();
response = responseTask.Result;
var statusLine = response.StatusCode;
Log.D(ReplicationTest.Tag, "statusLine " + statusLine);
Assert.AreEqual(HttpStatusCode.NotFound, statusLine.GetStatusCode());
}
catch (ProtocolViolationException e)
{
NUnit.Framework.Assert.IsNull(e, "Got ClientProtocolException: " + e.Message);
}
catch (IOException e)
{
NUnit.Framework.Assert.IsNull(e, "Got IOException: " + e.Message);
}
finally
{
httpRequestDoneSignal.CountDown();
}
});
getDocTask.Start();
Log.D(Tag, "Waiting for http request to finish");
try
{
httpRequestDoneSignal.Await(TimeSpan.FromSeconds(10));
Log.D(Tag, "http request finished");
}
catch (Exception e)
{
Sharpen.Runtime.PrintStackTrace(e);
}
Log.D(Tag, "testPusherDeletedDoc() finished");
}
示例11: TestAttachments
/// <exception cref="System.Exception"></exception>
public virtual void TestAttachments()
{
string testAttachmentName = "test_attachment";
BlobStore attachments = database.GetAttachments();
NUnit.Framework.Assert.AreEqual(0, attachments.Count());
NUnit.Framework.Assert.AreEqual(new HashSet<object>(), attachments.AllKeys());
Status status = new Status();
IDictionary<string, object> rev1Properties = new Dictionary<string, object>();
rev1Properties.Put("foo", 1);
rev1Properties.Put("bar", false);
RevisionInternal rev1 = database.PutRevision(new RevisionInternal(rev1Properties,
database), null, false, status);
NUnit.Framework.Assert.AreEqual(Status.Created, status.GetCode());
byte[] attach1 = Sharpen.Runtime.GetBytesForString("This is the body of attach1");
database.InsertAttachmentForSequenceWithNameAndType(new ByteArrayInputStream(attach1
), rev1.GetSequence(), testAttachmentName, "text/plain", rev1.GetGeneration());
NUnit.Framework.Assert.AreEqual(Status.Created, status.GetCode());
//We must set the no_attachments column for the rev to false, as we are using an internal
//private API call above (database.insertAttachmentForSequenceWithNameAndType) which does
//not set the no_attachments column on revs table
try
{
ContentValues args = new ContentValues();
args.Put("no_attachments=", false);
database.GetDatabase().Update("revs", args, "sequence=?", new string[] { rev1.GetSequence
().ToString() });
}
catch (SQLException e)
{
Log.E(Database.Tag, "Error setting rev1 no_attachments to false", e);
throw new CouchbaseLiteException(Status.InternalServerError);
}
Attachment attachment = database.GetAttachmentForSequence(rev1.GetSequence(), testAttachmentName
);
NUnit.Framework.Assert.AreEqual("text/plain", attachment.GetContentType());
byte[] data = IOUtils.ToByteArray(attachment.GetContent());
NUnit.Framework.Assert.IsTrue(Arrays.Equals(attach1, data));
IDictionary<string, object> innerDict = new Dictionary<string, object>();
innerDict.Put("content_type", "text/plain");
innerDict.Put("digest", "sha1-gOHUOBmIMoDCrMuGyaLWzf1hQTE=");
innerDict.Put("length", 27);
innerDict.Put("stub", true);
innerDict.Put("revpos", 1);
IDictionary<string, object> attachmentDict = new Dictionary<string, object>();
attachmentDict.Put(testAttachmentName, innerDict);
IDictionary<string, object> attachmentDictForSequence = database.GetAttachmentsDictForSequenceWithContent
(rev1.GetSequence(), EnumSet.NoneOf<Database.TDContentOptions>());
NUnit.Framework.Assert.AreEqual(attachmentDict, attachmentDictForSequence);
RevisionInternal gotRev1 = database.GetDocumentWithIDAndRev(rev1.GetDocId(), rev1
.GetRevId(), EnumSet.NoneOf<Database.TDContentOptions>());
IDictionary<string, object> gotAttachmentDict = (IDictionary<string, object>)gotRev1
.GetProperties().Get("_attachments");
NUnit.Framework.Assert.AreEqual(attachmentDict, gotAttachmentDict);
// Check the attachment dict, with attachments included:
Sharpen.Collections.Remove(innerDict, "stub");
innerDict.Put("data", Base64.EncodeBytes(attach1));
attachmentDictForSequence = database.GetAttachmentsDictForSequenceWithContent(rev1
.GetSequence(), EnumSet.Of(Database.TDContentOptions.TDIncludeAttachments));
NUnit.Framework.Assert.AreEqual(attachmentDict, attachmentDictForSequence);
gotRev1 = database.GetDocumentWithIDAndRev(rev1.GetDocId(), rev1.GetRevId(), EnumSet
.Of(Database.TDContentOptions.TDIncludeAttachments));
gotAttachmentDict = (IDictionary<string, object>)gotRev1.GetProperties().Get("_attachments"
);
NUnit.Framework.Assert.AreEqual(attachmentDict, gotAttachmentDict);
// Add a second revision that doesn't update the attachment:
IDictionary<string, object> rev2Properties = new Dictionary<string, object>();
rev2Properties.Put("_id", rev1.GetDocId());
rev2Properties.Put("foo", 2);
rev2Properties.Put("bazz", false);
RevisionInternal rev2 = database.PutRevision(new RevisionInternal(rev2Properties,
database), rev1.GetRevId(), false, status);
NUnit.Framework.Assert.AreEqual(Status.Created, status.GetCode());
database.CopyAttachmentNamedFromSequenceToSequence(testAttachmentName, rev1.GetSequence
(), rev2.GetSequence());
// Add a third revision of the same document:
IDictionary<string, object> rev3Properties = new Dictionary<string, object>();
rev3Properties.Put("_id", rev2.GetDocId());
rev3Properties.Put("foo", 2);
rev3Properties.Put("bazz", false);
RevisionInternal rev3 = database.PutRevision(new RevisionInternal(rev3Properties,
database), rev2.GetRevId(), false, status);
NUnit.Framework.Assert.AreEqual(Status.Created, status.GetCode());
byte[] attach2 = Sharpen.Runtime.GetBytesForString("<html>And this is attach2</html>"
);
database.InsertAttachmentForSequenceWithNameAndType(new ByteArrayInputStream(attach2
), rev3.GetSequence(), testAttachmentName, "text/html", rev2.GetGeneration());
// Check the 2nd revision's attachment:
Attachment attachment2 = database.GetAttachmentForSequence(rev2.GetSequence(), testAttachmentName
);
NUnit.Framework.Assert.AreEqual("text/plain", attachment2.GetContentType());
data = IOUtils.ToByteArray(attachment2.GetContent());
NUnit.Framework.Assert.IsTrue(Arrays.Equals(attach1, data));
// Check the 3rd revision's attachment:
Attachment attachment3 = database.GetAttachmentForSequence(rev3.GetSequence(), testAttachmentName
);
NUnit.Framework.Assert.AreEqual("text/html", attachment3.GetContentType());
data = IOUtils.ToByteArray(attachment3.GetContent());
NUnit.Framework.Assert.IsTrue(Arrays.Equals(attach2, data));
IDictionary<string, object> attachmentDictForRev3 = (IDictionary<string, object>)
//.........这里部分代码省略.........
示例12: UpdateIndex
//.........这里部分代码省略.........
// If the lastSequence has been reset to 0, make sure to remove
// any leftover rows:
string[] whereArgs = new string[] { Sharpen.Extensions.ToString(GetViewId()) };
database.GetDatabase().Delete("maps", "view_id=?", whereArgs);
}
else
{
// Delete all obsolete map results (ones from since-replaced
// revisions):
string[] args = new string[] { Sharpen.Extensions.ToString(GetViewId()), System.Convert.ToString
(lastSequence), System.Convert.ToString(lastSequence) };
database.GetDatabase().ExecSQL("DELETE FROM maps WHERE view_id=? AND sequence IN ("
+ "SELECT parent FROM revs WHERE sequence>? " + "AND parent>0 AND parent<=?)",
args);
}
int deleted = 0;
cursor = database.GetDatabase().RawQuery("SELECT changes()", null);
cursor.MoveToNext();
deleted = cursor.GetInt(0);
cursor.Close();
// This is the emit() block, which gets called from within the
// user-defined map() block
// that's called down below.
AbstractTouchMapEmitBlock emitBlock = new _AbstractTouchMapEmitBlock_446(this);
// find a better way to propagate this back
// Now scan every revision added since the last time the view was
// indexed:
string[] selectArgs = new string[] { System.Convert.ToString(lastSequence) };
cursor = database.GetDatabase().RawQuery("SELECT revs.doc_id, sequence, docid, revid, json FROM revs, docs "
+ "WHERE sequence>? AND current!=0 AND deleted=0 " + "AND revs.doc_id = docs.doc_id "
+ "ORDER BY revs.doc_id, revid DESC", selectArgs);
cursor.MoveToNext();
long lastDocID = 0;
while (!cursor.IsAfterLast())
{
long docID = cursor.GetLong(0);
if (docID != lastDocID)
{
// Only look at the first-iterated revision of any document,
// because this is the
// one with the highest revid, hence the "winning" revision
// of a conflict.
lastDocID = docID;
// Reconstitute the document as a dictionary:
sequence = cursor.GetLong(1);
string docId = cursor.GetString(2);
if (docId.StartsWith("_design/"))
{
// design docs don't get indexed!
cursor.MoveToNext();
continue;
}
string revId = cursor.GetString(3);
byte[] json = cursor.GetBlob(4);
IDictionary<string, object> properties = database.DocumentPropertiesFromJSON(json
, docId, revId, false, sequence, EnumSet.NoneOf<Database.TDContentOptions>());
if (properties != null)
{
// Call the user-defined map() to emit new key/value
// pairs from this revision:
Log.V(Database.Tag, " call map for sequence=" + System.Convert.ToString(sequence
));
emitBlock.SetSequence(sequence);
mapBlock.Map(properties, emitBlock);
}
}
cursor.MoveToNext();
}
// Finally, record the last revision sequence number that was
// indexed:
ContentValues updateValues = new ContentValues();
updateValues.Put("lastSequence", dbMaxSequence);
string[] whereArgs_1 = new string[] { Sharpen.Extensions.ToString(GetViewId()) };
database.GetDatabase().Update("views", updateValues, "view_id=?", whereArgs_1);
// FIXME actually count number added :)
Log.V(Database.Tag, "...Finished re-indexing view " + name + " up to sequence " +
System.Convert.ToString(dbMaxSequence) + " (deleted " + deleted + " added " + "?"
+ ")");
result.SetCode(Status.Ok);
}
catch (SQLException e)
{
throw new CouchbaseLiteException(e, new Status(Status.DbError));
}
finally
{
if (cursor != null)
{
cursor.Close();
}
if (!result.IsSuccessful())
{
Log.W(Database.Tag, "Failed to rebuild view " + name + ": " + result.GetCode());
}
if (database != null)
{
database.EndTransaction(result.IsSuccessful());
}
}
}
示例13: CreateDocumentsForPushReplication
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
private string CreateDocumentsForPushReplication(string docIdTimestamp, string attachmentType
)
{
string doc1Id;
string doc2Id;
// Create some documents:
IDictionary<string, object> doc1Properties = new Dictionary<string, object>();
doc1Id = string.Format("doc1-%s", docIdTimestamp);
doc1Properties.Put("_id", doc1Id);
doc1Properties.Put("foo", 1);
doc1Properties.Put("bar", false);
Body body = new Body(doc1Properties);
RevisionInternal rev1 = new RevisionInternal(body, database);
Status status = new Status();
rev1 = database.PutRevision(rev1, null, false, status);
NUnit.Framework.Assert.AreEqual(Status.Created, status.GetCode());
doc1Properties.Put("_rev", rev1.GetRevId());
doc1Properties.Put("UPDATED", true);
RevisionInternal rev2 = database.PutRevision(new RevisionInternal(doc1Properties,
database), rev1.GetRevId(), false, status);
NUnit.Framework.Assert.AreEqual(Status.Created, status.GetCode());
IDictionary<string, object> doc2Properties = new Dictionary<string, object>();
doc2Id = string.Format("doc2-%s", docIdTimestamp);
doc2Properties.Put("_id", doc2Id);
doc2Properties.Put("baz", 666);
doc2Properties.Put("fnord", true);
database.PutRevision(new RevisionInternal(doc2Properties, database), null, false,
status);
NUnit.Framework.Assert.AreEqual(Status.Created, status.GetCode());
Document doc2 = database.GetDocument(doc2Id);
UnsavedRevision doc2UnsavedRev = doc2.CreateRevision();
if (attachmentType.Equals("png"))
{
InputStream attachmentStream = GetAsset("attachment.png");
doc2UnsavedRev.SetAttachment("attachment.png", "image/png", attachmentStream);
}
else
{
if (attachmentType.Equals("txt"))
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 1000; i++)
{
sb.Append("This is a large attachemnt.");
}
ByteArrayInputStream attachmentStream = new ByteArrayInputStream(Sharpen.Runtime.GetBytesForString
(sb.ToString()));
doc2UnsavedRev.SetAttachment("attachment.txt", "text/plain", attachmentStream);
}
else
{
throw new RuntimeException("invalid attachment type: " + attachmentType);
}
}
SavedRevision doc2Rev = doc2UnsavedRev.Save();
NUnit.Framework.Assert.IsNotNull(doc2Rev);
return doc1Id;
}
开发者ID:transformersprimeabcxyz,项目名称:_TO-DO-couchbase-lite-net-couchbase,代码行数:59,代码来源:ReplicationTest.cs
示例14: Run
public bool Run()
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < this._enclosing.GetSizeOfAttachment(); i++)
{
sb.Append('1');
}
byte[] attach1 = Sharpen.Runtime.GetBytesForString(sb.ToString());
try
{
Status status = new Status();
for (int i_1 = 0; i_1 < this._enclosing.GetNumberOfDocuments(); i_1++)
{
IDictionary<string, object> rev1Properties = new Dictionary<string, object>();
rev1Properties.Put("foo", 1);
rev1Properties.Put("bar", false);
RevisionInternal rev1 = this._enclosing.database.PutRevision(new RevisionInternal
(rev1Properties, this._enclosing.database), null, false, status);
NUnit.Framework.Assert.AreEqual(Status.Created, status.GetCode());
this._enclosing.database.InsertAttachmentForSequenceWithNameAndType(new ByteArrayInputStream
(attach1), rev1.GetSequence(), Test3_CreateDocsWithAttachments._testAttachmentName
, "text/plain", rev1.GetGeneration());
NUnit.Framework.Assert.AreEqual(Status.Created, status.GetCode());
}
}
catch (Exception t)
{
Log.E(Test3_CreateDocsWithAttachments.Tag, "Document create with attachment failed"
, t);
return false;
}
return true;
}
示例15: TestPutLargeAttachment
/// <exception cref="System.Exception"></exception>
public virtual void TestPutLargeAttachment()
{
string testAttachmentName = "test_attachment";
BlobStore attachments = database.GetAttachments();
attachments.DeleteBlobs();
NUnit.Framework.Assert.AreEqual(0, attachments.Count());
Status status = new Status();
IDictionary<string, object> rev1Properties = new Dictionary<string, object>();
rev1Properties.Put("foo", 1);
rev1Properties.Put("bar", false);
RevisionInternal rev1 = database.PutRevision(new RevisionInternal(rev1Properties,
database), null, false, status);
NUnit.Framework.Assert.AreEqual(Status.Created, status.GetCode());
StringBuilder largeAttachment = new StringBuilder();
for (int i = 0; i < Database.kBigAttachmentLength; i++)
{
largeAttachment.Append("big attachment!");
}
byte[] attach1 = Sharpen.Runtime.GetBytesForString(largeAttachment.ToString());
database.InsertAttachmentForSequenceWithNameAndType(new ByteArrayInputStream(attach1
), rev1.GetSequence(), testAttachmentName, "text/plain", rev1.GetGeneration());
Attachment attachment = database.GetAttachmentForSequence(rev1.GetSequence(), testAttachmentName
);
NUnit.Framework.Assert.AreEqual("text/plain", attachment.GetContentType());
byte[] data = IOUtils.ToByteArray(attachment.GetContent());
NUnit.Framework.Assert.IsTrue(Arrays.Equals(attach1, data));
EnumSet<Database.TDContentOptions> contentOptions = EnumSet.Of(Database.TDContentOptions
.TDIncludeAttachments, Database.TDContentOptions.TDBigAttachmentsFollow);
IDictionary<string, object> attachmentDictForSequence = database.GetAttachmentsDictForSequenceWithContent
(rev1.GetSequence(), contentOptions);
IDictionary<string, object> innerDict = (IDictionary<string, object>)attachmentDictForSequence
.Get(testAttachmentName);
if (!innerDict.ContainsKey("stub"))
{
throw new RuntimeException("Expected attachment dict to have 'stub' key");
}
if (((bool)innerDict.Get("stub")) == false)
{
throw new RuntimeException("Expected attachment dict 'stub' key to be true");
}
if (!innerDict.ContainsKey("follows"))
{
throw new RuntimeException("Expected attachment dict to have 'follows' key");
}
RevisionInternal rev1WithAttachments = database.GetDocumentWithIDAndRev(rev1.GetDocId
(), rev1.GetRevId(), contentOptions);
// Map<String,Object> rev1PropertiesPrime = rev1WithAttachments.getProperties();
// rev1PropertiesPrime.put("foo", 2);
IDictionary<string, object> rev1WithAttachmentsProperties = rev1WithAttachments.GetProperties
();
IDictionary<string, object> rev2Properties = new Dictionary<string, object>();
rev2Properties.Put("_id", rev1WithAttachmentsProperties.Get("_id"));
rev2Properties.Put("foo", 2);
RevisionInternal newRev = new RevisionInternal(rev2Properties, database);
RevisionInternal rev2 = database.PutRevision(newRev, rev1WithAttachments.GetRevId
(), false, status);
NUnit.Framework.Assert.AreEqual(Status.Created, status.GetCode());
database.CopyAttachmentNamedFromSequenceToSequence(testAttachmentName, rev1WithAttachments
.GetSequence(), rev2.GetSequence());
// Check the 2nd revision's attachment:
Attachment rev2FetchedAttachment = database.GetAttachmentForSequence(rev2.GetSequence
(), testAttachmentName);
NUnit.Framework.Assert.AreEqual(attachment.GetLength(), rev2FetchedAttachment.GetLength
());
NUnit.Framework.Assert.AreEqual(attachment.GetMetadata(), rev2FetchedAttachment.GetMetadata
());
NUnit.Framework.Assert.AreEqual(attachment.GetContentType(), rev2FetchedAttachment
.GetContentType());
// Add a third revision of the same document:
IDictionary<string, object> rev3Properties = new Dictionary<string, object>();
rev3Properties.Put("_id", rev2.GetProperties().Get("_id"));
rev3Properties.Put("foo", 3);
rev3Properties.Put("baz", false);
RevisionInternal rev3 = new RevisionInternal(rev3Properties, database);
rev3 = database.PutRevision(rev3, rev2.GetRevId(), false, status);
NUnit.Framework.Assert.AreEqual(Status.Created, status.GetCode());
byte[] attach3 = Sharpen.Runtime.GetBytesForString("<html><blink>attach3</blink></html>"
);
database.InsertAttachmentForSequenceWithNameAndType(new ByteArrayInputStream(attach3
), rev3.GetSequence(), testAttachmentName, "text/html", rev3.GetGeneration());
// Check the 3rd revision's attachment:
Attachment rev3FetchedAttachment = database.GetAttachmentForSequence(rev3.GetSequence
(), testAttachmentName);
data = IOUtils.ToByteArray(rev3FetchedAttachment.GetContent());
NUnit.Framework.Assert.IsTrue(Arrays.Equals(attach3, data));
NUnit.Framework.Assert.AreEqual("text/html", rev3FetchedAttachment.GetContentType
());
// TODO: why doesn't this work?
// Assert.assertEquals(attach3.length, rev3FetchedAttachment.getLength());
ICollection<BlobKey> blobKeys = database.GetAttachments().AllKeys();
NUnit.Framework.Assert.AreEqual(2, blobKeys.Count);
database.Compact();
blobKeys = database.GetAttachments().AllKeys();
NUnit.Framework.Assert.AreEqual(1, blobKeys.Count);
}