提交 8e4b05ba 编写于 作者: T Tianan Li 提交者: XuYi

add check sg exists of path for cluster (#126)

上级 c7ad2afc
......@@ -148,11 +148,22 @@ public class MGraph implements Serializable {
* Check whether the input path is storage level for current Metadata Tree or not.
*
* @param path Format: root.node.(node)*
* @apiNote :for cluster
*/
public boolean checkStorageLevel(String path) {
return mtree.checkStorageGroup(path);
}
/**
* Check whether the storage group of the input path exists or not
*
* @param path Format: root.node.(node)*
* @apiNote :for cluster
*/
public boolean checkStorageExistOfPath(String path) {
return mtree.checkStorageExistOfPath(path);
}
/**
* Get all paths for given seriesPath regular expression if given seriesPath belongs to MTree, or
* get all linked seriesPath for given seriesPath if given seriesPath belongs to PTree Notice:
......
......@@ -343,6 +343,7 @@ public class MManager {
/**
* function for checking if the given path is storage level of mTree or not.
* @apiNote :for cluster
*/
public boolean checkStorageLevelOfMTree(String path) {
lock.readLock().lock();
......@@ -353,6 +354,19 @@ public class MManager {
}
}
/**
* function for checking if the storage group of given path exists in mTree or not.
* @apiNote :for cluster
*/
public boolean checkStorageExistOfPath(String path) {
lock.readLock().lock();
try {
return mgraph.checkStorageExistOfPath(path);
} finally {
lock.readLock().unlock();
}
}
/**
* function for adding a pTree.
*/
......
......@@ -216,6 +216,7 @@ public class MTree implements Serializable {
* check whether the input path is storage group or not
* @param path input path
* @return if it is storage group, return true. Else return false
* @apiNote :for cluster
*/
public boolean checkStorageGroup(String path) {
String[] nodeNames = path.split(DOUB_SEPARATOR);
......@@ -240,6 +241,38 @@ public class MTree implements Serializable {
}
}
/**
* Check whether the storage group of the path exists or not
* @param path input path
* @return If it's storage group exists, return true. Else return false
* @apiNote :for cluster
*/
public boolean checkStorageExistOfPath(String path) {
String[] nodeNames = path.split(DOUB_SEPARATOR);
MNode cur = root;
if (nodeNames.length <= 1 || !nodeNames[0].equals(root.getName())) {
return false;
}
int i = 1;
while (i < nodeNames.length - 1) {
MNode temp = cur.getChild(nodeNames[i]);
if (temp == null) {
return false;
}
if(temp.isStorageLevel()){
return true;
}
cur = cur.getChild(nodeNames[i]);
i++;
}
MNode temp = cur.getChild(nodeNames[i]);
if(temp != null && temp.isStorageLevel()) {
return true;
} else {
return false;
}
}
/**
* Check whether set file seriesPath for this node or not. If not, throw an exception
*/
......
......@@ -287,4 +287,32 @@ public class MManagerBasicTest {
fail(e.getMessage());
}
}
@Test
public void testCheckStorageExistOfPath() {
MManager manager = MManager.getInstance();
try {
assertEquals(false, manager.checkStorageExistOfPath("root"));
assertEquals(false, manager.checkStorageExistOfPath("root.vehicle"));
assertEquals(false, manager.checkStorageExistOfPath("root.vehicle.device"));
assertEquals(false, manager.checkStorageExistOfPath("root.vehicle.device.sensor"));
manager.setStorageLevelToMTree("root.vehicle");
assertEquals(true, manager.checkStorageExistOfPath("root.vehicle"));
assertEquals(true, manager.checkStorageExistOfPath("root.vehicle.device"));
assertEquals(true, manager.checkStorageExistOfPath("root.vehicle.device.sensor"));
assertEquals(false, manager.checkStorageExistOfPath("root.vehicle1"));
assertEquals(false, manager.checkStorageExistOfPath("root.vehicle1.device"));
manager.setStorageLevelToMTree("root.vehicle1.device");
assertEquals(false, manager.checkStorageExistOfPath("root.vehicle1.device1"));
assertEquals(false, manager.checkStorageExistOfPath("root.vehicle1.device2"));
assertEquals(false, manager.checkStorageExistOfPath("root.vehicle1.device3"));
assertEquals(true, manager.checkStorageExistOfPath("root.vehicle1.device"));
} catch (PathErrorException | IOException e) {
e.printStackTrace();
fail(e.getMessage());
}
}
}
......@@ -213,4 +213,33 @@ public class MTreeTest {
fail(e.getMessage());
}
}
@Test
public void testCheckStorageExistOfPath() {
// set storage group first
MTree root = new MTree("root");
try {
assertEquals(false, root.checkStorageExistOfPath("root"));
assertEquals(false, root.checkStorageExistOfPath("root.vehicle"));
assertEquals(false, root.checkStorageExistOfPath("root.vehicle.device"));
assertEquals(false, root.checkStorageExistOfPath("root.vehicle.device.sensor"));
root.setStorageGroup("root.vehicle");
assertEquals(true, root.checkStorageExistOfPath("root.vehicle"));
assertEquals(true, root.checkStorageExistOfPath("root.vehicle.device"));
assertEquals(true, root.checkStorageExistOfPath("root.vehicle.device.sensor"));
assertEquals(false, root.checkStorageExistOfPath("root.vehicle1"));
assertEquals(false, root.checkStorageExistOfPath("root.vehicle1.device"));
root.setStorageGroup("root.vehicle1.device");
assertEquals(false, root.checkStorageExistOfPath("root.vehicle1.device1"));
assertEquals(false, root.checkStorageExistOfPath("root.vehicle1.device2"));
assertEquals(false, root.checkStorageExistOfPath("root.vehicle1.device3"));
assertEquals(true, root.checkStorageExistOfPath("root.vehicle1.device"));
} catch (PathErrorException e) {
e.printStackTrace();
fail(e.getMessage());
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册