Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium Authors. 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 | package nosql |
| 6 | |
| 7 | import ( |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 8 | "math/rand" |
Adam Sadovsky | 232c366 | 2015-06-04 15:00:09 -0700 | [diff] [blame] | 9 | "path" |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 10 | "strconv" |
| 11 | "strings" |
| 12 | "sync" |
| 13 | "time" |
Adam Sadovsky | 232c366 | 2015-06-04 15:00:09 -0700 | [diff] [blame] | 14 | |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 15 | wire "v.io/syncbase/v23/services/syncbase/nosql" |
John Kline | ce0de2b | 2015-06-17 09:06:56 -0700 | [diff] [blame] | 16 | "v.io/syncbase/v23/syncbase/nosql/query_db" |
| 17 | "v.io/syncbase/v23/syncbase/nosql/query_exec" |
Raja Daoud | 7eea2da | 2015-07-17 19:27:56 -0700 | [diff] [blame] | 18 | "v.io/syncbase/x/ref/services/syncbase/localblobstore" |
| 19 | "v.io/syncbase/x/ref/services/syncbase/localblobstore/fs_cablobstore" |
Adam Sadovsky | bc00bd6 | 2015-05-22 12:50:03 -0700 | [diff] [blame] | 20 | "v.io/syncbase/x/ref/services/syncbase/server/interfaces" |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 21 | "v.io/syncbase/x/ref/services/syncbase/server/util" |
Adam Sadovsky | 8db7443 | 2015-05-29 17:37:32 -0700 | [diff] [blame] | 22 | "v.io/syncbase/x/ref/services/syncbase/server/watchable" |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 23 | "v.io/syncbase/x/ref/services/syncbase/store" |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 24 | "v.io/v23/context" |
| 25 | "v.io/v23/rpc" |
| 26 | "v.io/v23/security/access" |
Sergey Rogulenko | 448a5a0 | 2015-07-17 17:54:07 -0700 | [diff] [blame] | 27 | "v.io/v23/services/watch" |
John Kline | ce0de2b | 2015-06-17 09:06:56 -0700 | [diff] [blame] | 28 | "v.io/v23/vdl" |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 29 | "v.io/v23/verror" |
John Kline | ce0de2b | 2015-06-17 09:06:56 -0700 | [diff] [blame] | 30 | "v.io/v23/vom" |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 31 | "v.io/x/lib/vlog" |
| 32 | ) |
| 33 | |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 34 | // database is a per-database singleton (i.e. not per-request). It does not |
| 35 | // directly handle RPCs. |
| 36 | // Note: If a database does not exist at the time of a database RPC, the |
| 37 | // dispatcher creates a short-lived database object to service that particular |
| 38 | // request. |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 39 | type database struct { |
| 40 | name string |
Adam Sadovsky | bc00bd6 | 2015-05-22 12:50:03 -0700 | [diff] [blame] | 41 | a interfaces.App |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 42 | // The fields below are initialized iff this database exists. |
Adam Sadovsky | d6b9a23 | 2015-06-16 14:04:45 -0700 | [diff] [blame] | 43 | exists bool |
Adam Sadovsky | b6a5aa3 | 2015-07-07 13:05:26 -0700 | [diff] [blame] | 44 | // TODO(sadovsky): Make st point to a store.Store wrapper that handles paging, |
| 45 | // and do not actually open the store in NewDatabase. |
| 46 | st store.Store // stores all data for a single database |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 47 | |
Raja Daoud | 7eea2da | 2015-07-17 19:27:56 -0700 | [diff] [blame] | 48 | // Local blob store associated with this database. |
| 49 | bst localblobstore.BlobStore |
| 50 | |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 51 | // Active snapshots and transactions corresponding to client batches. |
| 52 | // TODO(sadovsky): Add timeouts and GC. |
| 53 | mu sync.Mutex // protects the fields below |
| 54 | sns map[uint64]store.Snapshot |
| 55 | txs map[uint64]store.Transaction |
| 56 | } |
| 57 | |
| 58 | // databaseReq is a per-request object that handles Database RPCs. |
| 59 | // It embeds database and tracks request-specific batch state. |
| 60 | type databaseReq struct { |
| 61 | *database |
| 62 | // If non-nil, sn or tx will be non-nil. |
| 63 | batchId *uint64 |
| 64 | sn store.Snapshot |
| 65 | tx store.Transaction |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | var ( |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 69 | _ wire.DatabaseServerMethods = (*databaseReq)(nil) |
Adam Sadovsky | bc00bd6 | 2015-05-22 12:50:03 -0700 | [diff] [blame] | 70 | _ interfaces.Database = (*database)(nil) |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 71 | ) |
| 72 | |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 73 | // DatabaseOptions configures a database. |
Adam Sadovsky | 232c366 | 2015-06-04 15:00:09 -0700 | [diff] [blame] | 74 | type DatabaseOptions struct { |
| 75 | // Database-level permissions. |
| 76 | Perms access.Permissions |
| 77 | // Root dir for data storage. |
| 78 | RootDir string |
| 79 | // Storage engine to use. |
| 80 | Engine string |
| 81 | } |
| 82 | |
Adam Sadovsky | b6a5aa3 | 2015-07-07 13:05:26 -0700 | [diff] [blame] | 83 | // OpenDatabase opens a database and returns a *database for it. Designed for |
| 84 | // use from within NewDatabase and server.NewService. |
| 85 | func OpenDatabase(ctx *context.T, a interfaces.App, name string, opts DatabaseOptions, openOpts util.OpenOptions) (*database, error) { |
| 86 | st, err := util.OpenStore(opts.Engine, path.Join(opts.RootDir, opts.Engine), openOpts) |
Adam Sadovsky | 232c366 | 2015-06-04 15:00:09 -0700 | [diff] [blame] | 87 | if err != nil { |
| 88 | return nil, err |
| 89 | } |
| 90 | st, err = watchable.Wrap(st, &watchable.Options{ |
Raja Daoud | d454307 | 2015-06-30 11:15:55 -0700 | [diff] [blame] | 91 | ManagedPrefixes: []string{util.RowPrefix, util.PermsPrefix}, |
Adam Sadovsky | 8db7443 | 2015-05-29 17:37:32 -0700 | [diff] [blame] | 92 | }) |
Adam Sadovsky | 63fafbb | 2015-05-26 14:22:31 -0700 | [diff] [blame] | 93 | if err != nil { |
| 94 | return nil, err |
| 95 | } |
Raja Daoud | 7eea2da | 2015-07-17 19:27:56 -0700 | [diff] [blame] | 96 | // Open a co-located blob store, adjacent to the structured store. |
| 97 | bst, err := fs_cablobstore.Create(ctx, path.Join(opts.RootDir, "blobs")) |
| 98 | if err != nil { |
| 99 | return nil, err |
| 100 | } |
Adam Sadovsky | b6a5aa3 | 2015-07-07 13:05:26 -0700 | [diff] [blame] | 101 | return &database{ |
Adam Sadovsky | d6b9a23 | 2015-06-16 14:04:45 -0700 | [diff] [blame] | 102 | name: name, |
| 103 | a: a, |
| 104 | exists: true, |
| 105 | st: st, |
Raja Daoud | 7eea2da | 2015-07-17 19:27:56 -0700 | [diff] [blame] | 106 | bst: bst, |
Adam Sadovsky | d6b9a23 | 2015-06-16 14:04:45 -0700 | [diff] [blame] | 107 | sns: make(map[uint64]store.Snapshot), |
| 108 | txs: make(map[uint64]store.Transaction), |
Adam Sadovsky | b6a5aa3 | 2015-07-07 13:05:26 -0700 | [diff] [blame] | 109 | }, nil |
| 110 | } |
| 111 | |
| 112 | // NewDatabase creates a new database instance and returns it. |
Adam Sadovsky | b6a5aa3 | 2015-07-07 13:05:26 -0700 | [diff] [blame] | 113 | // Designed for use from within App.CreateNoSQLDatabase. |
Jatin Lodhia | f6486d4 | 2015-07-17 15:57:36 -0700 | [diff] [blame] | 114 | func NewDatabase(ctx *context.T, a interfaces.App, name string, metadata *wire.SchemaMetadata, opts DatabaseOptions) (*database, error) { |
Adam Sadovsky | b6a5aa3 | 2015-07-07 13:05:26 -0700 | [diff] [blame] | 115 | if opts.Perms == nil { |
| 116 | return nil, verror.New(verror.ErrInternal, ctx, "perms must be specified") |
| 117 | } |
| 118 | d, err := OpenDatabase(ctx, a, name, opts, util.OpenOptions{CreateIfMissing: true, ErrorIfExists: true}) |
| 119 | if err != nil { |
| 120 | return nil, err |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 121 | } |
| 122 | data := &databaseData{ |
Jatin Lodhia | f6486d4 | 2015-07-17 15:57:36 -0700 | [diff] [blame] | 123 | Name: d.name, |
| 124 | Perms: opts.Perms, |
| 125 | SchemaMetadata: metadata, |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 126 | } |
Adam Sadovsky | a31a7cd | 2015-07-08 10:44:07 -0700 | [diff] [blame] | 127 | if err := util.Put(ctx, d.st, d.stKey(), data); err != nil { |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 128 | return nil, err |
| 129 | } |
| 130 | return d, nil |
| 131 | } |
| 132 | |
| 133 | //////////////////////////////////////// |
| 134 | // RPC methods |
| 135 | |
Jatin Lodhia | f6486d4 | 2015-07-17 15:57:36 -0700 | [diff] [blame] | 136 | func (d *databaseReq) Create(ctx *context.T, call rpc.ServerCall, perms access.Permissions, metadata *wire.SchemaMetadata) error { |
Adam Sadovsky | d6b9a23 | 2015-06-16 14:04:45 -0700 | [diff] [blame] | 137 | if d.exists { |
| 138 | return verror.New(verror.ErrExist, ctx, d.name) |
| 139 | } |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 140 | if d.batchId != nil { |
| 141 | return wire.NewErrBoundToBatch(ctx) |
| 142 | } |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 143 | // This database does not yet exist; d is just an ephemeral handle that holds |
| 144 | // {name string, a *app}. d.a.CreateNoSQLDatabase will create a new database |
| 145 | // handle and store it in d.a.dbs[d.name]. |
Jatin Lodhia | f6486d4 | 2015-07-17 15:57:36 -0700 | [diff] [blame] | 146 | return d.a.CreateNoSQLDatabase(ctx, call, d.name, perms, metadata) |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 149 | func (d *databaseReq) Delete(ctx *context.T, call rpc.ServerCall) error { |
| 150 | if d.batchId != nil { |
| 151 | return wire.NewErrBoundToBatch(ctx) |
| 152 | } |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 153 | return d.a.DeleteNoSQLDatabase(ctx, call, d.name) |
| 154 | } |
| 155 | |
Ivan Pilat | 8e4a4ab | 2015-07-14 12:14:52 -0700 | [diff] [blame] | 156 | func (d *databaseReq) Exists(ctx *context.T, call rpc.ServerCall) (bool, error) { |
| 157 | if !d.exists { |
| 158 | return false, nil |
| 159 | } |
| 160 | return util.ErrorToExists(util.GetWithAuth(ctx, call, d.st, d.stKey(), &databaseData{})) |
| 161 | } |
| 162 | |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 163 | var rng *rand.Rand = rand.New(rand.NewSource(time.Now().UTC().UnixNano())) |
| 164 | |
| 165 | func (d *databaseReq) BeginBatch(ctx *context.T, call rpc.ServerCall, bo wire.BatchOptions) (string, error) { |
Adam Sadovsky | d6b9a23 | 2015-06-16 14:04:45 -0700 | [diff] [blame] | 166 | if !d.exists { |
| 167 | return "", verror.New(verror.ErrNoExist, ctx, d.name) |
| 168 | } |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 169 | if d.batchId != nil { |
| 170 | return "", wire.NewErrBoundToBatch(ctx) |
| 171 | } |
| 172 | d.mu.Lock() |
| 173 | defer d.mu.Unlock() |
| 174 | var id uint64 |
| 175 | var batchType string |
| 176 | for { |
| 177 | id = uint64(rng.Int63()) |
| 178 | if bo.ReadOnly { |
| 179 | if _, ok := d.sns[id]; !ok { |
| 180 | d.sns[id] = d.st.NewSnapshot() |
| 181 | batchType = "sn" |
| 182 | break |
| 183 | } |
| 184 | } else { |
| 185 | if _, ok := d.txs[id]; !ok { |
| 186 | d.txs[id] = d.st.NewTransaction() |
| 187 | batchType = "tx" |
| 188 | break |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | return strings.Join([]string{d.name, batchType, strconv.FormatUint(id, 10)}, util.BatchSep), nil |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 195 | func (d *databaseReq) Commit(ctx *context.T, call rpc.ServerCall) error { |
Adam Sadovsky | d6b9a23 | 2015-06-16 14:04:45 -0700 | [diff] [blame] | 196 | if !d.exists { |
| 197 | return verror.New(verror.ErrNoExist, ctx, d.name) |
| 198 | } |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 199 | if d.batchId == nil { |
| 200 | return wire.NewErrNotBoundToBatch(ctx) |
| 201 | } |
| 202 | if d.tx == nil { |
| 203 | return wire.NewErrReadOnlyBatch(ctx) |
| 204 | } |
| 205 | var err error |
| 206 | if err = d.tx.Commit(); err == nil { |
| 207 | d.mu.Lock() |
| 208 | delete(d.txs, *d.batchId) |
| 209 | d.mu.Unlock() |
| 210 | } |
Adam Sadovsky | b5f8892 | 2015-07-26 17:41:37 -0700 | [diff] [blame^] | 211 | if verror.ErrorID(err) == store.ErrConcurrentTransaction.ID { |
| 212 | return verror.New(wire.ErrConcurrentBatch, ctx, err) |
| 213 | } |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 214 | return err |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 217 | func (d *databaseReq) Abort(ctx *context.T, call rpc.ServerCall) error { |
Adam Sadovsky | d6b9a23 | 2015-06-16 14:04:45 -0700 | [diff] [blame] | 218 | if !d.exists { |
| 219 | return verror.New(verror.ErrNoExist, ctx, d.name) |
| 220 | } |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 221 | if d.batchId == nil { |
| 222 | return wire.NewErrNotBoundToBatch(ctx) |
| 223 | } |
| 224 | var err error |
| 225 | if d.tx != nil { |
| 226 | if err = d.tx.Abort(); err == nil { |
| 227 | d.mu.Lock() |
| 228 | delete(d.txs, *d.batchId) |
| 229 | d.mu.Unlock() |
| 230 | } |
| 231 | } else { |
| 232 | if err = d.sn.Close(); err == nil { |
| 233 | d.mu.Lock() |
| 234 | delete(d.sns, *d.batchId) |
| 235 | d.mu.Unlock() |
| 236 | } |
| 237 | } |
| 238 | return err |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 239 | } |
| 240 | |
John Kline | ce0de2b | 2015-06-17 09:06:56 -0700 | [diff] [blame] | 241 | func (d *databaseReq) Exec(ctx *context.T, call wire.DatabaseExecServerCall, q string) error { |
| 242 | impl := func(headers []string, rs ResultStream, err error) error { |
| 243 | if err != nil { |
| 244 | return err |
| 245 | } |
| 246 | sender := call.SendStream() |
| 247 | // Push the headers first -- the client will retrieve them and return |
| 248 | // them separately from the results. |
| 249 | var resultHeaders []*vdl.Value |
| 250 | for _, header := range headers { |
| 251 | resultHeaders = append(resultHeaders, vdl.ValueOf(header)) |
| 252 | } |
| 253 | sender.Send(resultHeaders) |
| 254 | for rs.Advance() { |
| 255 | result := rs.Result() |
| 256 | sender.Send(result) |
| 257 | } |
| 258 | return rs.Err() |
| 259 | } |
| 260 | var st store.StoreReader |
| 261 | if d.batchId != nil { |
| 262 | st = d.batchReader() |
| 263 | } else { |
| 264 | sn := d.st.NewSnapshot() |
| 265 | st = sn |
| 266 | defer sn.Close() |
| 267 | } |
| 268 | // queryDb implements query_db.Database |
| 269 | // which is needed by the query package's |
| 270 | // Exec function. |
| 271 | db := &queryDb{ |
| 272 | ctx: ctx, |
| 273 | call: call, |
| 274 | req: d, |
| 275 | st: st, |
| 276 | } |
| 277 | |
| 278 | return impl(query_exec.Exec(db, q)) |
| 279 | } |
| 280 | |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 281 | func (d *databaseReq) SetPermissions(ctx *context.T, call rpc.ServerCall, perms access.Permissions, version string) error { |
Adam Sadovsky | d6b9a23 | 2015-06-16 14:04:45 -0700 | [diff] [blame] | 282 | if !d.exists { |
| 283 | return verror.New(verror.ErrNoExist, ctx, d.name) |
| 284 | } |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 285 | if d.batchId != nil { |
| 286 | return wire.NewErrBoundToBatch(ctx) |
| 287 | } |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 288 | return d.a.SetDatabasePerms(ctx, call, d.name, perms, version) |
| 289 | } |
| 290 | |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 291 | func (d *databaseReq) GetPermissions(ctx *context.T, call rpc.ServerCall) (perms access.Permissions, version string, err error) { |
Adam Sadovsky | d6b9a23 | 2015-06-16 14:04:45 -0700 | [diff] [blame] | 292 | if !d.exists { |
| 293 | return nil, "", verror.New(verror.ErrNoExist, ctx, d.name) |
| 294 | } |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 295 | if d.batchId != nil { |
| 296 | return nil, "", wire.NewErrBoundToBatch(ctx) |
| 297 | } |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 298 | data := &databaseData{} |
Adam Sadovsky | a31a7cd | 2015-07-08 10:44:07 -0700 | [diff] [blame] | 299 | if err := util.GetWithAuth(ctx, call, d.st, d.stKey(), data); err != nil { |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 300 | return nil, "", err |
| 301 | } |
| 302 | return data.Perms, util.FormatVersion(data.Version), nil |
| 303 | } |
| 304 | |
Sergey Rogulenko | 448a5a0 | 2015-07-17 17:54:07 -0700 | [diff] [blame] | 305 | func (d *databaseReq) WatchGlob(ctx *context.T, call watch.GlobWatcherWatchGlobServerCall, req watch.GlobRequest) error { |
Sergey Rogulenko | 4885709 | 2015-07-16 20:38:53 -0700 | [diff] [blame] | 306 | // TODO(rogulenko): Implement. |
| 307 | if !d.exists { |
| 308 | return verror.New(verror.ErrNoExist, ctx, d.name) |
| 309 | } |
| 310 | if d.batchId != nil { |
| 311 | return wire.NewErrBoundToBatch(ctx) |
| 312 | } |
| 313 | return verror.NewErrNotImplemented(ctx) |
| 314 | } |
| 315 | |
Sergey Rogulenko | 448a5a0 | 2015-07-17 17:54:07 -0700 | [diff] [blame] | 316 | func (d *databaseReq) GetResumeMarker(ctx *context.T, call rpc.ServerCall) (watch.ResumeMarker, error) { |
Sergey Rogulenko | 4885709 | 2015-07-16 20:38:53 -0700 | [diff] [blame] | 317 | // TODO(rogulenko): Implement. |
| 318 | if !d.exists { |
Sergey Rogulenko | 448a5a0 | 2015-07-17 17:54:07 -0700 | [diff] [blame] | 319 | return nil, verror.New(verror.ErrNoExist, ctx, d.name) |
Sergey Rogulenko | 4885709 | 2015-07-16 20:38:53 -0700 | [diff] [blame] | 320 | } |
Sergey Rogulenko | 448a5a0 | 2015-07-17 17:54:07 -0700 | [diff] [blame] | 321 | return nil, verror.NewErrNotImplemented(ctx) |
Sergey Rogulenko | 4885709 | 2015-07-16 20:38:53 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 324 | func (d *databaseReq) GlobChildren__(ctx *context.T, call rpc.ServerCall) (<-chan string, error) { |
Adam Sadovsky | d6b9a23 | 2015-06-16 14:04:45 -0700 | [diff] [blame] | 325 | if !d.exists { |
| 326 | return nil, verror.New(verror.ErrNoExist, ctx, d.name) |
| 327 | } |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 328 | if d.batchId != nil { |
| 329 | return nil, wire.NewErrBoundToBatch(ctx) |
| 330 | } |
Adam Sadovsky | 4926119 | 2015-05-19 17:39:59 -0700 | [diff] [blame] | 331 | // Check perms. |
| 332 | sn := d.st.NewSnapshot() |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 333 | closeSnapshot := func() error { |
| 334 | return sn.Close() |
| 335 | } |
Adam Sadovsky | a31a7cd | 2015-07-08 10:44:07 -0700 | [diff] [blame] | 336 | if err := util.GetWithAuth(ctx, call, sn, d.stKey(), &databaseData{}); err != nil { |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 337 | closeSnapshot() |
Adam Sadovsky | 4926119 | 2015-05-19 17:39:59 -0700 | [diff] [blame] | 338 | return nil, err |
| 339 | } |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 340 | return util.Glob(ctx, call, "*", sn, closeSnapshot, util.TablePrefix) |
Adam Sadovsky | 4926119 | 2015-05-19 17:39:59 -0700 | [diff] [blame] | 341 | } |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 342 | |
| 343 | //////////////////////////////////////// |
John Kline | ce0de2b | 2015-06-17 09:06:56 -0700 | [diff] [blame] | 344 | // ResultStream interface |
| 345 | |
| 346 | // ResultStream is an interface for iterating through results (a.k.a, rows) returned from a |
| 347 | // query. Each resulting rows are arrays of vdl objects. |
| 348 | type ResultStream interface { |
| 349 | // Advance stages an element so the client can retrieve it with Result. |
| 350 | // Advance returns true iff there is a result to retrieve. The client must |
| 351 | // call Advance before calling Result. The client must call Cancel if it |
| 352 | // does not iterate through all elements (i.e. until Advance returns false). |
| 353 | // Advance may block if an element is not immediately available. |
| 354 | Advance() bool |
| 355 | |
| 356 | // Result returns the row (i.e., array of vdl Values) that was staged by Advance. |
| 357 | // Result may panic if Advance returned false or was not called at all. |
| 358 | // Result does not block. |
| 359 | Result() []*vdl.Value |
| 360 | |
| 361 | // Err returns a non-nil error iff the stream encountered any errors. Err does |
| 362 | // not block. |
| 363 | Err() error |
| 364 | |
| 365 | // Cancel notifies the ResultStream provider that it can stop producing results. |
| 366 | // The client must call Cancel if it does not iterate through all results |
| 367 | // (i.e. until Advance returns false). Cancel is idempotent and can be called |
| 368 | // concurrently with a goroutine that is iterating via Advance/Result. |
| 369 | // Cancel causes Advance to subsequently return false. Cancel does not block. |
| 370 | Cancel() |
| 371 | } |
| 372 | |
| 373 | //////////////////////////////////////// |
Adam Sadovsky | bc00bd6 | 2015-05-22 12:50:03 -0700 | [diff] [blame] | 374 | // interfaces.Database methods |
| 375 | |
| 376 | func (d *database) St() store.Store { |
Adam Sadovsky | d6b9a23 | 2015-06-16 14:04:45 -0700 | [diff] [blame] | 377 | if !d.exists { |
Adam Sadovsky | bc00bd6 | 2015-05-22 12:50:03 -0700 | [diff] [blame] | 378 | vlog.Fatalf("database %q does not exist", d.name) |
| 379 | } |
| 380 | return d.st |
| 381 | } |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 382 | |
Raja Daoud | 7eea2da | 2015-07-17 19:27:56 -0700 | [diff] [blame] | 383 | func (d *database) BlobSt() localblobstore.BlobStore { |
| 384 | if !d.exists { |
| 385 | vlog.Fatalf("database %q does not exist", d.name) |
| 386 | } |
| 387 | return d.bst |
| 388 | } |
| 389 | |
Himabindu Pucha | f9ec56f | 2015-06-02 11:34:05 -0700 | [diff] [blame] | 390 | func (d *database) App() interfaces.App { |
| 391 | return d.a |
| 392 | } |
| 393 | |
Himabindu Pucha | 25a1d02 | 2015-07-21 20:46:33 -0700 | [diff] [blame] | 394 | func (d *database) CheckPermsInternal(ctx *context.T, call rpc.ServerCall, st store.StoreReader) error { |
Adam Sadovsky | d6b9a23 | 2015-06-16 14:04:45 -0700 | [diff] [blame] | 395 | if !d.exists { |
| 396 | vlog.Fatalf("database %q does not exist", d.name) |
| 397 | } |
Adam Sadovsky | a31a7cd | 2015-07-08 10:44:07 -0700 | [diff] [blame] | 398 | return util.GetWithAuth(ctx, call, st, d.stKey(), &databaseData{}) |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | func (d *database) SetPermsInternal(ctx *context.T, call rpc.ServerCall, perms access.Permissions, version string) error { |
Adam Sadovsky | d6b9a23 | 2015-06-16 14:04:45 -0700 | [diff] [blame] | 402 | if !d.exists { |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 403 | vlog.Fatalf("database %q does not exist", d.name) |
| 404 | } |
| 405 | return store.RunInTransaction(d.st, func(st store.StoreReadWriter) error { |
| 406 | data := &databaseData{} |
Adam Sadovsky | a31a7cd | 2015-07-08 10:44:07 -0700 | [diff] [blame] | 407 | return util.UpdateWithAuth(ctx, call, st, d.stKey(), data, func() error { |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 408 | if err := util.CheckVersion(ctx, version, data.Version); err != nil { |
| 409 | return err |
| 410 | } |
| 411 | data.Perms = perms |
| 412 | data.Version++ |
| 413 | return nil |
| 414 | }) |
| 415 | }) |
| 416 | } |
| 417 | |
Adam Sadovsky | f3b7abc | 2015-05-04 15:33:22 -0700 | [diff] [blame] | 418 | func (d *database) Name() string { |
| 419 | return d.name |
| 420 | } |
| 421 | |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 422 | //////////////////////////////////////// |
Adam Sadovsky | a31a7cd | 2015-07-08 10:44:07 -0700 | [diff] [blame] | 423 | // query_db implementation |
John Kline | ce0de2b | 2015-06-17 09:06:56 -0700 | [diff] [blame] | 424 | |
| 425 | // Implement query_db's Database, Table and KeyValueStream interfaces. |
| 426 | type queryDb struct { |
| 427 | ctx *context.T |
| 428 | call wire.DatabaseExecServerCall |
| 429 | req *databaseReq |
| 430 | st store.StoreReader |
| 431 | } |
| 432 | |
| 433 | func (db *queryDb) GetContext() *context.T { |
| 434 | return db.ctx |
| 435 | } |
| 436 | |
| 437 | func (db *queryDb) GetTable(name string) (query_db.Table, error) { |
| 438 | tDb := &tableDb{ |
| 439 | qdb: db, |
| 440 | req: &tableReq{ |
| 441 | name: name, |
| 442 | d: db.req, |
| 443 | }, |
| 444 | } |
| 445 | // Now that we have a table, we need to check permissions. |
Adam Sadovsky | a31a7cd | 2015-07-08 10:44:07 -0700 | [diff] [blame] | 446 | if err := util.GetWithAuth(db.ctx, db.call, db.st, tDb.req.stKey(), &tableData{}); err != nil { |
John Kline | ce0de2b | 2015-06-17 09:06:56 -0700 | [diff] [blame] | 447 | return nil, err |
| 448 | } |
| 449 | return tDb, nil |
| 450 | } |
| 451 | |
| 452 | type tableDb struct { |
| 453 | qdb *queryDb |
| 454 | req *tableReq |
| 455 | } |
| 456 | |
John Kline | 96b004f | 2015-06-19 15:20:00 -0700 | [diff] [blame] | 457 | func (t *tableDb) Scan(keyRanges query_db.KeyRanges) (query_db.KeyValueStream, error) { |
John Kline | 18834bd | 2015-06-26 10:07:46 -0700 | [diff] [blame] | 458 | streams := []store.Stream{} |
| 459 | for _, keyRange := range keyRanges { |
John Kline | 18834bd | 2015-06-26 10:07:46 -0700 | [diff] [blame] | 460 | // TODO(jkline): For now, acquire all of the streams at once to minimize the race condition. |
| 461 | // Need a way to Scan multiple ranges at the same state of uncommitted changes. |
John Kline | 8dbd265 | 2015-07-07 09:27:36 -0700 | [diff] [blame] | 462 | streams = append(streams, t.qdb.st.Scan(util.ScanRangeArgs(util.JoinKeyParts(util.RowPrefix, t.req.name), keyRange.Start, keyRange.Limit))) |
John Kline | 18834bd | 2015-06-26 10:07:46 -0700 | [diff] [blame] | 463 | } |
John Kline | ce0de2b | 2015-06-17 09:06:56 -0700 | [diff] [blame] | 464 | return &kvs{ |
John Kline | 8dbd265 | 2015-07-07 09:27:36 -0700 | [diff] [blame] | 465 | t: t, |
| 466 | curr: 0, |
| 467 | validRow: false, |
| 468 | it: streams, |
| 469 | err: nil, |
John Kline | ce0de2b | 2015-06-17 09:06:56 -0700 | [diff] [blame] | 470 | }, nil |
| 471 | } |
| 472 | |
| 473 | type kvs struct { |
| 474 | t *tableDb |
John Kline | 18834bd | 2015-06-26 10:07:46 -0700 | [diff] [blame] | 475 | curr int |
John Kline | ce0de2b | 2015-06-17 09:06:56 -0700 | [diff] [blame] | 476 | validRow bool |
| 477 | currKey string |
| 478 | currValue *vdl.Value |
John Kline | 18834bd | 2015-06-26 10:07:46 -0700 | [diff] [blame] | 479 | it []store.Stream // array of store.Streams |
John Kline | ce0de2b | 2015-06-17 09:06:56 -0700 | [diff] [blame] | 480 | err error |
| 481 | } |
| 482 | |
| 483 | func (s *kvs) Advance() bool { |
| 484 | if s.err != nil { |
| 485 | return false |
| 486 | } |
John Kline | 8dbd265 | 2015-07-07 09:27:36 -0700 | [diff] [blame] | 487 | for s.curr < len(s.it) { |
John Kline | 18834bd | 2015-06-26 10:07:46 -0700 | [diff] [blame] | 488 | if s.it[s.curr].Advance() { |
John Kline | ce0de2b | 2015-06-17 09:06:56 -0700 | [diff] [blame] | 489 | // key |
John Kline | 18834bd | 2015-06-26 10:07:46 -0700 | [diff] [blame] | 490 | keyBytes := s.it[s.curr].Key(nil) |
John Kline | ce0de2b | 2015-06-17 09:06:56 -0700 | [diff] [blame] | 491 | parts := util.SplitKeyParts(string(keyBytes)) |
Sergey Rogulenko | 1b92b4f | 2015-06-18 23:06:11 -0700 | [diff] [blame] | 492 | // TODO(rogulenko): Check access for the key. |
John Kline | ce0de2b | 2015-06-17 09:06:56 -0700 | [diff] [blame] | 493 | s.currKey = parts[len(parts)-1] |
| 494 | // value |
John Kline | 18834bd | 2015-06-26 10:07:46 -0700 | [diff] [blame] | 495 | valueBytes := s.it[s.curr].Value(nil) |
John Kline | ce0de2b | 2015-06-17 09:06:56 -0700 | [diff] [blame] | 496 | var currValue *vdl.Value |
| 497 | if err := vom.Decode(valueBytes, &currValue); err != nil { |
| 498 | s.validRow = false |
| 499 | s.err = err |
| 500 | return false |
| 501 | } |
| 502 | s.currValue = currValue |
| 503 | s.validRow = true |
| 504 | return true |
| 505 | } |
| 506 | // Advance returned false. It could be an err, or it could |
| 507 | // be we've reached the end. |
John Kline | 18834bd | 2015-06-26 10:07:46 -0700 | [diff] [blame] | 508 | if err := s.it[s.curr].Err(); err != nil { |
John Kline | ce0de2b | 2015-06-17 09:06:56 -0700 | [diff] [blame] | 509 | s.validRow = false |
| 510 | s.err = err |
| 511 | return false |
| 512 | } |
John Kline | 18834bd | 2015-06-26 10:07:46 -0700 | [diff] [blame] | 513 | // We've reached the end of the iterator for this keyRange. |
John Kline | ce0de2b | 2015-06-17 09:06:56 -0700 | [diff] [blame] | 514 | // Jump to the next one. |
John Kline | 8fe4a40 | 2015-07-23 16:40:19 -0700 | [diff] [blame] | 515 | s.it[s.curr] = nil |
John Kline | ce0de2b | 2015-06-17 09:06:56 -0700 | [diff] [blame] | 516 | s.curr++ |
John Kline | ce0de2b | 2015-06-17 09:06:56 -0700 | [diff] [blame] | 517 | s.validRow = false |
| 518 | } |
| 519 | // There are no more prefixes to scan. |
| 520 | return false |
| 521 | } |
| 522 | |
| 523 | func (s *kvs) KeyValue() (string, *vdl.Value) { |
| 524 | if !s.validRow { |
| 525 | return "", nil |
| 526 | } |
| 527 | return s.currKey, s.currValue |
| 528 | } |
| 529 | |
| 530 | func (s *kvs) Err() error { |
| 531 | return s.err |
| 532 | } |
| 533 | |
| 534 | func (s *kvs) Cancel() { |
| 535 | if s.it != nil { |
John Kline | 18834bd | 2015-06-26 10:07:46 -0700 | [diff] [blame] | 536 | for i := s.curr; i < len(s.it); i++ { |
| 537 | s.it[i].Cancel() |
| 538 | } |
John Kline | ce0de2b | 2015-06-17 09:06:56 -0700 | [diff] [blame] | 539 | s.it = nil |
| 540 | } |
John Kline | 96b004f | 2015-06-19 15:20:00 -0700 | [diff] [blame] | 541 | // set curr to end of keyRanges so Advance will return false |
John Kline | 8dbd265 | 2015-07-07 09:27:36 -0700 | [diff] [blame] | 542 | s.curr = len(s.it) |
John Kline | ce0de2b | 2015-06-17 09:06:56 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | //////////////////////////////////////// |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 546 | // Internal helpers |
| 547 | |
Adam Sadovsky | a31a7cd | 2015-07-08 10:44:07 -0700 | [diff] [blame] | 548 | func (d *database) stKey() string { |
| 549 | return util.DatabasePrefix |
| 550 | } |
| 551 | |
Adam Sadovsky | 1c91f2a | 2015-06-04 22:23:51 -0700 | [diff] [blame] | 552 | func (d *databaseReq) batchReader() store.StoreReader { |
| 553 | if d.batchId == nil { |
| 554 | return nil |
| 555 | } else if d.sn != nil { |
| 556 | return d.sn |
| 557 | } else { |
| 558 | return d.tx |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | func (d *databaseReq) batchReadWriter() (store.StoreReadWriter, error) { |
| 563 | if d.batchId == nil { |
| 564 | return nil, nil |
| 565 | } else if d.tx != nil { |
| 566 | return d.tx, nil |
| 567 | } else { |
| 568 | return nil, wire.NewErrReadOnlyBatch(nil) |
| 569 | } |
| 570 | } |