Queries and history

The fundamental property of immudb is that it’s an append-only database. This means that an update is a new insert of the same key with a new value. It’s possible to retrieve all the values for a particular key with the history command.

History accepts the following parameters:

  • Key: a key of an item
  • Offset: the starting index (excluded from the search). Optional
  • Limit: maximum returned items. Optional
  • Desc: items are returned in reverse order. Optional
  • SinceTx:
  1. client.Set(ctx, []byte(`hello`), []byte(`immutable world`))
  2. client.Set(ctx, []byte(`hello`), []byte(`immudb`))
  3. req := &schema.HistoryRequest{
  4. Key: []byte(`hello`),
  5. }
  6. entries, err := client.History(ctx, req)
  7. if err != nil {
  8. log.Fatal(err)
  9. }
  10. fmt.Printf("Successfully retrieved %d entries for key %s\n", len(entries), req.Key)
  1. try {
  2. immuClient.set("hello", value1);
  3. immuClient.set("hello", value2);
  4. } catch (CorruptedDataException e) {
  5. // ...
  6. }
  7. List<KV> historyResponse1 = immuClient.history("hello", 10, 0, false, 1);

Note that, similar with many other methods, history method is overloaded to allow different kinds/set of parameters.

This feature is not yet supported or not documented. Do you want to make a feature request or help out? Open an issue on Python sdk github projectQueries and history - 图1 (opens new window)

  1. import ImmudbClient from 'immudb-node'
  2. import Parameters from 'immudb-node/types/parameters'
  3. const IMMUDB_HOST = '127.0.0.1'
  4. const IMMUDB_PORT = '3322'
  5. const IMMUDB_USER = 'immudb'
  6. const IMMUDB_PWD = 'immudb'
  7. const cl = new ImmudbClient({ host: IMMUDB_HOST, port: IMMUDB_PORT });
  8. (async () => {
  9. await cl.login({ user: IMMUDB_USER, password: IMMUDB_PWD })
  10. const key = 'hello'
  11. await cl.set({ key, value: 'immutable world' })
  12. await cl.set({ key, value: 'immudb' })
  13. const historyReq: Parameters.History = {
  14. key
  15. }
  16. const historyRes = cl.history(historyReq)
  17. console.log('success: history', historyRes)
  18. })()

This feature is not yet supported or not documented. Do you want to make a feature request or help out? Open an issue on .Net sdk github projectQueries and history - 图2 (opens new window)

If you’re using another development language, please read up on our immugwQueries and history - 图3 (opens new window) option.

Counting

Counting entries is not supported at the moment.

This feature is not yet supported or not documented. Do you want to make a feature request or help out? Open an issue on Go sdk github projectQueries and history - 图4 (opens new window)

This feature is not yet supported or not documented. Do you want to make a feature request or help out? Open an issue on Java sdk github projectQueries and history - 图5 (opens new window)

This feature is not yet supported or not documented. Do you want to make a feature request or help out? Open an issue on Python sdk github projectQueries and history - 图6 (opens new window)

This feature is not yet supported or not documented. Do you want to make a feature request or help out? Open an issue on Node.js sdk github projectQueries and history - 图7 (opens new window)

This feature is not yet supported or not documented. Do you want to make a feature request or help out? Open an issue on .Net sdk github projectQueries and history - 图8 (opens new window)

If you’re using another development language, please read up on our immugwQueries and history - 图9 (opens new window) option.

Scan

The scan command is used to iterate over the collection of elements present in the currently selected database. Scan accepts the following parameters:

  • Prefix: prefix. If not provided all keys will be involved. Optional
  • SeekKey: initial key for the first entry in the iteration. Optional
  • Desc: DESC or ASC sorting order. Optional
  • Limit: maximum returned items. Optional
  • SinceTx: immudb will wait that the transaction provided by SinceTx be processed. Optional
  • NoWait: Default false. When true scan doesn’t wait for the index to be fully generated and returns the last indexed value. Optional

To gain speed it’s possible to specify noWait=true. The control will be returned to the caller immediately, without waiting for the indexing to complete. When noWait is used, keep in mind that the returned data may not be yet up to date with the inserted data, as the indexing might not have completed.

An ordinary scan command and a reversed one.

  1. client.Set(ctx, []byte(`aaa`), []byte(`item1`))
  2. client.Set(ctx, []byte(`bbb`), []byte(`item2`))
  3. client.Set(ctx, []byte(`abc`),[]byte(`item3`))
  4. scanReq := &schema.ScanRequest{
  5. Prefix: []byte(`a`),
  6. }
  7. list, err := client.Scan(ctx, scanReq)
  8. if err != nil {
  9. log.Fatal(err)
  10. }
  11. fmt.Printf("%v\n", list)
  12. scanReq1 := &schema.ScanRequest{
  13. SeekKey: []byte{0xFF},
  14. Prefix: []byte(`a`),
  15. Desc: true,
  16. }
  17. list, err = client.Scan(ctx, scanReq1)
  18. if err != nil {
  19. log.Fatal(err)
  20. }
  21. fmt.Printf("%v\n", list)
  22. // scan on all key values on the current database, with a fresh snapshot
  23. scanReq2 := &schema.ScanRequest{
  24. SeekKey: []byte{0xFF},
  25. Desc: true,
  26. SinceTx: math.MaxUint64,
  27. }
  28. list, err = client.Scan(ctx, scanReq2)
  29. if err != nil {
  30. log.Fatal(err)
  31. }
  32. fmt.Printf("%v\n", list)
  1. byte[] value1 = {0, 1, 2, 3};
  2. byte[] value2 = {4, 5, 6, 7};
  3. try {
  4. immuClient.set("scan1", value1);
  5. immuClient.set("scan2", value2);
  6. } catch (CorruptedDataException e) {
  7. // ...
  8. }
  9. // Example of using scan(prefix, sinceTxId, limit, desc).
  10. List<KV> scanResult = immuClient.scan("scan", 1, 5, false);
  11. // We expect two entries in the result.

scan is an overloaded method, therefore multiple flavours of it with different parameter options exist.

This feature is not yet supported or not documented. Do you want to make a feature request or help out? Open an issue on Python sdk github projectQueries and history - 图10 (opens new window)

  1. import ImmudbClient from 'immudb-node'
  2. import Parameters from 'immudb-node/types/parameters'
  3. const IMMUDB_HOST = '127.0.0.1'
  4. const IMMUDB_PORT = '3322'
  5. const IMMUDB_USER = 'immudb'
  6. const IMMUDB_PWD = 'immudb'
  7. const cl = new ImmudbClient({ host: IMMUDB_HOST, port: IMMUDB_PORT });
  8. (async () => {
  9. await cl.login({ user: IMMUDB_USER, password: IMMUDB_PWD })
  10. await cl.set({ key: 'aaa', value: 'item1' })
  11. await cl.set({ key: 'bbb', value: 'item2' })
  12. await cl.set({ key: 'abc', value: 'item3' })
  13. const scanReq: Parameters.Scan = {
  14. prefix: 'a',
  15. desc: false,
  16. limit: 0,
  17. sincetx: 0
  18. }
  19. const scanRes = await cl.scan(scanReq)
  20. console.log('success: scan', scanRes)
  21. const scanReq1: Parameters.Scan = {
  22. prefix: 'a',
  23. desc: true,
  24. limit: 0,
  25. sincetx: 0
  26. }
  27. const scanRes1 = await cl.scan(scanReq1)
  28. console.log('success: scan', scanRes1)
  29. })()

Example with an offset:

  1. import ImmudbClient from 'immudb-node'
  2. import Parameters from 'immudb-node/types/parameters'
  3. const IMMUDB_HOST = '127.0.0.1'
  4. const IMMUDB_PORT = '3322'
  5. const IMMUDB_USER = 'immudb'
  6. const IMMUDB_PWD = 'immudb'
  7. const cl = new ImmudbClient({ host: IMMUDB_HOST, port: IMMUDB_PORT });
  8. (async () => {
  9. await cl.login({ user: IMMUDB_USER, password: IMMUDB_PWD })
  10. await cl.set({ key: 'aaa', value: 'item1' })
  11. await cl.set({ key: 'bbb', value: 'item2' })
  12. await cl.set({ key: 'abc', value: 'item3' })
  13. const scanReq: Parameters.Scan = {
  14. seekkey: '',
  15. prefix: '',
  16. desc: true,
  17. limit: 0,
  18. sincetx: 0
  19. }
  20. const scanRes = await cl.scan(scanReq)
  21. console.log('success: scan', scanRes)
  22. const scanReq1: Parameters.Scan = {
  23. seekkey: 'bbb',
  24. prefix: '',
  25. desc: true,
  26. limit: 0,
  27. sincetx: 0
  28. }
  29. const scanRes1 = await cl.scan(scanReq1)
  30. console.log('success: scan', scanRes1)
  31. const scanReq2: Parameters.Scan = {
  32. seekkey: 'b',
  33. prefix: 'b',
  34. desc: true,
  35. limit: 0,
  36. sincetx: 0
  37. }
  38. const scanRes2 = await cl.scan(scanReq2)
  39. console.log('success: scan', scanRes2)
  40. })()

This feature is not yet supported or not documented. Do you want to make a feature request or help out? Open an issue on .Net sdk github projectQueries and history - 图11 (opens new window)

If you’re using another development language, please read up on our immugwQueries and history - 图12 (opens new window) option.

References

SetReference is like a “tag” operation. It appends a reference on a key/value element. As a consequence, when we retrieve that reference with a Get or VerifiedGet the value retrieved will be the original value associated with the original key. Its VerifiedReference counterpart is the same except that it also produces the inclusion and consistency proofs.

SetReference and VerifiedSetReference

  1. _, err = client.Set(ctx, []byte(`firstKey`),[]byte(`firstValue`))
  2. if err != nil {
  3. log.Fatal(err)
  4. }
  5. reference, err := client.SetReference(ctx, []byte(`myTag`), []byte(`firstKey`))
  6. if err != nil {
  7. log.Fatal(err)
  8. }
  9. fmt.Printf("%v\n", reference)
  10. firstItem, err := client.Get(ctx, []byte(`myTag`))
  11. if err != nil {
  12. log.Fatal(err)
  13. }
  14. fmt.Printf("%v\n", firstItem)

Example with verifications

  1. _, err = client.Set(ctx, []byte(`secondKey`),[]byte(`secondValue`))
  2. if err != nil {
  3. log.Fatal(err)
  4. }
  5. reference, err = client.VerifiedSetReference(ctx, []byte(`mySecondTag`), []byte(`secondKey`))
  6. if err != nil {
  7. log.Fatal(err)
  8. }
  9. fmt.Printf("%v\n", reference)
  10. secondItem, err := client.Get(ctx, []byte(`mySecondTag`))
  11. if err != nil {
  12. log.Fatal(err)
  13. }
  14. fmt.Printf("%v\n", secondItem)
  1. byte[] key = "testRef".getBytes(StandardCharsets.UTF_8);
  2. byte[] val = "abc".getBytes(StandardCharsets.UTF_8);
  3. TxMetadata txMd = null;
  4. try {
  5. txMd = immuClient.set(key, val);
  6. } catch (CorruptedDataException e) {
  7. // ...
  8. }
  9. byte[] ref1Key = "ref1_to_testRef".getBytes(StandardCharsets.UTF_8);
  10. byte[] ref2Key = "ref2_to_testRef".getBytes(StandardCharsets.UTF_8);
  11. try {
  12. txMd = immuClient.setReference(ref1Key, key);
  13. } catch (CorruptedDataException e) {
  14. // ...
  15. }
  16. try {
  17. txMd = immuClient.verifiedSetReference(ref2Key, key);
  18. } catch (VerificationException e) {
  19. // ...
  20. }

This feature is not yet supported or not documented. Do you want to make a feature request or help out? Open an issue on Python sdk github projectQueries and history - 图13 (opens new window)

  1. import ImmudbClient from 'immudb-node'
  2. import Parameters from 'immudb-node/types/parameters'
  3. const IMMUDB_HOST = '127.0.0.1'
  4. const IMMUDB_PORT = '3322'
  5. const IMMUDB_USER = 'immudb'
  6. const IMMUDB_PWD = 'immudb'
  7. const cl = new ImmudbClient({ host: IMMUDB_HOST, port: IMMUDB_PORT });
  8. (async () => {
  9. await cl.login({ user: IMMUDB_USER, password: IMMUDB_PWD })
  10. const setReq: Parameters.Set = {
  11. key: 'firstKey',
  12. value: 'firstValue'
  13. }
  14. await cl.set(setReq)
  15. const referenceReq: Parameters.SetReference = {
  16. key: 'myTag',
  17. referencedKey: 'firstKey'
  18. }
  19. const referenceRes = await cl.setReference(referenceReq)
  20. console.log('success: setReference', referenceRes)
  21. const getReq: Parameters.Get = {
  22. key: 'myTag'
  23. }
  24. const getRes = await cl.get(getReq)
  25. console.log('success: get by reference', getRes)
  26. })()

Example with verifications

  1. import ImmudbClient from 'immudb-node'
  2. import Parameters from 'immudb-node/types/parameters'
  3. const IMMUDB_HOST = '127.0.0.1'
  4. const IMMUDB_PORT = '3322'
  5. const IMMUDB_USER = 'immudb'
  6. const IMMUDB_PWD = 'immudb'
  7. const cl = new ImmudbClient({ host: IMMUDB_HOST, port: IMMUDB_PORT });
  8. (async () => {
  9. await cl.login({ user: IMMUDB_USER, password: IMMUDB_PWD })
  10. const setReq: Parameters.Set = {
  11. key: 'firstKey',
  12. value: 'firstValue'
  13. }
  14. await cl.set(setReq)
  15. const verifiedReferenceReq: Parameters.SetReference = {
  16. key: 'myTag',
  17. referencedKey: 'firstKey'
  18. }
  19. const verifiedReferenceRes = await cl.verifiedSetReference(verifiedReferenceReq)
  20. console.log('success: verifiedSetReference', verifiedReferenceRes)
  21. const getReq: Parameters.Get = {
  22. key: 'myTag'
  23. }
  24. const getRes = await cl.get(getReq)
  25. console.log('success: get by reference', getRes)
  26. })()

This feature is not yet supported or not documented. Do you want to make a feature request or help out? Open an issue on .Net sdk github projectQueries and history - 图14 (opens new window)

If you’re using another development language, please read up on our immugwQueries and history - 图15 (opens new window) option.

GetReference and VerifiedGetReference

When reference is resolved with get or verifiedGet in case of multiples equals references the last reference is returned.

  1. _, err = client.Set(ctx, []byte(`secondKey`),[]byte(`secondValue`))
  2. if err != nil {
  3. log.Fatal(err)
  4. }
  5. _, err = client.Set(ctx, []byte(`secondKey`),[]byte(`thirdValue`))
  6. if err != nil {
  7. log.Fatal(err)
  8. }
  9. reference, err = client.VerifiedSetReference(ctx, []byte(`myThirdTag`), []byte(`secondKey`))
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. fmt.Printf("%v\n", reference)
  14. thirdItem, err := client.Get(ctx, []byte(`myThirdTag`))
  15. if err != nil {
  16. log.Fatal(err)
  17. }
  18. fmt.Printf("%v\n", thirdItem)

This feature is not yet supported or not documented. Do you want to make a feature request or help out? Open an issue on Java sdk github projectQueries and history - 图16 (opens new window)

This feature is not yet supported or not documented. Do you want to make a feature request or help out? Open an issue on Python sdk github projectQueries and history - 图17 (opens new window)

  1. import ImmudbClient from 'immudb-node'
  2. import Parameters from 'immudb-node/types/parameters'
  3. const IMMUDB_HOST = '127.0.0.1'
  4. const IMMUDB_PORT = '3322'
  5. const IMMUDB_USER = 'immudb'
  6. const IMMUDB_PWD = 'immudb'
  7. const cl = new ImmudbClient({ host: IMMUDB_HOST, port: IMMUDB_PORT });
  8. (async () => {
  9. await cl.login({ user: IMMUDB_USER, password: IMMUDB_PWD })
  10. const setReq: Parameters.Set = {
  11. key: 'firstKey',
  12. value: 'firstValue'
  13. }
  14. await cl.set(setReq)
  15. const setReq1: Parameters.Set = {
  16. key: 'secondKey',
  17. value: 'secondValue'
  18. }
  19. await cl.set(setReq1)
  20. const verifiedReferenceReq: Parameters.SetReference = {
  21. key: 'myTag',
  22. referencedKey: 'firstKey'
  23. }
  24. await cl.verifiedSetReference(verifiedReferenceReq)
  25. const verifiedReferenceReq1: Parameters.SetReference = {
  26. key: 'myTag',
  27. referencedKey: 'secondKey'
  28. }
  29. await cl.verifiedSetReference(verifiedReferenceReq1)
  30. const getReq: Parameters.Get = {
  31. key: 'myTag'
  32. }
  33. const getSecondItemRes = await cl.get(getReq)
  34. console.log('success: get by second reference', getSecondItemRes)
  35. })()

This feature is not yet supported or not documented. Do you want to make a feature request or help out? Open an issue on .Net sdk github projectQueries and history - 图18 (opens new window)

If you’re using another development language, please read up on our immugwQueries and history - 图19 (opens new window) option.

Resolving reference with transaction id

It’s possible to bind a reference to a key on a specific transaction using SetReferenceAt and VerifiedSetReferenceAt

  1. meta, err := client.Set(ctx, []byte(`secondKey`),[]byte(`secondValue`))
  2. if err != nil {
  3. log.Fatal(err)
  4. }
  5. _ , err = client.Set(ctx, []byte(`secondKey`),[]byte(`thirdValue`))
  6. if err != nil {
  7. log.Fatal(err)
  8. }
  9. reference, err = client.VerifiedSetReferenceAt(ctx, []byte(`myThirdTag`), []byte(`secondKey`), meta.Id )
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. fmt.Printf("%v\n", reference)
  14. thirdItem, err := client.Get(ctx, []byte(`myThirdTag`))
  15. if err != nil {
  16. log.Fatal(err)
  17. }
  18. fmt.Printf("%v\n", thirdItem)
  1. byte[] key = "testRef".getBytes(StandardCharsets.UTF_8);
  2. byte[] val = "abc".getBytes(StandardCharsets.UTF_8);
  3. byte[] refKey = "ref1_to_testRef".getBytes(StandardCharsets.UTF_8);
  4. TxMetadata setTxMd = null;
  5. try {
  6. txMd = immuClient.set(key, val);
  7. } catch (CorruptedDataException e) {
  8. // ...
  9. }
  10. try {
  11. immuClient.setReferenceAt(refKey, key, txMd.id);
  12. } catch (CorruptedDataException e) {
  13. // ...
  14. }
  15. try {
  16. txMd = immuClient.verifiedSetReferenceAt(refKey, key, txMd.id);
  17. } catch (VerificationException e) {
  18. // ...
  19. }

This feature is not yet supported or not documented. Do you want to make a feature request or help out? Open an issue on Python sdk github projectQueries and history - 图20 (opens new window)

  1. import ImmudbClient from 'immudb-node'
  2. import Parameters from 'immudb-node/types/parameters'
  3. const IMMUDB_HOST = '127.0.0.1'
  4. const IMMUDB_PORT = '3322'
  5. const IMMUDB_USER = 'immudb'
  6. const IMMUDB_PWD = 'immudb'
  7. const cl = new ImmudbClient({ host: IMMUDB_HOST, port: IMMUDB_PORT });
  8. (async () => {
  9. await cl.login({ user: IMMUDB_USER, password: IMMUDB_PWD })
  10. const { id } = await cl.set({ key: 'firstKey', value: 'firstValue' })
  11. await cl.set({ key: 'firstKey', value: 'secondValue' })
  12. const verifiedSetReferenceAtReq: Parameters.VerifiedSetReferenceAt = {
  13. key: 'myFirstTag',
  14. referencedKey: 'firstKey',
  15. attx: id
  16. }
  17. const verifiedSetReferenceAtRes = await cl.verifiedSetReferenceAt(verifiedSetReferenceAtReq)
  18. console.log('success: verifiedSetReferenceAt', verifiedSetReferenceAtRes)
  19. const getSecondItemRes = await cl.get({ key: 'myFirstTag' })
  20. console.log('success: get second item by reference', getSecondItemRes)
  21. })()

This feature is not yet supported or not documented. Do you want to make a feature request or help out? Open an issue on .Net sdk github projectQueries and history - 图21 (opens new window)

If you’re using another development language, please read up on our immugwQueries and history - 图22 (opens new window) option.