2013-02-02 19:39:27 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Db4objects.Db4o;
|
|
|
|
|
using Db4objects.Db4o.Internal;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Datastore
|
|
|
|
|
{
|
|
|
|
|
public interface IObjectDbSession : IObjectContainer
|
|
|
|
|
{
|
2013-02-02 20:22:46 +00:00
|
|
|
|
void Create(object obj);
|
|
|
|
|
void Create(object obj, int depth);
|
2013-02-02 19:39:27 +00:00
|
|
|
|
void SaveAll<T>(Transaction transaction, IEnumerator<T> objects);
|
|
|
|
|
|
|
|
|
|
void Update(object obj);
|
|
|
|
|
void Update(object obj, int depth);
|
|
|
|
|
void UpdateAll<T>(Transaction transaction, IEnumerator<T> objects);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class ObjectDbSession : ObjectContainerSession, IObjectDbSession
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public ObjectDbSession(ObjectContainerBase server)
|
2013-02-02 20:22:46 +00:00
|
|
|
|
: base(server, server.NewTransaction(server.SystemTransaction(), new NoCacheReferenceSystem(), false))
|
2013-02-02 19:39:27 +00:00
|
|
|
|
{
|
|
|
|
|
_transaction.SetOutSideRepresentation(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Store(object obj)
|
|
|
|
|
{
|
2013-02-02 20:22:46 +00:00
|
|
|
|
throw new InvalidOperationException("Store is not supported. please use Create() or Update()");
|
2013-02-02 19:39:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void StoreAll(Transaction transaction, IEnumerator objects)
|
|
|
|
|
{
|
2013-02-02 20:22:46 +00:00
|
|
|
|
throw new InvalidOperationException("Store is not supported. please use Create() or Update()");
|
2013-02-02 19:39:27 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Store(object obj, int depth)
|
|
|
|
|
{
|
2013-02-02 20:22:46 +00:00
|
|
|
|
throw new InvalidOperationException("Store is not supported. please use Create() or Update()");
|
2013-02-02 19:39:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-02 20:22:46 +00:00
|
|
|
|
public void Create(object obj)
|
2013-02-02 19:39:27 +00:00
|
|
|
|
{
|
2013-02-02 20:22:46 +00:00
|
|
|
|
ValidateCreate(obj);
|
2013-02-02 19:39:27 +00:00
|
|
|
|
base.Store(obj);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-02 20:22:46 +00:00
|
|
|
|
public void Create(object obj, int depth)
|
2013-02-02 19:39:27 +00:00
|
|
|
|
{
|
2013-02-02 20:22:46 +00:00
|
|
|
|
ValidateCreate(obj);
|
2013-02-02 19:39:27 +00:00
|
|
|
|
base.Store(obj, depth);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SaveAll<T>(Transaction transaction, IEnumerator<T> objects)
|
|
|
|
|
{
|
|
|
|
|
var obj = objects.ToIEnumerable().ToList();
|
2013-02-02 20:22:46 +00:00
|
|
|
|
obj.ForEach(c => ValidateCreate(c));
|
2013-02-02 19:39:27 +00:00
|
|
|
|
|
|
|
|
|
base.StoreAll(transaction, obj.GetEnumerator());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Update(object obj)
|
|
|
|
|
{
|
|
|
|
|
ValidateUpdate(obj);
|
|
|
|
|
base.Store(obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update(object obj, int depth)
|
|
|
|
|
{
|
|
|
|
|
ValidateUpdate(obj);
|
|
|
|
|
base.Store(obj, depth);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateAll<T>(Transaction transaction, IEnumerator<T> objects)
|
|
|
|
|
{
|
|
|
|
|
var obj = objects.ToIEnumerable().ToList();
|
|
|
|
|
obj.ForEach(c => ValidateUpdate(c));
|
|
|
|
|
|
|
|
|
|
base.StoreAll(transaction, obj.GetEnumerator());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateAll(Transaction transaction, IEnumerator objects)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-02-02 20:22:46 +00:00
|
|
|
|
private void ValidateCreate(object obj)
|
2013-02-02 19:39:27 +00:00
|
|
|
|
{
|
|
|
|
|
if (IsAttached(obj))
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Attempted to save an object that is already attached to database");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ValidateUpdate(object obj)
|
|
|
|
|
{
|
|
|
|
|
if (!IsAttached(obj))
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Attempted to update an object that is not attached to database");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool IsAttached(object obj)
|
|
|
|
|
{
|
|
|
|
|
return base.Ext().GetID(obj) > 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static class Ext
|
|
|
|
|
{
|
|
|
|
|
public static IEnumerable<T> ToIEnumerable<T>(this IEnumerator<T> enumerator)
|
|
|
|
|
{
|
|
|
|
|
while (enumerator.MoveNext())
|
|
|
|
|
{
|
|
|
|
|
yield return enumerator.Current;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|