Class InMemoryGoogleCloudStorage
- java.lang.Object
-
- com.google.cloud.hadoop.gcsio.testing.InMemoryGoogleCloudStorage
-
- All Implemented Interfaces:
GoogleCloudStorage
public class InMemoryGoogleCloudStorage extends Object implements GoogleCloudStorage
InMemoryGoogleCloudStorage overrides the public methods of GoogleCloudStorage by implementing all the equivalent bucket/object semantics with local in-memory storage.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.google.cloud.hadoop.gcsio.GoogleCloudStorage
GoogleCloudStorage.ListPage<T>
-
-
Field Summary
-
Fields inherited from interface com.google.cloud.hadoop.gcsio.GoogleCloudStorage
LIST_MAX_RESULTS, MAX_COMPOSE_OBJECTS, MAX_RESULTS_UNLIMITED, PATH_DELIMITER
-
-
Constructor Summary
Constructors Constructor Description InMemoryGoogleCloudStorage()InMemoryGoogleCloudStorage(GoogleCloudStorageOptions storageOptions)InMemoryGoogleCloudStorage(GoogleCloudStorageOptions storageOptions, com.google.api.client.util.Clock clock)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Releases resources used by this instance.voidcompose(String bucketName, List<String> sources, String destination, String contentType)Composes inputs into a single GCS object.GoogleCloudStorageItemInfocomposeObjects(List<StorageResourceId> sources, StorageResourceId destination, CreateObjectOptions options)Composes inputs into a single GCS object.voidcopy(String srcBucketName, List<String> srcObjectNames, String dstBucketName, List<String> dstObjectNames)Copies metadata of the given objects.WritableByteChannelcreate(StorageResourceId resourceId, CreateObjectOptions options)Creates and opens an object for writing.voidcreateBucket(String bucketName, CreateBucketOptions options)Creates a bucket.voidcreateEmptyObject(StorageResourceId resourceId)Creates an empty object, useful for placeholders representing, for example, directories.voidcreateEmptyObject(StorageResourceId resourceId, CreateObjectOptions options)Creates an empty object, useful for placeholders representing, for example, directories.voidcreateEmptyObjects(List<StorageResourceId> resourceIds)Creates a list of empty objects; seeGoogleCloudStorage.createEmptyObject(StorageResourceId)for the single-item version of this method.voidcreateEmptyObjects(List<StorageResourceId> resourceIds, CreateObjectOptions options)Creates a list of empty objects; seeGoogleCloudStorage.createEmptyObject(StorageResourceId)for the single-item version of this method.voidcreateFolder(StorageResourceId resourceId, boolean recursive)Creates a native GCS folder resource.voiddeleteBuckets(List<String> bucketNames)Deletes a list of buckets.voiddeleteFolders(List<FolderInfo> folders)Deletes the given folder resources.voiddeleteObjects(List<StorageResourceId> fullObjectNames)Deletes the given objects.GoogleCloudStorageItemInfogetFolderInfo(StorageResourceId resourceId)Gets the metadata for a single folder.static GoogleCloudStorageOptionsgetInMemoryGoogleCloudStorageOptions()GoogleCloudStorageItemInfogetItemInfo(StorageResourceId resourceId)Gets information about an object or a bucket.List<GoogleCloudStorageItemInfo>getItemInfos(List<StorageResourceId> resourceIds)Gets information about multiple objects and/or buckets.GoogleCloudStorageOptionsgetOptions()Retrieve the options that were used to create this GoogleCloudStorage.Map<String,Long>getStatistics()Retrieves the statistics associated with theGoogleCloudStorageinstance.booleanisHnBucket(URI src)Checks ifresourceIdbelongs to a Hierarchical namespace enabled bucket.List<GoogleCloudStorageItemInfo>listBucketInfo()Gets a list of GoogleCloudStorageItemInfo for all buckets of this project.List<String>listBucketNames()Gets a list of names of buckets in this project.GoogleCloudStorage.ListPage<FolderInfo>listFolderInfoForPrefixPage(String bucketName, String objectNamePrefix, ListFolderOptions listFolderOptions, String pageToken)Returns the list of folder resources.List<GoogleCloudStorageItemInfo>listObjectInfo(String bucketName, String objectNamePrefix, ListObjectOptions listOptions)ListsGoogleCloudStorageItemInfoof objects contained in the given bucket and whose names begin with the given prefix.GoogleCloudStorage.ListPage<GoogleCloudStorageItemInfo>listObjectInfoPage(String bucketName, String objectNamePrefix, ListObjectOptions listOptions, String pageToken)The same semantics asGoogleCloudStorage.listObjectInfo(java.lang.String, java.lang.String), but returns only result of single list request (1 page).List<GoogleCloudStorageItemInfo>listObjectInfoStartingFrom(String bucketName, String startOffset, ListObjectOptions listOptions)ListsGoogleCloudStorageItemInfoof objects contained in the given bucket and their name is lexicographically greater than or equal the provided offset.voidmove(Map<StorageResourceId,StorageResourceId> sourceToDestinationObjectsMap)Moves objects within the same bucket.SeekableByteChannelopen(GoogleCloudStorageItemInfo itemInfo, GoogleCloudStorageReadOptions readOptions)Opens an object for reading using GoogleCloudStorageItemInfo.SeekableByteChannelopen(StorageResourceId resourceId, GoogleCloudStorageReadOptions readOptions)Opens an object for reading.voidrenameHnFolder(URI src, URI dst)Renamessrctodstusing the rename LRO API.List<GoogleCloudStorageItemInfo>updateItems(List<UpdatableItemInfo> itemInfoList)Attempt to update metadata of the objects referenced within the passed itemInfo objects.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.google.cloud.hadoop.gcsio.GoogleCloudStorage
copy, create, createBucket, listObjectInfo, listObjectInfoPage, listObjectInfoStartingFrom, open, open
-
-
-
-
Constructor Detail
-
InMemoryGoogleCloudStorage
public InMemoryGoogleCloudStorage()
-
InMemoryGoogleCloudStorage
public InMemoryGoogleCloudStorage(GoogleCloudStorageOptions storageOptions)
-
InMemoryGoogleCloudStorage
public InMemoryGoogleCloudStorage(GoogleCloudStorageOptions storageOptions, com.google.api.client.util.Clock clock)
-
-
Method Detail
-
getInMemoryGoogleCloudStorageOptions
public static GoogleCloudStorageOptions getInMemoryGoogleCloudStorageOptions()
-
getOptions
public GoogleCloudStorageOptions getOptions()
Description copied from interface:GoogleCloudStorageRetrieve the options that were used to create this GoogleCloudStorage.- Specified by:
getOptionsin interfaceGoogleCloudStorage
-
create
public WritableByteChannel create(StorageResourceId resourceId, CreateObjectOptions options) throws IOException
Description copied from interface:GoogleCloudStorageCreates and opens an object for writing. The bucket must already exist. IfresourceIdcontains a known generationId or a generationId of 0, it is used instead of any "overwrite" settings in the providedCreateObjectOptions(overwrite will only succeed if the provided generationId matches the existing object, where a generationId of 0 indicates no existing object expected).- Specified by:
createin interfaceGoogleCloudStorage- Parameters:
resourceId- identifies a StorageObjectoptions- Options to use when creating the object- Returns:
- a channel for writing to the given object
- Throws:
IOException- on IO error
-
createBucket
public void createBucket(String bucketName, CreateBucketOptions options) throws IOException
Description copied from interface:GoogleCloudStorageCreates a bucket.- Specified by:
createBucketin interfaceGoogleCloudStorage- Parameters:
bucketName- name of the bucket to createoptions- options to use when creating bucket- Throws:
IOException- on IO error
-
createEmptyObject
public void createEmptyObject(StorageResourceId resourceId) throws IOException
Description copied from interface:GoogleCloudStorageCreates an empty object, useful for placeholders representing, for example, directories. The bucket must already exist. If the object already exists, it is overwritten. SeeGoogleCloudStorage.create(StorageResourceId)for the behavior if StorageResourceId.getGenerationId() is explicitly set.- Specified by:
createEmptyObjectin interfaceGoogleCloudStorage- Parameters:
resourceId- identifies a StorageObject- Throws:
IOException- on IO error
-
createEmptyObject
public void createEmptyObject(StorageResourceId resourceId, CreateObjectOptions options) throws IOException
Description copied from interface:GoogleCloudStorageCreates an empty object, useful for placeholders representing, for example, directories. The bucket must already exist. If the object already exists, it is overwritten. SeeGoogleCloudStorage.create(StorageResourceId)for the behavior if StorageResourceId.getGenerationId() is explicitly set.- Specified by:
createEmptyObjectin interfaceGoogleCloudStorage- Parameters:
resourceId- identifies a StorageObjectoptions- options to use when creating the object- Throws:
IOException- on IO error
-
createEmptyObjects
public void createEmptyObjects(List<StorageResourceId> resourceIds) throws IOException
Description copied from interface:GoogleCloudStorageCreates a list of empty objects; seeGoogleCloudStorage.createEmptyObject(StorageResourceId)for the single-item version of this method. Implementations may use different flow than the single-item version for greater efficiency. SeeGoogleCloudStorage.create(StorageResourceId)for the behavior if StorageResourceId.getGenerationId() is explicitly set.- Specified by:
createEmptyObjectsin interfaceGoogleCloudStorage- Throws:
IOException
-
createEmptyObjects
public void createEmptyObjects(List<StorageResourceId> resourceIds, CreateObjectOptions options) throws IOException
Description copied from interface:GoogleCloudStorageCreates a list of empty objects; seeGoogleCloudStorage.createEmptyObject(StorageResourceId)for the single-item version of this method. Implementations may use different flow than the single-item version for greater efficiency. SeeGoogleCloudStorage.create(StorageResourceId)for the behavior if StorageResourceId.getGenerationId() is explicitly set.- Specified by:
createEmptyObjectsin interfaceGoogleCloudStorage- Throws:
IOException
-
open
public SeekableByteChannel open(StorageResourceId resourceId, GoogleCloudStorageReadOptions readOptions) throws IOException
Description copied from interface:GoogleCloudStorageOpens an object for reading.- Specified by:
openin interfaceGoogleCloudStorage- Parameters:
resourceId- identifies a StorageObjectreadOptions- Fine-grained options for behaviors of retries, buffering, etc.- Returns:
- a channel for reading from the given object
- Throws:
FileNotFoundException- if the given object does not existIOException- if object exists but cannot be opened
-
open
public SeekableByteChannel open(GoogleCloudStorageItemInfo itemInfo, GoogleCloudStorageReadOptions readOptions) throws IOException
Description copied from interface:GoogleCloudStorageOpens an object for reading using GoogleCloudStorageItemInfo.- Specified by:
openin interfaceGoogleCloudStorage- Parameters:
itemInfo- identifies a StorageObjectreadOptions- Fine-grained options for behaviors of retries, buffering, etc.- Returns:
- a channel for reading from the given object
- Throws:
FileNotFoundException- if the given object does not existIOException- if object exists but cannot be opened
-
deleteBuckets
public void deleteBuckets(List<String> bucketNames) throws IOException
Description copied from interface:GoogleCloudStorageDeletes a list of buckets. Does not throw any exception for "bucket not found" errors.- Specified by:
deleteBucketsin interfaceGoogleCloudStorage- Parameters:
bucketNames- name of the buckets to delete- Throws:
FileNotFoundException- if the given bucket does not existIOException- on IO error
-
deleteObjects
public void deleteObjects(List<StorageResourceId> fullObjectNames) throws IOException
Description copied from interface:GoogleCloudStorageDeletes the given objects. Does not throw any exception for "object not found" errors.- Specified by:
deleteObjectsin interfaceGoogleCloudStorage- Parameters:
fullObjectNames- names of objects to delete with their respective bucketNames.- Throws:
FileNotFoundException- if the given object does not existIOException- if object exists but cannot be deleted
-
deleteFolders
public void deleteFolders(List<FolderInfo> folders) throws IOException
Description copied from interface:GoogleCloudStorageDeletes the given folder resources. Does not throw any exception for "folders not found" errors.- Specified by:
deleteFoldersin interfaceGoogleCloudStorage- Parameters:
folders- names of folder resources to delete- Throws:
IOException- if folder exists but cannot be deleted
-
createFolder
public void createFolder(StorageResourceId resourceId, boolean recursive) throws IOException
Description copied from interface:GoogleCloudStorageCreates a native GCS folder resource. Applicable only for Hierarchical Namespace (HNS) enabled buckets.- Specified by:
createFolderin interfaceGoogleCloudStorage- Parameters:
resourceId- The StorageResourceId of the folder to create.recursive- If true, creates all nonexistent parent folders in the path.- Throws:
IOException- if the folder cannot be created, or if it already exists and is not a folder.
-
getFolderInfo
public GoogleCloudStorageItemInfo getFolderInfo(StorageResourceId resourceId) throws IOException
Description copied from interface:GoogleCloudStorageGets the metadata for a single folder.- Specified by:
getFolderInfoin interfaceGoogleCloudStorage- Parameters:
resourceId- The StorageResourceId of the folder.- Throws:
IOException- on IO error.
-
move
public void move(Map<StorageResourceId,StorageResourceId> sourceToDestinationObjectsMap) throws IOException
Description copied from interface:GoogleCloudStorageMoves objects within the same bucket. Moving objects between different buckets is not allowed.- Specified by:
movein interfaceGoogleCloudStorage- Parameters:
sourceToDestinationObjectsMap- map of destination objects to be moved, keyed by source- Throws:
FileNotFoundException- if the source object or the destination bucket does not existIOException- in all other error cases
-
copy
public void copy(String srcBucketName, List<String> srcObjectNames, String dstBucketName, List<String> dstObjectNames) throws IOException
Description copied from interface:GoogleCloudStorageCopies metadata of the given objects. After the copy is successfully complete, each object blob is reachable by two different names. Copying between two different locations or between two different storage classes is not allowed.- Specified by:
copyin interfaceGoogleCloudStorage- Parameters:
srcBucketName- name of the bucket containing the objects to copysrcObjectNames- names of the objects to copydstBucketName- name of the bucket to copy todstObjectNames- names of the objects after copy- Throws:
FileNotFoundException- if the source object or the destination bucket does not existIOException- in all other error cases
-
listBucketNames
public List<String> listBucketNames()
Description copied from interface:GoogleCloudStorageGets a list of names of buckets in this project.- Specified by:
listBucketNamesin interfaceGoogleCloudStorage
-
listBucketInfo
public List<GoogleCloudStorageItemInfo> listBucketInfo()
Description copied from interface:GoogleCloudStorageGets a list of GoogleCloudStorageItemInfo for all buckets of this project. This is no more expensive than calling listBucketNames(), since the list API for buckets already retrieves all the relevant bucket metadata.- Specified by:
listBucketInfoin interfaceGoogleCloudStorage
-
listObjectInfoPage
public GoogleCloudStorage.ListPage<GoogleCloudStorageItemInfo> listObjectInfoPage(String bucketName, String objectNamePrefix, ListObjectOptions listOptions, String pageToken) throws IOException
Description copied from interface:GoogleCloudStorageThe same semantics asGoogleCloudStorage.listObjectInfo(java.lang.String, java.lang.String), but returns only result of single list request (1 page).- Specified by:
listObjectInfoPagein interfaceGoogleCloudStorage- Parameters:
bucketName- bucket nameobjectNamePrefix- object name prefix or null if all objects in the bucket are desiredlistOptions- options to use when listing objectspageToken- the page token- Returns:
GoogleCloudStorage.ListPageobject with listedGoogleCloudStorageItemInfos and next page token if any- Throws:
IOException- on IO error
-
listFolderInfoForPrefixPage
public GoogleCloudStorage.ListPage<FolderInfo> listFolderInfoForPrefixPage(String bucketName, String objectNamePrefix, ListFolderOptions listFolderOptions, String pageToken) throws IOException
Description copied from interface:GoogleCloudStorageReturns the list of folder resources. Applicable only for HN enabled bucket- Specified by:
listFolderInfoForPrefixPagein interfaceGoogleCloudStorage- Parameters:
bucketName- bucket nameobjectNamePrefix- folder resource name prefixlistFolderOptions- options to use when listing folder resourcespageToken- the page token- Returns:
GoogleCloudStorage.ListPagefolder resources with listedFolders- Throws:
IOException- on IO error
-
listObjectInfo
public List<GoogleCloudStorageItemInfo> listObjectInfo(String bucketName, String objectNamePrefix, ListObjectOptions listOptions) throws IOException
Description copied from interface:GoogleCloudStorageListsGoogleCloudStorageItemInfoof objects contained in the given bucket and whose names begin with the given prefix.Note: Although GCS does not implement a file system, it treats objects that contain a delimiter (
ListObjectOptions.getDelimiter()) as different from other objects when listing objects. This will be clearer with an example.Consider a bucket with objects:
o1,d1/,d1/o1,d1/o2-
With
prefix == nullanddelimiter == /, we get:d1/,o1Withprefix == nullanddelimiter == null, we get:o1,d1/,d1/o1,d1/o2Thus when delimiter is
null, the entire key name is considered an opaque string, otherwise only the part up to the first delimiter is considered.- Specified by:
listObjectInfoin interfaceGoogleCloudStorage- Parameters:
bucketName- bucket nameobjectNamePrefix- object name prefix or null if all objects in the bucket are desiredlistOptions- options to use when listing objects- Returns:
- list of objects
- Throws:
IOException- on IO error
-
listObjectInfoStartingFrom
public List<GoogleCloudStorageItemInfo> listObjectInfoStartingFrom(String bucketName, String startOffset, ListObjectOptions listOptions) throws IOException
Description copied from interface:GoogleCloudStorageListsGoogleCloudStorageItemInfoof objects contained in the given bucket and their name is lexicographically greater than or equal the provided offset.Note: As GCS doesn't implement a file system, directory is also treated as an object (if it's been created). This APi filters out all those directory object and maintain the order of items. This APi strictly expects delimiter in listOptions to be not set.
Consider a bucket with objects:
o1,d1/,d1/o1,d1/o2-
With
startOffset == "o1", we get:o1WithstartOffset == "d1/", we get:d1/o1d1/o2WithstartOffset == "d1/"o1, we get:d1/o1d1/o2This is an experimental API and can change without notice.
- Specified by:
listObjectInfoStartingFromin interfaceGoogleCloudStorage- Parameters:
bucketName- bucket namestartOffset- offset sting, all object with name greater and equal will be listed.listOptions- options to use when listing objects- Returns:
- list of objects
- Throws:
IOException- on IO error
-
renameHnFolder
public void renameHnFolder(URI src, URI dst) throws IOException
Description copied from interface:GoogleCloudStorageRenamessrctodstusing the rename LRO API. This should be called only on an Hierarchical namespace enabled bucket.- Specified by:
renameHnFolderin interfaceGoogleCloudStorage- Parameters:
src- source pathdst- destination path- Throws:
IOException
-
isHnBucket
public boolean isHnBucket(URI src) throws IOException
Description copied from interface:GoogleCloudStorageChecks ifresourceIdbelongs to a Hierarchical namespace enabled bucket. This takes a path and not the bucket name since the caller may not have permission to query the bucket.- Specified by:
isHnBucketin interfaceGoogleCloudStorage- Parameters:
src- Path for which the check need to be performed- Returns:
- Throws:
IOException
-
getItemInfo
public GoogleCloudStorageItemInfo getItemInfo(StorageResourceId resourceId) throws IOException
Description copied from interface:GoogleCloudStorageGets information about an object or a bucket.- Specified by:
getItemInfoin interfaceGoogleCloudStorage- Parameters:
resourceId- identifies either root, a Bucket, or a StorageObject- Returns:
- information about the given item
- Throws:
IOException- on IO error
-
getItemInfos
public List<GoogleCloudStorageItemInfo> getItemInfos(List<StorageResourceId> resourceIds) throws IOException
Description copied from interface:GoogleCloudStorageGets information about multiple objects and/or buckets. Items that are "not found" will still have an entry in the returned list; exists() will return false for these entries. The return list will have the same ordering as the input arguments.- Specified by:
getItemInfosin interfaceGoogleCloudStorage- Parameters:
resourceIds- names of the GCS StorageObjects or Buckets for which to retrieve info.- Returns:
- information about the given resourceIds.
- Throws:
IOException- on IO error
-
updateItems
public List<GoogleCloudStorageItemInfo> updateItems(List<UpdatableItemInfo> itemInfoList) throws IOException
Description copied from interface:GoogleCloudStorageAttempt to update metadata of the objects referenced within the passed itemInfo objects.- Specified by:
updateItemsin interfaceGoogleCloudStorage- Returns:
- Updated GoogleCloudStorageItemInfo objects for the referenced objects.
- Throws:
IOException- on IO error
-
close
public void close()
Description copied from interface:GoogleCloudStorageReleases resources used by this instance.- Specified by:
closein interfaceGoogleCloudStorage
-
compose
public void compose(String bucketName, List<String> sources, String destination, String contentType) throws IOException
Description copied from interface:GoogleCloudStorageComposes inputs into a single GCS object. This performs a GCS Compose. Objects will be composed according to the order they appear in the input. The destination object, if already present, will be overwritten. Sources and destination are assumed to be in the same bucket.- Specified by:
composein interfaceGoogleCloudStorage- Parameters:
bucketName- name of the bucket containing the sources and the destinationsources- names of the objects to be composeddestination- name of the resulting objectcontentType- content-type of composed object- Throws:
IOException- if the Compose operation was unsuccessful
-
composeObjects
public GoogleCloudStorageItemInfo composeObjects(List<StorageResourceId> sources, StorageResourceId destination, CreateObjectOptions options) throws IOException
Description copied from interface:GoogleCloudStorageComposes inputs into a single GCS object. This performs a GCS Compose. Objects will be composed according to the order they appear in the input. The destination object will have metadata set according tooptions. Overwrite semantics for the destination object will follow the same semantics asGoogleCloudStorage.create(StorageResourceId, CreateObjectOptions). SeeGoogleCloudStorage.create(StorageResourceId)for the behavior if StorageResourceId.getGenerationId() is explicitly set. The bucket must be the same for all sources and the destination.- Specified by:
composeObjectsin interfaceGoogleCloudStorage- Throws:
IOException
-
getStatistics
public Map<String,Long> getStatistics()
Description copied from interface:GoogleCloudStorageRetrieves the statistics associated with theGoogleCloudStorageinstance.- Specified by:
getStatisticsin interfaceGoogleCloudStorage
-
-