Beispiel #1
0
        public Etag CreateSequentialUuid(UuidType type)
        {
            var bytes = new byte[16];

            bytes[15] += ++cur;
            return(Etag.Parse(bytes));
        }
        public Guid CreateSequentialUuid(UuidType type)
        {
            var bytes = new byte[16];

            bytes[15] += ++cur;
            return(new Guid(bytes));
        }
Beispiel #3
0
		public void Set(string name, string key, RavenJObject data, UuidType uuidType)
		{
			Api.JetSetCurrentIndex(session, Lists, "by_name_and_key");
			Api.MakeKey(session, Lists, name, Encoding.Unicode, MakeKeyGrbit.NewKey);
			Api.MakeKey(session, Lists, key, Encoding.Unicode, MakeKeyGrbit.None);

			var exists = Api.TrySeek(session, Lists, SeekGrbit.SeekEQ);


			using (var update = new Update(session, Lists, exists ? JET_prep.Replace : JET_prep.Insert))
			{
				Api.SetColumn(session, Lists, tableColumnsCache.ListsColumns["name"], name, Encoding.Unicode);
				Api.SetColumn(session, Lists, tableColumnsCache.ListsColumns["key"], key, Encoding.Unicode);
				Api.SetColumn(session, Lists, tableColumnsCache.ListsColumns["etag"], uuidGenerator.CreateSequentialUuid(uuidType).TransformToValueForEsentSorting());
				Api.SetColumn(session, Lists, tableColumnsCache.ListsColumns["created_at"], SystemTime.UtcNow);

				using (var columnStream = new ColumnStream(session, Lists, tableColumnsCache.ListsColumns["data"]))
				{
					if (exists)
						columnStream.SetLength(0);
					using (Stream stream = new BufferedStream(columnStream))
					{
						data.WriteTo(stream);
						stream.Flush();
					}
				}
				update.Save();
			}
		}
Beispiel #4
0
        public Etag CreateSequentialUuid(UuidType type)
        {
            long increment;

            switch (type)
            {
            case UuidType.Documents:
                increment = Interlocked.Increment(ref sequentialUuidCounterDocuments);
                break;

            case UuidType.Attachments:
                increment = Interlocked.Increment(ref sequentialUuidCounterAttachments);
                break;

            case UuidType.DocumentTransactions:
                increment = Interlocked.Increment(ref sequentialUuidCounterDocumentsTransactions);
                break;

            case UuidType.MappedResults:
                increment = Interlocked.Increment(ref sequentialUuidCounterMappedResults);
                break;

            case UuidType.ReduceResults:
                increment = Interlocked.Increment(ref sequentialUuidCounterReduceResults);
                break;

            case UuidType.Queue:
                increment = Interlocked.Increment(ref sequentialUuidCounterQueue);
                break;

            case UuidType.Tasks:
                increment = Interlocked.Increment(ref sequentialUuidCounterTasks);
                break;

            case UuidType.ScheduledReductions:
                increment = Interlocked.Increment(ref sequentialUuidCounterScheduledReductions);
                break;

            case UuidType.Indexing:
                increment = Interlocked.Increment(ref sequentialUuidCounterIndexing);
                break;

            case UuidType.DocumentReferences:
                increment = Interlocked.Increment(ref sequentialUuidDocumentReferences);
                break;

            default:
                throw new ArgumentOutOfRangeException("type", "Cannot understand: " + type);
            }

            var currentAsBytes = BitConverter.GetBytes(increment);

            Array.Reverse(currentAsBytes);
            var bytes = new byte[16];

            Array.Copy(ticksAsBytes, 0, bytes, 0, ticksAsBytes.Length);
            Array.Copy(currentAsBytes, 0, bytes, 8, currentAsBytes.Length);
            bytes[0] = (byte)type;              // record the etag type, if we need it for debug later
            return(Etag.Parse(bytes));
        }
Beispiel #5
0
        public void Set(string name, string key, RavenJObject data, UuidType uuidType)
        {
            Api.JetSetCurrentIndex(session, Lists, "by_name_and_key");
            Api.MakeKey(session, Lists, name, Encoding.Unicode, MakeKeyGrbit.NewKey);
            Api.MakeKey(session, Lists, key, Encoding.Unicode, MakeKeyGrbit.None);

            var exists = Api.TrySeek(session, Lists, SeekGrbit.SeekEQ);


            using (var update = new Update(session, Lists, exists ? JET_prep.Replace : JET_prep.Insert))
            {
                Api.SetColumn(session, Lists, tableColumnsCache.ListsColumns["name"], name, Encoding.Unicode);
                Api.SetColumn(session, Lists, tableColumnsCache.ListsColumns["key"], key, Encoding.Unicode);
                Api.SetColumn(session, Lists, tableColumnsCache.ListsColumns["etag"], uuidGenerator.CreateSequentialUuid(uuidType).TransformToValueForEsentSorting());
                using (var columnStream = new ColumnStream(session, Lists, tableColumnsCache.ListsColumns["data"]))
                {
                    if (exists)
                    {
                        columnStream.SetLength(0);
                    }
                    using (Stream stream = new BufferedStream(columnStream))
                    {
                        data.WriteTo(stream);
                        stream.Flush();
                    }
                }
                update.Save();
            }
        }
Beispiel #6
0
        public void Set(string name, string key, RavenJObject data, UuidType type)
        {
            var listsByName       = tableStorage.Lists.GetIndex(Tables.Lists.Indices.ByName);
            var listsByNameAndKey = tableStorage.Lists.GetIndex(Tables.Lists.Indices.ByNameAndKey);

            var etag         = generator.CreateSequentialUuid(type);
            var etagAsString = etag.ToString();
            var etagAsSlice  = (Slice)etagAsString;
            var createdAt    = SystemTime.UtcNow;

            tableStorage.Lists.Add(
                writeBatch.Value,
                etagAsSlice,
                new RavenJObject
            {
                { "name", name },
                { "key", key },
                { "etag", etag.ToByteArray() },
                { "data", data },
                { "createdAt", createdAt }
            });

            var nameKey = CreateKey(name);

            listsByName.MultiAdd(writeBatch.Value, (Slice)nameKey, etagAsSlice);
            listsByNameAndKey.Add(writeBatch.Value, (Slice)AppendToKey(nameKey, key), etagAsString);
        }
		public void Set(string name, string key, RavenJObject data, UuidType type)
		{
			var listsByName = tableStorage.Lists.GetIndex(Tables.Lists.Indices.ByName);
			var listsByNameAndKey = tableStorage.Lists.GetIndex(Tables.Lists.Indices.ByNameAndKey);

			var etag = generator.CreateSequentialUuid(type);
            var etagAsString = etag.ToString();
            var etagAsSlice = (Slice)etagAsString;
			var createdAt = SystemTime.UtcNow;

			tableStorage.Lists.Add(
				writeBatch.Value,
				etagAsSlice,
				new RavenJObject
				{
					{ "name", name }, 
					{ "key", key }, 
					{ "etag", etag.ToByteArray() }, 
					{ "data", data },
					{ "createdAt", createdAt}
				});

            var nameKey = CreateKey(name);

            listsByName.MultiAdd(writeBatch.Value, (Slice)nameKey, etagAsSlice);
            listsByNameAndKey.Add(writeBatch.Value, (Slice)AppendToKey(nameKey, key), etagAsString);
		}
Beispiel #8
0
 public Etag Setup(UuidType type, long restartsNum)
 {
     return(new Etag
     {
         restarts = ((long)type << 56) | restartsNum,
         changes = changes
     });
 }
Beispiel #9
0
        /// <summary>
        /// Returns the scalar type for the strings, decimals, uris, etc...
        /// </summary>
        /// <param name="type"></param>
        /// <param name="scalarType"></param>
        /// <returns></returns>
        public static bool TryGetBuiltInScalarType(
            Type type,
            [NotNullWhen(true)] out ScalarType?scalarType
            )
        {
            if (type == typeof(string))
            {
                scalarType = new StringType();
            }
            else if (type == typeof(decimal))
            {
                scalarType = new DecimalType();
            }
            else if (type == typeof(int))
            {
                scalarType = new IntType();
            }
            else if (type == typeof(bool))
            {
                scalarType = new BooleanType();
            }
            else if (type == typeof(float))
            {
                scalarType = new FloatType();
            }
            else if (type == typeof(Guid))
            {
                scalarType = new UuidType();
            }
            else if (type == typeof(DateTime))
            {
                scalarType = new DateTimeType();
            }
            else if (type == typeof(byte))
            {
                scalarType = new ByteType();
            }
            else if (type == typeof(Uri))
            {
                scalarType = new UrlType();
            }
            else if (type == typeof(long))
            {
                scalarType = new LongType();
            }
            else if (type == typeof(short))
            {
                scalarType = new ShortType();
            }
            else
            {
                scalarType = null;
            }

            return(scalarType is object);
        }
Beispiel #10
0
        public void Write(UuidType uuidType, object value)
        {
            var guid  = ExtractGuid(value);
            var bytes = guid.ToByteArray();

            Array.Reverse(bytes, 8, 8);
            writer.Write(bytes, 6, 2);
            writer.Write(bytes, 4, 2);
            writer.Write(bytes, 0, 4);
            writer.Write(bytes, 8, 8);
        }
Beispiel #11
0
		public void Set(string name, string key, RavenJObject data, UuidType type)
		{
			var memoryStream = new MemoryStream();
			data.WriteTo(memoryStream);
			
			storage.Lists.Put(new RavenJObject
			{
				{"name", name},
				{"key", key},
				{"etag", generator.CreateSequentialUuid(type).ToByteArray()}
			}, memoryStream.ToArray());
		}
        public object Read(UuidType uuidType)
        {
            // Byte manipulation because of ClickHouse's weird GUID/UUID implementation
            var bytes = new byte[16];

            reader.Read(bytes, 6, 2);
            reader.Read(bytes, 4, 2);
            reader.Read(bytes, 0, 4);
            reader.Read(bytes, 8, 8);
            Array.Reverse(bytes, 8, 8);
            return(new Guid(bytes));
        }
		public Etag CreateSequentialUuid(UuidType type)
		{
			long increment;
			switch (type)
			{
				case UuidType.Documents:
					increment = Interlocked.Increment(ref sequentialUuidCounterDocuments);
					break;
				case UuidType.Attachments:
					increment = Interlocked.Increment(ref sequentialUuidCounterAttachments);
					break;
				case UuidType.DocumentTransactions:
					increment = Interlocked.Increment(ref sequentialUuidCounterDocumentsTransactions);
					break;
				case UuidType.MappedResults:
					increment = Interlocked.Increment(ref sequentialUuidCounterMappedResults);
					break;
				case UuidType.ReduceResults:
					increment = Interlocked.Increment(ref sequentialUuidCounterReduceResults);
					break;
				case UuidType.Queue:
					increment = Interlocked.Increment(ref sequentialUuidCounterQueue);
					break;
				case UuidType.Tasks:
					increment = Interlocked.Increment(ref sequentialUuidCounterTasks);
					break;
				case UuidType.ScheduledReductions:
					increment = Interlocked.Increment(ref sequentialUuidCounterScheduledReductions);
					break;
				case UuidType.Indexing:
					increment = Interlocked.Increment(ref sequentialUuidCounterIndexing);
					break;
				case UuidType.Transformers:
					increment = Interlocked.Increment(ref sequentialUuidCounterTransformers);
					break;
				case UuidType.DocumentReferences:
					increment = Interlocked.Increment(ref sequentialUuidDocumentReferences);
					break;
				case UuidType.Subscriptions:
					increment = Interlocked.Increment(ref sequentialUuidSubscriptions);
					break;
				default:
					throw new ArgumentOutOfRangeException("type", "Cannot understand: " + type);
			}

			var currentAsBytes = BitConverter.GetBytes(increment);
			Array.Reverse(currentAsBytes);
			var bytes = new byte[16];
			Array.Copy(ticksAsBytes, 0, bytes, 0, ticksAsBytes.Length);
			Array.Copy(currentAsBytes, 0, bytes, 8, currentAsBytes.Length);
			bytes[0] = (byte) type; // record the etag type, if we need it for debug later
			return Etag.Parse(bytes);
		}
Beispiel #14
0
        public void Set(string name, string key, RavenJObject data, UuidType type)
        {
            var memoryStream = new MemoryStream();

            data.WriteTo(memoryStream);

            storage.Lists.Put(new RavenJObject
            {
                { "name", name },
                { "key", key },
                { "etag", generator.CreateSequentialUuid(type).ToByteArray() }
            }, memoryStream.ToArray());
        }
        public void Set(string name, string key, RavenJObject data, UuidType type)
        {
            var listsByName       = tableStorage.Lists.GetIndex(Tables.Lists.Indices.ByName);
            var listsByNameAndKey = tableStorage.Lists.GetIndex(Tables.Lists.Indices.ByNameAndKey);

            var nameKey         = CreateKey(name);
            var nameKeySlice    = (Slice)nameKey;
            var nameAndKeySlice = (Slice)AppendToKey(nameKey, key);

            string existingEtag = null;
            bool   update       = false;

            var read = listsByNameAndKey.Read(Snapshot, nameAndKeySlice, writeBatch.Value);

            if (read != null)
            {
                update = true;

                using (var stream = read.Reader.AsStream())
                {
                    using (var reader = new StreamReader(stream))
                        existingEtag = reader.ReadToEnd();
                }
            }

            var etag        = generator.CreateSequentialUuid(type);
            var internalKey = update == false?etag.ToString() : existingEtag;

            var internalKeyAsSlice = (Slice)internalKey;
            var createdAt          = SystemTime.UtcNow;

            tableStorage.Lists.Add(
                writeBatch.Value,
                internalKeyAsSlice,
                new RavenJObject
            {
                { "name", name },
                { "key", key },
                { "etag", etag.ToByteArray() },
                { "data", data },
                { "createdAt", createdAt }
            });

            if (update == false)
            {
                listsByName.MultiAdd(writeBatch.Value, nameKeySlice, internalKeyAsSlice);
                listsByNameAndKey.Add(writeBatch.Value, nameAndKeySlice, internalKey);
            }
        }
        public void Set(string name, string key, RavenJObject data, UuidType type)
        {
            var listsByName = tableStorage.Lists.GetIndex(Tables.Lists.Indices.ByName);
            var listsByNameAndKey = tableStorage.Lists.GetIndex(Tables.Lists.Indices.ByNameAndKey);

            var nameKey = CreateKey(name);
            var nameKeySlice = (Slice)nameKey;
            var nameAndKeySlice = (Slice)AppendToKey(nameKey, key);

            string existingEtag = null;
            bool update = false;

            var read = listsByNameAndKey.Read(Snapshot, nameAndKeySlice, writeBatch.Value);
            if (read != null)
            {
                update = true;

                using (var stream = read.Reader.AsStream())
                {
                    using (var reader = new StreamReader(stream))
                        existingEtag = reader.ReadToEnd();
                }
            }

            var etag = generator.CreateSequentialUuid(type);
            var internalKey = update == false ? etag.ToString() : existingEtag;
            var internalKeyAsSlice = (Slice)internalKey;
            var createdAt = SystemTime.UtcNow;

            tableStorage.Lists.Add(
                writeBatch.Value,
                internalKeyAsSlice,
                new RavenJObject
                {
                    { "name", name }, 
                    { "key", key }, 
                    { "etag", etag.ToByteArray() }, 
                    { "data", data },
                    { "createdAt", createdAt}
                });

            if (update == false)
            {
                listsByName.MultiAdd(writeBatch.Value, nameKeySlice, internalKeyAsSlice);
                listsByNameAndKey.Add(writeBatch.Value, nameAndKeySlice, internalKey);
            }
        }
        public void Touch(string name, string key, UuidType uuidType, out Etag preTouchEtag, out Etag afterTouchEtag)
        {
            var item = Read(name, key);

            if (item == null)
            {
                afterTouchEtag = null;
                preTouchEtag   = null;
                return;
            }

            preTouchEtag = item.Etag;

            Remove(name, key);

            afterTouchEtag = generator.CreateSequentialUuid(uuidType);
            var internalKey        = afterTouchEtag.ToString();
            var internalKeyAsSlice = (Slice)internalKey;

            tableStorage.Lists.Add(
                writeBatch.Value,
                internalKeyAsSlice,
                new RavenJObject
            {
                { "name", name },
                { "key", key },
                { "etag", afterTouchEtag.ToByteArray() },
                { "data", item.Data },
                { "createdAt", item.CreatedAt }
            });

            var listsByName       = tableStorage.Lists.GetIndex(Tables.Lists.Indices.ByName);
            var listsByNameAndKey = tableStorage.Lists.GetIndex(Tables.Lists.Indices.ByNameAndKey);

            var nameKey         = CreateKey(name);
            var nameKeySlice    = (Slice)nameKey;
            var nameAndKeySlice = (Slice)AppendToKey(nameKey, key);

            listsByName.MultiAdd(writeBatch.Value, nameKeySlice, internalKeyAsSlice);
            listsByNameAndKey.Add(writeBatch.Value, nameAndKeySlice, internalKey);
        }
Beispiel #18
0
        public void Touch(string name, string key, UuidType uuidType, out Etag preTouchEtag, out Etag afterTouchEtag)
        {
            Api.JetSetCurrentIndex(session, Lists, "by_name_and_key");
            Api.MakeKey(session, Lists, name, Encoding.Unicode, MakeKeyGrbit.NewKey);
            Api.MakeKey(session, Lists, key, Encoding.Unicode, MakeKeyGrbit.None);

            if (Api.TrySeek(session, Lists, SeekGrbit.SeekEQ) == false)
            {
                afterTouchEtag = null;
                preTouchEtag   = null;
                return;
            }

            preTouchEtag = Etag.Parse(Api.RetrieveColumn(session, Lists, tableColumnsCache.ListsColumns["etag"]));

            using (var update = new Update(session, Lists, JET_prep.Replace))
            {
                afterTouchEtag = uuidGenerator.CreateSequentialUuid(uuidType);
                Api.SetColumn(session, Lists, tableColumnsCache.ListsColumns["etag"], afterTouchEtag.TransformToValueForEsentSorting());
                update.Save();
            }
        }
		public void Set(string name, string key, RavenJObject data, UuidType type)
		{
			var listsByName = tableStorage.Lists.GetIndex(Tables.Lists.Indices.ByName);
			var listsByNameAndKey = tableStorage.Lists.GetIndex(Tables.Lists.Indices.ByNameAndKey);

			var etag = generator.CreateSequentialUuid(type);
			var etagAsString = etag.ToString();

			tableStorage.Lists.Add(
				writeBatch.Value,
				etagAsString,
				new RavenJObject
				{
					{ "name", name }, 
					{ "key", key }, 
					{ "etag", etag.ToByteArray() }, 
					{ "data", data }
				});

			listsByName.MultiAdd(writeBatch.Value, CreateKey(name), etagAsString);
			listsByNameAndKey.Add(writeBatch.Value, CreateKey(name, key), etagAsString);
		}
        public void Set(string name, string key, RavenJObject data, UuidType type)
        {
            var listsByName       = tableStorage.Lists.GetIndex(Tables.Lists.Indices.ByName);
            var listsByNameAndKey = tableStorage.Lists.GetIndex(Tables.Lists.Indices.ByNameAndKey);

            var etag         = generator.CreateSequentialUuid(type);
            var etagAsString = etag.ToString();

            tableStorage.Lists.Add(
                writeBatch.Value,
                etagAsString,
                new RavenJObject
            {
                { "name", name },
                { "key", key },
                { "etag", etag.ToByteArray() },
                { "data", data }
            });

            listsByName.MultiAdd(writeBatch.Value, CreateKey(name), etagAsString);
            listsByNameAndKey.Add(writeBatch.Value, CreateKey(name, key), etagAsString);
        }
 public static Error InvalidId(UuidType id)
 {
     return(new Error {
         ErrorMessage = $"Invalid id: {id}"
     });
 }
Beispiel #22
0
		public Guid CreateSequentialUuid(UuidType type)
		{
			var bytes = new byte[16];
			bytes[15] += ++cur;
			return new Guid(bytes);
		}
Beispiel #23
0
	    public Etag CreateSequentialUuid(UuidType type)
		{
			var bytes = new byte[16];
			bytes[15] += ++cur;
			return Etag.Parse(bytes);
		}
Beispiel #24
0
 public Etag(UuidType type, long restarts, long changes)
 {
     this.restarts = ((long)type << 56) | restarts;
     this.changes  = changes;
 }
        public void Touch(string name, string key, UuidType uuidType, out Etag preTouchEtag, out Etag afterTouchEtag)
        {
            var item = Read(name, key);
            if (item == null)
            {
                afterTouchEtag = null;
                preTouchEtag = null;
                return;
            }

            preTouchEtag = item.Etag;

            Remove(name, key);

            afterTouchEtag = generator.CreateSequentialUuid(uuidType);
            var internalKey = afterTouchEtag.ToString();
            var internalKeyAsSlice = (Slice) internalKey;

            tableStorage.Lists.Add(
                writeBatch.Value,
                internalKeyAsSlice,
                new RavenJObject
                {
                    {"name", name},
                    {"key", key},
                    {"etag", afterTouchEtag.ToByteArray()},
                    {"data", item.Data},
                    {"createdAt", item.CreatedAt}
                });

            var listsByName = tableStorage.Lists.GetIndex(Tables.Lists.Indices.ByName);
            var listsByNameAndKey = tableStorage.Lists.GetIndex(Tables.Lists.Indices.ByNameAndKey);

            var nameKey = CreateKey(name);
            var nameKeySlice = (Slice) nameKey;
            var nameAndKeySlice = (Slice) AppendToKey(nameKey, key);

            listsByName.MultiAdd(writeBatch.Value, nameKeySlice, internalKeyAsSlice);
            listsByNameAndKey.Add(writeBatch.Value, nameAndKeySlice, internalKey);
        }
Beispiel #26
0
        public void Touch(string name, string key, UuidType uuidType, out Etag preTouchEtag, out Etag afterTouchEtag)
        {
            Api.JetSetCurrentIndex(session, Lists, "by_name_and_key");
            Api.MakeKey(session, Lists, name, Encoding.Unicode, MakeKeyGrbit.NewKey);
            Api.MakeKey(session, Lists, key, Encoding.Unicode, MakeKeyGrbit.None);

            if (Api.TrySeek(session, Lists, SeekGrbit.SeekEQ) == false)
            {
                afterTouchEtag = null;
                preTouchEtag = null;
                return;
            }

            preTouchEtag = Etag.Parse(Api.RetrieveColumn(session, Lists, tableColumnsCache.ListsColumns["etag"]));

            using (var update = new Update(session, Lists, JET_prep.Replace))
            {
                afterTouchEtag = uuidGenerator.CreateSequentialUuid(uuidType);
                Api.SetColumn(session, Lists, tableColumnsCache.ListsColumns["etag"], afterTouchEtag.TransformToValueForEsentSorting());
                update.Save();
            }
        }