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