Advertisement
Guest User

Untitled

a guest
Sep 16th, 2014
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.50 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using VelocityDb;
  4. using VelocityDb.Collection.BTree;
  5. using VelocityDb.Collection.Comparer;
  6. using VelocityDb.Indexing;
  7. using VelocityDb.Session;
  8.  
  9. namespace CypherNerd
  10. {
  11.     class Product : OptimizedPersistable
  12.     {
  13.         [Index]
  14.         private readonly int? _id;
  15.  
  16.         public Product(int id)
  17.         {
  18.             _id = id;
  19.         }
  20.     }
  21.  
  22.     class Comment : OptimizedPersistable
  23.     {
  24.         [Index]
  25.         private readonly int? _id;
  26.  
  27.         public Comment(int id)
  28.         {
  29.             _id = id;
  30.         }
  31.     }
  32.  
  33.     class Like : OptimizedPersistable
  34.     {
  35.         [Index]
  36.         private readonly int? _id;
  37.  
  38.         public Like(int id)
  39.         {
  40.             _id = id;
  41.         }
  42.     }
  43.  
  44.     [Index]
  45.     class DomainObject : OptimizedPersistable
  46.     {
  47.         [Index]
  48.         private readonly int? _id;
  49.         private readonly BTreeSet<Product> _products;
  50.         private readonly BTreeSet<Comment> _comments;
  51.         private readonly BTreeSet<Like> _likes;
  52.  
  53.         public DomainObject(SessionBase session, int id)
  54.         {
  55.             _id = id;
  56.             _products = new BTreeSet<Product>(new VelocityDbComparer<Product>(), session);
  57.             _comments = new BTreeSet<Comment>(new VelocityDbComparer<Comment>(), session);
  58.             _likes = new BTreeSet<Like>(new VelocityDbComparer<Like>(), session);
  59.         }
  60.  
  61.         [FieldAccessor("_id")]
  62.         public new int? Id
  63.         {
  64.             get { return _id; }
  65.         }
  66.  
  67.         public BTreeSet<Product> Products { get { return _products; }}
  68.         public BTreeSet<Comment> Comments { get { return _comments; } }
  69.         public BTreeSet<Like> Likes { get { return _likes; } }
  70.     }
  71.  
  72.  
  73.     internal class Program
  74.     {
  75.         private static void Main()
  76.         {
  77.             var rnd = new Random(Environment.TickCount);
  78.  
  79.             using (var session = new ServerClientSession("test_db", "velocity.dc.local"))
  80.             {
  81.                 session.BeginUpdate();
  82.                 for (int i = 0; i < 500; i++)
  83.                 {
  84.                     var look = new DomainObject(session, i);
  85.                     for (int x = 0; x < rnd.Next(11); x++)
  86.                     {
  87.                         var product = new Product(rnd.Next());
  88.                         var comment = new Comment(rnd.Next());
  89.                         var like = new Like(rnd.Next());
  90.  
  91.                         session.Persist(product);
  92.                         session.Persist(comment);
  93.                         session.Persist(like);
  94.  
  95.                         look.Likes.Add(like);
  96.                         look.Products.Add(product);
  97.                         look.Comments.Add(comment);
  98.                     }
  99.                     session.Persist(look);
  100.                 }
  101.                 session.Commit();
  102.             }
  103.             Console.WriteLine();
  104.  
  105.             using (var session = new ServerClientSession("test_db", "velocity.dc.local"))
  106.             {
  107.                 session.BeginRead();
  108.  
  109.                 foreach (var obj in session.Index<DomainObject>().Take(10))
  110.                     Console.WriteLine("id: {0}, products: {1}, likes: {2}, comments: {3}", obj.Id, obj.Products.Count, obj.Likes.Count, obj.Comments.Count);
  111.  
  112.                 foreach (var obj in session.AllObjects<DomainObject>().Take(10))
  113.                     Console.WriteLine("id: {0}, products: {1}, likes: {2}, comments: {3}", obj.Id, obj.Products.Count, obj.Likes.Count, obj.Comments.Count);
  114.  
  115.             }
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement