Skip to content

Commit 2b9b287

Browse files
Add cache for ArangoSearchView Link and StoredValue types and primarySortCache for ArangoSearchView type (#449)
* Add `cache` for ArangoSearchView Link and StoredValue types and `primarySortCache` for ArangoSearchView type * Add PrimaryKeyCache field support too, rework and refactor tests * Adjust tests - cache properties are immutable. Allow running for 3.10.2 and newer versions
1 parent b89f42d commit 2b9b287

File tree

5 files changed

+126
-18
lines changed

5 files changed

+126
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [master](https://github.com/arangodb/go-driver/tree/master) (N/A)
44
- Add support for `checksum` in Collections
55
- Fix reusing same connection with different Authentication parameters passed via driver.NewClient
6+
- Add `cache` for ArangoSearchView Link and StoredValue types and `primarySortCache`, `primaryKeyCache` for ArangoSearchView type
67

78
## [1.4.0](https://github.com/arangodb/go-driver/tree/v1.4.0) (2022-10-04)
89
- Add `hex` property to analyzer's properties

test/arangosearch_analyzers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ func TestArangoSearchAnalyzerEnsureAnalyzer(t *testing.T) {
364364
if testCase.MaxVersion == nil {
365365
skipBelowVersion(c, *testCase.MinVersion, t)
366366
} else {
367-
skipBetweenVersion(c, *testCase.MinVersion, *testCase.MaxVersion, t)
367+
skipVersionNotInRange(c, *testCase.MinVersion, *testCase.MaxVersion, t)
368368
}
369369
}
370370
if testCase.EnterpriseOnly {

test/client_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ func skipNoEnterprise(t *testing.T) {
9393
}
9494
}
9595

96-
// skipBetweenVersion skips the test if the current server version is less than
96+
// skipVersionNotInRange skips the test if the current server version is less than
9797
// the min version or higher/equal max version
98-
func skipBetweenVersion(c driver.Client, minVersion, maxVersion driver.Version, t *testing.T) driver.VersionInfo {
98+
func skipVersionNotInRange(c driver.Client, minVersion, maxVersion driver.Version, t *testing.T) driver.VersionInfo {
9999
x, err := c.Version(nil)
100100
if err != nil {
101101
t.Fatalf("Failed to get version info: %s", describe(err))
@@ -109,6 +109,18 @@ func skipBetweenVersion(c driver.Client, minVersion, maxVersion driver.Version,
109109
return x
110110
}
111111

112+
// skipBetweenVersions skips test if DB version is in interval (close-ended)
113+
func skipBetweenVersions(c driver.Client, minVersion, maxVersion driver.Version, t *testing.T) driver.VersionInfo {
114+
x, err := c.Version(nil)
115+
if err != nil {
116+
t.Fatalf("Failed to get version info: %s", describe(err))
117+
}
118+
if x.Version.CompareTo(minVersion) >= 0 && x.Version.CompareTo(maxVersion) <= 0 {
119+
t.Skipf("Skipping between version '%s' and '%s': got version '%s'", minVersion, maxVersion, x.Version)
120+
}
121+
return x
122+
}
123+
112124
// skipBelowVersion skips the test if the current server version is less than
113125
// the given version.
114126
func skipBelowVersion(c driver.Client, version driver.Version, t *testing.T) driver.VersionInfo {

test/view_test.go

Lines changed: 94 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,6 @@ func tryAddArangoSearchLink(ctx context.Context, db driver.Database, view driver
8080
return checkLinkExists(ctx, view, colName, t)
8181
}
8282

83-
// assertArangoSearchView is a helper to check if an arangosearch view exists and fail if it does not.
84-
func assertArangoSearchView(ctx context.Context, db driver.Database, name string, t *testing.T) driver.ArangoSearchView {
85-
v, err := db.View(ctx, name)
86-
if driver.IsNotFound(err) {
87-
t.Fatalf("View '%s': does not exist", name)
88-
} else if err != nil {
89-
t.Fatalf("Failed to open view '%s': %s", name, describe(err))
90-
}
91-
result, err := v.ArangoSearchView()
92-
if err != nil {
93-
t.Fatalf("Failed to open view '%s' as arangosearch view: %s", name, describe(err))
94-
}
95-
return result
96-
}
97-
9883
// TestCreateArangoSearchView creates an arangosearch view and then checks that it exists.
9984
func TestCreateArangoSearchView(t *testing.T) {
10085
ctx := context.Background()
@@ -906,3 +891,97 @@ func TestArangoSearchViewProperties353(t *testing.T) {
906891
require.EqualValues(t, analyzer.Properties.Case, driver.ArangoSearchCaseLower)
907892
require.Equal(t, newBool(true), link.IncludeAllFields)
908893
}
894+
895+
func TestArangoSearchViewLinkAndStoredValueCache(t *testing.T) {
896+
ctx := context.Background()
897+
c := createClientFromEnv(t, true)
898+
// feature was introduced in 3.9.5 and in 3.10.2:
899+
skipBelowVersion(c, "3.9.5", t)
900+
skipBetweenVersions(c, "3.10.0", "3.10.1", t)
901+
skipNoEnterprise(t)
902+
db := ensureDatabase(ctx, c, "view_test_links_stored_value_cache", nil, t)
903+
linkedColName := "linkedColumn"
904+
ensureCollection(ctx, db, linkedColName, nil, t)
905+
name := "test_create_asview"
906+
opts := &driver.ArangoSearchViewProperties{
907+
StoredValues: []driver.StoredValue{
908+
{
909+
Fields: []string{"f1", "f2"},
910+
Cache: newBool(true),
911+
},
912+
},
913+
Links: driver.ArangoSearchLinks{
914+
linkedColName: driver.ArangoSearchElementProperties{
915+
Cache: newBool(false),
916+
},
917+
},
918+
}
919+
v, err := db.CreateArangoSearchView(ctx, name, opts)
920+
require.NoError(t, err)
921+
922+
// check props
923+
p, err := v.Properties(ctx)
924+
require.NoError(t, err)
925+
require.Len(t, p.StoredValues, 1)
926+
require.Equal(t, newBool(true), p.StoredValues[0].Cache)
927+
linkedColumnProps := p.Links[linkedColName]
928+
require.NotNil(t, linkedColumnProps)
929+
require.Nil(t, linkedColumnProps.Cache)
930+
// update props: set to cached
931+
p.Links[linkedColName] = driver.ArangoSearchElementProperties{Cache: newBool(true)}
932+
err = v.SetProperties(ctx, p)
933+
require.NoError(t, err)
934+
935+
// check updates applied
936+
p, err = v.Properties(ctx)
937+
require.NoError(t, err)
938+
linkedColumnProps = p.Links[linkedColName]
939+
require.NotNil(t, linkedColumnProps)
940+
require.Equal(t, newBool(true), linkedColumnProps.Cache)
941+
}
942+
943+
func TestArangoSearchViewInMemoryCache(t *testing.T) {
944+
ctx := context.Background()
945+
c := createClientFromEnv(t, true)
946+
947+
skipNoEnterprise(t)
948+
db := ensureDatabase(ctx, c, "view_test_in_memory_cache", nil, t)
949+
950+
t.Run("primarySortCache", func(t *testing.T) {
951+
// feature was introduced in 3.9.5 and in 3.10.2:
952+
skipBelowVersion(c, "3.9.5", t)
953+
skipBetweenVersions(c, "3.10.0", "3.10.1", t)
954+
955+
name := "test_create_asview"
956+
opts := &driver.ArangoSearchViewProperties{
957+
PrimarySortCache: newBool(true),
958+
}
959+
v, err := db.CreateArangoSearchView(ctx, name, opts)
960+
require.NoError(t, err)
961+
962+
p, err := v.Properties(ctx)
963+
require.NoError(t, err)
964+
// bug in arangod: the primarySortCache field is not returned in response. Fixed only in 3.9.6+:
965+
t.Run("must-be-returned-in-response", func(t *testing.T) {
966+
skipBelowVersion(c, "3.9.6", t)
967+
require.Equal(t, newBool(true), p.PrimarySortCache)
968+
})
969+
})
970+
971+
t.Run("primaryKeyCache", func(t *testing.T) {
972+
// feature was introduced in 3.9.6 and 3.10.2:
973+
skipBelowVersion(c, "3.9.6", t)
974+
skipBetweenVersions(c, "3.10.0", "3.10.1", t)
975+
976+
name := "test_view_"
977+
opts := &driver.ArangoSearchViewProperties{
978+
PrimaryKeyCache: newBool(true),
979+
}
980+
v, err := db.CreateArangoSearchView(ctx, name, opts)
981+
require.NoError(t, err)
982+
983+
p, err := v.Properties(ctx)
984+
require.NoError(t, err)
985+
require.Equal(t, newBool(true), p.PrimaryKeyCache)
986+
})
987+
}

view_arangosearch.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,16 @@ type ArangoSearchViewProperties struct {
345345
// ArangoDB v3.5 and v3.6 always compress the index using LZ4. This option is immutable.
346346
PrimarySortCompression PrimarySortCompression `json:"primarySortCompression,omitempty"`
347347

348+
// PrimarySortCache If you enable this option, then the primary sort columns are always cached in memory.
349+
// Can't be changed after creating View.
350+
// Introduced in v3.9.5, Enterprise Edition only
351+
PrimarySortCache *bool `json:"primarySortCache,omitempty"`
352+
353+
// PrimaryKeyCache If you enable this option, then the primary key columns are always cached in memory.
354+
// Introduced in v3.9.6, Enterprise Edition only
355+
// Can't be changed after creating View.
356+
PrimaryKeyCache *bool `json:"primaryKeyCache,omitempty"`
357+
348358
// StoredValues An array of objects to describe which document attributes to store in the View index (introduced in v3.7.1).
349359
// It can then cover search queries, which means the data can be taken from the index directly and accessing the storage engine can be avoided.
350360
// This option is immutable.
@@ -366,6 +376,9 @@ const (
366376
type StoredValue struct {
367377
Fields []string `json:"fields,omitempty"`
368378
Compression PrimarySortCompression `json:"compression,omitempty"`
379+
// Cache attribute allows you to always cache stored values in memory
380+
// Introduced in v3.9.5, Enterprise Edition only
381+
Cache *bool `json:"cache,omitempty"`
369382
}
370383

371384
// ArangoSearchSortDirection describes the sorting direction
@@ -479,6 +492,9 @@ type ArangoSearchElementProperties struct {
479492
// so that it remains basically available. inBackground is an option that can be set when adding links.
480493
// It does not get persisted as it is not a View property, but only a one-off option
481494
InBackground *bool `json:"inBackground,omitempty"`
495+
// Cache If you enable this option, then field normalization values are always cached in memory.
496+
// Introduced in v3.9.5, Enterprise Edition only
497+
Cache *bool `json:"cache,omitempty"`
482498
}
483499

484500
// ArangoSearchStoreValues is the type of the StoreValues option of an ArangoSearch element.

0 commit comments

Comments
 (0)