-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add LargeObjectCacheImpl and its tests
- Loading branch information
1 parent
3d4b39b
commit 6fa02c2
Showing
6 changed files
with
274 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
modules/session/src/main/java/com/tsurugidb/tsubakuro/sql/impl/LargeObjectCacheImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2023-2024 Project Tsurugi. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.tsurugidb.tsubakuro.sql.impl; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
import com.tsurugidb.tsubakuro.sql.LargeObjectCache; | ||
|
||
/** | ||
* An implementation of {@link LargeObjectCache}. | ||
*/ | ||
public class LargeObjectCacheImpl implements LargeObjectCache { | ||
|
||
private final Path path; | ||
private final boolean exists; | ||
private final AtomicBoolean closed = new AtomicBoolean(); | ||
|
||
public LargeObjectCacheImpl(@Nullable Path path) { | ||
this.path = path; | ||
if (path != null) { | ||
exists = new File(path.toString()).exists(); | ||
return; | ||
} | ||
exists = false; | ||
} | ||
public LargeObjectCacheImpl() { | ||
this.path = null; | ||
exists = false; | ||
} | ||
|
||
@Override | ||
public Optional<Path> find() { | ||
if (closed.get() || !exists) { | ||
return Optional.empty(); | ||
} | ||
return Optional.of(path); | ||
} | ||
|
||
@Override | ||
public void copyTo(@Nonnull Path destination) throws IOException { | ||
Objects.requireNonNull(destination); | ||
if (closed.get()) { | ||
throw new IOException("already closed"); | ||
} | ||
if (!exists) { | ||
throw new IOException("cannot find the file"); | ||
} | ||
Files.copy(path, destination); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
closed.set(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters