Package org.infinispan.loader.bucket

Examples of org.infinispan.loader.bucket.Bucket


    /**
     * {@inheritDoc}
     */
    public Bucket get(String key) throws S3ConnectionException {
        Bucket bucket = null;
        InputStream is = null;
        ObjectInputStream ois = null;

        try {
            S3Object s3Object = connection.getConnection().getObject(rootS3Bucket, key).get();
            // it is possible that the object never existed. in this case, fall out.
            if (s3Object != null && s3Object.getContent() != null) {
                is = (InputStream) s3Object.getContent();
                bucket = (Bucket) connection.marshaller.objectFromStream(is);
                // TODO hack until we are sure the bucket has an immutable name
                bucket.setBucketName(key);
            }
        } catch (Exception e) {
            throw connection.convertToS3ConnectionException("Error while reading from object: " + key, e);
        } finally {
            safeClose(ois);
View Full Code Here


    }

    public Set<Bucket> values() throws S3ConnectionException {
        Set<Bucket> buckets = new HashSet<Bucket>();
        for (String s : keySet()) {
            Bucket bucket = get(s);
            if (bucket != null)
                buckets.add(bucket);
        }
        return buckets;
    }
View Full Code Here

    }

    protected Set<InternalCacheEntry> loadAllLockSafe() throws CacheLoaderException {
        Set<InternalCacheEntry> result = new HashSet<InternalCacheEntry>();
        for (File bucketFile : root.listFiles()) {
            Bucket bucket = loadBucket(bucketFile);
            if (bucket != null) {
                if (bucket.removeExpiredEntries()) {
                    saveBucket(bucket);
                }
                result.addAll(bucket.getStoredEntries());
            }
        }
        return result;
    }
View Full Code Here

    protected Bucket loadBucket(String bucketName) throws CacheLoaderException {
        return loadBucket(new File(root, bucketName));
    }

    protected Bucket loadBucket(File bucketFile) throws CacheLoaderException {
        Bucket bucket = null;
        if (bucketFile.exists()) {
            if (log.isTraceEnabled()) log.trace("Found bucket file: '" + bucketFile + "'");
            FileInputStream is = null;
            ObjectInputStream ois = null;
            try {
                is = new FileInputStream(bucketFile);
                ois = new ObjectInputStream(is);
                bucket = (Bucket) ois.readObject();
            } catch (Exception e) {
                String message = "Error while reading from file: " + bucketFile.getAbsoluteFile();
                log.error(message, e);
                throw new CacheLoaderException(message, e);
            } finally {
                safeClose(ois);
                safeClose(is);
            }
        }
        if (bucket != null) {
            bucket.setBucketName(bucketFile.getName());
        }
        return bucket;
    }
View Full Code Here

         ps.setString(1, keyHashCode);
         rs = ps.executeQuery();
         if (!rs.next()) return null;
         String bucketName = rs.getString(1);
         InputStream inputStream = rs.getBinaryStream(2);
         Bucket bucket = (Bucket) JdbcUtil.unmarshall(getMarshaller(), inputStream);
         bucket.setBucketName(bucketName);//bucket name is volatile, so not persisted.
         return bucket;
      } catch (SQLException e) {
         String message = "sql failure while loading key: " + keyHashCode;
         log.error(message, e);
         throw new CacheLoaderException(message, e);
View Full Code Here

         rs = ps.executeQuery();
         rs.setFetchSize(config.getFetchSize());
         Set<InternalCacheEntry> result = new HashSet<InternalCacheEntry>();
         while (rs.next()) {
            InputStream binaryStream = rs.getBinaryStream(1);
            Bucket bucket = (Bucket) JdbcUtil.unmarshall(getMarshaller(), binaryStream);
            result.addAll(bucket.getStoredEntries());
         }
         return result;
      } catch (SQLException e) {
         String message = "sql failure while loading key: ";
         log.error(message, e);
View Full Code Here

         int readBuckets = 0;
         int batchSize = config.getBatchSize();
         String bucketName = (String) objectInput.readObject();
         while (!bucketName.equals(BINARY_STREAM_DELIMITER)) {
            Bucket bucket = (Bucket) objectInput.readObject();
            readBuckets++;
            ByteBuffer buffer = JdbcUtil.marshall(getMarshaller(), bucket);
            ps.setBinaryStream(1, buffer.getStream(), buffer.getLength());
            ps.setLong(2, bucket.timestampOfFirstEntryToExpire());
            ps.setString(3, bucketName);
            if (readBuckets % batchSize == 0) {
               ps.executeBatch();
               if (log.isTraceEnabled())
                  log.trace("Executing batch " + (readBuckets / batchSize) + ", batch size is " + batchSize);
View Full Code Here

         ps = conn.prepareStatement(sql);
         rs = ps.executeQuery();
         rs.setFetchSize(config.getFetchSize());
         while (rs.next()) {
            InputStream inputStream = rs.getBinaryStream(1);
            Bucket bucket = (Bucket) JdbcUtil.unmarshall(getMarshaller(), inputStream);
            String bucketName = rs.getString(2);
            objectOutput.writeObject(bucketName);
            objectOutput.writeObject(bucket);
         }
         objectOutput.writeObject(BINARY_STREAM_DELIMITER);
View Full Code Here

         while (rs.next()) {
            String key = rs.getString(2);
            if (immediateLockForWritting(key)) {
               if (log.isTraceEnabled()) log.trace("Adding bucket keyed " + key + " for purging.");
               InputStream binaryStream = rs.getBinaryStream(1);
               Bucket bucket = (Bucket) JdbcUtil.unmarshall(getMarshaller(), binaryStream);
               bucket.setBucketName(key);
               expiredBuckets.add(bucket);
            } else {
               if (log.isTraceEnabled())
                  log.trace("Could not acquire write lock for " + key + ", this won't be purged even though it has expired elements");
            }
         }
      } catch (SQLException ex) {
         //if something happens make sure buckets locks are being release
         releaseLocks(expiredBuckets);
         connectionFactory.releaseConnection(conn);
         logAndThrow(ex, "Failed clearing JdbcBinaryCacheStore");
      } finally {
         JdbcUtil.safeClose(ps);
         JdbcUtil.safeClose(rs);
      }

      if (log.isTraceEnabled())
         log.trace("Found following buckets: " + expiredBuckets + " which are about to be expired");

      if (expiredBuckets.isEmpty()) return;
      Set<Bucket> emptyBuckets = new HashSet<Bucket>();
      //now update all the buckets in batch
      try {
         String sql = tableManipulation.getUpdateRowSql();
         ps = conn.prepareStatement(sql);
         int updateCount = 0;
         Iterator<Bucket> it = expiredBuckets.iterator();
         while (it.hasNext()) {
            Bucket bucket = it.next();
            bucket.removeExpiredEntries();
            if (!bucket.isEmpty()) {
               ByteBuffer byteBuffer = JdbcUtil.marshall(getMarshaller(), bucket);
               ps.setBinaryStream(1, byteBuffer.getStream(), byteBuffer.getLength());
               ps.setLong(2, bucket.timestampOfFirstEntryToExpire());
               ps.addBatch();
               updateCount++;
               if (updateCount % batchSize == 0) {
                  ps.executeBatch();
                  if (log.isTraceEnabled()) log.trace("Flushing batch, update count is: " + updateCount);
               }
            } else {
               it.remove();
               emptyBuckets.add(bucket);
            }
         }
         //flush the batch
         if (updateCount % batchSize != 0) {
            ps.executeBatch();
         }
         if (log.isTraceEnabled()) log.trace("Updated " + updateCount + " buckets.");
      } catch (SQLException ex) {
         //if something happens make sure buckets locks are being release
         releaseLocks(emptyBuckets);
         connectionFactory.releaseConnection(conn);
         logAndThrow(ex, "Failed clearing JdbcBinaryCacheStore");
      } finally {
         //release locks for the updated buckets.This won't include empty buckets, as these were migrated to emptyBuckets
         releaseLocks(expiredBuckets);
         JdbcUtil.safeClose(ps);
      }


      if (log.isTraceEnabled()) log.trace("About to remove empty buckets " + emptyBuckets);

      if (emptyBuckets.isEmpty()) return;
      //then remove the empty buckets
      try {
         String sql = tableManipulation.getDeleteRowSql();
         ps = conn.prepareStatement(sql);
         int deletionCount = 0;
         for (Bucket bucket : emptyBuckets) {
            ps.setString(1, bucket.getBucketName());
            ps.addBatch();
            deletionCount++;
            if (deletionCount % batchSize == 0) {
               if (log.isTraceEnabled())
                  log.trace("Flushing deletion batch, total deletion count so far is " + deletionCount);
View Full Code Here

TOP

Related Classes of org.infinispan.loader.bucket.Bucket

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.