Skip to content

Commit 49f555a

Browse files
committed
clean the code
1 parent f43fa49 commit 49f555a

33 files changed

+5239
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.wesley.blobfs;
2+
3+
public class BfsBlobModel {
4+
String blobURI;
5+
String blobName;
6+
BfsBlobType bfsBlobType;
7+
BlobProperties blobProperties;
8+
9+
public BfsBlobModel() {
10+
}
11+
12+
public String getBlobURI() {
13+
return blobURI;
14+
}
15+
public void setBlobURI(String blobURI) {
16+
this.blobURI = blobURI;
17+
}
18+
public BfsBlobType getBfsBlobType() {
19+
return bfsBlobType;
20+
}
21+
22+
public void setBfsBlobType(BfsBlobType bfsBlobType) {
23+
this.bfsBlobType = bfsBlobType;
24+
}
25+
26+
public String getBlobName() {
27+
return blobName;
28+
}
29+
public void setBlobName(String blobName) {
30+
this.blobName = blobName;
31+
}
32+
public BlobProperties getBlobProperties() {
33+
return blobProperties;
34+
}
35+
public void setBlobProperties(BlobProperties blobProperties) {
36+
this.blobProperties = blobProperties;
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return "BfsBlobModel [blobURI=" + blobURI + ", blobName=" + blobName + ", bfsBlobType=" + bfsBlobType
42+
+ ", blobProperties=" + blobProperties.toString() + "]";
43+
}
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.wesley.blobfs;
2+
3+
public enum BfsBlobType {
4+
BLOCKBLOB, APPENDBLOB, PAGEBLOB, VIRTUALDIRECTORY
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.wesley.blobfs;
2+
3+
import java.util.Calendar;
4+
import java.util.Map.Entry;
5+
import java.util.concurrent.ConcurrentHashMap;
6+
7+
public abstract class BfsCacheBase {
8+
9+
protected ConcurrentHashMap<String, CachedObject> cacheStore = null;
10+
protected int capacity;
11+
protected int expireTime;
12+
13+
protected boolean put(String cacheKey, Object cached) throws BlobfsException{
14+
if (!trimToCapcity()){
15+
String errMessage = "Cache is full when puting the key: " + cacheKey + ".";
16+
throw new BlobfsException(errMessage);
17+
}
18+
Calendar timeout = Calendar.getInstance();
19+
timeout.setTimeInMillis(timeout.getTimeInMillis() + expireTime);
20+
CachedObject cacheObject = new CachedObject(cached, timeout);
21+
cacheStore.put(cacheKey, cacheObject);
22+
return true;
23+
}
24+
25+
protected boolean put(Long cacheKey, Object cached) throws BlobfsException{
26+
return put(Long.toString(cacheKey), cached);
27+
}
28+
29+
protected Object get(String cacheKey){
30+
if(this.has(cacheKey)){
31+
CachedObject cachedObject = cacheStore.get(cacheKey);
32+
if(cachedObject.expire != null){
33+
if(Calendar.getInstance().before(cachedObject.expire)){
34+
return cachedObject.cachedObject;
35+
}else{
36+
return null;
37+
}
38+
}
39+
return cachedObject.cachedObject;
40+
}
41+
return null;
42+
}
43+
44+
protected Object get(Long cacheKey){
45+
return get(Long.toString(cacheKey));
46+
}
47+
48+
protected boolean trimToCapcity(){
49+
boolean result = false;
50+
if (this.count() <= capacity || this.isEmpty()) {
51+
return true;
52+
}
53+
for (Entry<String, CachedObject> toRemove : cacheStore.entrySet()) {
54+
CachedObject cachedObject = toRemove.getValue();
55+
if(cachedObject.expire != null){
56+
if(Calendar.getInstance().after(cachedObject.expire)){
57+
this.delete(toRemove.getKey());
58+
}
59+
}
60+
}
61+
if (this.count() <= capacity){
62+
result = true;
63+
}
64+
return result;
65+
66+
}
67+
68+
protected boolean has(String cacheKey){
69+
return cacheStore.containsKey(cacheKey);
70+
}
71+
72+
protected boolean has(long cacheKey){
73+
return has(Long.toString(cacheKey));
74+
}
75+
76+
protected boolean isEmpty(){
77+
return cacheStore.isEmpty();
78+
}
79+
80+
protected void delete(String cacheKey){
81+
cacheStore.remove(cacheKey);
82+
}
83+
84+
protected void delete (Long cacheKey){
85+
delete(Long.toString(cacheKey));
86+
}
87+
88+
protected int count(){
89+
return cacheStore.size();
90+
}
91+
92+
protected void clear(){
93+
cacheStore.clear();
94+
}
95+
}
96+
/* the object that will be cached */
97+
class CachedObject{
98+
Calendar expire;
99+
Object cachedObject;
100+
public CachedObject(Object cachedObject){
101+
this.cachedObject = cachedObject;
102+
}
103+
public CachedObject(){
104+
}
105+
public CachedObject(Object cachedObject, Calendar expire){
106+
this.cachedObject = cachedObject;
107+
this.expire = expire;
108+
}
109+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.wesley.blobfs;
2+
3+
import java.util.ArrayList;
4+
import java.util.Map.Entry;
5+
import java.util.concurrent.ConcurrentHashMap;
6+
import java.util.concurrent.ExecutorService;
7+
import java.util.concurrent.Executors;
8+
import java.util.concurrent.ThreadFactory;
9+
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
import ru.serce.jnrfuse.struct.FileStat;
14+
15+
public class BfsFilesCache extends BfsCacheBase {
16+
private static BfsFilesCache instance;
17+
private static ExecutorService cacheAutoCleanupES;
18+
private static boolean clusterEnabled = Constants.BFS_CLUSTER_ENABLED;
19+
20+
private BfsFilesCache(){
21+
cacheStore = new ConcurrentHashMap<String, CachedObject>(Constants.BFS_FILES_CACHE_INIT_CAPACITY, 0.9f, 1);
22+
capacity = Constants.BFS_FILES_CACHE_MAX_CAPACITY;
23+
expireTime = Constants.BFS_FILES_CACHE_EXPIRE_TIME;
24+
/* start the lease auto cleanup service */
25+
if (clusterEnabled){
26+
startCacheAutoCleanupService();
27+
}
28+
}
29+
30+
private final void startCacheAutoCleanupService(){
31+
/* Make it a daemon */
32+
cacheAutoCleanupES = Executors.newSingleThreadExecutor(new ThreadFactory(){
33+
public Thread newThread(Runnable r) {
34+
Thread t = new Thread(r);
35+
t.setDaemon(true);
36+
return t;
37+
}
38+
});
39+
cacheAutoCleanupES.submit(new cacheAutoCleaner());
40+
}
41+
42+
public final boolean getFileStat(String path, FileStat stat){
43+
BfsPath bfsPath = new BfsPath(path);
44+
PathProperties pathProperties = (PathProperties) get(path);
45+
if (!bfsPath.fillFileStat(pathProperties, stat)){
46+
return false;
47+
}
48+
return true;
49+
50+
}
51+
52+
@Override
53+
public void finalize() {
54+
cacheAutoCleanupES.shutdown();
55+
}
56+
57+
public static BfsFilesCache getInstance(){
58+
if(instance == null){
59+
synchronized (BfsFilesCache.class) {
60+
if(instance == null){
61+
instance = new BfsFilesCache();
62+
}
63+
}
64+
}
65+
return instance;
66+
}
67+
68+
private class cacheAutoCleaner implements Runnable{
69+
private Logger logger = LoggerFactory.getLogger("cacheAutoCleaner.class");
70+
@Override
71+
public void run() {
72+
try {
73+
/* start the auto cleanup process */
74+
while (true){
75+
BfsFilesCache bfsFilesCache = BfsFilesCache.getInstance();
76+
/* Retrieve the msgs from the service bus topic */
77+
ArrayList<String> msgs = new ArrayList<>();
78+
msgs = MessageService.sbReceiveMessages();
79+
for (String msg: msgs){
80+
logger.trace("delete {} from cache", msg);
81+
BfsPath msgPath = new BfsPath(msg);
82+
BfsPathType msgPathType = msgPath.getBfsPathProperties().getBfsPathType();
83+
if ("ROOT".equals(msgPathType.toString())){
84+
bfsFilesCache.clear();
85+
} else if ("CONTAINER".equals(msgPathType.toString()) || "SUBDIR".equals(msgPathType.toString())){
86+
for (Entry<String, CachedObject> entry : bfsFilesCache.cacheStore.entrySet()) {
87+
if (entry.getKey().startsWith(msg)) {
88+
bfsFilesCache.delete(entry.getKey());
89+
}
90+
}
91+
} else if ("BLOB".equals(msgPathType.toString()) || "LINK".equals(msgPathType.toString())){
92+
if (bfsFilesCache.has(msg)) {
93+
bfsFilesCache.delete(msg);
94+
}
95+
}
96+
}
97+
Thread.sleep(Constants.DEFAULT_BFC_THREAD_SLEEP_MILLS);
98+
}
99+
} catch (Exception ex) {
100+
logger.error(ex.getMessage());
101+
}
102+
}
103+
104+
}
105+
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.wesley.blobfs;
2+
3+
import java.util.List;
4+
5+
public class BfsFuseOptions {
6+
private final String mMountPoint;
7+
private final String mBlobPrefix;
8+
private final boolean mDebug;
9+
private final List<String> mFuseOpts;
10+
11+
public BfsFuseOptions(String mountPoint, String blobPrefix, boolean debug, List<String> fuseOpts) {
12+
mMountPoint = mountPoint;
13+
mBlobPrefix = blobPrefix;
14+
mDebug = debug;
15+
mFuseOpts = fuseOpts;
16+
}
17+
18+
/**
19+
* @return The path to where the BlobFS should be mounted
20+
*/
21+
public String getMountPoint() {
22+
return mMountPoint;
23+
}
24+
25+
/**
26+
* @return The prefix of the blobs that will be used as the mounted BlobFS root
27+
* (e.g. /container1/blob1/)
28+
*/
29+
public String getBlobPrefix() {
30+
return mBlobPrefix;
31+
}
32+
33+
/**
34+
* @return extra options to pass to the FUSE mount command
35+
*/
36+
public List<String> getFuseOpts() {
37+
return mFuseOpts;
38+
}
39+
40+
/**
41+
* @return whether the file system should be mounted in debug mode
42+
*/
43+
public boolean isDebug() {
44+
return mDebug;
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.wesley.blobfs;
2+
3+
public interface BfsFuseService {
4+
}

0 commit comments

Comments
 (0)