public void Delete() { Exist(); try { using (var c = new TotalContext(_connectionString)) { var site = c.Sites.Find(Name); var users = site.Users; var auctions = site.Auctions; var sessionList = users.Select(x => x.Sessions.ToList()).ToList().AsReadOnly(); foreach (var y in sessionList) { c.Sessions.RemoveRange(y); } c.Sites.Remove(site); c.SaveChanges(); } } catch (Exception e) { Debug.WriteLine(e.Message); throw new UnavailableDbException("connection failed!", e); } }
public bool BidOnAuction(ISession session, double offer) { Exist(); CheckNull(session, "session"); if (!session.IsValid()) { throw new ArgumentException("Session not valid anymore"); } CheckOffer(offer); CheckEndsOn(EndsOn, _alarmClock.Now, false); CheckNotEquals(Seller, session.User); try { using (var c = new TotalContext(_connectionString)) { var auct = c.Auctions.Find(Id); var bidderSess = c.Sessions.Find(session.Id); var sel = (from u in c.Users where u.Name == Seller.Username && bidderSess.User.SiteName == u.SiteName select u).AsNoTracking().SingleOrDefault(); var bidder = new User(session.User.Username, auct.Site.Name, _alarmClock, _connectionString); if (sel == null) { throw new ArgumentException("User from another site can't bid in this auction!", nameof(bidderSess.UserId)); } bidderSess.ValidUntil = _alarmClock.Now.AddSeconds(auct.Site.SessionExp); var mySession = (Session)session; mySession.ValidUntil = bidderSess.ValidUntil; if (!ControlBidder(auct, bidder, offer)) { return(false); } UpdateAuctionBid(auct, bidderSess, offer); c.SaveChanges(); } } catch (Exception e) when(!(e is ArgumentException || e is InvalidOperationException)) { Debug.WriteLine(e.Message); throw new UnavailableDbException("connection error!"); } return(true); }
public void Setup(string connectionString) { CheckNull(connectionString, "connectionString"); try { using (var database = new TotalContext(connectionString)) { database.Database.Delete(); database.Database.Create(); } } catch (Exception e) { Debug.WriteLine(e.Message); throw new UnavailableDbException("setup failed!", e); } }
public void TestPerformance() { var jsonData = File.ReadAllBytes(JsonFilePath); FLError error; var unmanagedData = Marshal.AllocHGlobal(jsonData.Length); Marshal.Copy(jsonData, 0, unmanagedData, jsonData.Length); var slice = new FLSlice(unmanagedData.ToPointer(), (ulong)jsonData.Length); var fleeceData = NativeRaw.FLData_ConvertJSON(slice, &error); var root = Native.FLValue_AsArray(NativeRaw.FLValue_FromData(fleeceData)); RunTestVariants(() => { uint numDocs; { var st = Stopwatch.StartNew(); numDocs = InsertDocs(root); numDocs.Should().Be(12188, "because otherwise an incorrect number of documents was inserted"); st.PrintReport("Writing docs", numDocs, "doc"); } { var st = Stopwatch.StartNew(); IndexViews(); st.PrintReport("Indexing Albums/Artists views", numDocs, "doc"); } { var st = Stopwatch.StartNew(); IndexTracksView(); st.PrintReport("Indexing Tracks view", numDocs, "doc"); } { var st = Stopwatch.StartNew(); var context = new TotalContext(); var reduce = new C4ManagedReduceFunction(TotalAccumulate, TotalReduce, context); var numArtists = QueryGrouped(_artistsView, reduce.Native); reduce.Dispose(); numArtists.Should().Be(1141, "because otherwise the query returned incorrect information"); st.PrintReport("Grouped query of Artist view", numDocs, "doc"); } }); Marshal.FreeHGlobal(unmanagedData); }
public IAuction CreateAuction(string description, DateTime endsOn, double startingPrice) { CheckIsValid(this); CheckNull(description, "description"); CheckIsNotEmpty(description, "description"); CheckDouble(startingPrice, "startingPrice"); CheckEndsOn(endsOn, _alarmClock.Now, true); try { using (var c = new TotalContext(_connectionString)) { var session = c.Sessions.Find(Id); var site = c.Users.Find(session.UserId).Site; var user = c.Users.Find(session.UserId); var dbAuction = new AuctionEntity() { SellerId = user.Id, SiteName = site.Name, CurrentPrice = startingPrice, ShortDescription = description, EndsOn = endsOn, }; c.Auctions.Add(dbAuction); session.ValidUntil = _alarmClock.Now.AddSeconds(site.SessionExp); c.SaveChanges(); ValidUntil = _alarmClock.Now.AddSeconds(site.SessionExp); return(new Auction(dbAuction.Id, User, description, endsOn, _alarmClock, _connectionString)); } } catch (Exception e) { Debug.WriteLine(e.Message); throw new UnavailableDbException("connection failed!", e); } }