|
| 1 | +// |
| 2 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +// or more contributor license agreements. See the NOTICE file |
| 4 | +// distributed with this work for additional information |
| 5 | +// regarding copyright ownership. The ASF licenses this file |
| 6 | +// to you under the Apache License, Version 2.0 (the |
| 7 | +// "License"); you may not use this file except in compliance |
| 8 | +// with the License. You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, |
| 13 | +// software distributed under the License is distributed on an |
| 14 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +// KIND, either express or implied. See the License for the |
| 16 | +// specific language governing permissions and limitations |
| 17 | +// under the License. |
| 18 | +// |
| 19 | + |
| 20 | +/// Flags for controlling the storage of an `RDD`. Each ``StorageLevel`` records whether to use memory, |
| 21 | +/// or `ExternalBlockStore`, whether to drop the `RDD` to disk if it falls out of memory or |
| 22 | +/// `ExternalBlockStore`, whether to keep the data in memory in a serialized format, and whether |
| 23 | +/// to replicate the `RDD` partitions on multiple nodes. |
| 24 | +public struct StorageLevel: Sendable { |
| 25 | + /// Whether the cache should use disk or not. |
| 26 | + var useDisk: Bool |
| 27 | + |
| 28 | + /// Whether the cache should use memory or not. |
| 29 | + var useMemory: Bool |
| 30 | + |
| 31 | + /// Whether the cache should use off-heap or not. |
| 32 | + var useOffHeap: Bool |
| 33 | + |
| 34 | + /// Whether the cached data is deserialized or not. |
| 35 | + var deserialized: Bool |
| 36 | + |
| 37 | + /// The number of replicas. |
| 38 | + var replication: Int32 |
| 39 | + |
| 40 | + init(useDisk: Bool, useMemory: Bool, useOffHeap: Bool, deserialized: Bool, replication: Int32 = 1) |
| 41 | + { |
| 42 | + self.useDisk = useDisk |
| 43 | + self.useMemory = useMemory |
| 44 | + self.useOffHeap = useOffHeap |
| 45 | + self.deserialized = deserialized |
| 46 | + self.replication = replication |
| 47 | + } |
| 48 | + |
| 49 | + public static let NONE = StorageLevel( |
| 50 | + useDisk: false, useMemory: false, useOffHeap: false, deserialized: false) |
| 51 | + public static let DISK_ONLY = StorageLevel( |
| 52 | + useDisk: true, useMemory: false, useOffHeap: false, deserialized: false) |
| 53 | + public static let DISK_ONLY_2 = StorageLevel( |
| 54 | + useDisk: true, useMemory: false, useOffHeap: false, deserialized: false, replication: 2) |
| 55 | + public static let DISK_ONLY_3 = StorageLevel( |
| 56 | + useDisk: true, useMemory: false, useOffHeap: false, deserialized: false, replication: 3) |
| 57 | + public static let MEMORY_ONLY = StorageLevel( |
| 58 | + useDisk: false, useMemory: true, useOffHeap: false, deserialized: false) |
| 59 | + public static let MEMORY_ONLY_2 = StorageLevel( |
| 60 | + useDisk: false, useMemory: true, useOffHeap: false, deserialized: false, replication: 2) |
| 61 | + public static let MEMORY_AND_DISK = StorageLevel( |
| 62 | + useDisk: true, useMemory: true, useOffHeap: false, deserialized: false) |
| 63 | + public static let MEMORY_AND_DISK_2 = StorageLevel( |
| 64 | + useDisk: true, useMemory: true, useOffHeap: false, deserialized: false, replication: 2) |
| 65 | + public static let OFF_HEAP = StorageLevel( |
| 66 | + useDisk: true, useMemory: true, useOffHeap: true, deserialized: false) |
| 67 | + public static let MEMORY_AND_DISK_DESER = StorageLevel( |
| 68 | + useDisk: true, useMemory: true, useOffHeap: false, deserialized: true) |
| 69 | +} |
| 70 | + |
| 71 | +extension StorageLevel { |
| 72 | + var toSparkConnectStorageLevel: Spark_Connect_StorageLevel { |
| 73 | + var level = Spark_Connect_StorageLevel() |
| 74 | + level.useDisk = self.useDisk |
| 75 | + level.useMemory = self.useMemory |
| 76 | + level.useOffHeap = self.useOffHeap |
| 77 | + level.deserialized = self.deserialized |
| 78 | + level.replication = self.replication |
| 79 | + return level |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +extension StorageLevel: CustomStringConvertible { |
| 84 | + public var description: String { |
| 85 | + return |
| 86 | + "StorageLevel(useDisk: \(useDisk), useMemory: \(useMemory), useOffHeap: \(useOffHeap), deserialized: \(deserialized), replication: \(replication))" |
| 87 | + } |
| 88 | +} |
0 commit comments