예제 #1
0
        public CountersBatchCommandData(string documentId, List <CounterOperation> counterOperations)
        {
            if (string.IsNullOrWhiteSpace(documentId))
            {
                throw new ArgumentNullException(nameof(documentId));
            }

            Id           = documentId;
            Name         = null;
            ChangeVector = null;

            Counters = new DocumentCountersOperation
            {
                DocumentId = documentId,
                Operations = counterOperations
            };
        }
예제 #2
0
        public void CanGetCounterWhichDoesntExist()
        {
            using (var store = GetDocumentStore())
            {
                using (var session = store.OpenSession())
                {
                    session.Store(new User {
                        Name = "Raven"
                    }, "users/1-A");
                    session.SaveChanges();
                }

                var longCounterName = new string('a', 5);

                var documentCountersOperation = new DocumentCountersOperation
                {
                    DocumentId = "users/1-A",
                    Operations = new List <CounterOperation>()
                    {
                        new CounterOperation()
                        {
                            CounterName = longCounterName, Type = CounterOperationType.Increment, Delta = 5
                        }
                    }
                };

                var counterBatch = new CounterBatch {
                    Documents = new List <DocumentCountersOperation>()
                    {
                        documentCountersOperation
                    }
                };

                store.Operations.Send(new CounterBatchOperation(counterBatch));

                using (var session = store.OpenSession())
                {
                    var dic = session.CountersFor("users/1-A")
                              .Get(new string[] { longCounterName, "no_such" });

                    Assert.Equal(dic.Count, 1);
                }
            }
        }
예제 #3
0
        private static async Task <CommandData> ReadSingleCommand(
            JsonOperationContext ctx,
            Stream stream,
            JsonParserState state,
            UnmanagedJsonParser parser,
            JsonOperationContext.ManagedPinnedBuffer buffer,
            CancellationToken token)
        {
            var commandData = new CommandData();

            if (state.CurrentTokenType != JsonParserToken.StartObject)
            {
                ThrowUnexpectedToken(JsonParserToken.StartObject, state);
            }

            while (true)
            {
                while (parser.Read() == false)
                {
                    await RefillParserBuffer(stream, buffer, parser, token);
                }

                if (state.CurrentTokenType == JsonParserToken.EndObject)
                {
                    break;
                }

                if (state.CurrentTokenType != JsonParserToken.String)
                {
                    ThrowUnexpectedToken(JsonParserToken.String, state);
                }
                switch (GetPropertyType(state))
                {
                case CommandPropertyName.Type:
                    while (parser.Read() == false)
                    {
                        await RefillParserBuffer(stream, buffer, parser, token);
                    }
                    if (state.CurrentTokenType != JsonParserToken.String)
                    {
                        ThrowUnexpectedToken(JsonParserToken.String, state);
                    }
                    commandData.Type = GetCommandType(state, ctx);
                    break;

                case CommandPropertyName.Id:
                    while (parser.Read() == false)
                    {
                        await RefillParserBuffer(stream, buffer, parser, token);
                    }
                    switch (state.CurrentTokenType)
                    {
                    case JsonParserToken.Null:
                        commandData.Id = null;
                        break;

                    case JsonParserToken.String:
                        commandData.Id = GetStringPropertyValue(state);
                        break;

                    default:
                        ThrowUnexpectedToken(JsonParserToken.String, state);
                        break;
                    }
                    break;

                case CommandPropertyName.Name:
                    while (parser.Read() == false)
                    {
                        await RefillParserBuffer(stream, buffer, parser, token);
                    }
                    switch (state.CurrentTokenType)
                    {
                    case JsonParserToken.Null:
                        commandData.Name = null;
                        break;

                    case JsonParserToken.String:
                        commandData.Name = GetStringPropertyValue(state);
                        break;

                    default:
                        ThrowUnexpectedToken(JsonParserToken.String, state);
                        break;
                    }
                    break;

                case CommandPropertyName.DestinationId:
                    while (parser.Read() == false)
                    {
                        await RefillParserBuffer(stream, buffer, parser, token);
                    }
                    switch (state.CurrentTokenType)
                    {
                    case JsonParserToken.Null:
                        commandData.DestinationId = null;
                        break;

                    case JsonParserToken.String:
                        commandData.DestinationId = GetStringPropertyValue(state);
                        break;

                    default:
                        ThrowUnexpectedToken(JsonParserToken.String, state);
                        break;
                    }
                    break;

                case CommandPropertyName.DestinationName:
                    while (parser.Read() == false)
                    {
                        await RefillParserBuffer(stream, buffer, parser, token);
                    }
                    switch (state.CurrentTokenType)
                    {
                    case JsonParserToken.Null:
                        commandData.DestinationName = null;
                        break;

                    case JsonParserToken.String:
                        commandData.DestinationName = GetStringPropertyValue(state);
                        break;

                    default:
                        ThrowUnexpectedToken(JsonParserToken.String, state);
                        break;
                    }
                    break;

                case CommandPropertyName.ContentType:
                    while (parser.Read() == false)
                    {
                        await RefillParserBuffer(stream, buffer, parser, token);
                    }
                    switch (state.CurrentTokenType)
                    {
                    case JsonParserToken.Null:
                        commandData.ContentType = string.Empty;
                        break;

                    case JsonParserToken.String:
                        commandData.ContentType = GetStringPropertyValue(state);
                        break;

                    default:
                        ThrowUnexpectedToken(JsonParserToken.String, state);
                        break;
                    }
                    break;

                case CommandPropertyName.Document:
                    while (parser.Read() == false)
                    {
                        await RefillParserBuffer(stream, buffer, parser, token);
                    }
                    commandData.Document = await ReadJsonObject(ctx, stream, commandData.Id, parser, state, buffer, token);

                    break;

                case CommandPropertyName.Patch:
                    while (parser.Read() == false)
                    {
                        await RefillParserBuffer(stream, buffer, parser, token);
                    }
                    var patch = await ReadJsonObject(ctx, stream, commandData.Id, parser, state, buffer, token);

                    commandData.Patch = PatchRequest.Parse(patch, out commandData.PatchArgs);
                    break;

                case CommandPropertyName.PatchIfMissing:
                    while (parser.Read() == false)
                    {
                        await RefillParserBuffer(stream, buffer, parser, token);
                    }
                    var patchIfMissing = await ReadJsonObject(ctx, stream, commandData.Id, parser, state, buffer, token);

                    commandData.PatchIfMissing = PatchRequest.Parse(patchIfMissing, out commandData.PatchIfMissingArgs);
                    break;

                case CommandPropertyName.ChangeVector:
                    while (parser.Read() == false)
                    {
                        await RefillParserBuffer(stream, buffer, parser, token);
                    }
                    if (state.CurrentTokenType == JsonParserToken.Null)
                    {
                        commandData.ChangeVector = null;
                    }
                    else
                    {
                        if (state.CurrentTokenType != JsonParserToken.String)
                        {
                            ThrowUnexpectedToken(JsonParserToken.String, state);
                        }

                        commandData.ChangeVector = GetLazyStringValue(ctx, state);
                    }
                    break;

                case CommandPropertyName.Index:
                    while (parser.Read() == false)
                    {
                        await RefillParserBuffer(stream, buffer, parser, token);
                    }
                    if (state.CurrentTokenType != JsonParserToken.Integer)
                    {
                        ThrowUnexpectedToken(JsonParserToken.True, state);
                    }
                    commandData.Index = state.Long;

                    break;

                case CommandPropertyName.IdPrefixed:
                    while (parser.Read() == false)
                    {
                        await RefillParserBuffer(stream, buffer, parser, token);
                    }

                    if (state.CurrentTokenType != JsonParserToken.True && state.CurrentTokenType != JsonParserToken.False)
                    {
                        ThrowUnexpectedToken(JsonParserToken.True, state);
                    }

                    commandData.IdPrefixed = state.CurrentTokenType == JsonParserToken.True;
                    break;

                case CommandPropertyName.ReturnDocument:
                    while (parser.Read() == false)
                    {
                        await RefillParserBuffer(stream, buffer, parser, token);
                    }

                    if (state.CurrentTokenType != JsonParserToken.True && state.CurrentTokenType != JsonParserToken.False)
                    {
                        ThrowUnexpectedToken(JsonParserToken.True, state);
                    }

                    commandData.ReturnDocument = state.CurrentTokenType == JsonParserToken.True;
                    break;

                case CommandPropertyName.Counters:
                    while (parser.Read() == false)
                    {
                        await RefillParserBuffer(stream, buffer, parser, token);
                    }
                    var counterOps = await ReadJsonObject(ctx, stream, commandData.Id, parser, state, buffer, token);

                    commandData.Counters = DocumentCountersOperation.Parse(counterOps);
                    break;

                case CommandPropertyName.FromEtl:
                    while (parser.Read() == false)
                    {
                        await RefillParserBuffer(stream, buffer, parser, token);
                    }

                    if (state.CurrentTokenType != JsonParserToken.True && state.CurrentTokenType != JsonParserToken.False)
                    {
                        ThrowUnexpectedToken(JsonParserToken.True, state);
                    }

                    commandData.FromEtl = state.CurrentTokenType == JsonParserToken.True;
                    break;

                case CommandPropertyName.NoSuchProperty:
                    // unknown command - ignore it
                    while (parser.Read() == false)
                    {
                        await RefillParserBuffer(stream, buffer, parser, token);
                    }
                    if (state.CurrentTokenType == JsonParserToken.StartObject ||
                        state.CurrentTokenType == JsonParserToken.StartArray)
                    {
                        await ReadJsonObject(ctx, stream, commandData.Id, parser, state, buffer, token);
                    }
                    break;
                }
            }

            switch (commandData.Type)
            {
            case CommandType.None:
                ThrowInvalidType();
                break;

            case CommandType.PUT:
                if (commandData.Document == null)
                {
                    ThrowMissingDocumentProperty();
                }
                break;

            case CommandType.PATCH:
                if (commandData.Patch == null)
                {
                    ThrowMissingPatchProperty();
                }
                break;

            case CommandType.AttachmentPUT:
                if (commandData.Name == null)
                {
                    ThrowMissingNameProperty();
                }
                break;

            case CommandType.Counters:
                if (commandData.Counters == null)
                {
                    ThrowMissingNameProperty();
                }
                break;
            }

            return(commandData);
        }
예제 #4
0
        public async Task Post(CreatePerks request)
        {
            var perks = new List <Perk>()
            {
                new Perk
                {
                    Id        = "Perk/vegan",
                    Name      = "Веганство",
                    ImagePath = "https://github.com/Kibnet/Axiometr.Server/raw/master/resources/vegan.png",
                    KeyWords  = new List <string>
                    {
                        "сыроед",
                        "веган",
                        "вегетариан",
                    }
                },
                new Perk
                {
                    Id        = "Perk/eatmeet",
                    Name      = "Мясоедство",
                    ImagePath = "https://github.com/Kibnet/Axiometr.Server/raw/master/resources/eatmeet.png",
                    KeyWords  = new List <string>
                    {
                        "мясо",
                        "мясоед",
                    }
                },
                new Perk
                {
                    Id        = "Perk/antivaccination",
                    Name      = "Против прививок",
                    ImagePath = "https://github.com/Kibnet/Axiometr.Server/raw/master/resources/antivaccination.png",
                    KeyWords  = new List <string>
                    {
                        "прививка",
                        "вакцина",
                        "грип",
                        "covid",
                        "ковид",
                    }
                },
                new Perk
                {
                    Id        = "Perk/vaccination",
                    Name      = "За прививки",
                    ImagePath = "https://github.com/Kibnet/Axiometr.Server/raw/master/resources/vaccination.png",
                    KeyWords  = new List <string>
                    {
                        "прививка",
                        "вакцина",
                        "грип",
                        "covid",
                        "ковид",
                    }
                },
                new Perk
                {
                    Id        = "Perk/krymru",
                    Name      = "Крым - РФ",
                    ImagePath = "https://github.com/Kibnet/Axiometr.Server/raw/master/resources/krymru.png",
                    KeyWords  = new List <string>
                    {
                        "крым",
                    }
                },
                new Perk
                {
                    Id        = "Perk/krymua",
                    Name      = "Крым - Украина",
                    ImagePath = "https://github.com/Kibnet/Axiometr.Server/raw/master/resources/krymua.png",
                    KeyWords  = new List <string>
                    {
                        "крым",
                    }
                },
                new Perk
                {
                    Id        = "Perk/aborting",
                    Name      = "За аборты",
                    ImagePath = "https://github.com/Kibnet/Axiometr.Server/raw/master/resources/aborting.png",
                    KeyWords  = new List <string>
                    {
                        "аборт",
                    }
                },
                new Perk
                {
                    Id        = "Perk/antiaborting",
                    Name      = "Против абортов",
                    ImagePath = "https://github.com/Kibnet/Axiometr.Server/raw/master/resources/antiaborting.png",
                    KeyWords  = new List <string>
                    {
                        "аборт",
                    }
                },
                new Perk
                {
                    Id        = "Perk/traditionsex",
                    Name      = "Традиционные половые отношения",
                    ImagePath = "https://github.com/Kibnet/Axiometr.Server/raw/master/resources/traditionsex.png",
                    KeyWords  = new List <string>
                    {
                        "семья",
                        "дети",
                        "ребенок",
                        "любов",
                    }
                },
                new Perk
                {
                    Id        = "Perk/nontraditionsex",
                    Name      = "Не традиционные половые отношения",
                    ImagePath = "https://github.com/Kibnet/Axiometr.Server/raw/master/resources/nontraditionsex.png",
                    KeyWords  = new List <string>
                    {
                        "геи",
                        "лесби",
                        "лгбт",
                        "транссексуал",
                        "прайд",
                        "любов",
                    }
                },
                new Perk
                {
                    Id        = "Perk/filthylanguage",
                    Name      = "Мат",
                    ImagePath = "https://github.com/Kibnet/Axiometr.Server/raw/master/resources/filthylanguage.png",
                    KeyWords  = new List <string>
                    {
                        "редиска",
                        "f**k",
                    }
                },
                new Perk
                {
                    Id        = "Perk/adult",
                    Name      = "18+",
                    ImagePath = "https://github.com/Kibnet/Axiometr.Server/raw/master/resources/adult.png",
                    KeyWords  = new List <string>
                    {
                        "порно",
                        "секс",
                        "убить",
                        "кровь",
                    }
                }
            };

            foreach (var perk in perks)
            {
                await RavenSession.StoreAsync(perk);
            }

            var medusa = new PageProfile
            {
                Address = "https://meduza.io/"
            };
            var wonderzine = new PageProfile
            {
                Address = "https://www.wonderzine.com/"
            };
            var profiles = new List <PageProfile>
            {
                medusa,
                wonderzine,
            };

            foreach (var profile in profiles)
            {
                await RavenSession.StoreAsync(profile);
            }

            await RavenSession.SaveChangesAsync();

            DocumentCountersOperation operation1 = new DocumentCountersOperation
            {
                DocumentId = RavenSession.Advanced.GetDocumentId(medusa),
                Operations = new List <CounterOperation>
                {
                    new CounterOperation()
                    {
                        CounterName = "krymua",
                        Delta       = 10,
                        Type        = CounterOperationType.Increment,
                    },
                }
            };

            CounterBatch counterBatch = new CounterBatch();

            counterBatch.Documents.Add(operation1);
            var counteBatch = new CounterBatchOperation(counterBatch);

            RavenSession.Advanced.DocumentStore.Operations.Send(counteBatch);
        }