PutMany stores many records in the database. Warning: This is nearly a direct database access and omits many things: - Record locking - Hooks - Subscriptions - Caching Use with care.
(dbName string)
| 352 | // - Caching |
| 353 | // Use with care. |
| 354 | func (i *Interface) PutMany(dbName string) (put func(record.Record) error) { |
| 355 | interfaceBatch := make(chan record.Record, 100) |
| 356 | |
| 357 | // permission check |
| 358 | if !i.options.HasAllPermissions() { |
| 359 | return func(r record.Record) error { |
| 360 | return ErrPermissionDenied |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | // get database |
| 365 | db, err := getController(dbName) |
| 366 | if err != nil { |
| 367 | return func(r record.Record) error { |
| 368 | return err |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | // Check if database is read only. |
| 373 | if db.ReadOnly() { |
| 374 | return func(r record.Record) error { |
| 375 | return ErrReadOnly |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | // start database access |
| 380 | dbBatch, errs := db.PutMany() |
| 381 | finished := abool.New() |
| 382 | var internalErr error |
| 383 | |
| 384 | // interface options proxy |
| 385 | go func() { |
| 386 | defer close(dbBatch) // signify that we are finished |
| 387 | for { |
| 388 | select { |
| 389 | case r := <-interfaceBatch: |
| 390 | // finished? |
| 391 | if r == nil { |
| 392 | return |
| 393 | } |
| 394 | // apply options |
| 395 | i.options.Apply(r) |
| 396 | // pass along |
| 397 | dbBatch <- r |
| 398 | case <-time.After(1 * time.Second): |
| 399 | // bail out |
| 400 | internalErr = errors.New("timeout: putmany unused for too long") |
| 401 | finished.Set() |
| 402 | return |
| 403 | } |
| 404 | } |
| 405 | }() |
| 406 | |
| 407 | return func(r record.Record) error { |
| 408 | // finished? |
| 409 | if finished.IsSet() { |
| 410 | // check for internal error |
| 411 | if internalErr != nil { |