Exemple #1
0
		/// <summary>
		/// Delete all key-value pairs associated with the blob.
		/// </summary>
		public static Task DeleteAsync(this FdbBlob blob, IFdbTransactional db, CancellationToken cancellationToken)
		{
			if (blob == null) throw new ArgumentNullException("blob");
			if (db == null) throw new ArgumentNullException("db");

			return db.WriteAsync((tr) => blob.Delete(tr), cancellationToken);
		}
Exemple #2
0
		/// <summary>
		/// Write <paramref name="data"/> to the blob, starting at <param name="offset"/> and overwriting any existing data at that location. The length of the blob is increased if necessary.
		/// </summary>
		public static Task WriteAsync(this FdbBlob blob, IFdbTransactional db, long offset, Slice data, CancellationToken cancellationToken)
		{
			if (blob == null) throw new ArgumentNullException("blob");
			if (db == null) throw new ArgumentNullException("db");

			return db.ReadWriteAsync((tr) => blob.WriteAsync(tr, offset, data), cancellationToken);
		}
		/// <summary>Opens the directory with the given <paramref name="path"/>.
		/// If the directory does not exist, it is created (creating parent directories if necessary).
		/// If layer is specified, it is checked against the layer of an existing directory or set as the layer of a new directory.
		/// </summary>
		public static Task<FdbDirectorySubspace> CreateOrOpenAsync(this IFdbDirectory directory, IFdbTransactional db, IEnumerable<string> path, Slice layer, CancellationToken cancellationToken)
		{
			if (directory == null) throw new ArgumentNullException("directory");
			if (db == null) throw new ArgumentNullException("db");
			if (path == null) throw new ArgumentNullException("path");
			return db.ReadWriteAsync((tr) => directory.CreateOrOpenAsync(tr, path, layer), cancellationToken);
		}
		/// <summary>Opens the directory with the given <paramref name="name"/>.
		/// If the directory does not exist, it is created (creating parent directories if necessary).
		/// If layer is specified, it is checked against the layer of an existing directory or set as the layer of a new directory.
		/// </summary>
		public static Task<FdbDirectorySubspace> CreateOrOpenAsync(this IFdbDirectory directory, IFdbTransactional db, string name, Slice layer, CancellationToken cancellationToken)
		{
			if (directory == null) throw new ArgumentNullException("directory");
			if (db == null) throw new ArgumentNullException("db");
			if (name == null) throw new ArgumentNullException("name");
			return db.ReadWriteAsync((tr) => directory.CreateOrOpenAsync(tr, new[] { name }, layer), cancellationToken);
		}
Exemple #5
0
		/// <summary>
		/// Change the blob length to <paramref name="newLength"/>, erasing any data when shrinking, and filling new bytes with 0 when growing.
		/// </summary>
		public static Task TruncateAsync(this FdbBlob blob, IFdbTransactional db, long newLength, CancellationToken cancellationToken)
		{
			if (blob == null) throw new ArgumentNullException("blob");
			if (db == null) throw new ArgumentNullException("db");

			return db.ReadWriteAsync((tr) => blob.TruncateAsync(tr, newLength), cancellationToken);
		}
		/// <summary>Change the layer id of this directory</summary>
		public static Task<FdbDirectorySubspace> ChangeLayerAsync(this FdbDirectorySubspace subspace, IFdbTransactional db, Slice newLayer, CancellationToken cancellationToken)
		{
			if (subspace == null) throw new ArgumentNullException("subspace");
			if (db == null) throw new ArgumentNullException("db");
			return db.ReadWriteAsync((tr) => subspace.ChangeLayerAsync(tr, newLayer), cancellationToken);
		}
		/// <summary>Removes the directory, its contents, and all subdirectories.
		/// Warning: Clients that have already opened the directory might still insert data into its contents after it is removed.
		/// </summary>
		public static Task<bool> TryRemoveAsync(this IFdbDirectory directory, IFdbTransactional db, string name, CancellationToken cancellationToken)
		{
			if (directory == null) throw new ArgumentNullException("directory");
			if (db == null) throw new ArgumentNullException("db");
			if (name == null) throw new ArgumentNullException("name");
			return db.ReadWriteAsync((tr) => directory.TryRemoveAsync(tr, new [] { name }), cancellationToken);
		}
		/// <summary>Removes the directory, its contents, and all subdirectories.
		/// Warning: Clients that have already opened the directory might still insert data into its contents after it is removed.
		/// </summary>
		public static Task<bool> TryRemoveAsync(this IFdbDirectory directory, IFdbTransactional db, IEnumerable<string> path, CancellationToken cancellationToken)
		{
			if (directory == null) throw new ArgumentNullException("directory");
			if (db == null) throw new ArgumentNullException("db");
			return db.ReadWriteAsync((tr) => directory.TryRemoveAsync(tr, path), cancellationToken);
		}
		/// <summary>Removes the directory, its contents, and all subdirectories.
		/// Warning: Clients that have already opened the directory might still insert data into its contents after it is removed.
		/// </summary>
		public static Task RemoveAsync(this IFdbDirectory directory, IFdbTransactional db, CancellationToken cancellationToken)
		{
			if (directory == null) throw new ArgumentNullException("directory");
			if (db == null) throw new ArgumentNullException("db");
			return db.ReadWriteAsync((tr) => directory.RemoveAsync(tr), cancellationToken);
		}
		/// <summary>Attempts to move the current directory to <paramref name="newPath"/>.
		/// There is no effect on the physical prefix of the given directory, or on clients that already have the directory open.
		/// </summary>
		public static Task<FdbDirectorySubspace> TryMoveToAsync(this FdbDirectorySubspace subspace, IFdbTransactional db, IEnumerable<string> newPath, CancellationToken cancellationToken)
		{
			if (subspace == null) throw new ArgumentNullException("subspace");
			if (db == null) throw new ArgumentNullException("db");
			if (newPath == null) throw new ArgumentNullException("newPath");
			return db.ReadWriteAsync((tr) => subspace.TryMoveToAsync(tr, newPath), cancellationToken);
		}
		/// <summary>Attempts to move the directory found at <paramref name="oldPath"/> to <paramref name="newPath"/>.
		/// There is no effect on the physical prefix of the given directory, or on clients that already have the directory open.
		/// </summary>
		public static Task<FdbDirectorySubspace> TryMoveAsync(this IFdbDirectory directory, IFdbTransactional db, IEnumerable<string> oldPath, IEnumerable<string> newPath, CancellationToken cancellationToken)
		{
			if (directory == null) throw new ArgumentNullException("directory");
			if (db == null) throw new ArgumentNullException("db");
			if (oldPath == null) throw new ArgumentNullException("oldPath");
			if (newPath == null) throw new ArgumentNullException("newPath");
			return db.ReadWriteAsync((tr) => directory.TryMoveAsync(tr, oldPath, newPath), cancellationToken);
		}
Exemple #12
0
		/// <summary>Add and Schedule a new Task in the worker pool</summary>
		/// <param name="db"></param>
		/// <param name="taskId"></param>
		/// <param name="taskBody"></param>
		/// <param name="ct"></param>
		/// <returns></returns>
		public async Task ScheduleTaskAsync(IFdbTransactional db, Slice taskId, Slice taskBody, CancellationToken ct = default(CancellationToken))
		{
			if (db == null) throw new ArgumentNullException("db");
			var now = DateTime.UtcNow;

			await db.ReadWriteAsync(async (tr) =>
			{
				Interlocked.Increment(ref m_schedulingAttempts);
#if DEBUG
				if (tr.Context.Retries > 0) Console.WriteLine("# retry n°" + tr.Context.Retries + " for task " + taskId.ToAsciiOrHexaString());
#endif
				tr.Annotate("I want to schedule {0}", taskId.ToAsciiOrHexaString());

				// find a random worker from the idle ring
				var randomWorkerKey = await FindRandomItem(tr, this.IdleRing).ConfigureAwait(false);

				if (randomWorkerKey.Key != null)
				{
					Slice workerId = this.IdleRing.UnpackSingle<Slice>(randomWorkerKey.Key);

					tr.Annotate("Assigning {0} to {1}", taskId.ToAsciiOrHexaString(), workerId.ToAsciiOrHexaString());

					// remove worker from the idle ring
					tr.Clear(this.IdleRing.Pack(workerId));
					this.Counters.Decrement(tr, COUNTER_IDLE);

					// assign task to the worker
					tr.Set(this.BusyRing.Pack(workerId), taskId);
					this.Counters.Increment(tr, COUNTER_BUSY);
				}
				else
				{
					tr.Annotate("Queueing {0}", taskId.ToAsciiOrHexaString());

					await PushQueueAsync(tr, this.UnassignedTaskRing, taskId).ConfigureAwait(false);
				}

				// store the task in the db
				StoreTask(tr, taskId, now, taskBody);
			}, 
			onDone: (tr) =>
			{
				Interlocked.Increment(ref m_schedulingMessages);
			},
			cancellationToken: ct).ConfigureAwait(false);
		}