Example #1
0
        public void AfterBackupRestoreCanQueryIndex_CreatedBeforeRestore()
        {
            db.Documents.Put("ayende", null, RavenJObject.Parse("{'email':'*****@*****.**'}"), RavenJObject.Parse("{'Raven-Entity-Name':'Users'}"), null);
            db.SpinBackgroundWorkers();
            QueryResult queryResult;

            do
            {
                queryResult = db.Queries.Query("Raven/DocumentsByEntityName", new IndexQuery
                {
                    Query    = "Tag:[[Users]]",
                    PageSize = 10
                }, CancellationToken.None);
            } while (queryResult.IsStale);
            Assert.Equal(1, queryResult.Results.Count);

            db.Maintenance.StartBackup(BackupDir, false, new DatabaseDocument());
            WaitForBackup(db, true);

            db.Dispose();
            IOExtensions.DeleteDirectory(DataDir);

            MaintenanceActions.Restore(new RavenConfiguration(), new DatabaseRestoreRequest
            {
                BackupLocation   = BackupDir,
                DatabaseLocation = DataDir,
                Defrag           = true
            }, s => { });

            db = new DocumentDatabase(new RavenConfiguration {
                DataDirectory = DataDir
            });

            queryResult = db.Queries.Query("Raven/DocumentsByEntityName", new IndexQuery
            {
                Query    = "Tag:[[Users]]",
                PageSize = 10
            }, CancellationToken.None);
            Assert.Equal(1, queryResult.Results.Count);
        }
Example #2
0
        public static void Restore(RavenConfiguration configuration, DatabaseRestoreRequest restoreRequest, Action <string> output)
        {
            var databaseDocumentPath = FindDatabaseDocument(restoreRequest.BackupLocation);

            if (File.Exists(databaseDocumentPath) == false)
            {
                throw new InvalidOperationException("Cannot restore when the Database.Document file is missing in the backup folder: " + restoreRequest.BackupLocation);
            }

            var databaseDocumentText = File.ReadAllText(databaseDocumentPath);
            var databaseDocument     = RavenJObject.Parse(databaseDocumentText).JsonDeserialization <DatabaseDocument>();

            string storage;

            if (databaseDocument.Settings.TryGetValue("Raven/StorageTypeName", out storage) == false)
            {
                if (File.Exists(Path.Combine(restoreRequest.BackupLocation, BackupMethods.Filename)))
                {
                    storage = InMemoryRavenConfiguration.VoronTypeName;
                }
                else if (Directory.Exists(Path.Combine(restoreRequest.BackupLocation, "new")))
                {
                    storage = InMemoryRavenConfiguration.EsentTypeName;
                }
                else
                {
                    storage = InMemoryRavenConfiguration.EsentTypeName;
                }
            }

            if (!string.IsNullOrWhiteSpace(restoreRequest.DatabaseLocation))
            {
                configuration.DataDirectory = restoreRequest.DatabaseLocation;
            }

            using (var transactionalStorage = configuration.CreateTransactionalStorage(storage, () => { }, () => { }))
            {
                transactionalStorage.Restore(restoreRequest, output);
            }
        }
        public void CanGetReducedValues()
        {
            var values = new[]
            {
                "{blog_id: 3, comments: [{},{},{}]}",
                "{blog_id: 5, comments: [{},{},{},{}]}",
                "{blog_id: 6, comments: [{},{},{},{},{},{}]}",
                "{blog_id: 7, comments: [{}]}",
                "{blog_id: 3, comments: [{},{},{}]}",
                "{blog_id: 3, comments: [{},{},{},{},{}]}",
                "{blog_id: 2, comments: [{},{},{},{},{},{},{},{}]}",
                "{blog_id: 4, comments: [{},{},{}]}",
                "{blog_id: 5, comments: [{},{}]}",
                "{blog_id: 3, comments: [{},{},{}]}",
                "{blog_id: 5, comments: [{}]}",
            };

            for (int i = 0; i < values.Length; i++)
            {
                db.Put("docs/" + i, null, RavenJObject.Parse(values[i]), new RavenJObject(), null);
            }

            QueryResult q = null;

            for (var i = 0; i < 5; i++)
            {
                do
                {
                    q = db.Query("CommentsCountPerBlog", new IndexQuery
                    {
                        Query    = "blog_id:3",
                        Start    = 0,
                        PageSize = 10
                    });
                    Thread.Sleep(100);
                } while (q.IsStale);
            }
            q.Results[0].Remove("@metadata");
            Assert.Equal(@"{""blog_id"":3,""comments_length"":14}", q.Results[0].ToString(Formatting.None));
        }
Example #4
0
        public void CanReplicateDocumentWithOAuth()
        {
            var store1 = CreateStore();
            var store2 = CreateStore();

            foreach (var server in servers)
            {
                var writer = new StringWriter();
                store1.Conventions.CreateSerializer().Serialize(writer, new AuthenticationUser
                {
                    Name             = "Ayende",
                    Id               = "Raven/Users/Ayende",
                    AllowedDatabases = new[] { "*" }
                }.SetPassword("abc"));

                server.Database.Put("Raven/Users/Ayende", null, RavenJObject.Parse(writer.GetStringBuilder().ToString()), new RavenJObject(), null);
            }

            TellFirstInstanceToReplicateToSecondInstance();

            using (var session = store1.OpenSession())
            {
                session.Store(new Item());
                session.SaveChanges();
            }

            JsonDocument item = null;

            for (int i = 0; i < RetriesCount; i++)
            {
                item = store2.DatabaseCommands.Get("items/1");
                if (item != null)
                {
                    break;
                }
                Thread.Sleep(100);
            }
            Assert.NotNull(item);
        }
Example #5
0
 public IEnumerable <AttachmentInformation> GetAttachmentsByReverseUpdateOrder(int start)
 {
     Api.JetSetCurrentIndex(session, Files, "by_etag");
     Api.MoveAfterLast(session, Files);
     for (int i = 0; i < start; i++)
     {
         if (Api.TryMovePrevious(session, Files) == false)
         {
             yield break;
         }
     }
     while (Api.TryMovePrevious(session, Files))
     {
         yield return(new AttachmentInformation
         {
             Size = Api.RetrieveColumnSize(session, Files, tableColumnsCache.FilesColumns["data"]).Value,
             Etag = Api.RetrieveColumn(session, Files, tableColumnsCache.FilesColumns["etag"]).TransfromToGuidWithProperSorting(),
             Key = Api.RetrieveColumnAsString(session, Files, tableColumnsCache.FilesColumns["name"], Encoding.Unicode),
             Metadata = RavenJObject.Parse(Api.RetrieveColumnAsString(session, Files, tableColumnsCache.FilesColumns["metadata"], Encoding.Unicode))
         });
     }
 }
Example #6
0
        public void RemoveValueInNestedElement()
        {
            var patchedDoc = new JsonPatcher(doc).Apply(
                new[]
            {
                new PatchRequest
                {
                    Type    = PatchCommandType.Modify,
                    Name    = "user",
                    PrevVal = RavenJObject.Parse(@"{ ""name"": ""ayende"", ""id"": 13}"),
                    Nested  = new[]
                    {
                        new PatchRequest {
                            Type = PatchCommandType.Unset, Name = "name"
                        },
                    }
                },
            });

            Assert.Equal(@"{""title"":""A Blog Post"",""body"":""html markup"",""comments"":[{""author"":""ayende"",""text"":""good post 1""},{""author"":""ayende"",""text"":""good post 2""}],""user"":{""id"":13}}",
                         patchedDoc.ToString(Formatting.None));
        }
Example #7
0
        private async Task <PutResult> SaveEntryAsync(string tenant, string entityName, ApplicationUser ownerUser,
                                                      JObject jobject, string id)
        {
            UserIdentityHelper.SetUserIdentity(jobject, ownerUser);

            var dataEntry = JsonConvert.SerializeObject(jobject);
            var commands  = _documentStore.AsyncDatabaseCommands.ForDatabase(tenant);

            // NOTE: The PutAsync database command allows us to set the `Raven-Entity-Name` metadata which tells RavenDB to put this entity in it's own entity collection.
            //  Using the `session.Store` or `asyncSession.StoreAsync` methods will cause RavenDB to reflect on the parameter object causing all entites to be stored in
            //  a single entity collection of `JObject` which is not what we want.
            var result = await commands.PutAsync(
                id,
                null,
                RavenJObject.Parse(dataEntry),
                new RavenJObject
            {
                { "Raven-Entity-Name", entityName },
            });

            return(result);
        }
Example #8
0
        public void CanReplicateValuesFromIndexToDataTable()
        {
            db.PutIndex("test", new IndexDefinition
            {
                Map    = "from doc in docs from prj in doc.Projects select new{Project = prj}",
                Stores = { { "Project", FieldStorage.Yes } }
            });
            db.Put("t", null, RavenJObject.Parse("{'Projects': ['RavenDB', 'NHibernate']}"), new RavenJObject(), null);

            QueryResult queryResult;

            do
            {
                queryResult = db.Query("test", new IndexQuery {
                    Start = 0, PageSize = 2, Query = "Project:RavenDB"
                });
            } while (queryResult.IsStale);

            var indexToDataTable = db.IndexUpdateTriggers.OfType <IndexToDataTable>().Single();

            Assert.Equal(2, indexToDataTable.DataTable.Rows.Count);
        }
Example #9
0
        public void SetValueInNestedElement_WithConcurrency_Ok()
        {
            var patchedDoc = new JsonPatcher(doc).Apply(
                new[]
            {
                new PatchRequest
                {
                    Type    = PatchCommandType.Modify,
                    Name    = "user",
                    PrevVal = RavenJObject.Parse(@"{ ""name"": ""ayende"", ""id"": 13}"),
                    Nested  = new[]
                    {
                        new PatchRequest {
                            Type = PatchCommandType.Set, Name = "name", Value = new RavenJValue("rahien")
                        },
                    }
                },
            });

            Assert.Equal(RavenJToken.Parse(@"{""title"":""A Blog Post"",""body"":""html markup"",""comments"":[{""author"":""ayende"",""text"":""good post 1""},{""author"":""ayende"",""text"":""good post 2""}],""user"":{""name"":""rahien"",""id"":13}}"),
                         patchedDoc, RavenJTokenEqualityComparer.Default);
        }
Example #10
0
        public void ShouldThrowWhenTryingToUseTheSameIncrementalBackupLocationForDifferentDatabases(string storageName)
        {
            using (var store = NewRemoteDocumentStore(runInMemory: false, requestedStorage: storageName, databaseName: "RavenDB_2824_one"))
            {
                store.DatabaseCommands.Put("animals/1", null, RavenJObject.Parse("{'Name':'Daisy'}"), new RavenJObject());

                store.DatabaseCommands.GlobalAdmin.StartBackup(BackupDir, null, true, store.DefaultDatabase).WaitForCompletion();

                store.DatabaseCommands.Put("animals/2", null, RavenJObject.Parse("{'Name':'Banny'}"), new RavenJObject());

                store.DatabaseCommands.GlobalAdmin.StartBackup(BackupDir, null, true, store.DefaultDatabase).WaitForCompletion();
            }

            using (var store = NewRemoteDocumentStore(runInMemory: false, requestedStorage: storageName, databaseName: "RavenDB_2824_two"))
            {
                store.DatabaseCommands.Put("animals/1", null, RavenJObject.Parse("{'Name':'Daisy'}"), new RavenJObject());

                var ex = Assert.Throws <InvalidOperationException>(() => store.DatabaseCommands.GlobalAdmin.StartBackup(BackupDir, null, true, "RavenDB_2824_two").WaitForCompletion());

                Assert.Contains("Can't perform an incremental backup to a given folder because it already contains incremental backup data of different database. Existing incremental data origins from 'RavenDB_2824_one' database.", ex.Message);
            }
        }
Example #11
0
        public void AfterFailedBackupRestoreCanDetectError()
        {
            db.Put("ayende", null, RavenJObject.Parse("{'email':'*****@*****.**'}"), RavenJObject.Parse("{'Raven-Entity-Name':'Users'}"), null);
            db.SpinBackgroundWorkers();
            QueryResult queryResult;

            do
            {
                queryResult = db.Query("Raven/DocumentsByEntityName", new IndexQuery
                {
                    Query    = "Tag:[[Users]]",
                    PageSize = 10
                });
            } while (queryResult.IsStale);
            Assert.Equal(1, queryResult.Results.Count);

            File.WriteAllText("raven.db.test.backup.txt", "Sabotage!");
            db.StartBackup("raven.db.test.backup.txt", false, new DatabaseDocument());
            WaitForBackup(db, false);

            Assert.True(GetStateOfLastStatusMessage().Severity == BackupStatus.BackupMessageSeverity.Error);
        }
Example #12
0
        public void CanPerformSpatialSearchWithNulls()
        {
            var indexDefinition = new IndexDefinition
            {
                Map     = "from e in docs.Events select new { Tag = \"Event\", _ = SpatialIndex.Generate(e.Latitude, e.Longitude) }",
                Indexes =
                {
                    { "Tag", FieldIndexing.NotAnalyzed }
                }
            };

            db.PutIndex("eventsByLatLng", indexDefinition);

            db.Put("Events/1", null,
                   RavenJObject.Parse(@"{""Venue"": ""Jimmy's Old Town Tavern"", ""Latitude"": null, ""Longitude"": null }"),
                   RavenJObject.Parse("{'Raven-Entity-Name': 'Events'}"), null);

            const double radius = 6.0;
            QueryResult  queryResult;

            do
            {
                queryResult = db.Query("eventsByLatLng", new SpatialIndexQuery()
                {
                    Query        = "Tag:[[Event]]",
                    Latitude     = 0,
                    Longitude    = 0,
                    Radius       = radius,
                    SortedFields = new[] { new SortedField("__distance"), }
                });
                if (queryResult.IsStale)
                {
                    Thread.Sleep(100);
                }
            } while (queryResult.IsStale);

            Assert.Equal(1, queryResult.Results.Count);
        }
Example #13
0
        public void CanUpdateReduceValue_WhenChangingReduceKey()
        {
            var values = new[]
            {
                "{blog_id: 3, comments: [{},{},{}]}",
                "{blog_id: 5, comments: [{},{},{},{}]}",
                "{blog_id: 6, comments: [{},{},{},{},{},{}]}",
                "{blog_id: 7, comments: [{}]}",
                "{blog_id: 3, comments: [{},{},{}]}",
                "{blog_id: 3, comments: [{},{},{},{},{}]}",
                "{blog_id: 2, comments: [{},{},{},{},{},{},{},{}]}",
                "{blog_id: 4, comments: [{},{},{}]}",
                "{blog_id: 5, comments: [{},{}]}",
                "{blog_id: 3, comments: [{},{},{}]}",
                "{blog_id: 5, comments: [{}]}",
            };

            using (var store = GetDocumentStore())
            {
                Fill(store);

                for (int i = 0; i < values.Length; i++)
                {
                    store.DatabaseCommands.Put("blogs/" + i, null, RavenJObject.Parse(values[i]), new RavenJObject {
                        { "Raven-Entity-Name", "Blogs" }
                    });
                }

                GetUnstableQueryResult(store, "blog_id:3");

                store.DatabaseCommands.Put("blogs/0", null, RavenJObject.Parse("{blog_id: 7, comments: [{}]}"), new RavenJObject {
                    { "Raven-Entity-Name", "Blogs" }
                });

                var q = GetUnstableQueryResult(store, "blog_id:3");
                Assert.Equal(@"{""blog_id"":3,""comments_length"":11}", q.Results[0].ToString(Formatting.None));
            }
        }
        public void HandleResponses(GetResponse[] responses, ShardStrategy shardStrategy)
        {
            var list = new List <MultiLoadResult>(
                from response in responses
                let result = RavenJObject.Parse(response.Result)
                             select new MultiLoadResult
            {
                Includes = result.Value <RavenJArray>("Includes").Cast <RavenJObject>().ToList(),
                Results  = result.Value <RavenJArray>("Results").Cast <RavenJObject>().ToList()
            });

            var capacity = list.Max(x => x.Results.Count);

            var finalResult = new MultiLoadResult
            {
                Includes = new List <RavenJObject>(),
                Results  = new List <RavenJObject>(Enumerable.Range(0, capacity).Select(x => (RavenJObject)null))
            };


            foreach (var multiLoadResult in list)
            {
                finalResult.Includes.AddRange(multiLoadResult.Includes);

                for (int i = 0; i < multiLoadResult.Results.Count; i++)
                {
                    if (finalResult.Results[i] == null)
                    {
                        finalResult.Results[i] = multiLoadResult.Results[i];
                    }
                }
            }
            RequiresRetry = loadOperation.SetResult(finalResult);
            if (RequiresRetry == false)
            {
                Result = loadOperation.Complete <T>();
            }
        }
Example #15
0
        public void Can_Read_Values_Using_Deep_Nesting()
        {
            db.PutIndex(@"DocsByProject",
                        new IndexDefinition
            {
                Map = @"
from doc in docs
from prj in doc.projects
select new{project_name = prj.name}
"
            });

            var document =
                RavenJObject.Parse(
                    "{'name':'ayende','email':'*****@*****.**','projects':[{'name':'raven'}], '@metadata': { '@id': 1}}");

            db.Put("1", Guid.Empty, document, new RavenJObject(), null);

            QueryResult docs;

            do
            {
                docs = db.Query("DocsByProject", new IndexQuery
                {
                    Query    = "project_name:raven",
                    Start    = 0,
                    PageSize = 10
                });
                if (docs.IsStale)
                {
                    Thread.Sleep(100);
                }
            } while (docs.IsStale);
            Assert.Equal(1, docs.Results.Count);
            var jProperty = docs.Results[0]["name"];

            Assert.Equal("ayende", jProperty.Value <string>());
        }
Example #16
0
        public void AfterBackupRestoreCanQueryIndex_CreatedBeforeRestore()
        {
            DeleteIfExists("raven.db.test.backup");             // for full backups, we can't have anything in the target dir

            db.Put("ayende", null, RavenJObject.Parse("{'email':'*****@*****.**'}"), RavenJObject.Parse("{'Raven-Entity-Name':'Users'}"), null);
            db.SpinBackgroundWorkers();
            QueryResult queryResult;

            do
            {
                queryResult = db.Query("Raven/DocumentsByEntityName", new IndexQuery
                {
                    Query    = "Tag:[[Users]]",
                    PageSize = 10
                });
            } while (queryResult.IsStale);
            Assert.Equal(1, queryResult.Results.Count);

            db.StartBackup("raven.db.test.backup", false);
            WaitForBackup(true);

            db.Dispose();

            DeleteIfExists("raven.db.test.esent");

            DocumentDatabase.Restore(new RavenConfiguration(), "raven.db.test.backup", "raven.db.test.esent");

            db = new DocumentDatabase(new RavenConfiguration {
                DataDirectory = "raven.db.test.esent"
            });

            queryResult = db.Query("Raven/DocumentsByEntityName", new IndexQuery
            {
                Query    = "Tag:[[Users]]",
                PageSize = 10
            });
            Assert.Equal(1, queryResult.Results.Count);
        }
Example #17
0
        public void HandleResponse(GetResponse response)
        {
            if (response.Status == 404)
            {
                Result        = null;
                RequiresRetry = false;
                return;
            }

            var headers = new NameValueCollection();

            foreach (var header in response.Headers)
            {
                headers[header.Key] = header.Value;
            }
            var jsonDocument = SerializationHelper.DeserializeJsonDocument(key, RavenJObject.Parse(response.Result), headers, (HttpStatusCode)response.Status);

            RequiresRetry = loadOperation.SetResult(jsonDocument);
            if (RequiresRetry == false)
            {
                Result = loadOperation.Complete <T>();
            }
        }
Example #18
0
        public void AfterBackupRestoreCanReadDocument()
        {
            DeleteIfExists("raven.db.test.backup");             // for full backups, we can't have anything in the target dir

            db.Put("ayende", null, RavenJObject.Parse("{'email':'*****@*****.**'}"), new RavenJObject(), null);

            db.StartBackup("raven.db.test.backup", false);
            WaitForBackup(true);

            db.Dispose();

            DeleteIfExists("raven.db.test.esent");

            DocumentDatabase.Restore(new RavenConfiguration(), "raven.db.test.backup", "raven.db.test.esent");

            db = new DocumentDatabase(new RavenConfiguration {
                DataDirectory = "raven.db.test.esent"
            });

            var jObject = db.Get("ayende", null).ToJson();

            Assert.Equal("*****@*****.**", jObject.Value <string>("email"));
        }
Example #19
0
        private static MultiLoadResult CompleteMultiGet(Task <string> task)
        {
            try
            {
                var result = RavenJObject.Parse(task.Result);

                return(new MultiLoadResult
                {
                    Includes = result.Value <RavenJArray>("Includes").Cast <RavenJObject>().ToList(),
                    Results = result.Value <RavenJArray>("Results").Cast <RavenJObject>().ToList()
                });
            }
            catch (WebException e)
            {
                var httpWebResponse = e.Response as HttpWebResponse;
                if (httpWebResponse == null ||
                    httpWebResponse.StatusCode != HttpStatusCode.Conflict)
                {
                    throw;
                }
                throw ThrowConcurrencyException(e);
            }
        }
Example #20
0
        public void Can_get_stats_for_indexing()
        {
            db.Put("1", Guid.Empty,
                   RavenJObject.Parse(
                       @"{
				type: 'page', 
				some: 'val', 
				other: 'var', 
				content: 'this is the content', 
				title: 'hello world', 
				size: 1,
				'@metadata': {'@id': 1}
			}"            ),
                   new RavenJObject(), null);

            QueryResult docs;

            do
            {
                docs = db.Query("pagesByTitle2", new IndexQuery
                {
                    Query    = "f:val",
                    Start    = 0,
                    PageSize = 10
                });
                if (docs.IsStale)
                {
                    Thread.Sleep(100);
                }
            } while (docs.IsStale);

            var indexStats = db.Statistics.Indexes.First(x => x.Name == "pagesByTitle2");

            Assert.Equal("pagesByTitle2", indexStats.Name);
            Assert.Equal(1, indexStats.IndexingAttempts);
            Assert.Equal(1, indexStats.IndexingSuccesses);
        }
        public static Task <NameAndCount[]> GetTermsCount(this IAsyncDatabaseCommands cmds, string indexName, string field, string fromValue, int pageSize)
        {
            string[] terms = null;
            return(cmds.GetTermsAsync(indexName, field, fromValue, pageSize)
                   .ContinueWith(task =>
            {
                terms = task.Result;
                var termRequests = terms.Select(term => new IndexQuery
                {
                    Query = field + ":" + RavenQuery.Escape(term),
                    PageSize = 0,
                }.GetIndexQueryUrl("", indexName, "indexes"))
                                   .Select(url =>
                {
                    var uriParts = url.Split(new[] { '?' }, StringSplitOptions.RemoveEmptyEntries);
                    return new GetRequest
                    {
                        Url = uriParts[0],
                        Query = uriParts[1]
                    };
                })
                                   .ToArray();

                if (termRequests.Length == 0)
                {
                    return Task.Factory.StartNew(() => new NameAndCount[0]);
                }

                return cmds.MultiGetAsync(termRequests)
                .ContinueWith(termsResultsTask => termsResultsTask.Result.Select((t, i) => new NameAndCount
                {
                    Count = RavenJObject.Parse(t.Result).Value <int>("TotalResults"),
                    Name = terms[i]
                }).ToArray());
            })
                   .Unwrap());
        }
            private static RavenJObject CreateJsonDocumentFromLuceneDocument(Document document)
            {
                var field = document.GetField(Constants.ReduceValueFieldName);

                if (field != null)
                {
                    return(RavenJObject.Parse(field.StringValue));
                }

                var ravenJObject = new RavenJObject();

                foreach (var fieldable in document.GetFields())
                {
                    var         stringValue = GetStringValue(fieldable);
                    RavenJToken value;
                    if (ravenJObject.TryGetValue(fieldable.Name, out value) == false)
                    {
                        ravenJObject[fieldable.Name] = stringValue;
                    }
                    else
                    {
                        var ravenJArray = value as RavenJArray;
                        if (ravenJArray != null)
                        {
                            ravenJArray.Add(stringValue);
                        }
                        else
                        {
                            ravenJArray = new RavenJArray {
                                value, stringValue
                            };
                            ravenJObject[fieldable.Name] = ravenJArray;
                        }
                    }
                }
                return(ravenJObject);
            }
Example #23
0
        public void Can_read_values_from_indexes_of_documents_already_in_db_when_multiple_docs_exists()
        {
            db.Put(null, Guid.Empty,
                   RavenJObject.Parse(
                       "{type: 'page', some: 'val', other: 'var', content: 'this is the content', title: 'hello world', size: 5}"),
                   new RavenJObject(), null);
            db.Put(null, Guid.Empty,
                   RavenJObject.Parse(
                       "{type: 'page', some: 'val', other: 'var', content: 'this is the content', title: 'hello world', size: 5}"),
                   new RavenJObject(), null);

            db.PutIndex("pagesByTitle",
                        new IndexDefinition
            {
                Map = @"
	from doc in docs
	where doc.type == ""page""
	select new { doc.other };
"
            });
            QueryResult docs;

            do
            {
                docs = db.Query("pagesByTitle", new IndexQuery
                {
                    Query    = "other:var",
                    Start    = 0,
                    PageSize = 10
                });
                if (docs.IsStale)
                {
                    Thread.Sleep(100);
                }
            } while (docs.IsStale);
            Assert.Equal(2, docs.Results.Count);
        }
        public void FunctionalityTest()
        {
            var str = GenerateSimpleEntityForFunctionalityTest();

            using (var blittableContext = JsonOperationContext.ShortTermSingleUse())
                using (var employee = blittableContext.Read(new MemoryStream(Encoding.UTF8.GetBytes(str)), "doc1"))
                {
                    dynamic dynamicRavenJObject     = new DynamicJsonObject(RavenJObject.Parse(str));
                    dynamic dynamicBlittableJObject = new DynamicBlittableJson(employee);
                    Assert.Equal(dynamicRavenJObject.Age, dynamicBlittableJObject.Age);
                    Assert.Equal(dynamicRavenJObject.Name, dynamicBlittableJObject.Name);
                    Assert.Equal(dynamicRavenJObject.Dogs.Count, dynamicBlittableJObject.Dogs.Count);
                    for (var i = 0; i < dynamicBlittableJObject.Dogs.Length; i++)
                    {
                        Assert.Equal(dynamicRavenJObject.Dogs[i], dynamicBlittableJObject.Dogs[i]);
                    }
                    Assert.Equal(dynamicRavenJObject.Office.Name, dynamicRavenJObject.Office.Name);
                    Assert.Equal(dynamicRavenJObject.Office.Street, dynamicRavenJObject.Office.Street);
                    Assert.Equal(dynamicRavenJObject.Office.City, dynamicRavenJObject.Office.City);
                    var ms = new MemoryStream();
                    blittableContext.Write(ms, employee);
                    Assert.Equal(str, Encoding.UTF8.GetString(ms.ToArray()));
                }
        }
Example #25
0
        private static RavenJToken GetValue(string val)
        {
            try
            {
                if (val.StartsWith("{"))
                {
                    return(RavenJObject.Parse(val));
                }
                if (val.StartsWith("["))
                {
                    return(RavenJArray.Parse(val));
                }

                DateTime dateTime;
                if (DateTime.TryParseExact(val, Default.OnlyDateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out dateTime))
                {
                    if (val.EndsWith("Z"))
                    {
                        return(DateTime.SpecifyKind(dateTime, DateTimeKind.Utc));
                    }
                    return(new RavenJValue(dateTime));
                }

                DateTimeOffset dateTimeOffset;
                if (DateTimeOffset.TryParseExact(val, Default.DateTimeFormatsToRead, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out dateTimeOffset))
                {
                    return(new RavenJValue(dateTimeOffset));
                }

                return(new RavenJValue(Uri.UnescapeDataString(val)));
            }
            catch (Exception exc)
            {
                throw new JsonReaderException(string.Concat("Unable to get value: ", val), exc);
            }
        }
        public IEnumerable <AttachmentInformation> GetAttachmentsAfter(Guid etag, int take)
        {
            Api.JetSetCurrentIndex(session, Files, "by_etag");
            Api.MakeKey(session, Files, etag.TransformToValueForEsentSorting(), MakeKeyGrbit.NewKey);
            if (Api.TrySeek(session, Files, SeekGrbit.SeekGT) == false)
            {
                return(Enumerable.Empty <AttachmentInformation>());
            }

            var optimizer = new OptimizedIndexReader(Session, Files, take);

            do
            {
                optimizer.Add();
            } while (Api.TryMoveNext(session, Files) && optimizer.Count < take);

            return(optimizer.Select(() => new AttachmentInformation
            {
                Size = Api.RetrieveColumnSize(session, Files, tableColumnsCache.FilesColumns["data"]) ?? 0,
                Etag = Api.RetrieveColumn(session, Files, tableColumnsCache.FilesColumns["etag"]).TransfromToGuidWithProperSorting(),
                Key = Api.RetrieveColumnAsString(session, Files, tableColumnsCache.FilesColumns["name"], Encoding.Unicode),
                Metadata = RavenJObject.Parse(Api.RetrieveColumnAsString(session, Files, tableColumnsCache.FilesColumns["metadata"], Encoding.Unicode))
            }));
        }
Example #27
0
        public static List <object> ExtractError(Stream stream, HttpWebResponse httpWebResponse, IEnumerable <object> details)
        {
            var objects = new List <object>(details);

            if (stream.CanRead)
            {
                var error = new StreamReader(stream).ReadToEnd();

                try
                {
                    var item = RavenJObject.Parse(error);
                    objects.Insert(0, "Server Error:");
                    objects.Insert(1, "-----------------------------------------");
                    objects.Insert(2, item.Value <string>("Url"));
                    objects.Insert(3, item.Value <string>("Error"));
                    objects.Insert(4, "-----------------------------------------");
                    objects.Insert(5, Environment.NewLine);
                    objects.Insert(6, Environment.NewLine);
                }
                catch (Exception)
                {
                    objects.Insert(0, "Server sent:");
                    objects.Insert(1, error);
                    objects.Insert(2, Environment.NewLine);
                    objects.Insert(3, Environment.NewLine);
                }
            }
            if (httpWebResponse.StatusCode == HttpStatusCode.Unauthorized)
            {
                objects.Insert(0, "Could not get authorization for this command.");
                objects.Insert(1,
                               "If you should have access to this operation contact your admin and check the Raven/AnonymousAccess or the Windows Authentication settings in RavenDB ");
            }

            return(objects);
        }
Example #28
0
 private static RavenJToken GetValue(string val)
 {
     try
     {
         if (val.StartsWith("{"))
         {
             return(RavenJObject.Parse(val));
         }
         if (val.StartsWith("["))
         {
             return(RavenJArray.Parse(val));
         }
         DateTime result;
         if (DateTime.TryParseExact(val, new[] { "r", "o" }, CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
         {
             return(new RavenJValue(result));
         }
         return(new RavenJValue(val));
     }
     catch (Exception exc)
     {
         throw new JsonReaderException(string.Concat("Unable to get value: ", val), exc);
     }
 }
Example #29
0
        public Attachment GetAttachment(string key)
        {
            Api.JetSetCurrentIndex(session, Files, "by_name");
            Api.MakeKey(session, Files, key, Encoding.Unicode, MakeKeyGrbit.NewKey);
            if (Api.TrySeek(session, Files, SeekGrbit.SeekEQ) == false)
            {
                return(null);
            }

            var metadata = Api.RetrieveColumnAsString(session, Files, tableColumnsCache.FilesColumns["metadata"], Encoding.Unicode);

            return(new Attachment
            {
                Data = () =>
                {
                    StorageActionsAccessor storageActionsAccessor = transactionalStorage.GetCurrentBatch();
                    var documentStorageActions = ((DocumentStorageActions)storageActionsAccessor.Attachments);
                    return documentStorageActions.GetAttachmentStream(key);
                },
                Size = (int)GetAttachmentStream(key).Length,
                Etag = Api.RetrieveColumn(session, Files, tableColumnsCache.FilesColumns["etag"]).TransfromToGuidWithProperSorting(),
                Metadata = RavenJObject.Parse(metadata)
            });
        }
Example #30
0
        private static void CopyToServer()
        {
            var store = GetStore();

            store.Initialize(true);

            var serverStore = new DocumentStore
            {
                Url             = "http://it-webadb01.it.ntnu.no:8180",
                DefaultDatabase = _dbName
                                  //MaxNumberOfCachedRequests = 9999
            };

            serverStore.Initialize(true);


            using var session = store.OpenSession();
            var query = session.Query <object>(PrimaryIndex);

            using var enumerator = session.Advanced.Stream(query);
            var i = 0;

            using var bulkServer = serverStore.BulkInsert(null, null);
            while (enumerator.MoveNext())
            {
                var name = enumerator.Current.Key.StartsWith("Natur")
                    ? "Naturtypes"
                    : "Variasjons";
                Log2Console($"{i++:#0000}\t{name}\t{enumerator.Current.Key}", true);
                ////bulkServer.Store(enumerator.Current.Document, enumerator.Current.Key);
                bulkServer.Store(
                    RavenJObject.FromObject(enumerator.Current.Document),
                    RavenJObject.Parse($"{{'Raven-Entity-Name': '{name}'}}"),
                    enumerator.Current.Key);
            }
        }