| 1 |
// Copyright (c) 2011 CZ.NIC z.s.p.o. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// blame: jnml, labs.nic.cz |
| 6 |
|
| 7 |
/* |
| 8 |
WIP: Package hdb provides a "handle"/value DB like store, but actually it's |
| 9 |
closer to the model of a process's virtual memory and its alloc, free and move |
| 10 |
methods. |
| 11 |
|
| 12 |
The hdb package is a thin layer around falloc.File providing stable-only |
| 13 |
handles and the basic synchronizing primitives. The central functionality of |
| 14 |
hdb are the New, Set, Get and Delete methods of Store. |
| 15 |
|
| 16 |
Conceptual analogy: |
| 17 |
New alloc(sizeof(content)), return new "memory" pointer (a handle). |
| 18 |
|
| 19 |
Get memmove() from "memory" "pointed to" by handle to the result content. |
| 20 |
Note: Handle "knows" the size of its content. |
| 21 |
|
| 22 |
Set memmove() from content to "memory" pointed to by handle. |
| 23 |
In contrast to real memory, the new content may have different |
| 24 |
size than the previously stored one w/o additional handling |
| 25 |
and the "pointer" handle remains the same. |
| 26 |
|
| 27 |
Delete free() the "memory" "pointed to" by handle. |
| 28 |
*/ |
| 29 |
package hdb // import "modernc.org/fileutil/hdb" |
| 30 |
|
| 31 |
import ( |
| 32 |
"modernc.org/fileutil/falloc" |
| 33 |
"modernc.org/fileutil/storage" |
| 34 |
) |
| 35 |
|
| 36 |
type Store struct { |
| 37 |
f *falloc.File |
| 38 |
} |
| 39 |
|
| 40 |
// New returns a newly created Store backed by accessor, discarding its conents if any. |
| 41 |
// If successful, methods on the returned Store can be used for I/O. |
| 42 |
// It returns the Store and an error, if any. |
| 43 |
func New(accessor storage.Accessor) (store *Store, err error) { |
| 44 |
s := &Store{} |
| 45 |
if s.f, err = falloc.New(accessor); err == nil { |
| 46 |
store = s |
| 47 |
} |
| 48 |
return |
| 49 |
} |
| 50 |
|
| 51 |
// Open opens the Store from accessor. |
| 52 |
// If successful, methods on the returned Store can be used for data exchange. |
| 53 |
// It returns the Store and an error, if any. |
| 54 |
func Open(accessor storage.Accessor) (store *Store, err error) { |
| 55 |
s := &Store{} |
| 56 |
if s.f, err = falloc.Open(accessor); err == nil { |
| 57 |
store = s |
| 58 |
} |
| 59 |
return |
| 60 |
} |
| 61 |
|
| 62 |
// Close closes the store. Further access to the store has undefined behavior and may panic. |
| 63 |
// It returns an error, if any. |
| 64 |
func (s *Store) Close() (err error) { |
| 65 |
defer func() { |
| 66 |
s.f = nil |
| 67 |
}() |
| 68 |
|
| 69 |
return s.f.Close() |
| 70 |
} |
| 71 |
|
| 72 |
// Delete deletes the data associated with handle. |
| 73 |
// It returns an error if any. |
| 74 |
func (s *Store) Delete(handle falloc.Handle) (err error) { |
| 75 |
return s.f.Free(handle) |
| 76 |
} |
| 77 |
|
| 78 |
// Get gets the data associated with handle. |
| 79 |
// It returns the data and an error, if any. |
| 80 |
func (s *Store) Get(handle falloc.Handle) (b []byte, err error) { |
| 81 |
return s.f.Read(handle) |
| 82 |
} |
| 83 |
|
| 84 |
// New associates data with a new handle. |
| 85 |
// It returns the handle and an error, if any. |
| 86 |
func (s *Store) New(b []byte) (handle falloc.Handle, err error) { |
| 87 |
return s.f.Alloc(b) |
| 88 |
} |
| 89 |
|
| 90 |
// Set associates data with an existing handle. |
| 91 |
// It returns an error, if any. |
| 92 |
func (s *Store) Set(handle falloc.Handle, b []byte) (err error) { |
| 93 |
_, err = s.f.Realloc(handle, b, true) |
| 94 |
return |
| 95 |
} |
| 96 |
|
| 97 |
// Root returns the handle of the DB root (top level directory, ...). |
| 98 |
func (s *Store) Root() falloc.Handle { |
| 99 |
return s.f.Root() |
| 100 |
} |
| 101 |
|
| 102 |
// File returns the underlying falloc.File of 's'. |
| 103 |
func (s *Store) File() *falloc.File { |
| 104 |
return s.f |
| 105 |
} |
| 106 |
|
| 107 |
// Lock locks 's' for writing. If the lock is already locked for reading or writing, |
| 108 |
// Lock blocks until the lock is available. To ensure that the lock eventually becomes available, |
| 109 |
// a blocked Lock call excludes new readers from acquiring the lock. |
| 110 |
func (s *Store) Lock() { |
| 111 |
s.f.Lock() |
| 112 |
} |
| 113 |
|
| 114 |
// RLock locks 's' for reading. If the lock is already locked for writing or there is a writer |
| 115 |
// already waiting to release the lock, RLock blocks until the writer has released the lock. |
| 116 |
func (s *Store) RLock() { |
| 117 |
s.f.RLock() |
| 118 |
} |
| 119 |
|
| 120 |
// Unlock unlocks 's' for writing. It's a run-time error if 's' is not locked for writing on entry to Unlock. |
| 121 |
// |
| 122 |
// As with Mutexes, a locked RWMutex is not associated with a particular goroutine. |
| 123 |
// One goroutine may RLock (Lock) 's' and then arrange for another goroutine to RUnlock (Unlock) it. |
| 124 |
func (s *Store) Unlock() { |
| 125 |
s.f.Unlock() |
| 126 |
} |
| 127 |
|
| 128 |
// RUnlock undoes a single RLock call; it does not affect other simultaneous readers. |
| 129 |
// It's a run-time error if 's' is not locked for reading on entry to RUnlock. |
| 130 |
func (s *Store) RUnlock() { |
| 131 |
s.f.RUnlock() |
| 132 |
} |
| 133 |
|
| 134 |
// LockedNew wraps New in a Lock/Unlock pair. |
| 135 |
func (s *Store) LockedNew(b []byte) (handle falloc.Handle, err error) { |
| 136 |
return s.f.LockedAlloc(b) |
| 137 |
} |
| 138 |
|
| 139 |
// LockedDelete wraps Delete in a Lock/Unlock pair. |
| 140 |
func (s *Store) LockedDelete(handle falloc.Handle) (err error) { |
| 141 |
return s.f.LockedFree(handle) |
| 142 |
} |
| 143 |
|
| 144 |
// LockedGet wraps Get in a RLock/RUnlock pair. |
| 145 |
func (s *Store) LockedGet(handle falloc.Handle) (b []byte, err error) { |
| 146 |
return s.f.LockedRead(handle) |
| 147 |
} |
| 148 |
|
| 149 |
// LockedSet wraps Set in a Lock/Unlock pair. |
| 150 |
func (s *Store) LockedSet(handle falloc.Handle, b []byte) (err error) { |
| 151 |
_, err = s.f.Realloc(handle, b, true) |
| 152 |
return |
| 153 |
} |