View Javadoc

1   /**
2    * 
3    */
4   package org.treetank.revisioning;
5   
6   import static com.google.common.base.Objects.toStringHelper;
7   import static com.google.common.base.Preconditions.checkArgument;
8   
9   import org.treetank.bucket.DataBucket;
10  import org.treetank.io.LogValue;
11  
12  /**
13   * Sliding Snapshot versioning of {@link DataBucket}s.
14   * 
15   * @author Sebastian Graf, University of Konstanz
16   * 
17   */
18  public class SlidingSnapshot implements IRevisioning {
19  
20      /**
21       * {@inheritDoc}
22       */
23      @Override
24      public DataBucket combineBuckets(final DataBucket[] pBuckets) {
25          checkArgument(pBuckets.length > 0, "At least one DataBucket must be provided");
26          // create entire bucket..
27          final DataBucket returnVal = new DataBucket(pBuckets[0].getBucketKey(), pBuckets[0].getLastBucketPointer());
28          // ...iterate through the datas and check if it is stored..
29          for (int i = 0; i < pBuckets[0].getDatas().length; i++) {
30              boolean bucketSkip = false;
31              // ... form the newest version to the oldest one..
32              for (int j = 0; !bucketSkip && j < pBuckets.length; j++) {
33                  // if the data is not set yet but existing in the current version..
34                  if (pBuckets[j].getData(i) != null) {
35                      // ...break out the loop the next time and..
36                      bucketSkip = true;
37                      // ...set it
38                      returnVal.setData(i, pBuckets[j].getData(i));
39                  }
40  
41              }
42          }
43          return returnVal;
44      }
45  
46      /**
47       * {@inheritDoc}
48       */
49      @Override
50      public LogValue combineBucketsForModification(int pRevisionsToRestore, long pNewBucketKey, DataBucket[] pBuckets,
51          boolean pFullDump) {
52          checkArgument(pBuckets.length > 0, "At least one DataBucket must be provided");
53          // create buckets for container..
54          final DataBucket[] returnVal =
55              {
56                  new DataBucket(pBuckets[0].getBucketKey(), pBuckets[0].getLastBucketPointer()),
57                  new DataBucket(pNewBucketKey, pBuckets[0].getBucketKey())
58              };
59          // ...iterate through the datas and check if it is stored..
60          for (int i = 0; i < pBuckets[0].getDatas().length; i++) {
61              boolean continueVal = true;
62              // ... form the newest version to the oldest one..
63              for (int j = 0; j < pBuckets.length && continueVal; j++) {
64                  // check if the data is not set..
65                  if (returnVal[0].getData(i) == null && pBuckets[j].getData(i) != null) {
66                      // ...set it to the read-cache and..
67                      returnVal[0].setData(i, pBuckets[j].getData(i));
68                      // ..if we receive the oldest version where the data was not set yet, then copy it by hand
69                      if (pBuckets.length >= pRevisionsToRestore && j == pBuckets.length - 1) {
70                          returnVal[1].setData(i, pBuckets[j].getData(i));
71                      }
72                      // escape this loop since val was set
73                      continueVal = false;
74                  }
75              }
76          }
77          // return the container
78          return new LogValue(returnVal[0], returnVal[1]);
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public String toString() {
86          return toStringHelper(this).toString();
87      }
88  
89  }