本站源代码
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

69 Zeilen
1.7KB

  1. package rupture
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. )
  8. const metaFilename = "rupture_meta.json"
  9. func indexMetadataPath(dir string) string {
  10. return filepath.Join(dir, metaFilename)
  11. }
  12. // IndexMetadata contains metadata about a bleve index.
  13. type IndexMetadata struct {
  14. // The version of the data in the index. This can be useful for tracking
  15. // schema changes or data migrations.
  16. Version int `json:"version"`
  17. }
  18. // in addition to the user-exposed metadata, we keep additional, internal-only
  19. // metadata for sharded indices.
  20. const shardedMetadataFilename = "rupture_sharded_meta.json"
  21. func shardedIndexMetadataPath(dir string) string {
  22. return filepath.Join(dir, shardedMetadataFilename)
  23. }
  24. type shardedIndexMetadata struct {
  25. NumShards int `json:"num_shards"`
  26. }
  27. func readJSON(path string, meta interface{}) error {
  28. metaBytes, err := ioutil.ReadFile(path)
  29. if err != nil {
  30. return err
  31. }
  32. return json.Unmarshal(metaBytes, meta)
  33. }
  34. func writeJSON(path string, meta interface{}) error {
  35. metaBytes, err := json.Marshal(meta)
  36. if err != nil {
  37. return err
  38. }
  39. return ioutil.WriteFile(path, metaBytes, 0666)
  40. }
  41. // ReadIndexMetadata returns the metadata for the index at the specified path.
  42. // If no such index metadata exists, an empty metadata and a nil error are
  43. // returned.
  44. func ReadIndexMetadata(path string) (*IndexMetadata, error) {
  45. meta := &IndexMetadata{}
  46. metaPath := indexMetadataPath(path)
  47. if _, err := os.Stat(metaPath); os.IsNotExist(err) {
  48. return meta, nil
  49. } else if err != nil {
  50. return nil, err
  51. }
  52. return meta, readJSON(metaPath, meta)
  53. }
  54. // WriteIndexMetadata writes metadata for the index at the specified path.
  55. func WriteIndexMetadata(path string, meta *IndexMetadata) error {
  56. return writeJSON(indexMetadataPath(path), meta)
  57. }
上海开阖软件有限公司 沪ICP备12045867号-1