KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > cleaner > TrackedFileSummary


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: TrackedFileSummary.java,v 1.9 2006/10/30 21:14:13 bostic Exp $
7  */

8
9 package com.sleepycat.je.cleaner;
10
11 import com.sleepycat.je.dbi.MemoryBudget;
12
13 /**
14  * Delta file summary info for a tracked file. Tracked files are managed by
15  * the UtilizationTracker.
16  *
17  * <p>The methods in this class for reading obsolete offsets may be used by
18  * multiple threads without synchronization even while another thread is adding
19  * offsets. This is possible because elements are never deleted from the
20  * lists. The thread adding obsolete offsets does so under the log write
21  * latch to prevent multiple threads from adding concurrently.</p>
22  */

23 public class TrackedFileSummary extends FileSummary {
24
25     private UtilizationTracker tracker;
26     private long fileNum;
27     private OffsetList obsoleteOffsets;
28     private int memSize;
29     private boolean trackDetail;
30     private boolean allowFlush = true;
31
32     /**
33      * Creates an empty tracked summary.
34      */

35     TrackedFileSummary(UtilizationTracker tracker,
36                        long fileNum,
37                        boolean trackDetail) {
38         this.tracker = tracker;
39         this.fileNum = fileNum;
40         this.trackDetail = trackDetail;
41     }
42
43     /**
44      * Returns whether this summary is allowed or prohibited from being flushed
45      * or evicted during cleaning. By default, flushing is allowed.
46      */

47     public boolean getAllowFlush() {
48         return allowFlush;
49     }
50
51     /**
52      * Allows or prohibits this summary from being flushed or evicted during
53      * cleaning. By default, flushing is allowed.
54      */

55     void setAllowFlush(boolean allowFlush) {
56         this.allowFlush = allowFlush;
57     }
58
59     /**
60      * Returns the file number being tracked.
61      */

62     public long getFileNumber() {
63         return fileNum;
64     }
65
66     /**
67      * Return the total memory size for this object. We only bother to budget
68      * obsolete detail, not the overhead for this object, for two reasons:
69      * 1) The number of these objects is very small, and 2) unit tests disable
70      * detail tracking as a way to prevent budget adjustments here.
71      */

72     int getMemorySize() {
73         return memSize;
74     }
75
76     /**
77      * Overrides reset for a tracked file, and is called when a FileSummaryLN
78      * is written to the log.
79      *
80      * <p>Must be called under the log write latch.</p>
81      */

82     public void reset() {
83
84         obsoleteOffsets = null;
85
86         tracker.resetFile(this);
87
88         if (memSize > 0) {
89             updateMemoryBudget(0 - memSize);
90         }
91
92         super.reset();
93     }
94
95     /**
96      * Tracks the given offset as obsolete or non-obsolete.
97      *
98      * <p>Must be called under the log write latch.</p>
99      */

100     void trackObsolete(long offset) {
101
102         if (!trackDetail) {
103             return;
104         }
105         int adjustMem = 0;
106         if (obsoleteOffsets == null) {
107             obsoleteOffsets = new OffsetList();
108             adjustMem += MemoryBudget.TFS_LIST_INITIAL_OVERHEAD;
109         }
110         if (obsoleteOffsets.add(offset, tracker.getEnvironment().isOpen())) {
111             adjustMem += MemoryBudget.TFS_LIST_SEGMENT_OVERHEAD;
112         }
113         if (adjustMem != 0) {
114             updateMemoryBudget(adjustMem);
115         }
116     }
117
118     /**
119      * Adds the obsolete offsets as well as the totals of the given object.
120      */

121     void addTrackedSummary(TrackedFileSummary other) {
122
123         /* Add the totals. */
124         add(other);
125
126         /*
127          * Add the offsets. The memory budget has already been updated for the
128          * offsets to be added, so we only need to account for a difference
129          * when we merge them.
130          */

131         if (other.obsoleteOffsets != null) {
132             if (obsoleteOffsets != null) {
133                 /* Merge the other offsets into our list. */
134                 if (obsoleteOffsets.merge(other.obsoleteOffsets)) {
135                     /* There is one segment less as a result of the merge. */
136                     updateMemoryBudget
137                         (- MemoryBudget.TFS_LIST_SEGMENT_OVERHEAD);
138                 }
139             } else {
140                 /* Adopt the other's offsets as our own. */
141                 obsoleteOffsets = other.obsoleteOffsets;
142             }
143         }
144     }
145
146     /**
147      * Returns obsolete offsets as an array of longs, or null if none.
148      */

149     public long[] getObsoleteOffsets() {
150
151         if (obsoleteOffsets != null) {
152             return obsoleteOffsets.toArray();
153         } else {
154             return null;
155         }
156     }
157
158     /**
159      * Returns whether the given offset is present in the tracked offsets.
160      * This does not indicate whether the offset is obsolete in general, but
161      * only if it is known to be obsolete in this version of the tracked
162      * information.
163      */

164     boolean containsObsoleteOffset(long offset) {
165
166         if (obsoleteOffsets != null) {
167             return obsoleteOffsets.contains(offset);
168         } else {
169             return false;
170         }
171     }
172
173     private void updateMemoryBudget(int delta) {
174         memSize += delta;
175         tracker.getEnvironment().
176                 getMemoryBudget().
177                 updateMiscMemoryUsage(delta);
178     }
179 }
180
Popular Tags