Package com.sleepycat.je

Examples of com.sleepycat.je.Transaction


    public void createLayer(final String layerName) throws InterruptedException {
        issueSync(new Callable<Void>() {

            public Void call() throws Exception {
                final Transaction transaction = entityStore.getEnvironment().beginTransaction(null,
                        null);
                try {
                    createLayer(layerName, transaction);
                    transaction.commit();
                } catch (RuntimeException e) {
                    transaction.abort();
                }
                return null;
            }
        });
    }
View Full Code Here


    }

    private class StartUpInitializer implements Callable<Void> {
        public Void call() throws Exception {
            final Transaction transaction = entityStore.getEnvironment().beginTransaction(null,
                    null);
            try {
                if (null == usedQuotaByTileSetId.get(transaction, GLOBAL_QUOTA_NAME,
                        LockMode.DEFAULT)) {
                    log.debug("First time run: creating global quota object");
                    // need a global TileSet cause the Quota->TileSet relationship is enforced
                    TileSet globalTileSet = new TileSet(GLOBAL_QUOTA_NAME);
                    tileSetById.put(transaction, globalTileSet);

                    Quota globalQuota = new Quota();
                    globalQuota.setTileSetId(GLOBAL_QUOTA_NAME);
                    usedQuotaById.put(transaction, globalQuota);
                    log.debug("created Global Quota");
                }

                final Set<String> layerNames = tilePageCalculator.getLayerNames();
                final Set<String> existingLayers = new GetLayerNames().call();

                final Set<String> layersToDelete = new HashSet<String>(existingLayers);
                layersToDelete.removeAll(layerNames);

                for (String layerName : layersToDelete) {
                    log.info("Deleting disk quota information for layer '" + layerName
                            + "' as it does not exist anymore...");
                    // do not call issue since we're already running on the transaction thread here
                    try {
                        new DeleteLayer(layerName).call(transaction);
                    } catch (Exception e) {
                        log.warn("Error deleting disk quota information for layer '" + layerName
                                + "'", e);
                    }
                }

                // add any missing tileset
                for (String layerName : layerNames) {
                    createLayer(layerName, transaction);
                }
                transaction.commit();
            } catch (RuntimeException e) {
                transaction.abort();
                throw e;
            }
            return null;
        }
View Full Code Here

        public DeleteLayer(String layerName) {
            this.layerName = layerName;
        }

        public Void call() throws Exception {
            Transaction transaction = entityStore.getEnvironment().beginTransaction(null, null);
            try {
                call(transaction);
                transaction.commit();
            } catch (RuntimeException e) {
                transaction.abort();
                throw e;
            }
            return null;
        }
View Full Code Here

         * oldLayerName to newLayerName and delete the old ones
         *
         * @see java.util.concurrent.Callable#call()
         */
        public Void call() throws Exception {
            Transaction transaction = entityStore.getEnvironment().beginTransaction(null, null);
            try {
                copyTileSets(transaction);
                DeleteLayer deleteCommand = new DeleteLayer(oldLayerName);
                deleteCommand.call(transaction);
                transaction.commit();
            } catch (RuntimeException e) {
                transaction.abort();
                throw e;
            }
            return null;
        }
View Full Code Here

            this.quotaDiff = quotaDiff;
            this.tileCountDiffs = tileCountDiffs;
        }

        public Void call() throws Exception {
            final Transaction tx = entityStore.getEnvironment().beginTransaction(null, null);
            try {
                TileSet storedTileset = getOrCreateTileSet(tx, tileSet);
                // increase the tileset used quota
                addToUsedQuota(tx, storedTileset, quotaDiff);

                // and each page's fillFactor for lru/lfu expiration
                if (tileCountDiffs.size() > 0) {
                    TilePage page;
                    String pageKey;
                    for (PageStatsPayload payload : tileCountDiffs) {
                        page = payload.getPage();
                        pageKey = page.getKey();
                        PageStats pageStats;

                        TilePage storedPage = pageByKey.get(tx, pageKey, LockMode.DEFAULT);
                        if (null == storedPage) {
                            pageById.put(tx, page);
                            storedPage = page;
                            pageStats = new PageStats(storedPage.getId());
                            // pageStatsById.put(tx, pageStats);
                        } else {
                            pageStats = pageStatsByPageId.get(tx, storedPage.getId(), null);
                        }

                        final byte level = page.getZoomLevel();
                        final BigInteger tilesPerPage = tilePageCalculator.getTilesPerPage(tileSet,
                                level);
                        final int tilesAdded = payload.getNumTiles();

                        pageStats.addTiles(tilesAdded, tilesPerPage);
                        pageStatsById.putNoReturn(tx, pageStats);
                    }
                }
                tx.commit();
                return null;
            } catch (RuntimeException e) {
                e.printStackTrace();
                tx.abort();
                throw e;
            }
        }
View Full Code Here

        }

        public List<PageStats> call() throws Exception {
            List<PageStats> allStats = new ArrayList<PageStats>(statsUpdates.size());
            PageStats pageStats = null;
            final Transaction tx = entityStore.getEnvironment().beginTransaction(null, null);
            try {
                for (PageStatsPayload payload : statsUpdates) {
                    TilePage page = payload.getPage();
                    TileSet storedTileset = tileSetById.get(tx, page.getTileSetId(),
                            LockMode.DEFAULT);
                    if (null == storedTileset) {
                        log.info("Can't add usage stats. TileSet does not exist. Was it deleted? "
                                + page.getTileSetId());
                        continue;
                    }

                    TilePage storedPage = pageByKey.get(tx, page.getKey(), null);

                    if (storedPage == null) {
                        pageById.put(tx, page);
                        storedPage = page;
                        pageStats = new PageStats(storedPage.getId());
                    } else {
                        pageStats = pageStatsByPageId.get(tx, storedPage.getId(), null);
                    }

                    final int addedHits = payload.getNumHits();
                    final int lastAccessTimeMinutes = (int) (payload.getLastAccessTime() / 1000 / 60);
                    final int creationTimeMinutes = storedPage.getCreationTimeMinutes();
                    pageStats.addHitsAndAccessTime(addedHits, lastAccessTimeMinutes,
                            creationTimeMinutes);
                    pageStatsById.putNoReturn(tx, pageStats);
                    allStats.add(pageStats);
                }
                tx.commit();
                return allStats;
            } catch (RuntimeException e) {
                tx.abort();
                throw e;
            }
        }
View Full Code Here

        public TruncatePage(TilePage tilePage) {
            this.tilePage = tilePage;
        }

        public PageStats call() throws Exception {
            Transaction tx = entityStore.getEnvironment().beginTransaction(null, null);
            try {
                PageStats pageStats = pageStatsByPageId.get(tx, tilePage.getId(), null);
                if (pageStats != null) {
                    pageStats.setFillFactor(0f);
                    pageStatsById.putNoReturn(tx, pageStats);
                }
                tx.commit();
                return pageStats;
            } catch (Exception e) {
                tx.abort();
                throw e;
            }
        }
View Full Code Here

    return new DatabaseEntry(makeBytes(USERS.getData(), SEPARATOR, publicHash, SEPARATOR, KEY));
  }
  @Override
  public boolean addUser(byte[] publicKey, byte[] publicHash) {
   
    Transaction txn = null;
    try {
      txn = env.beginTransaction(null, null);
      if (haveUser(txn, publicHash)) {
        txn.commit();
        return false;// User already exists
      }

      DatabaseEntry ph = new DatabaseEntry(publicHash);
      OperationStatus res = db.put(txn, USERS, ph);
      if (res != OperationStatus.SUCCESS) {
        log.severe("Could not add ph: " + res.toString());
        txn.abort();
        return false;
      }

      DatabaseEntry regDateKey = makeRegDateKey(publicHash);
      DatabaseEntry regTime = new DatabaseEntry(Util.long2bin(System.currentTimeMillis()));
      res = db.put(txn, regDateKey, regTime);
      if (res != OperationStatus.SUCCESS) {
        log.severe("Could not add regTime: " + res.toString());
        txn.abort();
        return false;
      }

      DatabaseEntry regPublicKeyKey = makePublicKeyKey(publicHash);
      DatabaseEntry regPublicKey = new DatabaseEntry(publicKey);
      res = db.put(txn, regPublicKeyKey, regPublicKey);
      if (res != OperationStatus.SUCCESS) {
        log.severe("Could not add publicKey: " + res.toString());
        txn.abort();
        return false;
      }
      txn.commit();
      return true;
    } catch (DatabaseException e) {
      severe("Exception while adding user", e);
      try {
        if (txn != null) {
          txn.abort();
        }
      } catch (DatabaseException e1) {
        // we already had a failure, ignore this one.
      }
      return false;
View Full Code Here

  }

  @Override
  public boolean deleteUser(User existingUser) {
    try {
      final Transaction txn = env.beginTransaction(null, null);
      byte[] publicHash = existingUser.getPublicHash();
      db.delete(txn, makeRegDateKey(publicHash));
      db.delete(txn, makePublicKeyKey(publicHash));
      Cursor cursor = db.openCursor(txn, null);
      try {
        OperationStatus status =
            cursor.getSearchBoth(USERS, new DatabaseEntry(publicHash), null);
        if (status == OperationStatus.SUCCESS) {
          cursor.delete();
          deleteUserData(existingUser, txn);
          return true;
        } else {
          return false;
        }
      } finally {
        cursor.close();
        txn.commit();
      }
    } catch (DatabaseException e) {
      severe("Exception while deleting user", e);
      return false;
    }
View Full Code Here

    return new DatabaseEntry(makeBytes("users/nonces/".getBytes(), publicKey));
  }

  @Override
  public boolean checkAndAddNonce(Nonce nonce, byte[] publicKey) {
    Transaction txn = null;
    try {
      txn = env.beginTransaction(null, null);
      DatabaseEntry noncesKey = makeNoncesKey(publicKey);
      DatabaseEntry nonceValue = new DatabaseEntry(nonce.toToken());
      OperationStatus status = db.getSearchBoth(txn, noncesKey, nonceValue, null);
      if (status == OperationStatus.SUCCESS) {
        txn.commit();
        return false;// Nonce already used
      }
      status = db.put(txn, noncesKey, nonceValue);
      if (status == OperationStatus.SUCCESS) {
        txn.commit();
        return true;
      } else {
        txn.abort();
        return false;
      }
    } catch (DatabaseException e) {
      severe("Exception while checking nonce for user", e);
      try {
        if (txn != null)
          txn.abort();
      } catch (DatabaseException e1) {
        // we already had a failure, ignore this one.
      }
      return false;
    }
View Full Code Here

TOP

Related Classes of com.sleepycat.je.Transaction

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.