# PropertyDatabase

> The PropertyDatabase is a system that can store, in a thread-safe manner, properties of different kinds into a single file that we call a property database.

## Definition

* **Type:** Class
* **Namespace:** [UnityEditor.Search](/engine/6000.5/script-reference/unityeditor/search.md)
* **Assembly:** UnityEditor
* **Implements:** [IDisposable](https://learn.microsoft.com/dotnet/api/system.idisposable)

```csharp
public class PropertyDatabase : IDisposable
```

## Remarks

The idea of the [PropertyDatabase](/engine/6000.5/script-reference/unityeditor/search/propertydatabase.md) is that most of the time, fetching a property value can be costly. Therefore, caching those values for reuse at a later time can save a lot of cycles. The cache is stored in a file, so it is not lost on a domain reload or when quitting Unity (not all types can be serialized in the file but they can still be stored in the [PropertyDatabase](/engine/6000.5/script-reference/unityeditor/search/propertydatabase.md), see PropertyDatabase.IsPersistedType). You can open multiple [PropertyDatabase](/engine/6000.5/script-reference/unityeditor/search/propertydatabase.md)s on different files, but you cannot open multiple [PropertyDatabase](/engine/6000.5/script-reference/unityeditor/search/propertydatabase.md)s on a single file. Doing so will result in an invalid [PropertyDatabase](/engine/6000.5/script-reference/unityeditor/search/propertydatabase.md) (see [valid](/engine/6000.5/script-reference/unityeditor/search/propertydatabase/valid.md)). You can do concurrent operations on a single instance of a [PropertyDatabase](/engine/6000.5/script-reference/unityeditor/search/propertydatabase.md), but for performance reasons you should use a view (see [PropertyDatabase.GetView](/engine/6000.5/script-reference/unityeditor/search/propertydatabase/getview.md) and [IPropertyDatabaseView](/engine/6000.5/script-reference/unityeditor/search/ipropertydatabaseview.md)).

There are three main operations that you can do with the [PropertyDatabase](/engine/6000.5/script-reference/unityeditor/search/propertydatabase.md): storing values, loading values and invalidating values. When storing values, the values are stored in an array, sorted by their keys (see [PropertyDatabase.CreateRecordKey](/engine/6000.5/script-reference/unityeditor/search/propertydatabase/createrecordkey.md) and [PropertyDatabaseRecordKey](/engine/6000.5/script-reference/unityeditor/search/propertydatabaserecordkey.md). A key is a number representing a document and a property. You can store any values coming from anywhere in the [PropertyDatabase](/engine/6000.5/script-reference/unityeditor/search/propertydatabase.md), but the most common use case is to store properties of an asset or a game object. In these cases, the asset or game object would be the document, and the property is any property on that document you want to store. You can then load those values at a later time, either for a single property or an entire document.

```csharp
using System.Collections.Generic;
using NUnit.Framework;
using UnityEditor;
using UnityEditor.Search;
using UnityEngine;

static class Example_PropertyDatabase
{
    // Where the property database will be written.
    const string propertyDatabaseFilePath = "Temp/test_property_db";

    static PropertyDatabase propertyDatabase;

    static void InitializePropertyDatabase()
    {
        // Setup the property database. We configure it with automatic flushing so the file store
        // will be updated automatically after an update.
        propertyDatabase = new PropertyDatabase(propertyDatabaseFilePath, true);
    }

    static void ClearPropertyDatabase()
    {
        propertyDatabase.Clear();

        // Since we are done with the property database, we should dispose it
        // to clear all resources.
        propertyDatabase.Dispose();
    }

    static PropertyDatabaseRecordKey GetPropertyKey(int id)
    {
        return PropertyDatabase.CreateRecordKey((ulong)id / 100, PropertyDatabase.CreatePropertyHash((id % 100).ToString()));
    }

    static object LoadPropertyValue(int id)
    {
        var recordKey = GetPropertyKey(id);
        if (propertyDatabase.TryLoad(recordKey, out object value))
            return value;

        // Fetch the value with the time consuming operation and store it for future
        // accesses.
        value = id.ToString();
        propertyDatabase.Store(recordKey, value);
        return value;
    }

    [MenuItem("Examples/PropertyDatabase/Class")]
    public static void RunExample()
    {
        InitializePropertyDatabase();
        if (!propertyDatabase.valid)
        {
            Debug.LogFormat(LogType.Error, LogOption.NoStacktrace, null, $"PropertyDatabase \"{propertyDatabase.filePath}\" failed to open properly.");
            return;
        }

        var allValues = new Dictionary<int, object>();
        // Load the property values to do something with it.
        for (var i = 0; i < 1000; ++i)
        {
            var value = LoadPropertyValue(i);
            allValues.Add(i, value);
        }

        // Validate everything is in the database
        for (var i = 0; i < 1000; ++i)
        {
            var key = GetPropertyKey(i);
            if (!propertyDatabase.TryLoad(key, out object value))
                Assert.Fail("Record should be in the database.");
            Assert.AreEqual(i.ToString(), value);
        }

        ClearPropertyDatabase();
    }
}
```

If a property or an entire document change, you can invalidate the stored values with [PropertyDatabase.Invalidate](/engine/6000.5/script-reference/unityeditor/search/propertydatabase/invalidate.md) and [PropertyDatabase.InvalidateMask](/engine/6000.5/script-reference/unityeditor/search/propertydatabase/invalidatemask.md).

## Constructors

| Constructor                                                                                                                                                                        | Description                                                                                                                |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
| [PropertyDatabase(System.String)](/engine/6000.5/script-reference/unityeditor/search/propertydatabase/ctor.md#propertydatabase\(string\))                                          | Constructs a new instance of a [PropertyDatabase](/engine/6000.5/script-reference/unityeditor/search/propertydatabase.md). |
| [PropertyDatabase(System.String,System.Boolean,System.Double)](/engine/6000.5/script-reference/unityeditor/search/propertydatabase/ctor.md#propertydatabase\(string-bool-double\)) | Constructs a new instance of a [PropertyDatabase](/engine/6000.5/script-reference/unityeditor/search/propertydatabase.md). |

## Properties

| Property                                                                                      | Description                                                                                                                                                             |
| --------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [autoFlush](/engine/6000.5/script-reference/unityeditor/search/propertydatabase/autoflush.md) | Boolean indicating if the [PropertyDatabase](/engine/6000.5/script-reference/unityeditor/search/propertydatabase.md) will update the backing file automatically or not. |
| [filePath](/engine/6000.5/script-reference/unityeditor/search/propertydatabase/filepath.md)   | The file on which this [PropertyDatabase](/engine/6000.5/script-reference/unityeditor/search/propertydatabase.md) is opened.                                            |
| [valid](/engine/6000.5/script-reference/unityeditor/search/propertydatabase/valid.md)         | Boolean indicating if the [PropertyDatabase](/engine/6000.5/script-reference/unityeditor/search/propertydatabase.md) is valid.                                          |

## Methods

| Method                                                                                                          | Description                                                                                                                                               |
| --------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [Clear](/engine/6000.5/script-reference/unityeditor/search/propertydatabase/clear.md)                           | Clears the entire content of the [PropertyDatabase](/engine/6000.5/script-reference/unityeditor/search/propertydatabase.md).                              |
| [Dispose](/engine/6000.5/script-reference/unityeditor/search/propertydatabase/dispose.md)                       | Dispose of the resources used by this [PropertyDatabase](/engine/6000.5/script-reference/unityeditor/search/propertydatabase.md).                         |
| [Flush](/engine/6000.5/script-reference/unityeditor/search/propertydatabase/flush.md)                           | Triggers a manual update of the backing file.                                                                                                             |
| [GetInfo](/engine/6000.5/script-reference/unityeditor/search/propertydatabase/getinfo.md)                       | Returns a formatted string showing various information of the [PropertyDatabase](/engine/6000.5/script-reference/unityeditor/search/propertydatabase.md). |
| [GetValueFromRecord](/engine/6000.5/script-reference/unityeditor/search/propertydatabase/getvaluefromrecord.md) | Deserialize a record value into its proper type.                                                                                                          |
| [GetView](/engine/6000.5/script-reference/unityeditor/search/propertydatabase/getview.md)                       | Opens a view on the [PropertyDatabase](/engine/6000.5/script-reference/unityeditor/search/propertydatabase.md).                                           |
| [Invalidate](/engine/6000.5/script-reference/unityeditor/search/propertydatabase/invalidate.md)                 | Invalidates all the properties stored for an entire document.                                                                                             |
| [InvalidateMask](/engine/6000.5/script-reference/unityeditor/search/propertydatabase/invalidatemask.md)         | Invalidate all properties stored from multiple documents that match a document key mask.                                                                  |
| [Store](/engine/6000.5/script-reference/unityeditor/search/propertydatabase/store.md)                           | Stores a document property.                                                                                                                               |
| [TryLoad](/engine/6000.5/script-reference/unityeditor/search/propertydatabase/tryload.md)                       | Loads all the properties of a document, already deseriazed into objects.                                                                                  |

## Static Methods

| Method                                                                                                          | Description                                                                    |
| --------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------ |
| [CreateDocumentKey](/engine/6000.5/script-reference/unityeditor/search/propertydatabase/createdocumentkey.md)   | Creates a document key from a document identifier.                             |
| [CreatePropertyHash](/engine/6000.5/script-reference/unityeditor/search/propertydatabase/createpropertyhash.md) | Creates a property hash from a property path.                                  |
| [CreateRecordKey](/engine/6000.5/script-reference/unityeditor/search/propertydatabase/createrecordkey.md)       | Creates a record key from a property path.                                     |
| [IsPersistableType](/engine/6000.5/script-reference/unityeditor/search/propertydatabase/ispersistabletype.md)   | Returns a boolean indicating if a type can be persisted into the backing file. |
