memorygraphstorage.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package graphstorage
  11. import "devt.de/krotik/eliasdb/storage"
  12. /*
  13. MgsRetClose is the return value on successful close
  14. */
  15. var MgsRetClose error
  16. /*
  17. MgsRetFlushAll is the return value on successful flush all
  18. */
  19. var MgsRetFlushAll error
  20. /*
  21. MgsRetFlushMain is the return value on successful flush
  22. */
  23. var MgsRetFlushMain error
  24. /*
  25. MgsRetRollbackMain is the return value on successful rollback
  26. */
  27. var MgsRetRollbackMain error
  28. /*
  29. MemoryGraphStorage data structure
  30. */
  31. type MemoryGraphStorage struct {
  32. name string // Name of the graph storage
  33. mainDB map[string]string // Database storing names
  34. storagemanagers map[string]storage.Manager // Map of StorageManagers
  35. }
  36. /*
  37. NewMemoryGraphStorage creates a new MemoryGraphStorage instance.
  38. */
  39. func NewMemoryGraphStorage(name string) Storage {
  40. return &MemoryGraphStorage{name, make(map[string]string),
  41. make(map[string]storage.Manager)}
  42. }
  43. /*
  44. Name returns the name of the MemoryGraphStorage instance.
  45. */
  46. func (mgs *MemoryGraphStorage) Name() string {
  47. return mgs.name
  48. }
  49. /*
  50. MainDB returns the main database.
  51. */
  52. func (mgs *MemoryGraphStorage) MainDB() map[string]string {
  53. return mgs.mainDB
  54. }
  55. /*
  56. RollbackMain rollback th/e main database.
  57. */
  58. func (mgs *MemoryGraphStorage) RollbackMain() error {
  59. return MgsRetRollbackMain
  60. }
  61. /*
  62. FlushMain writes the main database to the storage.
  63. */
  64. func (mgs *MemoryGraphStorage) FlushMain() error {
  65. return MgsRetFlushMain
  66. }
  67. /*
  68. StorageManager gets a storage manager with a certain name. A non-existing
  69. StorageManager is created automatically if the create flag is set to true.
  70. */
  71. func (mgs *MemoryGraphStorage) StorageManager(smname string, create bool) storage.Manager {
  72. sm, ok := mgs.storagemanagers[smname]
  73. if !ok && create {
  74. sm = storage.NewMemoryStorageManager(mgs.name + "/" + smname)
  75. mgs.storagemanagers[smname] = sm
  76. }
  77. return sm
  78. }
  79. /*
  80. FlushAll writes all pending changes to the storage.
  81. */
  82. func (mgs *MemoryGraphStorage) FlushAll() error {
  83. return MgsRetFlushAll
  84. }
  85. /*
  86. Close closes the storage.
  87. */
  88. func (mgs *MemoryGraphStorage) Close() error {
  89. return MgsRetClose
  90. }