Skip to content

Commit

Permalink
more javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
jingwei committed Apr 24, 2012
1 parent 9525768 commit 02fc07e
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 19 deletions.
103 changes: 85 additions & 18 deletions krati-main/src/main/java/krati/core/segment/SegmentManager.java
Expand Up @@ -123,36 +123,65 @@ private SegmentManager(String segmentHomePath, SegmentFactory segmentFactory, in
this._recycleLimit = computeRecycleLimit(segmentFileSizeMB);
this.open();
}


/**
* Computes the recycle limit based on the segment file size in MB.
*
* @param segmentFileSizeMB - the segment file size in MB
* @return the recycle limit
*/
private int computeRecycleLimit(int segmentFileSizeMB) {
// Should always return an integer greater than zero.
return (segmentFileSizeMB <= 64) ? 5 : ((segmentFileSizeMB <= 256) ? 3 : 2);
}


/**
* Gets the segment file size in MB.
*/
public int getSegmentFileSizeMB() {
return _segFileSizeMB;
}


/**
* Gets the file path to segment home.
*/
public String getSegmentHomePath() {
return _segHomePath;
}


/**
* Gets the segment factory.
*/
public SegmentFactory getSegmentFactory() {
return _segFactory;
}


/**
* Gets the current segment.
*/
public Segment getCurrentSegment() {
return _segCurrent;
}


/**
* Gets the segment at the specified <code>index</code>.
*
* @param index - the segment index (i.e., segmentId)
*/
public Segment getSegment(int index) {
return _segList.get(index);
}


/**
* Gets the count of segments managed by this SegmentManager.
*/
public int getSegmentCount() {
return _segList.size();
}


/**
* Gets the count of live segments managed by this SegmentManager.
*/
public int getLiveSegmentCount() {
int num = 0;

Expand All @@ -163,13 +192,16 @@ public int getLiveSegmentCount() {

return num;
}


/**
* Clears this SegmentManger.
*/
public synchronized void clear() {
clearInternal(true /* CLEAR META */);
}

/**
* Frees a segment.
* Frees the specified segment.
*/
public synchronized boolean freeSegment(Segment seg) throws IOException {
if (seg == null)
Expand Down Expand Up @@ -202,15 +234,15 @@ public synchronized boolean freeSegment(Segment seg) throws IOException {

return false;
}

/**
* Gets the next segment available for read and write.
*/
public synchronized Segment nextSegment() throws IOException {
_segCurrent = nextSegment(false);
return _segCurrent;
}

/**
* Gets the next segment available for read and write.
*
Expand Down Expand Up @@ -253,11 +285,21 @@ private synchronized Segment nextSegment(boolean newOnly) throws IOException {

return seg;
}


/**
* Initializes the segment meta file.
*
* @throws IOException
*/
private void initMeta() throws IOException {
_segMeta = new SegmentMeta(new File(_segHomePath, ".meta"));
}


/**
* Initializes the segments managed by this SegmentManager
*
* @throws IOException
*/
private void initSegs() throws IOException {
int loaded = 0;
File[] segFiles = listSegmentFiles();
Expand Down Expand Up @@ -293,7 +335,12 @@ private void initSegs() throws IOException {

_log.info("loaded: " + loaded + "/" + segFiles.length);
}


/**
* Clears this SegmentManager.
*
* @param clearMeta - whether to clear the segment meta file
*/
private void clearInternal(boolean clearMeta) {
// Close all known segments
for(int segId = 0, cnt = _segList.size(); segId < cnt; segId++) {
Expand Down Expand Up @@ -336,6 +383,9 @@ private boolean recycle(Segment seg) {
return false;
}

/**
* Lists all the segment files managed by this SegmentManager.
*/
protected File[] listSegmentFiles() {
File segDir = new File(_segHomePath);
File[] segFiles = segDir.listFiles(new FileFilter() {
Expand Down Expand Up @@ -364,15 +414,32 @@ public int compare(File f1, File f2) {

return segFiles;
}


/**
* Gets the meta data of all the segments managed by this SegmentManager.
*/
public SegmentMeta getMeta() {
return _segMeta;
}


/**
* Updates the meta data of all the segments accordingly.
*
* @throws IOException
*/
public synchronized void updateMeta() throws IOException {
_segMeta.wrap(this);
}


/**
* Gets the instance of SegmentManager for the specified <code>segmentHomePath</code>.
*
* @param segmentHomePath - the file path to segment home.
* @param segmentFactory - the segment factory
* @param segmentFileSizeMB - the segment file size in MB
* @return the instance of SegmentManager
* @throws IOException
*/
public synchronized static SegmentManager getInstance(String segmentHomePath, SegmentFactory segmentFactory, int segmentFileSizeMB) throws IOException {
if (segmentFileSizeMB < Segment.minSegmentFileSizeMB) {
throw new IllegalArgumentException("Invalid argument segmentFileSizeMB " + segmentFileSizeMB + ", smaller than " + Segment.minSegmentFileSizeMB);
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -6,7 +6,7 @@
<artifactId>project</artifactId>
<packaging>pom</packaging>
<version>0.4.5-SNAPSHOT</version>
<name>projects</name>
<name>project</name>
<description>A hash-based high-performance data store</description>
<url>http://sna-projects.com/krati</url>

Expand Down

0 comments on commit 02fc07e

Please sign in to comment.