1 // Copyright (c) 2012, Suryandaru Triandana <syndtr@gmail.com>
2 // All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
7 // Package storage provides storage abstraction for LevelDB.
16 // FileType represent a file type.
21 TypeManifest FileType = 1 << iota
26 TypeAll = TypeManifest | TypeJournal | TypeTable | TypeTemp
29 func (t FileType) String() string {
40 return fmt.Sprintf("<unknown:%d>", t)
45 ErrInvalidFile = errors.New("leveldb/storage: invalid file for argument")
46 ErrLocked = errors.New("leveldb/storage: already locked")
47 ErrClosed = errors.New("leveldb/storage: closed")
50 // ErrCorrupted is the type that wraps errors that indicate corruption of
51 // a file. Package storage has its own type instead of using
52 // errors.ErrCorrupted to prevent circular import.
53 type ErrCorrupted struct {
58 func (e *ErrCorrupted) Error() string {
60 return fmt.Sprintf("%v [file=%v]", e.Err, e.Fd)
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.
71 // Reader is the interface that groups the basic Read, Seek, ReadAt and Close
73 type Reader interface {
79 // Writer is the interface that groups the basic Write, Sync and Close
81 type Writer interface {
86 // Locker is the interface that wraps Unlock method.
87 type Locker interface {
91 // FileDesc is a 'file descriptor'.
92 type FileDesc struct {
97 func (fd FileDesc) String() string {
100 return fmt.Sprintf("MANIFEST-%06d", fd.Num)
102 return fmt.Sprintf("%06d.log", fd.Num)
104 return fmt.Sprintf("%06d.ldb", fd.Num)
106 return fmt.Sprintf("%06d.tmp", fd.Num)
108 return fmt.Sprintf("%#x-%d", fd.Type, fd.Num)
112 // Zero returns true if fd == (FileDesc{}).
113 func (fd FileDesc) Zero() bool {
114 return fd == (FileDesc{})
117 // FileDescOk returns true if fd is a valid 'file descriptor'.
118 func FileDescOk(fd FileDesc) bool {
130 // Storage is the storage. A storage instance must be safe for concurrent use.
131 type Storage interface {
132 // Lock locks the storage. Any subsequent attempt to call Lock will fail
133 // until the last lock released.
134 // Caller should call Unlock method after use.
135 Lock() (Locker, error)
137 // Log logs a string. This is used for logging.
138 // An implementation may write to a file, stdout or simply do nothing.
141 // SetMeta store 'file descriptor' that can later be acquired using GetMeta
142 // method. The 'file descriptor' should point to a valid file.
143 // SetMeta should be implemented in such way that changes should happen
145 SetMeta(fd FileDesc) error
147 // GetMeta returns 'file descriptor' stored in meta. The 'file descriptor'
148 // can be updated using SetMeta method.
149 // Returns os.ErrNotExist if meta doesn't store any 'file descriptor', or
150 // 'file descriptor' point to nonexistent file.
151 GetMeta() (FileDesc, error)
153 // List returns file descriptors that match the given file types.
154 // The file types may be OR'ed together.
155 List(ft FileType) ([]FileDesc, error)
157 // Open opens file with the given 'file descriptor' read-only.
158 // Returns os.ErrNotExist error if the file does not exist.
159 // Returns ErrClosed if the underlying storage is closed.
160 Open(fd FileDesc) (Reader, error)
162 // Create creates file with the given 'file descriptor', truncate if already
163 // exist and opens write-only.
164 // Returns ErrClosed if the underlying storage is closed.
165 Create(fd FileDesc) (Writer, error)
167 // Remove removes file with the given 'file descriptor'.
168 // Returns ErrClosed if the underlying storage is closed.
169 Remove(fd FileDesc) error
171 // Rename renames file from oldfd to newfd.
172 // Returns ErrClosed if the underlying storage is closed.
173 Rename(oldfd, newfd FileDesc) error
175 // Close closes the storage.
176 // It is valid to call Close multiple times. Other methods should not be
177 // called after the storage has been closed.