Пример #1
0
        public override T PutIfAbsent(T entity, string key, object value)
        {
            KernelTransaction ktx = TxBridge.get();

            try
            {
                using (Statement ignore = ktx.AcquireStatement())
                {
                    // Does it already exist?
                    T existing = Iterators.single(InternalGet(key, value, ktx), null);
                    if (existing != null)
                    {
                        return(existing);
                    }

                    // No, OK so Grab lock
                    ktx.Locks().acquireExclusiveExplicitIndexLock(explicitIndexResourceId(NameConflict, key));
                    // and check again -- now holding an exclusive lock
                    existing = Iterators.single(InternalGet(key, value, ktx), null);
                    if (existing != null)
                    {
                        // Someone else created this entry before us just before we got the lock,
                        // release the lock as we won't be needing it
                        ktx.Locks().releaseExclusiveExplicitIndexLock(explicitIndexResourceId(NameConflict, key));
                        return(existing);
                    }

                    InternalAdd(entity, key, value, ktx);
                    return(default(T));
                }
            }
            catch (EntityNotFoundException e)
            {
                throw new NotFoundException(format("%s %d not found", Type, Type.id(entity)), e);
            }
            catch (InvalidTransactionTypeKernelException e)
            {
                throw new ConstraintViolationException(e.Message, e);
            }
            catch (ExplicitIndexNotFoundKernelException e)
            {
                throw new Exception(e);
            }
        }
Пример #2
0
 public virtual Lock SharedLock(KernelTransaction ktx, PropertyContainer container)
 {
     using (Statement ignore = ktx.AcquireStatement())
     {
         if (container is Node)
         {
             long id = (( Node )container).Id;
             ktx.Locks().acquireSharedNodeLock(id);
             return(new CoreAPILock(() => ktx.Locks().releaseSharedNodeLock(id)));
         }
         else if (container is Relationship)
         {
             long id = (( Relationship )container).Id;
             ktx.Locks().acquireSharedRelationshipLock(id);
             return(new CoreAPILock(() => ktx.Locks().releaseSharedRelationshipLock(id)));
         }
         else
         {
             throw new System.NotSupportedException("Only relationships and nodes can be locked.");
         }
     }
 }