public static async Task <FdbSystemStatus> GetStatusAsync([NotNull] IFdbReadOnlyTransaction trans)
            {
                Contract.NotNull(trans, nameof(trans));

                Slice data = await trans.GetAsync(StatusJsonKey).ConfigureAwait(false);

                if (data.IsNullOrEmpty)
                {
                    return(null);
                }

                string jsonText = data.ToUnicode();

                var doc = TinyJsonParser.ParseObject(jsonText);

                if (doc == null)
                {
                    return(null);
                }

                long rv = 0;

                if (doc.ContainsKey("cluster"))
                {
                    rv = await trans.GetReadVersionAsync();
                }

                return(new FdbSystemStatus(doc, rv, jsonText));
            }
            public static async Task <FdbSystemStatus?> GetStatusAsync(IFdbReadOnlyTransaction trans)
            {
                Contract.NotNull(trans);

                var data = await trans.GetAsync(StatusJsonKey).ConfigureAwait(false);

                if (data.IsNullOrEmpty)
                {
                    return(null);
                }

                var doc = TinyJsonParser.ParseObject(data);

                if (doc == null)
                {
                    return(null);
                }

                long rv = 0;

                if (doc.ContainsKey("cluster"))
                {
                    rv = await trans.GetReadVersionAsync();
                }

                return(new FdbSystemStatus(doc, rv, data));
            }
Ejemplo n.º 3
0
		/// <summary>Checks if a (key, value) pair exists</summary>
		public async Task<bool> ContainsAsync([NotNull] IFdbReadOnlyTransaction trans, TKey key, TValue value)
		{
			if (trans == null) throw new ArgumentNullException(nameof(trans));
		
			var v = await trans.GetAsync(this.Subspace.Keys[key, value]).ConfigureAwait(false);
			return this.AllowNegativeValues ? v.IsPresent : v.ToInt64() > 0;
		}
Ejemplo n.º 4
0
		/// <summary>Return the count for the value of a specific key</summary>
		/// <param name="trans"></param>
		/// <param name="key"></param>
		/// <param name="value"></param>
		/// <returns>Value for this value, or null if the index does not contains that particular value</returns>
		/// <remarks>The count can be zero or negative if AllowNegativeValues is enable.</remarks>
		public async Task<long?> GetCountAsync([NotNull] IFdbReadOnlyTransaction trans, TKey key, TValue value)
		{
			if (trans == null) throw new ArgumentNullException(nameof(trans));

			Slice v = await trans.GetAsync(this.Subspace.Keys[key, value]).ConfigureAwait(false);
			if (v.IsNullOrEmpty) return null;
			long c = v.ToInt64();
			return this.AllowNegativeValues || c > 0 ? c : default(long?);
		}
Ejemplo n.º 5
0
        /// <summary>Checks if a (key, value) pair exists</summary>
        public async Task <bool> ContainsAsync([NotNull] IFdbReadOnlyTransaction trans, TKey key, TValue value)
        {
            if (trans == null)
            {
                throw new ArgumentNullException("trans");
            }

            var v = await trans.GetAsync(this.Location.Keys.Encode(key, value)).ConfigureAwait(false);

            return(this.AllowNegativeValues ? v.IsPresent : v.ToInt64() > 0);
        }
            public async Task <bool> ContainsAsync(IFdbReadOnlyTransaction trans, Slice key)
            {
                if (trans == null)
                {
                    throw new ArgumentNullException(nameof(trans));
                }
                if (key.IsNull)
                {
                    throw new ArgumentException("Empty key not allowed in set", nameof(key));
                }

                return((await trans.GetAsync(this.Subspace.Encode(0, key)).ConfigureAwait(false)).HasValue);
            }
        private async Task <string> LookupSlowAsync(IFdbReadOnlyTransaction trans, Slice uid)
        {
            var valueBytes = await trans.GetAsync(UidKey(uid)).ConfigureAwait(false);

            if (valueBytes.IsNull)
            {
                throw new KeyNotFoundException("String intern indentifier not found");
            }

            string value = valueBytes.ToUnicode();

            //BUGBUG: if the uid has just been Interned in the current transaction, and if the transaction fails to commit (conflict, ...) we will insert a bad value in the cache!
            AddToCache(value, uid);

            return(value);
        }
        /// <summary>Read the value of a counter</summary>
        /// <param name="transaction">Transaction to use for the operation</param>
        /// <param name="counterKey">Key of the counter, relative to the list's subspace</param>
        /// <returns></returns>
        public async Task <long?> ReadAsync(IFdbReadOnlyTransaction transaction, TKey counterKey)
        {
            if (transaction == null)
            {
                throw new ArgumentNullException(nameof(transaction));
            }
            if (counterKey == null)
            {
                throw new ArgumentNullException(nameof(counterKey));
            }

            var data = await transaction.GetAsync(this.Location[counterKey]).ConfigureAwait(false);

            if (data.IsNullOrEmpty)
            {
                return(default);
Ejemplo n.º 9
0
        /// <summary>Return the count for the value of a specific key</summary>
        /// <param name="trans"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns>Value for this value, or null if the index does not contains that particular value</returns>
        /// <remarks>The count can be zero or negative if AllowNegativeValues is enable.</remarks>
        public async Task <long?> GetCountAsync([NotNull] IFdbReadOnlyTransaction trans, TKey key, TValue value)
        {
            if (trans == null)
            {
                throw new ArgumentNullException("trans");
            }

            Slice v = await trans.GetAsync(this.Location.Keys.Encode(key, value)).ConfigureAwait(false);

            if (v.IsNullOrEmpty)
            {
                return(null);
            }
            long c = v.ToInt64();

            return(this.AllowNegativeValues || c > 0 ? c : default(long?));
        }
Ejemplo n.º 10
0
        /// <summary>Return the value of a specific field of an hashset</summary>
        /// <param name="trans">Transaction that will be used for this request</param>
        /// <param name="id">Unique identifier of the hashset</param>
        /// <param name="field">Name of the field to read</param>
        /// <returns>Value of the corresponding field, or <see cref="Slice.Nil"/> if it the hashset does not exist, or doesn't have a field with this name</returns>
        public Task <Slice> GetValueAsync(IFdbReadOnlyTransaction trans, IVarTuple id, string field)
        {
            if (trans == null)
            {
                throw new ArgumentNullException(nameof(trans));
            }
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }
            if (string.IsNullOrEmpty(field))
            {
                throw new ArgumentNullException(nameof(field));
            }

            return(trans.GetAsync(GetFieldKey(id, field)));
        }
Ejemplo n.º 11
0
        /// <summary>Return the value of a specific field of an hashset</summary>
        /// <param name="trans">Transaction that will be used for this request</param>
        /// <param name="id">Unique identifier of the hashset</param>
        /// <param name="field">Name of the field to read</param>
        /// <returns>Value of the corresponding field, or Slice.Nil if it the hashset does not exist, or doesn't have a field with this name</returns>
        public Task <Slice> GetValueAsync([NotNull] IFdbReadOnlyTransaction trans, [NotNull] IFdbTuple id, string field)
        {
            if (trans == null)
            {
                throw new ArgumentNullException("trans");
            }
            if (id == null)
            {
                throw new ArgumentNullException("id");
            }
            if (string.IsNullOrEmpty(field))
            {
                throw new ArgumentNullException("field");
            }

            return(trans.GetAsync(GetFieldKey(id, field)));
        }
Ejemplo n.º 12
0
            private async Task <long?> GetSizeInternalAsync(IFdbReadOnlyTransaction trans)
            {
                Slice value = await trans.GetAsync(SizeKey()).ConfigureAwait(false);

                if (value.IsNullOrEmpty)
                {
                    return(default(long?));
                }

                //note: python code stores the size as a string
                long size = Int64.Parse(value.ToString());

                if (size < 0)
                {
                    throw new InvalidOperationException("The internal blob size cannot be negative");
                }
                return(size);
            }
        /// <summary>Returns the value of an existing entry in the map</summary>
        /// <param name="trans">Transaction used for the operation</param>
        /// <param name="id">Key of the entry to read from the map</param>
        /// <returns>Value of the entry if it exists; otherwise, throws an exception</returns>
        /// <exception cref="System.ArgumentNullException">If either <paramref name="trans"/> or <paramref name="id"/> is null.</exception>
        /// <exception cref="System.Collections.Generic.KeyNotFoundException">If the map does not contain an entry with this key.</exception>
        public async Task <TValue> GetAsync([NotNull] IFdbReadOnlyTransaction trans, TKey id)
        {
            if (trans == null)
            {
                throw new ArgumentNullException("trans");
            }
            if (id == null)
            {
                throw new ArgumentNullException("id");
            }

            var data = await trans.GetAsync(this.Location.Keys.Encode(id)).ConfigureAwait(false);

            if (data.IsNull)
            {
                throw new KeyNotFoundException("The given id was not present in the map.");
            }
            return(this.ValueEncoder.DecodeValue(data));
        }
Ejemplo n.º 14
0
        /// <summary>Returns the value of an entry in the map if it exists.</summary>
        /// <param name="trans">Transaction used for the operation</param>
        /// <param name="id">Key of the entry to read from the map</param>
        /// <returns>Optional with the value of the entry it it exists, or an empty result if it is not present in the map.</returns>
        public async Task <(TValue Value, bool HasValue)> TryGetAsync([NotNull] IFdbReadOnlyTransaction trans, TKey id)
        {
            if (trans == null)
            {
                throw new ArgumentNullException(nameof(trans));
            }
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }

            var data = await trans.GetAsync(this.Subspace.Keys[id]).ConfigureAwait(false);

            if (data.IsNull)
            {
                return(default(TValue), false);
            }
            return(this.ValueEncoder.DecodeValue(data), true);
        }
        /// <summary>Returns the value of an entry in the map if it exists.</summary>
        /// <param name="trans">Transaction used for the operation</param>
        /// <param name="id">Key of the entry to read from the map</param>
        /// <returns>Optional with the value of the entry it it exists, or an empty result if it is not present in the map.</returns>
        public async Task <Optional <TValue> > TryGetAsync([NotNull] IFdbReadOnlyTransaction trans, TKey id)
        {
            if (trans == null)
            {
                throw new ArgumentNullException("trans");
            }
            if (id == null)
            {
                throw new ArgumentNullException("id");
            }

            var data = await trans.GetAsync(this.Location.Keys.Encode(id)).ConfigureAwait(false);

            if (data.IsNull)
            {
                return(default(Optional <TValue>));
            }
            return(this.ValueEncoder.DecodeValue(data));
        }
        /// <summary>Read the value of a counter</summary>
        /// <param name="transaction">Transaction to use for the operation</param>
        /// <param name="counterKey">Key of the counter, relative to the list's subspace</param>
        /// <returns></returns>
        public async Task <long?> ReadAsync([NotNull] IFdbReadOnlyTransaction transaction, [NotNull] TKey counterKey)
        {
            if (transaction == null)
            {
                throw new ArgumentNullException("transaction");
            }
            if (counterKey == null)
            {
                throw new ArgumentNullException("counterKey");
            }

            var data = await transaction.GetAsync(this.Location.Keys.Encode(counterKey)).ConfigureAwait(false);

            if (data.IsNullOrEmpty)
            {
                return(default(long?));
            }
            return(data.ToInt64());
        }
Ejemplo n.º 17
0
            /// <summary>Returns the value of an existing entry in the map</summary>
            /// <param name="trans">Transaction used for the operation</param>
            /// <param name="id">Key of the entry to read from the map</param>
            /// <returns>Value of the entry if it exists; otherwise, throws an exception</returns>
            /// <exception cref="System.ArgumentNullException">If either <paramref name="trans"/> or <paramref name="id"/> is null.</exception>
            /// <exception cref="System.Collections.Generic.KeyNotFoundException">If the map does not contain an entry with this key.</exception>
            public async Task <TValue> GetAsync(IFdbReadOnlyTransaction trans, TKey id)
            {
                if (trans == null)
                {
                    throw new ArgumentNullException(nameof(trans));
                }
                if (id == null)
                {
                    throw new ArgumentNullException(nameof(id));
                }

                var data = await trans.GetAsync(this.Subspace[id]).ConfigureAwait(false);

                if (data.IsNull)
                {
                    throw new KeyNotFoundException("The given id was not present in the map.");
                }
                return(this.ValueEncoder.DecodeValue(data) !);
            }
Ejemplo n.º 18
0
        /// <summary>Returns a list of ids matching a specific value</summary>
        /// <param name="trans"></param>
        /// <param name="value">Value to lookup</param>
        /// <param name="reverse"></param>
        /// <returns>List of document ids matching this value for this particular index (can be empty if no document matches)</returns>
        public async Task <IEnumerable <long> > LookupAsync([NotNull] IFdbReadOnlyTransaction trans, TValue value, bool reverse = false)
        {
            var key  = this.Location.Keys.Encode(value);
            var data = await trans.GetAsync(key).ConfigureAwait(false);

            if (data.IsNull)
            {
                return(null);
            }
            if (data.IsEmpty)
            {
                return(Enumerable.Empty <long>());
            }
            var bitmap = new CompressedBitmap(data);

            if (reverse)
            {
                throw new NotImplementedException();                      //TODO: GetView(reverse:true) !
            }
            return(bitmap.GetView().Select(x => (long)x /*BUGBUG 64 bits*/));
        }
        /// <summary>
        /// Get the size (in bytes) of the blob.
        /// </summary>
        /// <returns>Return null if the blob does not exists, 0 if is empty, or the size in bytes</returns>
        public async Task <long?> GetSizeAsync([NotNull] IFdbReadOnlyTransaction trans)
        {
            if (trans == null)
            {
                throw new ArgumentNullException("trans");
            }

            Slice value = await trans.GetAsync(SizeKey()).ConfigureAwait(false);

            if (value.IsNullOrEmpty)
            {
                return(default(long?));
            }

            //note: python code stores the size as a string
            long size = Int64.Parse(value.ToAscii());

            if (size < 0)
            {
                throw new InvalidOperationException("The internal blob size cannot be negative");
            }
            return(size);
        }
		private async Task CheckReadVersionAsync(IFdbReadOnlyTransaction trans)
		{
			var value = await trans.GetAsync(this.RootNode.Pack(VersionKey)).ConfigureAwait(false);
			if (!value.IsNullOrEmpty)
			{
				CheckVersion(value, false);
			}
		}
        /// <summary>Read a key inside a subspace</summary>
        /// <example>
        /// Both lines are equivalent:
        /// tr.GetAsync(new FdbSubspace("Hello"), FdbTuple.Create("World"));
        /// tr.GetAsync(FdbTuple.Create("Hello", "World"));
        /// </example>
        public static Task <Slice> GetAsync(this IFdbReadOnlyTransaction trans, [NotNull] FdbSubspace subspace, IFdbTuple key)
        {
            Contract.Requires(trans != null && subspace != null);

            return(trans.GetAsync(subspace.Pack(key)));
        }
 public virtual Task <Slice> GetAsync(Slice key)
 {
     return(m_transaction.GetAsync(key));
 }
 public Task <Slice> GetAsync([NotNull] IFdbReadOnlyTransaction trans, T key)
 {
     return(trans.GetAsync(EncodeKey(key)));
 }
		/// <summary>
		/// Get the size (in bytes) of the blob.
		/// </summary>
		/// <returns>Return null if the blob does not exists, 0 if is empty, or the size in bytes</returns>
		public async Task<long?> GetSizeAsync(IFdbReadOnlyTransaction trans)
		{
			if (trans == null) throw new ArgumentNullException("trans");

			Slice value = await trans.GetAsync(SizeKey()).ConfigureAwait(false);

			if (value.IsNullOrEmpty) return default(long?);

			//note: python code stores the size as a string
			long size = Int64.Parse(value.ToAscii());
			if (size < 0) throw new InvalidOperationException("The internal blob size cannot be negative");
			return size;
		}
 public Task <Slice> GetAsync([NotNull] IFdbReadOnlyTransaction trans, T1 key1, T2 key2, T3 key3)
 {
     return(trans.GetAsync(EncodeKey(key1, key2, key3)));
 }
Ejemplo n.º 26
0
 /// <inheritdoc />
 public virtual Task <Slice> GetAsync(ReadOnlySpan <byte> key)
 {
     return(m_transaction.GetAsync(key));
 }
		/// <summary>Finds a node subspace, given its path, by walking the tree from the root.</summary>
		/// <returns>Node if it was found, or null</returns>
		private async Task<Node> FindAsync(IFdbReadOnlyTransaction tr, IFdbTuple path)
		{
			Contract.Requires(tr != null && path != null);

			// look for the node by traversing from the root down. Stop when crossing a partition...

			var n = this.RootNode;
			int i = 0;
			Slice layer = Slice.Nil;
			while (i < path.Count)
			{
				if (FdbDirectoryLayer.AnnotateTransactions) tr.Annotate("Looking for child {0} under node {1}...", path.Get<string>(i), n.Key);
				n = NodeWithPrefix(await tr.GetAsync(GetSubDirKey(n, path.Get<string>(i))).ConfigureAwait(false));
				if (n == null)
				{
					return new Node(null, path.Substring(0, i + 1), path, Slice.Empty);
				}

				if (FdbDirectoryLayer.AnnotateTransactions) tr.Annotate("Reading Layer value for subfolder {0} found at {1}", path, n.Key);
				layer = await tr.GetAsync(n.Pack(LayerSuffix)).ConfigureAwait(false);
				if (layer == FdbDirectoryPartition.LayerId)
				{ // stop when reaching a partition
					return new Node(n, path.Substring(0, i + 1), path, FdbDirectoryPartition.LayerId);
				}

				++i;
			}
			return new Node(n, path, path, layer);
		}
		/// <summary>Return the value of a specific field of an hashset</summary>
		/// <param name="trans">Transaction that will be used for this request</param>
		/// <param name="id">Unique identifier of the hashset</param>
		/// <param name="field">Name of the field to read</param>
		/// <returns>Value of the corresponding field, or Slice.Nil if it the hashset does not exist, or doesn't have a field with this name</returns>
		public Task<Slice> GetValueAsync(IFdbReadOnlyTransaction trans, IFdbTuple id, string field)
		{
			if (trans == null) throw new ArgumentNullException("trans");
			if (id == null) throw new ArgumentNullException("id");
			if (string.IsNullOrEmpty(field)) throw new ArgumentNullException("field");

			return trans.GetAsync(GetFieldKey(id, field));
		}