Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using VelocityDb;
- using VelocityDb.Collection.BTree;
- using VelocityDb.Collection.Comparer;
- using VelocityDb.Indexing;
- using VelocityDb.Session;
- namespace CypherNerd
- {
- class Product : OptimizedPersistable
- {
- [Index]
- private readonly int? _id;
- public Product(int id)
- {
- _id = id;
- }
- }
- class Comment : OptimizedPersistable
- {
- [Index]
- private readonly int? _id;
- public Comment(int id)
- {
- _id = id;
- }
- }
- class Like : OptimizedPersistable
- {
- [Index]
- private readonly int? _id;
- public Like(int id)
- {
- _id = id;
- }
- }
- [Index]
- class DomainObject : OptimizedPersistable
- {
- [Index]
- private readonly int? _id;
- private readonly BTreeSet<Product> _products;
- private readonly BTreeSet<Comment> _comments;
- private readonly BTreeSet<Like> _likes;
- public DomainObject(SessionBase session, int id)
- {
- _id = id;
- _products = new BTreeSet<Product>(new VelocityDbComparer<Product>(), session);
- _comments = new BTreeSet<Comment>(new VelocityDbComparer<Comment>(), session);
- _likes = new BTreeSet<Like>(new VelocityDbComparer<Like>(), session);
- }
- [FieldAccessor("_id")]
- public new int? Id
- {
- get { return _id; }
- }
- public BTreeSet<Product> Products { get { return _products; }}
- public BTreeSet<Comment> Comments { get { return _comments; } }
- public BTreeSet<Like> Likes { get { return _likes; } }
- }
- internal class Program
- {
- private static void Main()
- {
- var rnd = new Random(Environment.TickCount);
- using (var session = new ServerClientSession("test_db", "velocity.dc.local"))
- {
- session.BeginUpdate();
- for (int i = 0; i < 500; i++)
- {
- var look = new DomainObject(session, i);
- for (int x = 0; x < rnd.Next(11); x++)
- {
- var product = new Product(rnd.Next());
- var comment = new Comment(rnd.Next());
- var like = new Like(rnd.Next());
- session.Persist(product);
- session.Persist(comment);
- session.Persist(like);
- look.Likes.Add(like);
- look.Products.Add(product);
- look.Comments.Add(comment);
- }
- session.Persist(look);
- }
- session.Commit();
- }
- Console.WriteLine();
- using (var session = new ServerClientSession("test_db", "velocity.dc.local"))
- {
- session.BeginRead();
- foreach (var obj in session.Index<DomainObject>().Take(10))
- Console.WriteLine("id: {0}, products: {1}, likes: {2}, comments: {3}", obj.Id, obj.Products.Count, obj.Likes.Count, obj.Comments.Count);
- foreach (var obj in session.AllObjects<DomainObject>().Take(10))
- Console.WriteLine("id: {0}, products: {1}, likes: {2}, comments: {3}", obj.Id, obj.Products.Count, obj.Likes.Count, obj.Comments.Count);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement