Firestore文件系统优化

huangapple go评论64阅读模式
英文:

Firestore file system optimization

问题

I'm writing a basic user file system that uses Firestore for file metadata and Cloud Storage for Firebase for storing actual files. Firebase Storage isn't exclusively used because we want to add several attributes to folders that could become cumbersome considering GCS doesn't have "directories".

In this system, there are two types of items: directories and folders. To limit Firestore writes, children currently do not have a reference to their parent directories. Parent directories have an array containing each of their child IDs, and paths are built by traversing down each parent's children array. This is done to make moving files easier (basically, we switch the child ID from one directory's array to another directory's array). Otherwise, we would have to write a new parent into every child whenever a folder moves.

This is the data structure:

interface Directory {
  id: string;
  entryType: "directory";
  name: string;
  createdTimestamp?: Timestamp;
  modifiedTimestamp?: Timestamp;
  owners?: string[];
  viewers?: string[];
  editors?: string[];
  children: string[]; // contains references to File's ID
  color: string;
  icon: string;
  root: bool;
}

interface File {
  id: string;
  entryType: "file";
  name: string;
  createdTimestamp?: Timestamp;
  modifiedTimestamp?: Timestamp;
  owners?: string[];
  viewers?: string[];
  editors?: string[];
  storagePath: string;
  storageBucket: string;
  contentType: string;
  metadata: Record<string, string>;
}

However, based on my observations, large companies providing drive services tend to provide a parent reference with each of their children. These large companies are less constrained by the cost of multiple writes and I have yet to find a fatal flaw in this system (besides some inconvenience when constructing file paths), so I'm curious whether someone smarter than I am can explain why a parent reference should or should not be added? Thanks!

英文:

I'm writing a basic user file system that uses Firestore for file metadata and Cloud Storage for Firebase for storing actual files. Firebase Storage isn't exclusively used because we want to add several attributes to folders that could become cumbersome considering GCS doesn't have "directories".

In this system, there are two types of items: directories and folders. To limit Firestore writes, children currently do not have a reference to their parent directories. Parent directories have an array containing each of their child IDs, and paths are built by traversing down each parent's children array. This is done to make moving files easier (basically, we switch the child ID from one directory's array to another directory's array). Otherwise, we would have to write a new parent into every child whenever a folder moves.

This is the data structure:

interface Directory {
  id: string;
  entryType: &quot;directory&quot;;
  name: string;
  createdTimestamp?: Timestamp;
  modifiedTimestamp?: Timestamp;
  owners?: string[];
  viewers?: string[];
  editors?: string[];
  children: string[]; // contains references to File&#39;s ID
  color: string;
  icon: string;
  root: bool;

}

interface File{
 id: string;
  entryType: &quot;file&quot;;
  name: string;
  createdTimestamp?: Timestamp;
  modifiedTimestamp?: Timestamp;
  owners?: string[];
  viewers?: string[];
  editors?: string[];
  storagePath: string; 
  storageBucket: string;
  contentType: string;
  metadata: Record&lt;string, string&gt;
}

However, based on my observations, large companies providing drive services tend to provide a parent reference with each of their children. These large companies are less constrained by the cost of multiple writes and I have yet to find a fatal flaw in this system (besides some inconvenience when constructing file paths), so I'm curious whether someone smarter than I am can explain why a parent reference should or should not be added? Thanks!

答案1

得分: 1

为了限制Firestore的写入操作,子文档目前没有对其父目录的引用。

如果问题与您在Firestore中执行的写入操作数量有关,那么为什么不使用Firebase实时数据库呢?写入操作是免费的。

父目录包含一个包含其每个子文档ID的数组,并且路径是通过遍历每个父文档的子文档数组来构建的。

由于实时数据库是一个完整的JSON对象,您可以简单地创建对存储中文件的引用,使用键值对。

英文:

> To limit Firestore writes, children currently do not have a reference to their parent directories.

If the problem is related to the number of writes you perform in Firestore, then why are you not using Firebase Realtime Database? The write operations are free of charge.

> Parent directories have an array containing each of their child IDs, and paths are built by traversing down each parent's children array.

Since the Realtime Database is an entire JSON object you can simply create references to the files in Storage, using key-value pairs.

huangapple
  • 本文由 发表于 2023年6月8日 14:20:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429096.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定