public void IndexSample2(bool useServerSession) { CreateDirectoryAndCopyLicenseDb(); Random rd = new Random(); using (SessionBase session = useServerSession ? (SessionBase) new ServerClientSession(systemDir) : (SessionBase) new SessionNoServer(systemDir)) { session.BeginUpdate(); for (int i = 0; i < 100; i++) { MeasurePoint p = new MeasurePoint(i + 1) { Data = new List <float>() }; for (int j = 0; j < 440000; j++) { p.Data.Add(rd.Next(100)); } session.Persist(p); } var value = session.DatabaseNumberOf(typeof(MeasurePoint)); Database db = session.OpenDatabase(value, false, false); if (db != null) { var q = from point in session.Index <MeasurePoint>("key", db) where point.Key == 10 select point; foreach (MeasurePoint obj in q) { Console.WriteLine(obj.Key + " " + obj.Data.Count); } } session.Commit(); } }
protected override void LoadChildren() { Database db = m_session.OpenDatabase(m_dbNum, false, false); Page page = m_session.OpenPage(db, m_pageNum); using (System.Windows.Application.Current.Dispatcher.DisableProcessing()) { foreach (IOptimizedPersistable o in page.ObjectsLazyLoaded()) { base.Children.Add(new ObjectViewModel(o, this, m_session)); } } }
private VersionManager <VelocityDbSchema.Samples.Sample1.Person> GetVersionManager(SessionBase session) { Database db = session.OpenDatabase(VersionManager <VelocityDbSchema.Samples.Sample1.Person> .versionMangerDatabase, false, false); if (db != null) { var versionManager = db.AllObjects <VersionManager <VelocityDbSchema.Samples.Sample1.Person> >().FirstOrDefault(vmObject => vmObject.RecordType == RecordType.Person); return(versionManager); } else { return(default(VersionManager <VelocityDbSchema.Samples.Sample1.Person>)); } }
protected override void LoadChildren() { using (System.Windows.Application.Current.Dispatcher.DisableProcessing()) { Database db = m_session.OpenDatabase(m_dbid, false, false); if (db != null) { foreach (Page page in db) { base.Children.Add(new PageViewModel(page, this, m_session)); } } } }
void loadCities(SessionBase session) { Database db = session.OpenDatabase(session.DatabaseNumberOf(typeof(City))); if (db != null) { var src = from City city in db.AllObjects<City>() select city; CityName.ItemsSource = src; Country.ItemsSource = src; Latitude.ItemsSource = src; Longitude.ItemsSource = src; //State.ItemsSource = src; //Population.ItemsSource = src; CreateDatabasePanel.Visibility = System.Windows.Visibility.Collapsed; HelpLabel.Visibility = System.Windows.Visibility.Collapsed; } }
void loadCities(SessionBase session) { Database db = session.OpenDatabase(session.DatabaseNumberOf(typeof(City))); if (db != null) { var src = from City city in db.AllObjects <City>() select city; CityName.ItemsSource = src; Country.ItemsSource = src; Latitude.ItemsSource = src; Longitude.ItemsSource = src; //State.ItemsSource = src; //Population.ItemsSource = src; CreateDatabasePanel.Visibility = System.Windows.Visibility.Collapsed; HelpLabel.Visibility = System.Windows.Visibility.Collapsed; } }
public static Graph Open(SessionBase session, int graphInstance = 0) { UInt32 dbNum = session.DatabaseNumberOf(typeof(Graph)); Database db = session.OpenDatabase(dbNum, false, false); if (db != null) { int ct = 0; foreach (Graph g in db.AllObjects <Graph>()) { if (ct++ == graphInstance) { return(g); } } } return(null); }
public void verifyDatabaseLocations(SessionBase session) { session.BeginRead(); foreach (Person person in session.AllObjects <Person>()) { Console.WriteLine(person.ToString()); Assert.AreEqual(person.FirstName, "Mats"); } Database db = session.OpenDatabase(otherStartdbId); foreach (Person person in db.AllObjects <Person>()) { Console.WriteLine(person.ToString()); Assert.AreEqual(person.FirstName, "Mats"); } session.Commit(); session.Verify(); }
/// <summary> /// Restores database files, pages and objects from a .csv file data created with ExportToCSV /// </summary> /// <param name="session">the active session</param> /// <param name="csvDirectory">Path to directory containing CSV files</param> static public void ImportFromCSV(this SessionBase session, string csvDirectory) { const char fieldSeperator = ','; DirectoryInfo di = new DirectoryInfo(csvDirectory); #if WINDOWS_PHONE List <FileInfo> files = di.GetFiles("*.csv").ToList(); #else List <FileInfo> files = di.GetFiles("*.csv", SearchOption.TopDirectoryOnly).ToList(); #endif Schema schema = session.OpenSchema(false); using (StreamReader textReader = new StreamReader(Path.Combine(csvDirectory, "Database.csv"))) { string header = textReader.ReadLine(); string dbInfoString = textReader.ReadLine(); while (dbInfoString != null) { string[] dbInfo = dbInfoString.Split(fieldSeperator); UInt32 dbNum = UInt32.Parse(dbInfo[0]); string dbName = dbInfo[1].Trim(new char[] { '"' }); Database db = null; if (dbNum < 10) { db = session.OpenDatabase(dbNum, false, false); } if (db == null) { db = session.NewDatabase(dbNum, 0, dbName); } dbInfoString = textReader.ReadLine(); } } using (StreamReader textReader = new StreamReader(Path.Combine(csvDirectory, "Page.csv"))) { string header = textReader.ReadLine(); string pageInfoString = textReader.ReadLine(); while (pageInfoString != null) { int i = 0; string[] pageInfo = pageInfoString.Split(fieldSeperator); UInt32 dbNum = UInt32.Parse(pageInfo[i++]); UInt16 pageNum = UInt16.Parse(pageInfo[i++]); UInt16 numberOfSlots = UInt16.Parse(pageInfo[i++]); UInt16 firstFreeSlot = UInt16.Parse(pageInfo[i++]); UInt64 versionNumber = UInt64.Parse(pageInfo[i++]); PageInfo.encryptionKind encryptionKind = (PageInfo.encryptionKind)Enum.Parse(typeof(PageInfo.encryptionKind), pageInfo[i++]); PageInfo.compressionKind compressed = (PageInfo.compressionKind)Enum.Parse(typeof(PageInfo.compressionKind), pageInfo[i++]); UInt32 typeVersion = UInt32.Parse(pageInfo[i]); Database db = session.OpenDatabase(dbNum, false, false); Page page = db.CachedPage(pageNum); if (page == null) { page = new Page(db, pageNum, typeVersion, numberOfSlots); page.PageInfo.Compressed = compressed; page.PageInfo.Encryption = encryptionKind; page.PageInfo.VersionNumber = versionNumber; page.PageInfo.NumberOfSlots = numberOfSlots; page.PageInfo.FirstFreeSlot = firstFreeSlot; session.UpdatePage(ref page); } pageInfoString = textReader.ReadLine(); } } var schemaInternalTypeFiles = new[] { "VelocityDb.TypeInfo.Schema65747.csv", "VelocityDb.TypeInfo.TypeVersion65749.csv", "VelocityDb.TypeInfo.VelocityDbType65753.csv", "VelocityDb.TypeInfo.DataMember65745.csv", "VelocityDb.Collection.BTree.BTreeSetOidShortVelocityDb.TypeInfo.VelocityDbType65623.csv", "VelocityDb.Collection.Comparer.VelocityDbTypeComparer65655.csv" }; var schemaInternalTypeFileInfo = new List <FileInfo>(); foreach (var fileName in schemaInternalTypeFiles) { schemaInternalTypeFileInfo.Add(di.GetFiles(fileName, SearchOption.TopDirectoryOnly).First()); } foreach (FileInfo info in schemaInternalTypeFileInfo) { string numberString = Regex.Match(info.Name, @"\d+").Value; if (numberString.Length > 0) { UInt32 typeShortId = UInt32.Parse(numberString); UInt16 slotNumber = (UInt16)typeShortId; TypeVersion tv = schema.GetTypeVersion(typeShortId, session); if (tv != null) { using (StreamReader textReader = new StreamReader(info.FullName)) { CsvReader csvReader = new CsvReader(textReader, true); string[] fileldNames = csvReader.GetFieldHeaders(); foreach (string[] record in csvReader) { tv.ObjectBytesFromStrings(record, fileldNames, session, schema); } } } } } Database schemaDb = session.OpenDatabase(Schema.SchemaDB); Page schemaPage = schemaDb.CachedPage(1); schemaPage.FinishUpCsvImport(); var schemaTypes = new UInt32[] { 65747, 65749, 65753, 65745, 65623, 65655 }; for (int i = 0; i < 2; i++) { foreach (FileInfo info in files) { string numberString = Regex.Match(info.Name, @"\d+").Value; if (numberString.Length > 0) { UInt32 typeShortId = UInt32.Parse(numberString); UInt16 slotNumber = (UInt16)typeShortId; if (((i == 0 && slotNumber < Schema.s_bootupTypeCountExpanded) || (i == 1 && slotNumber >= Schema.s_bootupTypeCountExpanded)) && !schemaTypes.Contains(typeShortId)) { TypeVersion tv = schema.GetTypeVersion(typeShortId, session); if (tv != null) { using (StreamReader textReader = new StreamReader(info.FullName)) { var csvReader = new CsvReader(textReader, true); string[] fileldNames = csvReader.GetFieldHeaders(); foreach (string[] record in csvReader) { tv.ObjectBytesFromStrings(record, fileldNames, session, schema); } } } } } } } }
/// <summary> /// Restores database files, pages and objects from a .csv file data created with ExportToCSV /// </summary> /// <param name="session">the active session</param> /// <param name="csvDirectory">Path to directory containing CSV files</param> static public void ImportFromCSV(this SessionBase session, string csvDirectory) { const char fieldSeperator = ','; DirectoryInfo di = new DirectoryInfo(csvDirectory); #if WINDOWS_PHONE List <FileInfo> files = di.GetFiles("*.csv").ToList(); #else List <FileInfo> files = di.GetFiles("*.csv", SearchOption.TopDirectoryOnly).ToList(); #endif Schema schema = session.OpenSchema(false); using (StreamReader textReader = new StreamReader(Path.Combine(csvDirectory, "Database.csv"))) { string header = textReader.ReadLine(); string dbInfoString = textReader.ReadLine(); while (dbInfoString != null) { string[] dbInfo = dbInfoString.Split(fieldSeperator); UInt32 dbNum = UInt32.Parse(dbInfo[0]); string dbName = dbInfo[1].Trim(new char[] { '"' }); Database db = null; if (dbNum < 10) { db = session.OpenDatabase(dbNum, false, false); } if (db == null) { db = session.NewDatabase(dbNum, 0, dbName); } dbInfoString = textReader.ReadLine(); } } using (StreamReader textReader = new StreamReader(Path.Combine(csvDirectory, "Page.csv"))) { string header = textReader.ReadLine(); string pageInfoString = textReader.ReadLine(); while (pageInfoString != null) { int i = 0; string[] pageInfo = pageInfoString.Split(fieldSeperator); UInt32 dbNum = UInt32.Parse(pageInfo[i++]); UInt16 pageNum = UInt16.Parse(pageInfo[i++]); UInt16 numberOfSlots = UInt16.Parse(pageInfo[i++]); UInt16 firstFreeSlot = UInt16.Parse(pageInfo[i++]); UInt64 versionNumber = UInt64.Parse(pageInfo[i++]); PageInfo.encryptionKind encryptionKind = (PageInfo.encryptionKind)Enum.Parse(typeof(PageInfo.encryptionKind), pageInfo[i++]); PageInfo.compressionKind compressed = (PageInfo.compressionKind)Enum.Parse(typeof(PageInfo.compressionKind), pageInfo[i++]); UInt32 typeVersion = UInt32.Parse(pageInfo[i]); Database db = session.OpenDatabase(dbNum, false, false); Page page = db.CachedPage(pageNum); if (page == null) { page = new Page(db, pageNum, typeVersion, numberOfSlots); page.PageInfo.Compressed = compressed; page.PageInfo.Encryption = encryptionKind; page.PageInfo.VersionNumber = versionNumber; page.PageInfo.NumberOfSlots = numberOfSlots; page.PageInfo.FirstFreeSlot = firstFreeSlot; session.UpdatePage(ref page); } pageInfoString = textReader.ReadLine(); } } for (int i = 0; i < 2; i++) { foreach (FileInfo info in files) { string numberString = Regex.Match(info.Name, @"\d+").Value; if (numberString.Length > 0) { UInt32 typeShortId = UInt32.Parse(numberString); UInt16 slotNumber = (UInt16)typeShortId; if ((i == 0 && slotNumber < Schema.s_bootupTypeCount) || (i == 1 && slotNumber >= Schema.s_bootupTypeCount)) { TypeVersion tv = schema.GetTypeVersion(typeShortId, session); if (tv != null) { using (StreamReader textReader = new StreamReader(info.FullName)) { CsvReader csvReader = new CsvReader(textReader, true); string[] fileldNames = csvReader.GetFieldHeaders(); foreach (string[] record in csvReader) { tv.ObjectBytesFromStrings(record, fileldNames, session, schema); } } } } } } } }
static public void SyncWith(this SessionBase sessionToUpdate, SessionBase sessionToRead, Func <SessionBase, UInt64, Change, bool> doUpdate) { UInt64 currentVersion; UInt64 pageToReadVersion; bool conflictFound = false; using (var reader = sessionToRead.BeginRead()) { Changes changes = (Changes)sessionToRead.Open(5, 1, 1, false); if (changes != null) { using (var updater = sessionToUpdate.BeginUpdate()) { var dbs = sessionToUpdate.OpenAllDatabases(); ReplicaSync matchingReplicaSync = null; foreach (ReplicaSync sync in sessionToUpdate.AllObjects <ReplicaSync>()) { if (sync.SyncFromHost == sessionToRead.SystemHostName && sync.SyncFromPath == sessionToRead.SystemDirectory) { matchingReplicaSync = sync; break; } } if (changes.ChangeList.Count > 0) { foreach (TransactionChanges transactionChanges in changes.ChangeList) { if (matchingReplicaSync == null || matchingReplicaSync.TransactionNumber < transactionChanges.TransactionNumber) { foreach (Change change in transactionChanges.ChangeList) { Database dbToUpdate = sessionToUpdate.OpenDatabase(change.DatabaseId, false, false); Database dbToRead = sessionToRead.OpenDatabase(change.DatabaseId, false, false); string dbName = dbToRead != null ? dbToRead.Name : null; if (change.Deleted) { if (dbToUpdate == null) // does not exist { continue; } if (change.PageId == 0) // Database delete { currentVersion = dbToUpdate.Page.PageInfo.VersionNumber; if (currentVersion < change.Version) { sessionToUpdate.DeleteDatabase(dbToUpdate); } else { conflictFound = true; if (doUpdate(sessionToUpdate, currentVersion, change)) { sessionToUpdate.DeleteDatabase(dbToUpdate); } } } else { Page page = sessionToUpdate.OpenPage(dbToUpdate, change.PageId); if (page == null) // page does not exist { continue; } currentVersion = page.PageInfo.VersionNumber; if (currentVersion < change.Version) { sessionToUpdate.DeletePage(dbToUpdate, page); } else { conflictFound = true; if (doUpdate(sessionToUpdate, currentVersion, change)) { sessionToUpdate.DeleteDatabase(dbToUpdate); } } } } else { if (dbToUpdate == null) // does not exist { dbToUpdate = sessionToUpdate.NewDatabase(change.DatabaseId, 0, dbName); } if (change.PageId > 0) { Page pageToUpdate = sessionToUpdate.OpenPage(dbToUpdate, change.PageId); Page pageToRead = sessionToRead.OpenPage(dbToRead, change.PageId); if (pageToRead == null) // upcoming (not yet processed) changes must have deleted this page { continue; } currentVersion = pageToUpdate == null ? 0 : pageToUpdate.PageInfo.VersionNumber; pageToReadVersion = pageToRead.PageInfo.VersionNumber; if (currentVersion < pageToReadVersion || dbToUpdate.IsNew) { sessionToUpdate.ReplacePage(dbToUpdate, pageToUpdate, pageToRead); } else { conflictFound = true; if (doUpdate(sessionToUpdate, currentVersion, change)) { sessionToUpdate.ReplacePage(dbToUpdate, pageToUpdate, pageToRead); } } } } } } } UInt64 lastTransactionNumber = changes.ChangeList.Last().TransactionNumber; if (matchingReplicaSync != null) { matchingReplicaSync.TransactionNumber = lastTransactionNumber; } if (conflictFound) { sessionToUpdate.Verify(); } sessionToUpdate.Commit(); if (matchingReplicaSync == null) { sessionToUpdate.BeginUpdate(); // separate transaction or else gets confused with databases added by sync matchingReplicaSync = new ReplicaSync(sessionToRead, lastTransactionNumber); sessionToUpdate.Persist(matchingReplicaSync); sessionToUpdate.Commit(); } } } } } }
string DisplayData(bool useServerSession) { using (SessionBase session = useServerSession ? (SessionBase) new ServerClientSession(systemDir) : (SessionBase) new SessionNoServer(systemDir)) { StringBuilder sb = new StringBuilder(); session.BeginRead(); try { BTreeBase <RDerby1, RDerby1> index = null; try { session.Index <RDerby1>(); } catch (IndexDatabaseNotSpecifiedException) { UInt32 dbId = session.DatabaseNumberOf(typeof(RDerby1)); Database db = session.OpenDatabase(dbId); index = session.Index <RDerby1>(db); } // for testing //var index = session.Index<Test1_0_0Class>("name2"); // create variable so that can examine in Locals if (index != null) // should not be null? { // get all var testEntries = from t in index select t; int count = testEntries.ToList <RDerby1>().Count; int i = 0; sb.AppendLine("Session Details:"); sb.AppendLine(index.ToStringDetails(session)); sb.AppendLine("Results:"); foreach (RDerby1 test in testEntries) { try { string values = test.Name + ", " + test.Name2 + ", " + test.Added; sb.AppendLine(string.Format("{0, -5:00000}: {1}", i++, values)); } catch (Exception ex) { return("Exception thrown in for each loop: " + ex.Message); } } } else { return("Index was null"); // why is the index null? } } catch (Exception ex) { return("Exception occured prior to loop: " + ex.Message); // examine problem using breakpoint. } finally { session.Commit(); } return(sb.ToString()); } }
public void IndexSample(bool useServerSession) { string brandName = "Toyota"; string color = "Blue"; int maxPassengers = 5; int fuelCapacity = 40; double litresPer100Kilometers = 5; DateTime modelYear = new DateTime(2003, 1, 1); string modelName = "Highlander"; int maxSpeed = 200; int odometer = 100000; string registrationState = "Texas"; string registrationPlate = "TX343434"; string insurancePolicy = "CAA7878787"; DriversLicense license = new DriversLicense("California", "B7788888", DateTime.Now + new TimeSpan(1825, 0, 0, 0)); Person person = new Person("Mats Persson", license); InsuranceCompany insuranceCompany = new InsuranceCompany("Allstate", "858727878"); Car car = new Car(color, maxPassengers, fuelCapacity, litresPer100Kilometers, modelYear, brandName, modelName, maxSpeed, odometer, registrationState, registrationPlate, insuranceCompany, insurancePolicy); using (SessionBase session = useServerSession ? (SessionBase) new ServerClientSession(systemDir) : (SessionBase) new SessionNoServer(systemDir)) { session.BeginUpdate(); foreach (Database db in session.OpenAllDatabases(true)) { if (db.DatabaseNumber >= 10 || db.DatabaseNumber == SessionBase.IndexDescriptorDatabaseNumber) { session.DeleteDatabase(db); } } session.Commit(); session.BeginUpdate(); DatabaseLocation defaultLocation = session.DatabaseLocations.Default(); List <Database> dbList = session.OpenLocationDatabases(defaultLocation, true); foreach (Database db in dbList) { if (db.DatabaseNumber > Database.InitialReservedDatabaseNumbers) { session.DeleteDatabase(db); } } session.DeleteLocation(defaultLocation); session.Commit(); CreateDirectoryAndCopyLicenseDb(); } using (SessionBase session = useServerSession ? (SessionBase) new ServerClientSession(systemDir) : (SessionBase) new SessionNoServer(systemDir)) { session.BeginUpdate(); session.Commit(); } using (SessionBase session = useServerSession ? (SessionBase) new ServerClientSession(systemDir) : (SessionBase) new SessionNoServer(systemDir)) { session.BeginRead(); int ct = 0; var mIndex = session.Index <Motorcycle>(); if (mIndex != null) { foreach (Motorcycle mc in session.Index <Motorcycle>()) { Assert.NotNull(mc); ++ct; } } Assert.AreEqual(ct, 0); session.Commit(); } using (SessionBase session = useServerSession ? (SessionBase) new ServerClientSession(systemDir) : (SessionBase) new SessionNoServer(systemDir)) { session.BeginUpdate(); for (int i = 0; i < 10000; i++) { Motorcycle mc = new Motorcycle(); session.Persist(mc); } session.Commit(); session.BeginUpdate(); int ct = 0; var mIndex = session.Index <Car>(); if (mIndex != null) { foreach (Car c in mIndex) { Assert.NotNull(c); ++ct; } } Assert.AreEqual(ct, 0); ct = 0; session.RegisterClass(typeof(Person)); foreach (Person p in session.AllObjects <Person>(true, false)) { Assert.NotNull(p); ++ct; } session.Commit(); session.BeginRead(); ct = 0; foreach (Motorcycle mc in session.AllObjects <Motorcycle>(false, true)) { Assert.NotNull(mc); ++ct; } Assert.AreEqual(ct, 10000); session.Commit(); session.BeginRead(); ct = 0; foreach (Motorcycle mc in session.AllObjects <Motorcycle>(false, true)) { Assert.NotNull(mc); ++ct; } Assert.AreEqual(ct, 10000); session.Abort(); session.BeginRead(); ct = 0; foreach (Motorcycle mc in session.AllObjects <Motorcycle>(false, true)) { Assert.NotNull(mc); ++ct; } Assert.AreEqual(ct, 10000); session.Commit(); session.BeginRead(); ct = 0; foreach (Motorcycle mc in session.Index <Motorcycle>()) { Assert.NotNull(mc); ++ct; } Assert.AreEqual(ct, 10000); session.Abort(); session.BeginRead(); ct = 0; foreach (Motorcycle mc in session.Index <Motorcycle>()) { Assert.NotNull(mc); ++ct; } Assert.AreEqual(ct, 10000); session.Commit(); try { ct = 0; foreach (Motorcycle mc in session.Index <Motorcycle>()) { Assert.NotNull(mc); ++ct; } Assert.AreEqual(ct, 10000); } catch (NotInTransactionException ex) { Console.WriteLine(ex.Message); } session.BeginUpdate(); ct = 0; foreach (Motorcycle mc in session.AllObjects <Motorcycle>(false, true)) { if (++ct % 2 == 0) { mc.Unpersist(session); } } ct = 0; foreach (Motorcycle mc in session.Index <Motorcycle>()) { ++ct; } Assert.AreEqual(ct, 5000); session.Abort(); session.BeginUpdate(); ct = 0; foreach (Motorcycle mc in session.AllObjects <Motorcycle>(false, true)) { if (++ct % 2 == 0) { mc.Unpersist(session); } } ct = 0; foreach (Motorcycle mc in session.Index <Motorcycle>()) { Assert.NotNull(mc); ++ct; } Assert.AreEqual(ct, 5000); ct = 0; foreach (Motorcycle mc in session.Index <Motorcycle>("cc")) { Assert.NotNull(mc); ++ct; } Assert.AreEqual(ct, 5000); ct = 0; try { foreach (Motorcycle mc in session.Index <Motorcycle>("ccx")) { Assert.NotNull(mc); ++ct; } Assert.AreEqual(ct, 5000); } catch (FieldDoesNotExistException) { } session.Commit(); session.BeginUpdate(); ct = 0; double prior = -44.0; foreach (Motorcycle mc in session.Index <Motorcycle>("cc")) { Assert.NotNull(mc); mc.CC = mc.CC - prior; prior = mc.CC; ++ct; } Assert.AreEqual(ct, 2500); for (int i = 0; i < 95000; i++) { Motorcycle mc = new Motorcycle(); session.Persist(mc); } session.FlushUpdates(); ct = 0; foreach (Motorcycle mc in session.Index <Motorcycle>()) { Assert.NotNull(mc); ++ct; } Assert.AreEqual(ct, 100000); session.Abort(); session.BeginUpdate(); ct = 0; prior = double.MinValue; foreach (Motorcycle mc in session.Index <Motorcycle>("cc")) { Assert.NotNull(mc); Assert.GreaterOrEqual(mc.CC, prior); prior = mc.CC; ++ct; if (ct < 25) { Console.Write(prior.ToString() + " "); } } ct = 0; prior = -44.0; foreach (Motorcycle mc in session.Index <Motorcycle>("cc")) { Assert.NotNull(mc); mc.CC = mc.CC - prior; prior = mc.CC; ++ct; } Assert.AreEqual(ct, 2500); session.Commit(); session.BeginUpdate(); ct = 0; prior = double.MinValue; foreach (Motorcycle mc in session.Index <Motorcycle>("cc")) { Assert.NotNull(mc); Assert.GreaterOrEqual(mc.CC, prior); prior = mc.CC; ++ct; } for (int i = 0; i < 95000; i++) { Motorcycle mc = new Motorcycle(); session.Persist(mc); DataBaseFileEntry dbEntry = new DataBaseFileEntry { Something = "Something" }; session.Persist(dbEntry); mc.AddChild(dbEntry); mc.AddChild(dbEntry); } session.FlushUpdates(); ct = 0; foreach (Motorcycle mc in session.Index <Motorcycle>()) { Assert.NotNull(mc); ++ct; } Assert.AreEqual(ct, 100000); session.Commit(); session.BeginRead(); session.Abort(); session.BeginUpdate(); foreach (Motorcycle mc in session.Index <Motorcycle>()) { Assert.NotNull(mc); VelocityDbList <DataBaseFileEntry> children = mc.Children; if (children != null && children.Count > 0) { mc.RemoveChild(children[0]); } ++ct; } session.Commit(); session.BeginRead(); ct = 0; prior = double.MinValue; foreach (Motorcycle mc in session.Index <Motorcycle>("cc")) { Assert.NotNull(mc); Assert.GreaterOrEqual(mc.CC, prior); prior = mc.CC; ++ct; } Assert.AreEqual(ct, 100000); session.Commit(); Console.WriteLine("Motorcycle index Test OK"); } using (SessionBase session = useServerSession ? (SessionBase) new ServerClientSession(systemDir) : (SessionBase) new SessionNoServer(systemDir)) { session.BeginUpdate(); session.Persist(car); registrationState = "Maine"; car = new Car(color, maxPassengers, fuelCapacity, litresPer100Kilometers, modelYear, brandName, modelName, maxSpeed, odometer, registrationState, registrationPlate, insuranceCompany, insurancePolicy); session.Persist(car); color = "Red"; maxPassengers = 5; fuelCapacity = 50; litresPer100Kilometers = 8; modelYear = new DateTime(2006, 1, 1); brandName = "Toyota"; modelName = "Tacoma"; maxSpeed = 210; odometer = 50000; registrationState = "Texas"; registrationPlate = "TX343433"; insurancePolicy = "CAA7878777"; car = new Car(color, maxPassengers, fuelCapacity, litresPer100Kilometers, modelYear, brandName, modelName, maxSpeed, odometer, registrationState, registrationPlate, insuranceCompany, insurancePolicy); session.Persist(car); color = "Black"; maxPassengers = 5; fuelCapacity = 60; litresPer100Kilometers = 3; modelYear = new DateTime(2001, 1, 1); brandName = "Lincoln"; modelName = "Town Car"; maxSpeed = 220; odometer = 250000; registrationState = "Texas"; registrationPlate = "TX543433"; insurancePolicy = "CAA7878775"; for (int i = 0; i < 1; i++) { registrationState = RandomString(2); registrationPlate = RandomString(12); car = new Car(color, maxPassengers, fuelCapacity, litresPer100Kilometers, modelYear, brandName, modelName, maxSpeed, odometer, registrationState, registrationPlate, insuranceCompany, insurancePolicy); session.Persist(car); color = null; maxPassengers = i; fuelCapacity = 60; litresPer100Kilometers = 3; modelYear = new DateTime(2001, 1, 1); brandName = "Null Car"; modelName = null; maxSpeed = 220; odometer = 250000; insurancePolicy = null; } session.Commit(); } using (SessionBase session = useServerSession ? (SessionBase) new ServerClientSession(systemDir) : (SessionBase) new SessionNoServer(systemDir)) { session.BeginRead(); Console.WriteLine("Blue Cars"); BTreeSet <Car> bTree = session.Index <Car>("color"); foreach (Car c in (from aCar in bTree where aCar.Color == "Blue" select aCar)) { Console.WriteLine(c.ToStringDetails(session)); } Console.WriteLine("Cars in fuel efficierncy order"); foreach (Car c in session.Index <Car>("litresPer100Kilometers")) { Console.WriteLine(c.ToStringDetails(session)); } Console.WriteLine("Vehicles ordered modelYear, brandName, modelName, color"); foreach (Vehicle v in session.Index <Vehicle>()) { Console.WriteLine(v.ToStringDetails(session)); } session.Commit(); } using (SessionBase session = useServerSession ? (SessionBase) new ServerClientSession(systemDir) : (SessionBase) new SessionNoServer(systemDir)) { session.BeginUpdate(); // these LINQ statements will trigger a binary search lookup (not a linear serach) of the matching Car objects in the BTreeSet Car c = (from aCar in session.Index <Car>("color") where aCar.Color == "Blue" select aCar).First(); c.Color = "Green"; session.Commit(); } using (SessionBase session = useServerSession ? (SessionBase) new ServerClientSession(systemDir) : (SessionBase) new SessionNoServer(systemDir)) { session.BeginUpdate(); // these LINQ statements will trigger a binary search lookup (not a linear serach) of the matching Car objects in the BTreeSet Car c = (from aCar in session.Index <Car>("color") where aCar.Color == "Green" select aCar).First(); UInt64 id = c.Id; session.DeleteObject(id); session.Abort(); session.BeginUpdate(); session.DeleteObject(id); session.Commit(); } using (SessionBase session = useServerSession ? (SessionBase) new ServerClientSession(systemDir) : (SessionBase) new SessionNoServer(systemDir)) { Stopwatch sw = new Stopwatch(); sw.Start(); session.BeginRead(); // these LINQ statements will trigger a binary search lookup (not a linear serach) of the matching Car objects in the BTreeSet Console.WriteLine("Blue Cars"); foreach (Car c in (from aCar in session.Index <Car>("color") where aCar.Color == "Blue" select aCar)) { Console.WriteLine(c.ToStringDetails(session)); } session.Commit(); sw.Stop(); Console.WriteLine(sw.Elapsed); } using (SessionBase session = useServerSession ? (SessionBase) new ServerClientSession(systemDir) : (SessionBase) new SessionNoServer(systemDir)) { session.BeginUpdate(); for (int i = 0; i < 10000; i++) { // add some junk to make search harder car = new Car(color, maxPassengers, fuelCapacity, litresPer100Kilometers, modelYear, brandName, modelName, i, odometer, registrationState, registrationPlate + i, insuranceCompany, insurancePolicy); session.Persist(car); } session.Commit(); } using (SessionBase session = useServerSession ? (SessionBase) new ServerClientSession(systemDir) : (SessionBase) new SessionNoServer(systemDir)) { session.BeginRead(); // these LINQ statements will trigger a binary search lookup (not a linear serach) of the matching Car objects in the BTreeSet Console.WriteLine("Blue Cars"); foreach (Car c in (from aCar in session.Index <Car>("color") where aCar.Color == "Blue" select aCar)) { Console.WriteLine(c.ToStringDetails(session)); } session.Commit(); } using (SessionBase session = useServerSession ? (SessionBase) new ServerClientSession(systemDir) : (SessionBase) new SessionNoServer(systemDir)) { session.BeginUpdate(); Car c = (from aCar in session.Index <Car>("color") where aCar.Color == "Blue" select aCar).First(); c.Unpersist(session); session.Commit(); } using (SessionBase session = useServerSession ? (SessionBase) new ServerClientSession(systemDir) : (SessionBase) new SessionNoServer(systemDir)) { session.BeginRead(); foreach (Car c in session.Index <Car>()) { Console.WriteLine(c.ToStringDetails(session)); } Console.WriteLine("Blue Cars"); foreach (Car c in (from aCar in session.Index <Car>() where aCar.Color == "Blue" select aCar)) { Console.WriteLine(c.ToStringDetails(session)); } session.Commit(); } using (SessionBase session = useServerSession ? (SessionBase) new ServerClientSession(systemDir) : (SessionBase) new SessionNoServer(systemDir)) { session.BeginUpdate(); InsuranceCompany prior = insuranceCompany; try { for (int i = 0; i < 100000; i++) { insuranceCompany = new InsuranceCompany("AAA", "858787878"); insuranceCompany.Persist(session, prior); prior = insuranceCompany; } Assert.IsTrue(false); // should not get here } catch (UniqueConstraintException) { } session.Commit(); } using (SessionBase session = useServerSession ? (SessionBase) new ServerClientSession(systemDir) : (SessionBase) new SessionNoServer(systemDir)) { session.BeginRead(); Database db = session.OpenDatabase(session.DatabaseNumberOf(typeof(InsuranceCompany))); var q = from company in session.Index <InsuranceCompany>("name", db) where company.Name == "AAA" select company; foreach (InsuranceCompany company in q) { Console.WriteLine(company.ToStringDetails(session)); // only one will match } session.Commit(); } using (SessionBase session = useServerSession ? (SessionBase) new ServerClientSession(systemDir) : (SessionBase) new SessionNoServer(systemDir)) { session.BeginUpdate(); InsuranceCompany prior = insuranceCompany; try { for (int i = 0; i < 100000; i++) { insuranceCompany = new InsuranceCompany("AAA", "858787878"); session.Persist(insuranceCompany); } Assert.IsTrue(false); // should not get here } catch (UniqueConstraintException) { } session.Commit(); } using (SessionBase session = useServerSession ? (SessionBase) new ServerClientSession(systemDir) : (SessionBase) new SessionNoServer(systemDir)) { session.BeginRead(); Database db = session.OpenDatabase(session.DatabaseNumberOf(typeof(InsuranceCompany))); var q = from company in session.Index <InsuranceCompany>("name", db) where company.Name == "AAA" select company; foreach (InsuranceCompany company in q) { Console.WriteLine(company.ToStringDetails(session)); // only one will match } bool exists = (from anEntry in session.Index <InsuranceCompany>("name", db) where anEntry.Name == "AAA" select 0).Any(); Assert.IsTrue(exists); session.Commit(); } using (SessionBase session = useServerSession ? (SessionBase) new ServerClientSession(systemDir) : (SessionBase) new SessionNoServer(systemDir)) { session.BeginUpdate(); try { for (int i = 0; i < 100000; i++) { insuranceCompany = new InsuranceCompanySpecial("AAA", "858787878"); session.Persist(insuranceCompany); } Assert.IsTrue(false); // should not get here } catch (UniqueConstraintException) { } session.Commit(); } using (SessionBase session = useServerSession ? (SessionBase) new ServerClientSession(systemDir) : (SessionBase) new SessionNoServer(systemDir)) { session.BeginUpdate(); try { for (int i = 0; i < 100000; i++) { insuranceCompany = new InsuranceCompanySpecial2("AAA", "858787878"); session.Persist(insuranceCompany); } Assert.IsTrue(false); // should not get here } catch (UniqueConstraintException) { } session.Commit(); } using (SessionBase session = useServerSession ? (SessionBase) new ServerClientSession(systemDir) : (SessionBase) new SessionNoServer(systemDir)) { session.BeginRead(); Database db = session.OpenDatabase(session.DatabaseNumberOf(typeof(InsuranceCompanySpecial))); var q = from company in session.Index <InsuranceCompany>("name", db) where company.Name == "AAA" select company; foreach (InsuranceCompany company in q) { Console.WriteLine(company.ToStringDetails(session)); // only one will match } bool exists = (from anEntry in session.Index <InsuranceCompanySpecial>("name", db) where anEntry.Name == "AAA" select 0).Any(); Assert.IsTrue(exists); session.Commit(); } }
public void Recover1(SessionBase session) { Database db = null; session.BeginUpdate(); session.RegisterClass(typeof(SortedSetAny <int>)); session.RegisterClass(typeof(SortedSetAny <float>)); db = session.OpenDatabase(88, true, false); if (db != null) { session.DeleteDatabase(db); } db = session.OpenDatabase(89, true, false); if (db != null) { session.DeleteDatabase(db); } session.Commit(); session.BeginUpdate(); db = session.NewDatabase(88); session.FlushUpdates(); session.Abort(); session.BeginUpdate(); db = session.NewDatabase(88); SortedSetAny <float> floatSet; Oid floatSetOid; string dbPath = System.IO.Path.Combine(systemDir, "89.odb"); Placement place = new Placement(88); for (int i = 0; i < 10; i++) { floatSet = new SortedSetAny <float>(); floatSet.Persist(place, session); floatSetOid = floatSet.Oid; } db = session.NewDatabase(89); session.Commit(); File.Delete(dbPath); session.BeginUpdate(); db = session.NewDatabase(89); session.Commit(); FileInfo info = new FileInfo(dbPath); info.CopyTo(dbPath + "X"); session.BeginUpdate(); SortedSetAny <int> intSet; place = new Placement(89); for (int i = 0; i < 10; i++) { intSet = new SortedSetAny <int>(); intSet.Persist(place, session); } db = session.OpenDatabase(88); var list = db.AllObjects <SortedSetAny <float> >(); foreach (SortedSetAny <float> set in list) { set.Unpersist(session); } db = session.OpenDatabase(89); session.Commit(); intSet = null; db = null; // release refs so that cached data isn't stale File.Delete(dbPath); info = new FileInfo(dbPath + "X"); info.MoveTo(dbPath); session.BeginUpdate(); intSet = (SortedSetAny <int>)session.Open(89, 1, 1, false); Debug.Assert(intSet == null); object o = session.Open(88, 1, 1, false); floatSet = (SortedSetAny <float>)o; Debug.Assert(floatSet != null); session.Checkpoint(); db = session.OpenDatabase(88); session.DeleteDatabase(db); db = session.OpenDatabase(89); session.DeleteDatabase(db); session.Commit(); }
public void Recover1(SessionBase session) { Database db = null; session.BeginUpdate(); db = session.OpenDatabase(88, true, false); if (db != null) session.DeleteDatabase(db); db = session.OpenDatabase(89, true, false); if (db != null) session.DeleteDatabase(db); session.Commit(); session.BeginUpdate(); db = session.NewDatabase(88); session.FlushUpdates(); session.Abort(); session.BeginUpdate(); db = session.NewDatabase(88); SortedSetAny<float> floatSet; Oid floatSetOid; string dbPath = System.IO.Path.Combine(systemDir, "89.odb"); Placement place = new Placement(88); for (int i = 0; i < 10; i++) { floatSet = new SortedSetAny<float>(); floatSet.Persist(place, session); floatSetOid = floatSet.Oid; } db = session.NewDatabase(89); session.Commit(); File.Delete(dbPath); session.BeginUpdate(); db = session.NewDatabase(89); session.Commit(); FileInfo info = new FileInfo(dbPath); info.CopyTo(dbPath + "X"); session.BeginUpdate(); SortedSetAny<int> intSet; place = new Placement(89); for (int i = 0; i < 10; i++) { intSet = new SortedSetAny<int>(); intSet.Persist(place, session); } db = session.OpenDatabase(88); var list = db.AllObjects<SortedSetAny<float>>(); foreach (SortedSetAny<float> set in list) set.Unpersist(session); db = session.OpenDatabase(89); session.Commit(); intSet = null; db = null; // release refs so that cached data isn't stale File.Delete(dbPath); info = new FileInfo(dbPath + "X"); info.MoveTo(dbPath); session.BeginUpdate(); intSet = (SortedSetAny<int>)session.Open(89, 1, 1, false); Debug.Assert(intSet == null); object o = session.Open(88, 1, 1, false); floatSet = (SortedSetAny<float>)o; Debug.Assert(floatSet != null); session.Commit(); session.BeginUpdate(); db = session.OpenDatabase(88); session.DeleteDatabase(db); db = session.OpenDatabase(89); session.DeleteDatabase(db); session.Commit(); }
static public void SyncWith(this SessionBase sessionToUpdate, SessionBase sessionToRead, Func<SessionBase, UInt64, Change, bool> doUpdate) { UInt64 currentVersion; UInt64 pageToReadVersion; bool conflictFound = false; using (var reader = sessionToRead.BeginRead()) { Changes changes = (Changes)sessionToRead.Open(5, 1, 1, false); if (changes != null) { using (var updater = sessionToUpdate.BeginUpdate()) { var dbs = sessionToUpdate.OpenAllDatabases(); ReplicaSync matchingReplicaSync = null; foreach (ReplicaSync sync in sessionToUpdate.AllObjects<ReplicaSync>()) { if (sync.SyncFromHost == sessionToRead.SystemHostName && sync.SyncFromPath == sessionToRead.SystemDirectory) { matchingReplicaSync = sync; break; } } if (changes.ChangeList.Count > 0) { foreach (TransactionChanges transactionChanges in changes.ChangeList) { if (matchingReplicaSync == null || matchingReplicaSync.TransactionNumber < transactionChanges.TransactionNumber) { foreach (Change change in transactionChanges.ChangeList) { Database dbToUpdate = sessionToUpdate.OpenDatabase(change.DatabaseId, false, false); Database dbToRead = sessionToRead.OpenDatabase(change.DatabaseId, false, false); string dbName = dbToRead != null ? dbToRead.Name : null; if (change.Deleted) { if (dbToUpdate == null) // does not exist continue; if (change.PageId == 0) // Database delete { currentVersion = dbToUpdate.Page.PageInfo.VersionNumber; if (currentVersion < change.Version) sessionToUpdate.DeleteDatabase(dbToUpdate); else { conflictFound = true; if (doUpdate(sessionToUpdate, currentVersion, change)) sessionToUpdate.DeleteDatabase(dbToUpdate); } } else { Page page = sessionToUpdate.OpenPage(dbToUpdate, change.PageId); if (page == null) // page does not exist continue; currentVersion = page.PageInfo.VersionNumber; if (currentVersion < change.Version) sessionToUpdate.DeletePage(dbToUpdate, page); else { conflictFound = true; if (doUpdate(sessionToUpdate, currentVersion, change)) sessionToUpdate.DeleteDatabase(dbToUpdate); } } } else { if (dbToUpdate == null) // does not exist dbToUpdate = sessionToUpdate.NewDatabase(change.DatabaseId, 0, dbName); if (change.PageId > 0) { Page pageToUpdate = sessionToUpdate.OpenPage(dbToUpdate, change.PageId); Page pageToRead = sessionToRead.OpenPage(dbToRead, change.PageId); if (pageToRead == null) // upcoming (not yet processed) changes must have deleted this page continue; currentVersion = pageToUpdate == null ? 0 : pageToUpdate.PageInfo.VersionNumber; pageToReadVersion = pageToRead.PageInfo.VersionNumber; if (currentVersion < pageToReadVersion || dbToUpdate.IsNew) sessionToUpdate.ReplacePage(dbToUpdate, pageToUpdate, pageToRead); else { conflictFound = true; if (doUpdate(sessionToUpdate, currentVersion, change)) sessionToUpdate.ReplacePage(dbToUpdate, pageToUpdate, pageToRead); } } } } } } UInt64 lastTransactionNumber = changes.ChangeList.Last().TransactionNumber; if (matchingReplicaSync != null) matchingReplicaSync.TransactionNumber = lastTransactionNumber; if (conflictFound) { sessionToUpdate.Verify(); } sessionToUpdate.Commit(); if (matchingReplicaSync == null) { sessionToUpdate.BeginUpdate(); // separate transaction or else gets confused with databases added by sync matchingReplicaSync = new ReplicaSync(sessionToRead, lastTransactionNumber); sessionToUpdate.Persist(matchingReplicaSync); sessionToUpdate.Commit(); } } } } } }