github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/syndtr/goleveldb/leveldb/storage/storage.go (about) 1 // Copyright (c) 2012, Suryandaru Triandana <syndtr@gmail.com> 2 // All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style license that can be 5 // found in the LICENSE file. 6 7 // Package storage provides storage abstraction for LevelDB. 8 package storage 9 10 import ( 11 "errors" 12 "fmt" 13 "io" 14 15 "github.com/insionng/yougam/libraries/syndtr/goleveldb/leveldb/util" 16 ) 17 18 type FileType int 19 20 const ( 21 TypeManifest FileType = 1 << iota 22 TypeJournal 23 TypeTable 24 TypeTemp 25 26 TypeAll = TypeManifest | TypeJournal | TypeTable | TypeTemp 27 ) 28 29 func (t FileType) String() string { 30 switch t { 31 case TypeManifest: 32 return "manifest" 33 case TypeJournal: 34 return "journal" 35 case TypeTable: 36 return "table" 37 case TypeTemp: 38 return "temp" 39 } 40 return fmt.Sprintf("<unknown:%d>", t) 41 } 42 43 var ( 44 ErrInvalidFile = errors.New("leveldb/storage: invalid file for argument") 45 ErrLocked = errors.New("leveldb/storage: already locked") 46 ErrClosed = errors.New("leveldb/storage: closed") 47 ) 48 49 // ErrCorrupted is the type that wraps errors that indicate corruption of 50 // a file. Package storage has its own type instead of using 51 // errors.ErrCorrupted to prevent circular import. 52 type ErrCorrupted struct { 53 Fd FileDesc 54 Err error 55 } 56 57 func (e *ErrCorrupted) Error() string { 58 if !e.Fd.Nil() { 59 return fmt.Sprintf("%v [file=%v]", e.Err, e.Fd) 60 } else { 61 return e.Err.Error() 62 } 63 } 64 65 // Syncer is the interface that wraps basic Sync method. 66 type Syncer interface { 67 // Sync commits the current contents of the file to stable storage. 68 Sync() error 69 } 70 71 // Reader is the interface that groups the basic Read, Seek, ReadAt and Close 72 // methods. 73 type Reader interface { 74 io.ReadSeeker 75 io.ReaderAt 76 io.Closer 77 } 78 79 // Writer is the interface that groups the basic Write, Sync and Close 80 // methods. 81 type Writer interface { 82 io.WriteCloser 83 Syncer 84 } 85 86 type Lock interface { 87 util.Releaser 88 } 89 90 // FileDesc is a file descriptor. 91 type FileDesc struct { 92 Type FileType 93 Num int64 94 } 95 96 func (fd FileDesc) String() string { 97 switch fd.Type { 98 case TypeManifest: 99 return fmt.Sprintf("MANIFEST-%06d", fd.Num) 100 case TypeJournal: 101 return fmt.Sprintf("%06d.log", fd.Num) 102 case TypeTable: 103 return fmt.Sprintf("%06d.ldb", fd.Num) 104 case TypeTemp: 105 return fmt.Sprintf("%06d.tmp", fd.Num) 106 default: 107 return fmt.Sprintf("%#x-%d", fd.Type, fd.Num) 108 } 109 } 110 111 // Nil returns true if fd == (FileDesc{}). 112 func (fd FileDesc) Nil() bool { 113 return fd == (FileDesc{}) 114 } 115 116 // FileDescOk returns true if fd is a valid file descriptor. 117 func FileDescOk(fd FileDesc) bool { 118 switch fd.Type { 119 case TypeManifest: 120 case TypeJournal: 121 case TypeTable: 122 case TypeTemp: 123 default: 124 return false 125 } 126 return fd.Num >= 0 127 } 128 129 // Storage is the storage. A storage instance must be goroutine-safe. 130 type Storage interface { 131 // Lock locks the storage. Any subsequent attempt to call Lock will fail 132 // until the last lock released. 133 // After use the caller should call the Release method. 134 Lock() (Lock, error) 135 136 // Log logs a string. This is used for logging. 137 // An implementation may write to a file, stdout or simply do nothing. 138 Log(str string) 139 140 // SetMeta sets to point to the given fd, which then can be acquired using 141 // GetMeta method. 142 // SetMeta should be implemented in such way that changes should happened 143 // atomically. 144 SetMeta(fd FileDesc) error 145 146 // GetManifest returns a manifest file. 147 // Returns os.ErrNotExist if meta doesn't point to any fd, or point to fd 148 // that doesn't exist. 149 GetMeta() (FileDesc, error) 150 151 // List returns fds that match the given file types. 152 // The file types may be OR'ed together. 153 List(ft FileType) ([]FileDesc, error) 154 155 // Open opens file with the given fd read-only. 156 // Returns os.ErrNotExist error if the file does not exist. 157 // Returns ErrClosed if the underlying storage is closed. 158 Open(fd FileDesc) (Reader, error) 159 160 // Create creates file with the given fd, truncate if already exist and 161 // opens write-only. 162 // Returns ErrClosed if the underlying storage is closed. 163 Create(fd FileDesc) (Writer, error) 164 165 // Remove removes file with the given fd. 166 // Returns ErrClosed if the underlying storage is closed. 167 Remove(fd FileDesc) error 168 169 // Rename renames file from oldfd to newfd. 170 // Returns ErrClosed if the underlying storage is closed. 171 Rename(oldfd, newfd FileDesc) error 172 173 // Close closes the storage. 174 // It is valid to call Close multiple times. Other methods should not be 175 // called after the storage has been closed. 176 Close() error 177 }