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
14
15
16
17
18 public class SlidingSnapshot implements IRevisioning {
19
20
21
22
23 @Override
24 public DataBucket combineBuckets(final DataBucket[] pBuckets) {
25 checkArgument(pBuckets.length > 0, "At least one DataBucket must be provided");
26
27 final DataBucket returnVal = new DataBucket(pBuckets[0].getBucketKey(), pBuckets[0].getLastBucketPointer());
28
29 for (int i = 0; i < pBuckets[0].getDatas().length; i++) {
30 boolean bucketSkip = false;
31
32 for (int j = 0; !bucketSkip && j < pBuckets.length; j++) {
33
34 if (pBuckets[j].getData(i) != null) {
35
36 bucketSkip = true;
37
38 returnVal.setData(i, pBuckets[j].getData(i));
39 }
40
41 }
42 }
43 return returnVal;
44 }
45
46
47
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
54 final DataBucket[] returnVal =
55 {
56 new DataBucket(pBuckets[0].getBucketKey(), pBuckets[0].getLastBucketPointer()),
57 new DataBucket(pNewBucketKey, pBuckets[0].getBucketKey())
58 };
59
60 for (int i = 0; i < pBuckets[0].getDatas().length; i++) {
61 boolean continueVal = true;
62
63 for (int j = 0; j < pBuckets.length && continueVal; j++) {
64
65 if (returnVal[0].getData(i) == null && pBuckets[j].getData(i) != null) {
66
67 returnVal[0].setData(i, pBuckets[j].getData(i));
68
69 if (pBuckets.length >= pRevisionsToRestore && j == pBuckets.length - 1) {
70 returnVal[1].setData(i, pBuckets[j].getData(i));
71 }
72
73 continueVal = false;
74 }
75 }
76 }
77
78 return new LogValue(returnVal[0], returnVal[1]);
79 }
80
81
82
83
84 @Override
85 public String toString() {
86 return toStringHelper(this).toString();
87 }
88
89 }