Ejemplo n.º 1
0
    private <A> A getNoLock(long recid, Serializer<A> serializer) {
      if (fullTx) {
        Fun.Tuple2 tu = mod.get(recid);
        if (tu != null) {
          if (tu.a == TOMBSTONE) return null;
          return (A) tu.a;
        }
      }

      Object oldVal = old.get(recid);
      if (oldVal != null) {
        if (oldVal == TOMBSTONE) return null;
        return (A) oldVal;
      }
      return TxEngine.this.get(recid, serializer);
    }
Ejemplo n.º 2
0
    public void lock(final long recid) {
      if (CC.LOG_LOCKS)
        Utils.LOG.finest("TRYLOCK R:" + recid + " T:" + Thread.currentThread().getId());

      // feel free to rewrite, if you know better (more efficient) way
      if (CC.ASSERT && locks.get(recid) == Thread.currentThread()) {
        // check node is not already locked by this thread
        throw new InternalError("node already locked by current thread: " + recid);
      }

      while (locks.putIfAbsent(recid, Thread.currentThread()) != null) {
        Thread.yield();
      }
      if (CC.LOG_LOCKS)
        Utils.LOG.finest("LOCK R:" + recid + " T:" + Thread.currentThread().getId());
    }
Ejemplo n.º 3
0
    public void unlock(final long recid) {
      if (CC.LOG_LOCKS)
        Utils.LOG.finest("UNLOCK R:" + recid + " T:" + Thread.currentThread().getId());

      final Thread t = locks.remove(recid);
      if (t != Thread.currentThread()) throw new InternalError("unlocked wrong thread");
    }
Ejemplo n.º 4
0
 public void assertNoLocks() {
   if (CC.PARANOID) {
     LongMap.LongMapIterator<Thread> i = locks.longMapIterator();
     while (i.moveToNext()) {
       if (i.value() == Thread.currentThread()) {
         throw new InternalError("Node " + i.key() + " is still locked");
       }
     }
   }
 }
Ejemplo n.º 5
0
 @Override
 public <A> void update(long recid, A value, Serializer<A> serializer) {
   if (!fullTx) throw new UnsupportedOperationException("read-only");
   commitLock.readLock().lock();
   try {
     mod.put(recid, Fun.t2(value, serializer));
   } finally {
     commitLock.readLock().unlock();
   }
 }
Ejemplo n.º 6
0
 @Override
 public <A> long put(A value, Serializer<A> serializer) {
   if (!fullTx) throw new UnsupportedOperationException("read-only");
   commitLock.writeLock().lock();
   try {
     Long recid = preallocRecidTake();
     usedPreallocatedRecids.add(recid);
     mod.put(recid, Fun.t2(value, serializer));
     return recid;
   } finally {
     commitLock.writeLock().unlock();
   }
 }
Ejemplo n.º 7
0
    @Override
    public <A> boolean compareAndSwap(
        long recid, A expectedOldValue, A newValue, Serializer<A> serializer) {
      if (!fullTx) throw new UnsupportedOperationException("read-only");

      commitLock.readLock().lock();
      try {

        Lock lock = locks[Store.lockPos(recid)].writeLock();
        lock.lock();
        try {
          A oldVal = getNoLock(recid, serializer);
          boolean ret = oldVal != null && oldVal.equals(expectedOldValue);
          if (ret) {
            mod.put(recid, Fun.t2(newValue, serializer));
          }
          return ret;
        } finally {
          lock.unlock();
        }
      } finally {
        commitLock.readLock().unlock();
      }
    }
Ejemplo n.º 8
0
    @Override
    public void commit() {
      if (!fullTx) throw new UnsupportedOperationException("read-only");

      commitLock.writeLock().lock();
      try {
        if (closed) return;
        if (uncommitedData) throw new IllegalAccessError("uncomitted data");
        txs.remove(ref);
        cleanTxQueue();

        if (pojo.hasUnsavedChanges()) pojo.save(this);

        // check no other TX has modified our data
        LongMap.LongMapIterator oldIter = old.longMapIterator();
        while (oldIter.moveToNext()) {
          long recid = oldIter.key();
          for (Reference<Tx> ref2 : txs) {
            Tx tx = ref2.get();
            if (tx == this || tx == null) continue;
            if (tx.mod.containsKey(recid)) {
              close();
              throw new TxRollbackException();
            }
          }
        }

        LongMap.LongMapIterator<Fun.Tuple2> iter = mod.longMapIterator();
        while (iter.moveToNext()) {
          long recid = iter.key();
          if (old.containsKey(recid)) {
            close();
            throw new TxRollbackException();
          }
        }

        iter = mod.longMapIterator();
        while (iter.moveToNext()) {
          long recid = iter.key();

          Fun.Tuple2 val = iter.value();
          Serializer ser = (Serializer) val.b;
          Object old = superGet(recid, ser);
          if (old == null) old = TOMBSTONE;
          for (Reference<Tx> txr : txs) {
            Tx tx = txr.get();
            if (tx == null || tx == this) continue;
            tx.old.putIfAbsent(recid, old);
          }

          if (val.a == TOMBSTONE) {
            superDelete(recid, ser);
          } else {
            superUpdate(recid, val.a, ser);
          }
        }

        // there are no conflicts, so update the POJO in parent
        // TODO sort of hack, is it thread safe?
        getWrappedEngine().getSerializerPojo().registered = pojo.registered;
        superCommit();

        close();
      } finally {
        commitLock.writeLock().unlock();
      }
    }
Ejemplo n.º 9
0
 @Override
 public void close() {
   closed = true;
   old.clear();
   ref.clear();
 }