/// <summary>List directory contents for a resource folder.</summary>
 /// <remarks>List directory contents for a resource folder. Not recursive.</remarks>
 /// <author>Andrew Reslan</author>
 /// <param name="clazz">Any java class that lives in the same place as the resources folder
 ///     </param>
 /// <param name="path">Should end with "/", but not start with one.</param>
 /// <returns>An array of the name of each member item, or null if path does not denote a directory
 ///     </returns>
 /// <exception cref="Sharpen.URISyntaxException">Sharpen.URISyntaxException</exception>
 /// <exception cref="System.IO.IOException">System.IO.IOException</exception>
 public static string[] GetResourceListing(Type clazz, string path)
 {
     Uri dirURL = clazz.GetClassLoader().GetResource(path);
     if (dirURL != null && dirURL.Scheme.Equals("file"))
     {
         return new FilePath(dirURL.ToURI()).List();
     }
     if (dirURL != null && dirURL.Scheme.Equals("jar"))
     {
         string jarPath = Sharpen.Runtime.Substring(dirURL.AbsolutePath, 5, dirURL.AbsolutePath
             .IndexOf("!"));
         JarFile jar = new JarFile(URLDecoder.Decode(jarPath, "UTF-8"));
         Enumeration<JarEntry> entries = ((Enumeration<JarEntry>)jar.Entries());
         ICollection<string> result = new HashSet<string>();
         while (entries.MoveNext())
         {
             string name = entries.Current.GetName();
             if (name.StartsWith(path))
             {
                 string entry = Sharpen.Runtime.Substring(name, path.Length);
                 int checkSubdir = entry.IndexOf("/");
                 if (checkSubdir >= 0)
                 {
                     // if it is a subdirectory, we just return the directory name
                     entry = Sharpen.Runtime.Substring(entry, 0, checkSubdir);
                 }
                 result.AddItem(entry);
             }
         }
         return Sharpen.Collections.ToArray(result, new string[result.Count]);
     }
     throw new NotSupportedException("Cannot list files for URL " + dirURL);
 }
 public ICollection<BlobKey> AllKeys()
 {
     ICollection<BlobKey> result = new HashSet<BlobKey>();
     FilePath file = new FilePath(path);
     FilePath[] contents = file.ListFiles();
     foreach (FilePath attachment in contents)
     {
         if (attachment.IsDirectory())
         {
             continue;
         }
         BlobKey attachmentKey = new BlobKey();
         GetKeyForFilename(attachmentKey, attachment.GetPath());
         result.AddItem(attachmentKey);
     }
     return result;
 }
		/// <exception cref="NGit.Errors.TransportException"></exception>
		private void QueueWants(ICollection<Ref> want)
		{
			HashSet<ObjectId> inWorkQueue = new HashSet<ObjectId>();
			foreach (Ref r in want)
			{
				ObjectId id = r.GetObjectId();
				try
				{
					RevObject obj = revWalk.ParseAny(id);
					if (obj.Has(COMPLETE))
					{
						continue;
					}
					if (inWorkQueue.AddItem(id))
					{
						obj.Add(IN_WORK_QUEUE);
						workQueue.AddItem(obj);
					}
				}
				catch (MissingObjectException)
				{
					if (inWorkQueue.AddItem(id))
					{
						workQueue.AddItem(id);
					}
				}
				catch (IOException e)
				{
					throw new TransportException(MessageFormat.Format(JGitText.Get().cannotRead, id.Name
						), e);
				}
			}
		}
Example #4
0
		private ICollection<string> ListPackDirectory()
		{
			string[] nameList = packDirectory.List();
			if (nameList == null)
			{
				return Sharpen.Collections.EmptySet<string>();
			}
			ICollection<string> nameSet = new HashSet<string>();
			foreach (string name in nameList)
			{
				if (name.StartsWith("pack-"))
				{
					nameSet.AddItem(name);
				}
			}
			return nameSet;
		}
Example #5
0
		/// <exception cref="System.IO.IOException"></exception>
		private void SendPack()
		{
			bool sideband = options.Contains(OPTION_SIDE_BAND) || options.Contains(OPTION_SIDE_BAND_64K
				);
			if (!biDirectionalPipe)
			{
				// Ensure the request was fully consumed. Any remaining input must
				// be a protocol error. If we aren't at EOF the implementation is broken.
				int eof = rawIn.Read();
				if (0 <= eof)
				{
					throw new CorruptObjectException(MessageFormat.Format(JGitText.Get().expectedEOFReceived
						, "\\x" + Sharpen.Extensions.ToHexString(eof)));
				}
			}
			ProgressMonitor pm = NullProgressMonitor.INSTANCE;
			OutputStream packOut = rawOut;
			SideBandOutputStream msgOut = null;
			if (sideband)
			{
				int bufsz = SideBandOutputStream.SMALL_BUF;
				if (options.Contains(OPTION_SIDE_BAND_64K))
				{
					bufsz = SideBandOutputStream.MAX_BUF;
				}
				packOut = new SideBandOutputStream(SideBandOutputStream.CH_DATA, bufsz, rawOut);
				if (!options.Contains(OPTION_NO_PROGRESS))
				{
					msgOut = new SideBandOutputStream(SideBandOutputStream.CH_PROGRESS, bufsz, rawOut
						);
					pm = new SideBandProgressMonitor(msgOut);
				}
			}
			try
			{
				if (wantAll.IsEmpty())
				{
					preUploadHook.OnSendPack(this, wantIds, commonBase);
				}
				else
				{
					preUploadHook.OnSendPack(this, wantAll, commonBase);
				}
			}
			catch (UploadPackMayNotContinueException noPack)
			{
				if (sideband && noPack.Message != null)
				{
					noPack.SetOutput();
					SideBandOutputStream err = new SideBandOutputStream(SideBandOutputStream.CH_ERROR
						, SideBandOutputStream.SMALL_BUF, rawOut);
					err.Write(Constants.Encode(noPack.Message));
					err.Flush();
				}
				throw;
			}
			PackConfig cfg = packConfig;
			if (cfg == null)
			{
				cfg = new PackConfig(db);
			}
			PackWriter pw = new PackWriter(cfg, walk.GetObjectReader());
			try
			{
				pw.SetUseCachedPacks(true);
				pw.SetReuseDeltaCommits(true);
				pw.SetDeltaBaseAsOffset(options.Contains(OPTION_OFS_DELTA));
				pw.SetThin(options.Contains(OPTION_THIN_PACK));
				pw.SetReuseValidatingObjects(false);
				if (commonBase.IsEmpty())
				{
					ICollection<ObjectId> tagTargets = new HashSet<ObjectId>();
					foreach (Ref @ref in refs.Values)
					{
						if (@ref.GetPeeledObjectId() != null)
						{
							tagTargets.AddItem(@ref.GetPeeledObjectId());
						}
						else
						{
							if (@ref.GetObjectId() == null)
							{
								continue;
							}
							else
							{
								if (@ref.GetName().StartsWith(Constants.R_HEADS))
								{
									tagTargets.AddItem(@ref.GetObjectId());
								}
							}
						}
					}
					pw.SetTagTargets(tagTargets);
				}
				RevWalk rw = walk;
				if (wantAll.IsEmpty())
				{
					pw.PreparePack(pm, wantIds, commonBase);
				}
				else
				{
					walk.Reset();
					ObjectWalk ow = walk.ToObjectWalkWithSameObjects();
					pw.PreparePack(pm, ow, wantAll, commonBase);
					rw = ow;
				}
				if (options.Contains(OPTION_INCLUDE_TAG))
				{
					foreach (Ref vref in refs.Values)
					{
						Ref @ref = vref;
						ObjectId objectId = @ref.GetObjectId();
						// If the object was already requested, skip it.
						if (wantAll.IsEmpty())
						{
							if (wantIds.Contains(objectId))
							{
								continue;
							}
						}
						else
						{
							RevObject obj = rw.LookupOrNull(objectId);
							if (obj != null && obj.Has(WANT))
							{
								continue;
							}
						}
						if ([email protected]())
						{
							@ref = db.Peel(@ref);
						}
						ObjectId peeledId = @ref.GetPeeledObjectId();
						if (peeledId == null)
						{
							continue;
						}
						objectId = @ref.GetObjectId();
						if (pw.WillInclude(peeledId) && !pw.WillInclude(objectId))
						{
							pw.AddObject(rw.ParseAny(objectId));
						}
					}
				}
				pw.WritePack(pm, NullProgressMonitor.INSTANCE, packOut);
				statistics = pw.GetStatistics();
				if (msgOut != null)
				{
					string msg = pw.GetStatistics().GetMessage() + '\n';
					msgOut.Write(Constants.Encode(msg));
					msgOut.Flush();
				}
			}
			finally
			{
				pw.Release();
			}
			if (sideband)
			{
				pckOut.End();
			}
			if (logger != null && statistics != null)
			{
				logger.OnPackStatistics(statistics);
			}
		}
		/// <summary>
		/// Objects known to exist but not expressed by
		/// <see cref="NGit.Repository.GetAllRefs()">NGit.Repository.GetAllRefs()</see>
		/// .
		/// <p>
		/// When a repository borrows objects from another repository, it can
		/// advertise that it safely has that other repository's references, without
		/// exposing any other details about the other repository.  This may help
		/// a client trying to push changes avoid pushing more than it needs to.
		/// </summary>
		/// <returns>unmodifiable collection of other known objects.</returns>
		public override ICollection<ObjectId> GetAdditionalHaves()
		{
			HashSet<ObjectId> r = new HashSet<ObjectId>();
			foreach (FileObjectDatabase.AlternateHandle d in objectDatabase.MyAlternates())
			{
				if (d is FileObjectDatabase.AlternateRepository)
				{
					Repository repo;
					repo = ((FileObjectDatabase.AlternateRepository)d).repository;
					foreach (Ref @ref in repo.GetAllRefs().Values)
					{
						r.AddItem(@ref.GetObjectId());
					}
					Sharpen.Collections.AddAll(r, repo.GetAdditionalHaves());
				}
			}
			return r;
		}
Example #7
0
		/// <exception cref="System.IO.IOException"></exception>
		private void SendPack(bool sideband)
		{
			ProgressMonitor pm = NullProgressMonitor.INSTANCE;
			OutputStream packOut = rawOut;
			SideBandOutputStream msgOut = null;
			if (sideband)
			{
				int bufsz = SideBandOutputStream.SMALL_BUF;
				if (options.Contains(OPTION_SIDE_BAND_64K))
				{
					bufsz = SideBandOutputStream.MAX_BUF;
				}
				packOut = new SideBandOutputStream(SideBandOutputStream.CH_DATA, bufsz, rawOut);
				if (!options.Contains(OPTION_NO_PROGRESS))
				{
					msgOut = new SideBandOutputStream(SideBandOutputStream.CH_PROGRESS, bufsz, rawOut
						);
					pm = new SideBandProgressMonitor(msgOut);
				}
			}
			try
			{
				if (wantAll.IsEmpty())
				{
					preUploadHook.OnSendPack(this, wantIds, commonBase);
				}
				else
				{
					preUploadHook.OnSendPack(this, wantAll, commonBase);
				}
			}
			catch (ServiceMayNotContinueException noPack)
			{
				if (sideband && noPack.Message != null)
				{
					noPack.SetOutput();
					SideBandOutputStream err = new SideBandOutputStream(SideBandOutputStream.CH_ERROR
						, SideBandOutputStream.SMALL_BUF, rawOut);
					err.Write(Constants.Encode(noPack.Message));
					err.Flush();
				}
				throw;
			}
			PackConfig cfg = packConfig;
			if (cfg == null)
			{
				cfg = new PackConfig(db);
			}
			PackWriter pw = new PackWriter(cfg, walk.GetObjectReader());
			try
			{
				pw.SetUseCachedPacks(true);
				pw.SetReuseDeltaCommits(true);
				pw.SetDeltaBaseAsOffset(options.Contains(OPTION_OFS_DELTA));
				pw.SetThin(options.Contains(OPTION_THIN_PACK));
				pw.SetReuseValidatingObjects(false);
				if (commonBase.IsEmpty() && refs != null)
				{
					ICollection<ObjectId> tagTargets = new HashSet<ObjectId>();
					foreach (Ref @ref in refs.Values)
					{
						if (@ref.GetPeeledObjectId() != null)
						{
							tagTargets.AddItem(@ref.GetPeeledObjectId());
						}
						else
						{
							if (@ref.GetObjectId() == null)
							{
								continue;
							}
							else
							{
								if (@ref.GetName().StartsWith(Constants.R_HEADS))
								{
									tagTargets.AddItem(@ref.GetObjectId());
								}
							}
						}
					}
					pw.SetTagTargets(tagTargets);
				}
				if (depth > 0)
				{
					pw.SetShallowPack(depth, unshallowCommits);
				}
				RevWalk rw = walk;
				if (wantAll.IsEmpty())
				{
					pw.PreparePack(pm, wantIds, commonBase);
				}
				else
				{
					walk.Reset();
					ObjectWalk ow = walk.ToObjectWalkWithSameObjects();
					pw.PreparePack(pm, ow, wantAll, commonBase);
					rw = ow;
				}
				if (options.Contains(OPTION_INCLUDE_TAG) && refs != null)
				{
					foreach (Ref vref in refs.Values)
					{
						Ref @ref = vref;
						ObjectId objectId = @ref.GetObjectId();
						// If the object was already requested, skip it.
						if (wantAll.IsEmpty())
						{
							if (wantIds.Contains(objectId))
							{
								continue;
							}
						}
						else
						{
							RevObject obj = rw.LookupOrNull(objectId);
							if (obj != null && obj.Has(WANT))
							{
								continue;
							}
						}
						if ([email protected]())
						{
							@ref = db.Peel(@ref);
						}
						ObjectId peeledId = @ref.GetPeeledObjectId();
						if (peeledId == null)
						{
							continue;
						}
						objectId = @ref.GetObjectId();
						if (pw.WillInclude(peeledId) && !pw.WillInclude(objectId))
						{
							pw.AddObject(rw.ParseAny(objectId));
						}
					}
				}
				pw.WritePack(pm, NullProgressMonitor.INSTANCE, packOut);
				statistics = pw.GetStatistics();
				if (msgOut != null)
				{
					string msg = pw.GetStatistics().GetMessage() + '\n';
					msgOut.Write(Constants.Encode(msg));
					msgOut.Flush();
				}
			}
			finally
			{
				pw.Release();
			}
			if (sideband)
			{
				pckOut.End();
			}
			if (statistics != null)
			{
				logger.OnPackStatistics(statistics);
			}
		}
Example #8
0
        private static ICollection<RefSpec> ExpandPushWildcardsFor(Repository db, ICollection
			<RefSpec> specs)
        {
            IDictionary<string, Ref> localRefs = db.GetAllRefs();
            ICollection<RefSpec> procRefs = new HashSet<RefSpec>();
            foreach (RefSpec spec in specs)
            {
                if (spec.IsWildcard())
                {
                    foreach (Ref localRef in localRefs.Values)
                    {
                        if (spec.MatchSource(localRef))
                        {
                            procRefs.AddItem(spec.ExpandFromSource(localRef));
                        }
                    }
                }
                else
                {
                    procRefs.AddItem(spec);
                }
            }
            return procRefs;
        }
Example #9
0
 /// <exception cref="Kirikiri.Tjs2.VariantException"></exception>
 /// <exception cref="Kirikiri.Tjs2.TJSException"></exception>
 public NativeJavaClass(string name, Type c)
     : base(name)
 {
     mJavaClass = c;
     string classname = name;
     mClassID = TJS.RegisterNativeClass(classname);
     try
     {
         HashSet<string> registProp = new HashSet<string>();
         // set/getで重复しないようにチェック
         MethodInfo[] methods = c.GetMethods();
         foreach (MethodInfo m in methods)
         {
             string methodName = m.Name;
             int flag = 0;
             if (m.IsStatic)
             {
                 flag |= Interface.STATICMEMBER;
             }
             if ("constructor".Equals(methodName))
             {
                 // コンストラクタ
                 RegisterNCM(classname, new NativeJavaClassConstructor(m, mClassID), classname, Interface
                     .nitMethod, flag);
             }
             else
             {
                 if (methodName.StartsWith("prop_"))
                 {
                     // プロパティ prop_ で始まるものはプロパティとみなす
                     Type[] @params = Sharpen.Runtime.GetParameterTypes(m);
                     MethodInfo setMethod = null;
                     MethodInfo getMethod = null;
                     string propName = null;
                     if (methodName.StartsWith("prop_set_"))
                     {
                         if (@params.Length == 1)
                         {
                             setMethod = m;
                             propName = Sharpen.Runtime.Substring(methodName, "prop_set_".Length);
                             if (registProp.Contains(propName) == false)
                             {
                                 string getMethodName = "prop_get_" + propName;
                                 foreach (MethodInfo getm in methods)
                                 {
                                     if (getm.Name.Equals(getMethodName))
                                     {
                                         Type[] p = Sharpen.Runtime.GetParameterTypes(getm);
                                         if (p.Length == 0 && getm.ReturnType.Equals(typeof(void)) != true)
                                         {
                                             getMethod = getm;
                                             break;
                                         }
                                     }
                                 }
                             }
                         }
                     }
                     else
                     {
                         if (methodName.StartsWith("prop_get_"))
                         {
                             if (@params.Length == 0 && m.ReturnType.Equals(typeof(void)) != true)
                             {
                                 getMethod = m;
                                 propName = Sharpen.Runtime.Substring(methodName, "prop_get_".Length);
                                 if (registProp.Contains(propName) == false)
                                 {
                                     string setMethodName = "prop_set_" + propName;
                                     foreach (MethodInfo setm in methods)
                                     {
                                         if (setm.Name.Equals(setMethodName))
                                         {
                                             Type[] p = Sharpen.Runtime.GetParameterTypes(setm);
                                             if (p.Length == 1)
                                             {
                                                 setMethod = setm;
                                                 break;
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                     if (propName != null && registProp.Contains(propName) == false)
                     {
                         if (setMethod != null || getMethod != null)
                         {
                             RegisterNCM(propName, new NativeJavaClassProperty(getMethod, setMethod, mClassID)
                                 , classname, Interface.nitProperty, flag);
                             registProp.AddItem(propName);
                         }
                     }
                 }
                 else
                 {
                     // 通常メソッド
                     RegisterNCM(methodName, new NativeJavaClassMethod(m, mClassID), classname, Interface
                         .nitMethod, flag);
                 }
             }
         }
         registProp = null;
     }
     catch (SecurityException e)
     {
         throw new TJSException(Error.InternalError + e.ToString());
     }
 }
		/// <exception cref="System.IO.IOException"></exception>
		private void WritePack(IDictionary<string, RemoteRefUpdate> refUpdates, ProgressMonitor
			 monitor)
		{
			ICollection<ObjectId> remoteObjects = new HashSet<ObjectId>();
			ICollection<ObjectId> newObjects = new HashSet<ObjectId>();
			PackWriter writer = new PackWriter(transport.GetPackConfig(), local.NewObjectReader
				());
			try
			{
				foreach (Ref r in GetRefs())
				{
					remoteObjects.AddItem(r.GetObjectId());
				}
				Sharpen.Collections.AddAll(remoteObjects, additionalHaves);
				foreach (RemoteRefUpdate r_1 in refUpdates.Values)
				{
					if (!ObjectId.ZeroId.Equals(r_1.GetNewObjectId()))
					{
						newObjects.AddItem(r_1.GetNewObjectId());
					}
				}
				writer.SetUseCachedPacks(true);
				writer.SetThin(thinPack);
				writer.SetReuseValidatingObjects(false);
				writer.SetDeltaBaseAsOffset(capableOfsDelta);
				writer.PreparePack(monitor, newObjects, remoteObjects);
				writer.WritePack(monitor, monitor, @out);
			}
			finally
			{
				writer.Release();
			}
			packTransferTime = writer.GetStatistics().GetTimeWriting();
		}
        public void TestAttachments()
        {
            var testAttachmentName = "test_attachment";
            var attachments = database.Attachments;
            Assert.AreEqual(0, attachments.Count());
            Assert.AreEqual(0, attachments.AllKeys().Count());
            
            var rev1Properties = new Dictionary<string, object>();
            rev1Properties["foo"] = 1;
            rev1Properties["bar"] = false;

            var status = new Status();
            var rev1 = database.PutRevision(
                new RevisionInternal(rev1Properties, database), null, false, status);
            Assert.AreEqual(StatusCode.Created, status.GetCode());

            var attach1 = Runtime.GetBytesForString(
                "This is the body of attach1").ToArray();
            database.InsertAttachmentForSequenceWithNameAndType(
                new ByteArrayInputStream(attach1), 
                rev1.GetSequence(), 
                testAttachmentName, 
                "text/plain", 
                rev1.GetGeneration());

            //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
            {
                var args = new ContentValues();
                args.Put("no_attachments", false);
                database.StorageEngine.Update(
                    "revs", 
                    args, 
                    "sequence=?", 
                    new[] { rev1.GetSequence().ToString() }
                );
            }
            catch (SQLException e)
            {
                Log.E(Tag, "Error setting rev1 no_attachments to false", e);
                throw new CouchbaseLiteException(StatusCode.InternalServerError);
            }

            var attachment = database.GetAttachmentForSequence(
                rev1.GetSequence(), 
                testAttachmentName
            );
            Assert.AreEqual("text/plain", attachment.ContentType);
            var data = attachment.Content.ToArray();
            Assert.IsTrue(Arrays.Equals(attach1, data));

            // Workaround :
            // Not closing the content stream will cause Sharing Violation
            // Exception when trying to get the same attachment going forward.
            attachment.ContentStream.Close();

            var innerDict = new Dictionary<string, object>();
            innerDict["content_type"] = "text/plain";
            innerDict["digest"] = "sha1-gOHUOBmIMoDCrMuGyaLWzf1hQTE=";
            innerDict["length"] = 27;
            innerDict["stub"] = true;
            innerDict["revpos"] = 1;

            var attachmentDict = new Dictionary<string, object>();
            attachmentDict[testAttachmentName] = innerDict;
            var attachmentDictForSequence = database.GetAttachmentsDictForSequenceWithContent(rev1.GetSequence(), DocumentContentOptions.None);
            Assert.AreEqual(new SortedDictionary<string,object>(attachmentDict), new SortedDictionary<string,object>(attachmentDictForSequence));//Assert.AreEqual(1, attachmentDictForSequence.Count);
            var gotRev1 = database.GetDocumentWithIDAndRev(rev1.GetDocId(), 
                rev1.GetRevId(), DocumentContentOptions.IncludeAttachments);
            var gotAttachmentDict = gotRev1.GetProperties()
                .Get("_attachments")
                .AsDictionary<string,object>();
            Assert.AreEqual(attachmentDict.Select(kvp => kvp.Key).OrderBy(k => k), gotAttachmentDict.Select(kvp => kvp.Key).OrderBy(k => k));

            // Check the attachment dict, with attachments included:
            innerDict.Remove("stub");
            innerDict.Put("data", Convert.ToBase64String(attach1));
            attachmentDictForSequence = database.GetAttachmentsDictForSequenceWithContent(
                rev1.GetSequence(), DocumentContentOptions.IncludeAttachments);
            Assert.AreEqual(new SortedDictionary<string,object>(attachmentDict[testAttachmentName].AsDictionary<string,object>()), new SortedDictionary<string,object>(attachmentDictForSequence[testAttachmentName].AsDictionary<string,object>()));

            gotRev1 = database.GetDocumentWithIDAndRev(
                rev1.GetDocId(), rev1.GetRevId(), DocumentContentOptions.IncludeAttachments);
            gotAttachmentDict = gotRev1.GetProperties()
                .Get("_attachments")
                .AsDictionary<string, object>()
                .Get(testAttachmentName)
                .AsDictionary<string,object>();
            Assert.AreEqual(innerDict.Select(kvp => kvp.Key).OrderBy(k => k), gotAttachmentDict.Select(kvp => kvp.Key).OrderBy(k => k));

            // Add a second revision that doesn't update the attachment:
            var rev2Properties = new Dictionary<string, object>();
            rev2Properties.Put("_id", rev1.GetDocId());
            rev2Properties["foo"] = 2;
            rev2Properties["bazz"] = false;
            var rev2 = database.PutRevision(new RevisionInternal(rev2Properties, 
                database), rev1.GetRevId(), false, status);
            Assert.AreEqual(StatusCode.Created, status.GetCode());

            database.CopyAttachmentNamedFromSequenceToSequence(
                testAttachmentName, rev1.GetSequence(), rev2.GetSequence());           
            // Add a third revision of the same document:
            var rev3Properties = new Dictionary<string, object>();
            rev3Properties.Put("_id", rev2.GetDocId());
            rev3Properties["foo"] = 2;
            rev3Properties["bazz"] = false;
            var rev3 = database.PutRevision(new RevisionInternal(
                rev3Properties, database), rev2.GetRevId(), false, status);
            Assert.AreEqual(StatusCode.Created, status.GetCode());

            var attach2 = Runtime.GetBytesForString("<html>And this is attach2</html>").ToArray();
            database.InsertAttachmentForSequenceWithNameAndType(
                new ByteArrayInputStream(attach2), rev3.GetSequence(), 
                testAttachmentName, "text/html", rev2.GetGeneration());
            // Check the 2nd revision's attachment:
            var attachment2 = database.GetAttachmentForSequence(rev2.GetSequence(), testAttachmentName);
            Assert.AreEqual("text/plain", attachment2.ContentType);

            data = attachment2.Content.ToArray();
            Assert.IsTrue(Arrays.Equals(attach1, data));

            // Workaround :
            // Not closing the content stream will cause Sharing Violation
            // Exception when trying to get the same attachment going forward.
            attachment2.ContentStream.Close();

            // Check the 3rd revision's attachment:
            var attachment3 = database.GetAttachmentForSequence(rev3.GetSequence(), testAttachmentName);
            Assert.AreEqual("text/html", attachment3.ContentType);

            data = attachment3.Content.ToArray();
            Assert.IsTrue(Arrays.Equals(attach2, data));

            var attachmentDictForRev3 = database.GetAttachmentsDictForSequenceWithContent(rev3.GetSequence(), DocumentContentOptions.None)
                .Get(testAttachmentName)
                .AsDictionary<string,object>();
            if (attachmentDictForRev3.ContainsKey("follows"))
            {
                if (((bool)attachmentDictForRev3.Get("follows")) == true)
                {
                    throw new RuntimeException("Did not expected attachment dict 'follows' key to be true"
                    );
                }
                else
                {
                    throw new RuntimeException("Did not expected attachment dict to have 'follows' key"
                    );
                }
            }

            // Workaround :
            // Not closing the content stream will cause Sharing Violation
            // Exception when trying to get the same attachment going forward.
            attachment3.ContentStream.Close();

            // Examine the attachment store:
            Assert.AreEqual(2, attachments.Count());

            var expected = new HashSet<BlobKey>();
            expected.AddItem(BlobStore.KeyForBlob(attach1));
            expected.AddItem(BlobStore.KeyForBlob(attach2));
            Assert.AreEqual(expected.Count, attachments.AllKeys().Count());

            foreach(var key in attachments.AllKeys()) {
                Assert.IsTrue(expected.Contains(key));
            }

            database.Compact();

            // This clears the body of the first revision
            Assert.AreEqual(1, attachments.Count());

            var expected2 = new HashSet<BlobKey>();
            expected2.AddItem(BlobStore.KeyForBlob(attach2));
            Assert.AreEqual(expected2.Count, attachments.AllKeys().Count());

            foreach(var key in attachments.AllKeys()) {
                Assert.IsTrue(expected2.Contains(key));
            }
        }
		/// <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());
			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));
			// Examine the attachment store:
			NUnit.Framework.Assert.AreEqual(2, attachments.Count());
			ICollection<BlobKey> expected = new HashSet<BlobKey>();
			expected.AddItem(BlobStore.KeyForBlob(attach1));
			expected.AddItem(BlobStore.KeyForBlob(attach2));
			NUnit.Framework.Assert.AreEqual(expected, attachments.AllKeys());
			database.Compact();
			// This clears the body of the first revision
			NUnit.Framework.Assert.AreEqual(1, attachments.Count());
			ICollection<BlobKey> expected2 = new HashSet<BlobKey>();
			expected2.AddItem(BlobStore.KeyForBlob(attach2));
			NUnit.Framework.Assert.AreEqual(expected2, attachments.AllKeys());
		}
Example #13
0
		// kick something off that will s
		/// <exception cref="System.Exception"></exception>
		public virtual void RunLiveQuery(string methodNameToCall)
		{
			Database db = StartDatabase();
			CountDownLatch doneSignal = new CountDownLatch(11);
			// 11 corresponds to startKey=23; endKey=33
			// run a live query
			View view = db.GetView("vu");
			view.SetMap(new _Mapper_817(), "1");
			LiveQuery query = view.CreateQuery().ToLiveQuery();
			query.SetStartKey(23);
			query.SetEndKey(33);
			Log.I(Tag, "Created  " + query);
			// these are the keys that we expect to see in the livequery change listener callback
			ICollection<int> expectedKeys = new HashSet<int>();
			for (int i = 23; i < 34; i++)
			{
				expectedKeys.AddItem(i);
			}
			// install a change listener which decrements countdown latch when it sees a new
			// key from the list of expected keys
			LiveQuery.ChangeListener changeListener = new _ChangeListener_836(expectedKeys, doneSignal
				);
			query.AddChangeListener(changeListener);
			// create the docs that will cause the above change listener to decrement countdown latch
			int kNDocs = 50;
			CreateDocumentsAsync(db, kNDocs);
			if (methodNameToCall.Equals("start"))
			{
				// start the livequery running asynchronously
				query.Start();
			}
			else
			{
				if (methodNameToCall.Equals("startWaitForRows"))
				{
					query.Start();
					query.WaitForRows();
				}
				else
				{
					NUnit.Framework.Assert.IsNull(query.GetRows());
					query.Run();
					// this will block until the query completes
					NUnit.Framework.Assert.IsNotNull(query.GetRows());
				}
			}
			// wait for the doneSignal to be finished
			bool success = doneSignal.Await(300, TimeUnit.Seconds);
			NUnit.Framework.Assert.IsTrue("Done signal timed out, live query never ran", success
				);
			// stop the livequery since we are done with it
			query.RemoveChangeListener(changeListener);
			query.Stop();
		}
 /// <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>)
         database.GetAttachmentsDictForSequenceWithContent(rev3.GetSequence(), EnumSet.NoneOf
         <Database.TDContentOptions>()).Get(testAttachmentName);
     if (attachmentDictForRev3.ContainsKey("follows"))
     {
         if (((bool)attachmentDictForRev3.Get("follows")) == true)
         {
             throw new RuntimeException("Did not expected attachment dict 'follows' key to be true"
                 );
         }
         else
         {
             throw new RuntimeException("Did not expected attachment dict to have 'follows' key"
                 );
         }
     }
     // Examine the attachment store:
     NUnit.Framework.Assert.AreEqual(2, attachments.Count());
     ICollection<BlobKey> expected = new HashSet<BlobKey>();
     expected.AddItem(BlobStore.KeyForBlob(attach1));
     expected.AddItem(BlobStore.KeyForBlob(attach2));
     NUnit.Framework.Assert.AreEqual(expected, attachments.AllKeys());
     database.Compact();
     // This clears the body of the first revision
     NUnit.Framework.Assert.AreEqual(1, attachments.Count());
     ICollection<BlobKey> expected2 = new HashSet<BlobKey>();
     expected2.AddItem(BlobStore.KeyForBlob(attach2));
     NUnit.Framework.Assert.AreEqual(expected2, attachments.AllKeys());
 }
 public void OnCompletion(object result, Exception e)
 {
     try
     {
         if (e == null)
         {
             ICollection<string> failedIDs = new HashSet<string>();
             // _bulk_docs response is really an array, not a dictionary!
             IList<IDictionary<string, object>> items = (IList)result;
             foreach (IDictionary<string, object> item in items)
             {
                 Status status = this._enclosing.StatusFromBulkDocsResponseItem(item);
                 if (status.IsError())
                 {
                     // One of the docs failed to save.
                     Log.W(Log.TagSync, "%s: _bulk_docs got an error: %s", item, this);
                     // 403/Forbidden means validation failed; don't treat it as an error
                     // because I did my job in sending the revision. Other statuses are
                     // actual replication errors.
                     if (status.GetCode() != Status.Forbidden)
                     {
                         string docID = (string)item.Get("id");
                         failedIDs.AddItem(docID);
                     }
                 }
             }
             // TODO - port from iOS
             // NSURL* url = docID ? [_remote URLByAppendingPathComponent: docID] : nil;
             // error = CBLStatusToNSError(status, url);
             // Remove from the pending list all the revs that didn't fail:
             foreach (RevisionInternal revisionInternal in changes)
             {
                 if (!failedIDs.Contains(revisionInternal.GetDocId()))
                 {
                     this._enclosing.RemovePending(revisionInternal);
                 }
             }
         }
         if (e != null)
         {
             this._enclosing.SetError(e);
             this._enclosing.RevisionFailed();
         }
         else
         {
             Log.V(Log.TagSync, "%s: POSTed to _bulk_docs", this._enclosing);
         }
         this._enclosing.AddToCompletedChangesCount(numDocsToSend);
     }
     finally
     {
         Log.V(Log.TagSync, "%s | %s: uploadBulkDocs.sendAsyncRequest() calling asyncTaskFinished()"
             , this, Sharpen.Thread.CurrentThread());
         this._enclosing.AsyncTaskFinished(1);
     }
 }
Example #16
0
		/// <returns>list of files with the flag assume-unchanged</returns>
		public virtual ICollection<string> GetAssumeUnchanged()
		{
			if (assumeUnchanged == null)
			{
				HashSet<string> unchanged = new HashSet<string>();
				for (int i = 0; i < dirCache.GetEntryCount(); i++)
				{
					if (dirCache.GetEntry(i).IsAssumeValid)
					{
						unchanged.AddItem(dirCache.GetEntry(i).PathString);
					}
				}
				assumeUnchanged = unchanged;
			}
			return assumeUnchanged;
		}
Example #17
0
//        public static Task CreateDocumentsAsync(Database db, int n)
//      {
//            return db.RunAsync((database)=>
//                {
//                    database.BeginTransaction();
//                    ApiTest.CreateDocuments(database, n);
//                    database.EndTransaction(true);
//                });
//      }

//          public static void CreateDocuments(Database db, int numberOfDocsToCreate)
//      {
//          //TODO should be changed to use db.runInTransaction
//          for (int i = 0; i < numberOfDocsToCreate; i++)
//          {
//                var properties = new Dictionary<String, Object>();
//                properties["testName"] = "testDatabase";
//                properties["sequence"] = i;
//              CreateDocumentWithProperties(db, properties);
//          }
//      }
//
//        public static Document CreateDocumentWithProperties(Database db, IDictionary<String, Object> properties)
//      {
//            var doc = db.CreateDocument();
//
//          Assert.IsNotNull(doc);
//          Assert.IsNull(doc.CurrentRevisionId);
//          Assert.IsNull(doc.CurrentRevision);
//          Assert.IsNotNull("Document has no ID", doc.Id);
//
//          // 'untitled' docs are no longer untitled (8/10/12)
//          try
//          {
//              doc.PutProperties(properties);
//          }
//          catch (Exception e)
//          {
//              Log.E(Tag, "Error creating document", e);
//                Assert.IsTrue( false, "can't create new document in db:" + db.Name +
//                    " with properties:" + properties.Aggregate(new StringBuilder(" >>> "), (str, kvp)=> { str.AppendFormat("'{0}:{1}' ", kvp.Key, kvp.Value); return str; }, str=>str.ToString()));
//          }
//
//          Assert.IsNotNull(doc.Id);
//          Assert.IsNotNull(doc.CurrentRevisionId);
//          Assert.IsNotNull(doc.UserProperties);
//          Assert.AreEqual(db.GetDocument(doc.Id), doc);
//
//          return doc;
//      }

        /// <exception cref="System.Exception"></exception>
        public void RunLiveQuery(String methodNameToCall)
        {
            var db = database;

            var doneSignal = new CountdownEvent(11); // FIXME.ZJG: Not sure why, but now Changed is only called once.

            // 11 corresponds to startKey = 23; endKey = 33
            // run a live query
            var view = db.GetView("vu");
            view.SetMap((document, emitter) => emitter (document ["sequence"], 1), "1");

            var query = view.CreateQuery().ToLiveQuery();
            query.StartKey = 23;
            query.EndKey = 33;

            Log.I(Tag, "Created  " + query);

            // these are the keys that we expect to see in the livequery change listener callback
            var expectedKeys = new HashSet<Int64>();
            for (var i = 23; i < 34; i++)
            {
                expectedKeys.AddItem(i);
            }

            // install a change listener which decrements countdown latch when it sees a new
            // key from the list of expected keys
            EventHandler<QueryChangeEventArgs> handler = (sender, e) => {
                var rows = e.Rows;
                foreach(var row in rows)
                {
                    if (expectedKeys.Contains((Int64)row.Key))
                    {
                        Log.I(Tag, " doneSignal decremented " + doneSignal.CurrentCount);
                        doneSignal.Signal();
                    }
                }
            };

            query.Changed += handler;

            // create the docs that will cause the above change listener to decrement countdown latch
            var createTask = CreateDocumentsAsync(db, n: 50);
            createTask.Wait(TimeSpan.FromSeconds(5));
            if (methodNameToCall.Equals("start"))
            {
                // start the livequery running asynchronously
                query.Start();
            }
            else if (methodNameToCall.Equals("startWaitForRows")) 
            {
                query.Start();
                query.WaitForRows();
            }
            else
            {
                Assert.IsNull(query.Rows);

                query.Run();

                // this will block until the query completes
                Assert.IsNotNull(query.Rows);
            }

            // wait for the doneSignal to be finished
            var success = doneSignal.Wait(TimeSpan.FromSeconds(5));
            Assert.IsTrue(success, "Done signal timed out live query never ran");

            // stop the livequery since we are done with it
            query.Changed -= handler;
            query.Stop();
            query.Dispose();

            db.Close();
            createTask.Dispose();
            doneSignal.Dispose();
        }
Example #18
0
		/// <exception cref="System.IO.IOException"></exception>
		private void RecvWants()
		{
			HashSet<ObjectId> wantIds = new HashSet<ObjectId>();
			bool isFirst = true;
			for (; ; )
			{
				string line;
				try
				{
					line = pckIn.ReadString();
				}
				catch (EOFException eof)
				{
					if (isFirst)
					{
						break;
					}
					throw;
				}
				if (line == PacketLineIn.END)
				{
					break;
				}
				if (!line.StartsWith("want ") || line.Length < 45)
				{
					throw new PackProtocolException(MessageFormat.Format(JGitText.Get().expectedGot, 
						"want", line));
				}
				if (isFirst && line.Length > 45)
				{
					string opt = Sharpen.Runtime.Substring(line, 45);
					if (opt.StartsWith(" "))
					{
						opt = Sharpen.Runtime.Substring(opt, 1);
					}
					foreach (string c in opt.Split(" "))
					{
						options.AddItem(c);
					}
					line = Sharpen.Runtime.Substring(line, 0, 45);
				}
				wantIds.AddItem(ObjectId.FromString(Sharpen.Runtime.Substring(line, 5)));
				isFirst = false;
			}
			if (wantIds.IsEmpty())
			{
				return;
			}
			AsyncRevObjectQueue q = walk.ParseAny(wantIds.AsIterable (), true);
			try
			{
				for (; ; )
				{
					RevObject o;
					try
					{
						o = q.Next();
					}
					catch (IOException error)
					{
						throw new PackProtocolException(MessageFormat.Format(JGitText.Get().notValid, error
							.Message), error);
					}
					if (o == null)
					{
						break;
					}
					if (o.Has(WANT))
					{
					}
					else
					{
						// Already processed, the client repeated itself.
						if (o.Has(ADVERTISED))
						{
							o.Add(WANT);
							wantAll.AddItem(o);
							if (o is RevTag)
							{
								o = walk.Peel(o);
								if (o is RevCommit)
								{
									if (!o.Has(WANT))
									{
										o.Add(WANT);
										wantAll.AddItem(o);
									}
								}
							}
						}
						else
						{
							throw new PackProtocolException(MessageFormat.Format(JGitText.Get().notValid, o.Name
								));
						}
					}
				}
			}
			finally
			{
				q.Release();
			}
		}
Example #19
0
			/// <summary>Parse the first line of a receive-pack request.</summary>
			/// <remarks>Parse the first line of a receive-pack request.</remarks>
			/// <param name="line">line from the client.</param>
			public FirstLine(string line)
			{
				if (line.Length > 45)
				{
					HashSet<string> opts = new HashSet<string>();
					string opt = Sharpen.Runtime.Substring(line, 45);
					if (opt.StartsWith(" "))
					{
						opt = Sharpen.Runtime.Substring(opt, 1);
					}
					foreach (string c in opt.Split(" "))
					{
						opts.AddItem(c);
					}
					this.line = Sharpen.Runtime.Substring(line, 0, 45);
					this.options = Sharpen.Collections.UnmodifiableSet(opts);
				}
				else
				{
					this.line = line;
					this.options = Sharpen.Collections.EmptySet<string>();
				}
			}
Example #20
0
			public bool Run()
			{
				foreach (string docID in docsToRevs.Keys)
				{
					long docNumericID = this._enclosing.GetDocNumericID(docID);
					if (docNumericID == -1)
					{
						continue;
					}
					IList<string> revsPurged = new AList<string>();
					IList<string> revIDs = (IList<string>)docsToRevs.Get(docID);
					if (revIDs == null)
					{
						return false;
					}
					else
					{
						if (revIDs.Count == 0)
						{
							revsPurged = new AList<string>();
						}
						else
						{
							if (revIDs.Contains("*"))
							{
								try
								{
									string[] args = new string[] { System.Convert.ToString(docNumericID) };
									this._enclosing.database.ExecSQL("DELETE FROM revs WHERE doc_id=?", args);
								}
								catch (SQLException e)
								{
									Log.E(Database.Tag, "Error deleting revisions", e);
									return false;
								}
								revsPurged = new AList<string>();
								revsPurged.AddItem("*");
							}
							else
							{
								Cursor cursor = null;
								try
								{
									string[] args = new string[] { System.Convert.ToString(docNumericID) };
									string queryString = "SELECT revid, sequence, parent FROM revs WHERE doc_id=? ORDER BY sequence DESC";
									cursor = this._enclosing.database.RawQuery(queryString, args);
									if (!cursor.MoveToNext())
									{
										Log.W(Database.Tag, "No results for query: " + queryString);
										return false;
									}
									ICollection<long> seqsToPurge = new HashSet<long>();
									ICollection<long> seqsToKeep = new HashSet<long>();
									ICollection<string> revsToPurge = new HashSet<string>();
									while (!cursor.IsAfterLast())
									{
										string revID = cursor.GetString(0);
										long sequence = cursor.GetLong(1);
										long parent = cursor.GetLong(2);
										if (seqsToPurge.Contains(sequence) || revIDs.Contains(revID) && !seqsToKeep.Contains
											(sequence))
										{
											seqsToPurge.AddItem(sequence);
											revsToPurge.AddItem(revID);
											if (parent > 0)
											{
												seqsToPurge.AddItem(parent);
											}
										}
										else
										{
											seqsToPurge.Remove(sequence);
											revsToPurge.Remove(revID);
											seqsToKeep.AddItem(parent);
										}
										cursor.MoveToNext();
									}
									seqsToPurge.RemoveAll(seqsToKeep);
									Log.I(Database.Tag, string.Format("Purging doc '%s' revs (%s); asked for (%s)", docID
										, revsToPurge, revIDs));
									if (seqsToPurge.Count > 0)
									{
										string seqsToPurgeList = TextUtils.Join(",", seqsToPurge);
										string sql = string.Format("DELETE FROM revs WHERE sequence in (%s)", seqsToPurgeList
											);
										try
										{
											this._enclosing.database.ExecSQL(sql);
										}
										catch (SQLException e)
										{
											Log.E(Database.Tag, "Error deleting revisions via: " + sql, e);
											return false;
										}
									}
									Sharpen.Collections.AddAll(revsPurged, revsToPurge);
								}
								catch (SQLException e)
								{
									Log.E(Database.Tag, "Error getting revisions", e);
									return false;
								}
								finally
								{
									if (cursor != null)
									{
										cursor.Close();
									}
								}
							}
						}
					}
					result.Put(docID, revsPurged);
				}
				return true;
			}
 internal static Boolean PurgeRevisionsTask(Database enclosingDatabase, IDictionary<String, IList<String>> docsToRevs, IDictionary<String, Object> result)
 {
     foreach (string docID in docsToRevs.Keys)
     {
         long docNumericID = enclosingDatabase.GetDocNumericID(docID);
         if (docNumericID == -1)
         {
             continue;
         }
         var revsPurged = new AList<string>();
         var revIDs = docsToRevs [docID];
         if (revIDs == null)
         {
             return false;
         }
         else
         {
             if (revIDs.Count == 0)
             {
                 revsPurged = new AList<string>();
             }
             else
             {
                 if (revIDs.Contains("*"))
                 {
                     try
                     {
                         var args = new[] { Convert.ToString(docNumericID) };
                         enclosingDatabase.StorageEngine.ExecSQL("DELETE FROM revs WHERE doc_id=?", args);
                     }
                     catch (SQLException e)
                     {
                         Log.E(Tag, "Error deleting revisions", e);
                         return false;
                     }
                     revsPurged = new AList<string>();
                     revsPurged.AddItem("*");
                 }
                 else
                 {
                     Cursor cursor = null;
                     try
                     {
                         var args = new [] { Convert.ToString(docNumericID) };
                         var queryString = "SELECT revid, sequence, parent FROM revs WHERE doc_id=? ORDER BY sequence DESC";
                         cursor = enclosingDatabase.StorageEngine.RawQuery(queryString, args);
                         if (!cursor.MoveToNext())
                         {
                             Log.W(Tag, "No results for query: " + queryString);
                             return false;
                         }
                         var seqsToPurge = new HashSet<long>();
                         var seqsToKeep = new HashSet<long>();
                         var revsToPurge = new HashSet<string>();
                         while (!cursor.IsAfterLast())
                         {
                             string revID = cursor.GetString(0);
                             long sequence = cursor.GetLong(1);
                             long parent = cursor.GetLong(2);
                             if (seqsToPurge.Contains(sequence) || revIDs.Contains(revID) && !seqsToKeep.Contains
                                     (sequence))
                             {
                                 seqsToPurge.AddItem(sequence);
                                 revsToPurge.AddItem(revID);
                                 if (parent > 0)
                                 {
                                     seqsToPurge.AddItem(parent);
                                 }
                             }
                             else
                             {
                                 seqsToPurge.Remove(sequence);
                                 revsToPurge.Remove(revID);
                                 seqsToKeep.AddItem(parent);
                             }
                             cursor.MoveToNext();
                         }
                         seqsToPurge.RemoveAll(seqsToKeep);
                         Log.I(Tag, String.Format("Purging doc '{0}' revs ({1}); asked for ({2})", docID, revsToPurge, revIDs));
                         if (seqsToPurge.Count > 0)
                         {
                             string seqsToPurgeList = String.Join(",", seqsToPurge);
                             string sql = string.Format("DELETE FROM revs WHERE sequence in ({0})", seqsToPurgeList);
                             try
                             {
                                 enclosingDatabase.StorageEngine.ExecSQL(sql);
                             }
                             catch (SQLException e)
                             {
                                 Log.E(Tag, "Error deleting revisions via: " + sql, e);
                                 return false;
                             }
                         }
                         Collections.AddAll(revsPurged, revsToPurge);
                     }
                     catch (SQLException e)
                     {
                         Log.E(Tag, "Error getting revisions", e);
                         return false;
                     }
                     finally
                     {
                         if (cursor != null)
                         {
                             cursor.Close();
                         }
                     }
                 }
             }
         }
         result[docID] = revsPurged;
     }
     return true;
 }
Example #22
0
		/// <exception cref="System.IO.IOException"></exception>
		private ObjectDirectory.CachedPackList ScanCachedPacks(ObjectDirectory.CachedPackList
			 old)
		{
			FileSnapshot s = FileSnapshot.Save(cachedPacksFile);
			byte[] buf;
			try
			{
				buf = IOUtil.ReadFully(cachedPacksFile);
			}
			catch (FileNotFoundException)
			{
				buf = new byte[0];
			}
			if (old != null && old.snapshot.Equals(s) && Arrays.Equals(old.raw, buf))
			{
				old.snapshot.SetClean(s);
				return old;
			}
			AList<LocalCachedPack> list = new AList<LocalCachedPack>(4);
			ICollection<ObjectId> tips = new HashSet<ObjectId>();
			int ptr = 0;
			while (ptr < buf.Length)
			{
				if (buf[ptr] == '#' || buf[ptr] == '\n')
				{
					ptr = RawParseUtils.NextLF(buf, ptr);
					continue;
				}
				if (buf[ptr] == '+')
				{
					tips.AddItem(ObjectId.FromString(buf, ptr + 2));
					ptr = RawParseUtils.NextLF(buf, ptr + 2);
					continue;
				}
				IList<string> names = new AList<string>(4);
				while (ptr < buf.Length && buf[ptr] == 'P')
				{
					int end = RawParseUtils.NextLF(buf, ptr);
					if (buf[end - 1] == '\n')
					{
						end--;
					}
					names.AddItem(RawParseUtils.Decode(buf, ptr + 2, end));
					ptr = RawParseUtils.NextLF(buf, end);
				}
				if (!tips.IsEmpty() && !names.IsEmpty())
				{
					list.AddItem(new LocalCachedPack(this, tips, names));
					tips = new HashSet<ObjectId>();
				}
			}
			list.TrimToSize();
			return new ObjectDirectory.CachedPackList(s, Sharpen.Collections.UnmodifiableList
				(list), buf);
		}
Example #23
0
		/// <summary>Generate and write the bundle to the output stream.</summary>
		/// <remarks>
		/// Generate and write the bundle to the output stream.
		/// <p>
		/// This method can only be called once per BundleWriter instance.
		/// </remarks>
		/// <param name="monitor">progress monitor to report bundle writing status to.</param>
		/// <param name="os">
		/// the stream the bundle is written to. The stream should be
		/// buffered by the caller. The caller is responsible for closing
		/// the stream.
		/// </param>
		/// <exception cref="System.IO.IOException">
		/// an error occurred reading a local object's data to include in
		/// the bundle, or writing compressed object data to the output
		/// stream.
		/// </exception>
		public virtual void WriteBundle(ProgressMonitor monitor, OutputStream os)
		{
			PackConfig pc = packConfig;
			if (pc == null)
			{
				pc = new PackConfig(db);
			}
			PackWriter packWriter = new PackWriter(pc, db.NewObjectReader());
			try
			{
				HashSet<ObjectId> inc = new HashSet<ObjectId>();
				HashSet<ObjectId> exc = new HashSet<ObjectId>();
				Sharpen.Collections.AddAll(inc, include.Values);
				foreach (RevCommit r in assume)
				{
					exc.AddItem(r.Id);
				}
				packWriter.SetDeltaBaseAsOffset(true);
				packWriter.SetThin(exc.Count > 0);
				packWriter.SetReuseValidatingObjects(false);
				if (exc.Count == 0)
				{
					packWriter.SetTagTargets(tagTargets);
				}
				packWriter.PreparePack(monitor, inc, exc);
				TextWriter w = new OutputStreamWriter(os, Constants.CHARSET);
				w.Write(NGit.Transport.TransportBundleConstants.V2_BUNDLE_SIGNATURE);
				w.Write('\n');
				char[] tmp = new char[Constants.OBJECT_ID_STRING_LENGTH];
				foreach (RevCommit a in assume)
				{
					w.Write('-');
					a.CopyTo(tmp, w);
					if (a.RawBuffer != null)
					{
						w.Write(' ');
						w.Write(a.GetShortMessage());
					}
					w.Write('\n');
				}
				foreach (KeyValuePair<string, ObjectId> e in include.EntrySet())
				{
					e.Value.CopyTo(tmp, w);
					w.Write(' ');
					w.Write(e.Key);
					w.Write('\n');
				}
				w.Write('\n');
				w.Flush();
				packWriter.WritePack(monitor, monitor, os);
			}
			finally
			{
				packWriter.Release();
			}
		}
Example #24
0
		/// <exception cref="NGit.Errors.TransportException"></exception>
		private void Sendpack(IList<RemoteRefUpdate> updates, ProgressMonitor monitor)
		{
			string pathPack = null;
			string pathIdx = null;
			PackWriter writer = new PackWriter(transport.GetPackConfig(), local.NewObjectReader
				());
			try
			{
				ICollection<ObjectId> need = new HashSet<ObjectId>();
				ICollection<ObjectId> have = new HashSet<ObjectId>();
				foreach (RemoteRefUpdate r in updates)
				{
					need.AddItem(r.GetNewObjectId());
				}
				foreach (Ref r_1 in GetRefs())
				{
					have.AddItem(r_1.GetObjectId());
					if (r_1.GetPeeledObjectId() != null)
					{
						have.AddItem(r_1.GetPeeledObjectId());
					}
				}
				writer.PreparePack(monitor, need, have);
				// We don't have to continue further if the pack will
				// be an empty pack, as the remote has all objects it
				// needs to complete this change.
				//
				if (writer.GetObjectCount() == 0)
				{
					return;
				}
				packNames = new LinkedHashMap<string, string>();
				foreach (string n in dest.GetPackNames())
				{
					packNames.Put(n, n);
				}
				string @base = "pack-" + writer.ComputeName().Name;
				string packName = @base + ".pack";
				pathPack = "pack/" + packName;
				pathIdx = "pack/" + @base + ".idx";
				if (Sharpen.Collections.Remove(packNames, packName) != null)
				{
					// The remote already contains this pack. We should
					// remove the index before overwriting to prevent bad
					// offsets from appearing to clients.
					//
					dest.WriteInfoPacks(packNames.Keys);
					dest.DeleteFile(pathIdx);
				}
				// Write the pack file, then the index, as readers look the
				// other direction (index, then pack file).
				//
				string wt = "Put " + Sharpen.Runtime.Substring(@base, 0, 12);
				OutputStream os = dest.WriteFile(pathPack, monitor, wt + "..pack");
				try
				{
					os = new SafeBufferedOutputStream(os);
					writer.WritePack(monitor, monitor, os);
				}
				finally
				{
					os.Close();
				}
				os = dest.WriteFile(pathIdx, monitor, wt + "..idx");
				try
				{
					os = new SafeBufferedOutputStream(os);
					writer.WriteIndex(os);
				}
				finally
				{
					os.Close();
				}
				// Record the pack at the start of the pack info list. This
				// way clients are likely to consult the newest pack first,
				// and discover the most recent objects there.
				//
				AList<string> infoPacks = new AList<string>();
				infoPacks.AddItem(packName);
				Sharpen.Collections.AddAll(infoPacks, packNames.Keys);
				dest.WriteInfoPacks(infoPacks);
			}
			catch (IOException err)
			{
				SafeDelete(pathIdx);
				SafeDelete(pathPack);
				throw new TransportException(uri, JGitText.Get().cannotStoreObjects, err);
			}
			finally
			{
				writer.Release();
			}
		}