Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing method in BMS service for equals block #152

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,82 @@
package com.stnetix.ariaddna.blockmanipulation.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.stnetix.ariaddna.blockmanipulation.exception.BlockDoesNotExistException;
import com.stnetix.ariaddna.blockmanipulation.exception.MetafileIsFolderException;
import com.stnetix.ariaddna.commonutils.dto.vufs.FileType;
import com.stnetix.ariaddna.vufs.bo.Block;
import com.stnetix.ariaddna.vufs.bo.Metafile;
import com.stnetix.ariaddna.vufs.bo.Metatable;



@Service
@Scope(value = "session")
public class BlockManipulationServiceImpl implements IBlockManipulationService {

private List<String> blockUuidList;
private HashMap<String, HashSet<String>> blockMap = new HashMap<>();

private Metafile GetMetafileByFileUuid(Metatable metatable, String fileUuid) {
Set<Metafile> metafileSet = metatable.getMetafileSet();
for (Metafile metafile : metafileSet) {
if (metafile.getFileUuid().equals(fileUuid)) {
return metafile;
}
}
return null;
}

private boolean containsBlock(Metafile metafile, Block block) {
for (String blockUuid : metafile.getBlockUuidList()) {
if (blockUuid.equals(block.getBlockUuid())) {
return true;
}
}
return false;
}

@Override
public boolean isExistOnVufs(Block block, Metatable metatable) {

HashSet<String> blockOfFile = null;
if (blockMap.containsKey(block.getFileUuid())) {
blockOfFile = blockMap.get(block.getFileUuid());
if (blockOfFile.contains(block.getBlockUuid())) {
return true;
}
Metafile metafile = GetMetafileByFileUuid(metatable, block.getFileUuid());
if (containsBlock(metafile, block)) {
blockOfFile.add(block.getBlockUuid());
return true;
} else {
return false;
}
} else {
Metafile metafile = GetMetafileByFileUuid(metatable, block.getFileUuid());
if (metafile != null) {
blockOfFile = new HashSet<>();
blockMap.put(block.getFileUuid(), blockOfFile);
if (containsBlock(metafile, block)) {
blockOfFile.add(block.getBlockUuid());
return true;
} else {
return false;
}

} else {
return false;
}
}
}

@Override
public String getNextBlockUuid(Metafile metafile)
Expand Down Expand Up @@ -78,6 +139,17 @@ public String getBlockUuidByNumber(Metafile metafile, int blockNumber)
return blockUuidList.get(blockNumber);
}

@Override public void removeBlockFromCache(Block block, Metatable metatable) {
HashSet<String> blocksOfFile = null;
if (blockMap.containsKey(block.getFileUuid())) {
blocksOfFile = blockMap.get(block.getFileUuid());
blocksOfFile.remove(block.getBlockUuid());
if (blocksOfFile.size() == 0) {
blockMap.remove(block.getFileUuid());
}
}
}

private StringBuilder getMetafileInfoForLog(Metafile metafile, String methodName) {
return new StringBuilder()
.append("Method ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

import com.stnetix.ariaddna.blockmanipulation.exception.BlockDoesNotExistException;
import com.stnetix.ariaddna.blockmanipulation.exception.MetafileIsFolderException;
import com.stnetix.ariaddna.vufs.bo.Block;
import com.stnetix.ariaddna.vufs.bo.Metafile;
import com.stnetix.ariaddna.vufs.bo.Metatable;

public interface IBlockManipulationService {

Expand All @@ -35,11 +37,26 @@ String getNextBlockUuid(Metafile metafile) throws MetafileIsFolderException,
* Method return Block uuid by number from Metafile. If metafile is folder, method
* throws {@link MetafileIsFolderException}.
* if Metafile doesn`t exist block with external number, throws {@link BlockDoesNotExistException}.
* @param metafile Metafil object.
* @param metafile Metafile object.
* @param blockNumber number of requested block.
* @return uuid of block with requsted number.
* @throws {@link BlockDoesNotExistException}, {@link MetafileIsFolderException}
* */
String getBlockUuidByNumber(Metafile metafile, int blockNumber)
throws MetafileIsFolderException, BlockDoesNotExistException;

/**
* Method checks the presence of a block in Metatable
* @param block Block object
* @param metatable Metatable object
* @return return true if Metatable contains block and otherwise return false
*/
boolean isExistOnVufs(Block block,Metatable metatable);

/**
* Methot remove block from cache
* @param block Block object
* @param metatable Metatable object
*/
void removeBlockFromCache(Block block,Metatable metatable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@
package com.stnetix.ariaddna.blockmanipulation.service;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import com.stnetix.ariaddna.commonutils.datetime.DateTime;
import com.stnetix.ariaddna.commonutils.dto.vufs.MetatableType;
import com.stnetix.ariaddna.vufs.bo.Block;
import com.stnetix.ariaddna.vufs.bo.Metatable;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -81,6 +88,51 @@ public void getNextBlockUuid() throws BlockDoesNotExistException, MetafileIsFold
blockManipulationService.getNextBlockUuid(metafile);
}

@Test
public void CheckBlock() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please change name of method to "isExistOnVufs"

int blockSize = 1024;
String version = "1";
byte[] data = new byte[blockSize];
DateTime date = new DateTime();

Map<String, String> properties = new HashMap<>();
properties.put("typeOnFs", FileType.FILE_BIN.toString());
metafile.setProperties(properties);

Random random = new Random();
random.nextBytes(data);
Block block1 = new Block(version, 0L, metafile.getFileUuid(), data,
date.getTimeInMillisec(),
(long) blockSize);
random.nextBytes(data);
Block block2 = new Block(version, 1L, metafile.getFileUuid(), data,
date.getTimeInMillisec(),
(long) blockSize);
random.nextBytes(data);
Block block3 = new Block(version, 1L, metafile.getFileUuid(), data,
date.getTimeInMillisec(),
(long) blockSize);
random.nextBytes(data);
Block block4 = new Block(version, 1L, metafile.getFileUuid(), data,
date.getTimeInMillisec(),
(long) blockSize);
metafile.addBlockUuid(block1.getBlockUuid());
metafile.addBlockUuid(block2.getBlockUuid());
metafile.addBlockUuid(block3.getBlockUuid());

Metatable metatable = new Metatable(MetatableType.MASTER, UUID.randomUUID().toString(),
UUID.randomUUID().toString());
metatable.addMetafile(metafile);
assertTrue(blockManipulationService.isExistOnVufs(block3, metatable));
assertFalse(blockManipulationService.isExistOnVufs(block4, metatable));
assertTrue(blockManipulationService.isExistOnVufs(block3, metatable));

//remove block
metafile.removeBlockUuid(block3.getBlockUuid());
blockManipulationService.removeBlockFromCache(block3, metatable);
assertFalse(blockManipulationService.isExistOnVufs(block3, metatable));
}

@Test(expected = MetafileIsFolderException.class)
public void getNextBlockUuidByFolder()
throws BlockDoesNotExistException, MetafileIsFolderException {
Expand Down