Skip to content

Commit

Permalink
add a getTimePartitionWithCheck in TsFileResource
Browse files Browse the repository at this point in the history
  • Loading branch information
qiaojialin committed Apr 1, 2020
1 parent df92e70 commit 9d9a4ef
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 36 deletions.
Expand Up @@ -1493,7 +1493,7 @@ protected void mergeEndAction(List<TsFileResource> seqFiles, List<TsFileResource
*/
public void loadNewTsFileForSync(TsFileResource newTsFileResource) throws LoadFileException {
File tsfileToBeInserted = newTsFileResource.getFile();
long newFilePartitionId = getNewFilePartitionId(newTsFileResource);
long newFilePartitionId = newTsFileResource.getTimePartitionWithCheck();
writeLock();
mergeLock.writeLock().lock();
try {
Expand Down Expand Up @@ -1529,7 +1529,7 @@ public void loadNewTsFileForSync(TsFileResource newTsFileResource) throws LoadFi
*/
public void loadNewTsFile(TsFileResource newTsFileResource) throws LoadFileException {
File tsfileToBeInserted = newTsFileResource.getFile();
long newFilePartitionId = getNewFilePartitionId(newTsFileResource);
long newFilePartitionId = newTsFileResource.getTimePartitionWithCheck();
writeLock();
mergeLock.writeLock().lock();
try {
Expand Down Expand Up @@ -1577,40 +1577,6 @@ public void loadNewTsFile(TsFileResource newTsFileResource) throws LoadFileExcep
}
}

/**
* Check and get the partition id of a TsFile to be inserted using the start times and end
* times of devices.
* TODO: when the partition violation happens, split the file and load into different partitions
* @throws LoadFileException if the data of the file cross partitions or it is empty
*/
private long getNewFilePartitionId(TsFileResource resource) throws LoadFileException {
long partitionId = -1;
for (Long startTime : resource.getStartTimeMap().values()) {
long p = StorageEngine.getTimePartition(startTime);
if (partitionId == -1) {
partitionId = p;
} else {
if (partitionId != p) {
throw new PartitionViolationException(resource);
}
}
}
for (Long endTime : resource.getEndTimeMap().values()) {
long p = StorageEngine.getTimePartition(endTime);
if (partitionId == -1) {
partitionId = p;
} else {
if (partitionId != p) {
throw new PartitionViolationException(resource);
}
}
}
if (partitionId == -1) {
throw new LoadEmptyFileException();
}
return partitionId;
}

/**
* Find the position of "newTsFileResource" in the sequence files if it can be inserted into them.
* @param newTsFileResource
Expand Down
Expand Up @@ -24,6 +24,9 @@
import org.apache.iotdb.db.engine.modification.ModificationFile;
import org.apache.iotdb.db.engine.querycontext.ReadOnlyMemChunk;
import org.apache.iotdb.db.engine.upgrade.UpgradeTask;
import org.apache.iotdb.db.exception.LoadEmptyFileException;
import org.apache.iotdb.db.exception.LoadFileException;
import org.apache.iotdb.db.exception.PartitionViolationException;
import org.apache.iotdb.db.service.UpgradeSevice;
import org.apache.iotdb.db.utils.FilePathUtils;
import org.apache.iotdb.db.utils.UpgradeUtils;
Expand Down Expand Up @@ -485,4 +488,38 @@ public long getTimePartition() {
String[] splits = FilePathUtils.splitTsFilePath(this);
return Long.parseLong(splits[splits.length - 2]);
}

/**
* Used when load new TsFiles not generated by the server
* Check and get the time partition
* TODO: when the partition violation happens, split the file and load into different partitions
* @throws PartitionViolationException if the data of the file cross partitions or it is empty
*/
public long getTimePartitionWithCheck() throws PartitionViolationException {
long partitionId = -1;
for (Long startTime : startTimeMap.values()) {
long p = StorageEngine.getTimePartition(startTime);
if (partitionId == -1) {
partitionId = p;
} else {
if (partitionId != p) {
throw new PartitionViolationException(this);
}
}
}
for (Long endTime : endTimeMap.values()) {
long p = StorageEngine.getTimePartition(endTime);
if (partitionId == -1) {
partitionId = p;
} else {
if (partitionId != p) {
throw new PartitionViolationException(this);
}
}
}
if (partitionId == -1) {
throw new PartitionViolationException(this);
}
return partitionId;
}
}

0 comments on commit 9d9a4ef

Please sign in to comment.