Skip to content

Add Remote File Cache documentation for getRef method #18

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
91 changes: 91 additions & 0 deletions opensearch/reference/index-modules/store/remote-file-cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Remote File Cache

The Remote File Cache module in OpenSearch provides functionality for managing cached files in a distributed environment. This document describes the `getRef` method, which has been added to enhance the cache management capabilities.

## FileCache

The `FileCache` class is a key component of the Remote Store module, implementing the `RefCountedCache` interface for managing cached files.

### getRef Method

The `getRef` method has been added to the `FileCache` class to retrieve the reference count for a given file path:

```java
@Override
public Integer getRef(Path key) {
return theCache.getRef(key);
}
```

This method delegates to the underlying cache implementation to get the reference count for the specified key (file path).

## RefCountedCache Interface

The `RefCountedCache` interface has been updated to include the `getRef` method:

```java
/**
* Get the reference count for key {@code key}.
*/
Integer getRef(K key);
```

This method is implemented by various cache implementations, including `LRUCache` and `SegmentedCache`.

## Cache Implementations

### LRUCache

The `LRUCache` class implements the `getRef` method as follows:

```java
@Override
public Integer getRef(K key) {
Objects.requireNonNull(key);
lock.lock();
try {
Node node = data.get(key);
if (node != null) {
return node.refCount;
}
return null;
} finally {
lock.unlock();
}
}
```

This implementation ensures thread-safety by using a lock and returns the reference count of the specified key if it exists in the cache.

### SegmentedCache

The `SegmentedCache` class implements the `getRef` method as follows:

```java
@Override
public Integer getRef(K key) {
if (key == null) throw new NullPointerException();
return segmentFor(key).getRef(key);
}
```

This implementation delegates to the appropriate segment of the cache to retrieve the reference count.

## Usage

The `getRef` method can be used to check the reference count of cached items. This is particularly useful for debugging and managing cache entries. For example:

```java
FileCache fileCache = // initialize FileCache
Path filePath = // specify file path
Integer refCount = fileCache.getRef(filePath);
if (refCount != null) {
System.out.println("Reference count for " + filePath + ": " + refCount);
} else {
System.out.println("File not found in cache: " + filePath);
}
```

## Conclusion

The addition of the `getRef` method to the Remote File Cache module enhances the ability to manage and monitor cached files. This feature provides developers with more control and insight into the state of the file cache, which is crucial for optimizing performance and troubleshooting in OpenSearch deployments that utilize remote storage.